在R中执行加权分层Logistic回归:分层结果展示问题求助
解决加权分层Logistic回归的分层结果展示问题
问题根源
你当前用svyglm()拟合的是整合所有分层的全局模型,而非按gd分层分别拟合独立模型,因此summary()和tbl_regression()只会返回整体回归结果,无法展示各分层的单独结果。tbl_uvregression()能输出分层单变量结果是因为它内置了by参数支持自动分层拟合,而tbl_regression()需要手动实现分层拟合逻辑。
解决方案:按分层批量拟合模型并合并结果
方法1:手动拆分+合并表格
先按分层变量gd拆分数据集,分别拟合模型后,用tbl_regression()生成各分层结果表格,再通过tbl_merge()合并展示:
library(tidyverse) library(gtsummary) library(survey) # 原数据集构建(同你提供的代码) gender <- factor(c(1, 2, 1, 1, 1, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1, 1, 2)) age <- factor(c(3, 4, 2, 1, 3, 1, 1, 2, 3, 3, 4, 1, 3, 1, 3, 4, 2, 1, 2, 1)) prema.5cl <- factor(c(5, 4, 5, 2, 1, 1, 3, 1, 5, 1, 2, 4, 4, 3, 1, 2, 1, 4, 5, 2)) meduc.4cl <- factor(c(1, 2, 3, 3, 1, 4, 2, 1, 4, 2, 1, 3, 3, 4, 4, 2, 4, 3, 1, 2)) mhealth <- factor(c(1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0)) parttime <- factor(c(1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0)) ref1 <- factor(c(0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0)) ref2 <- factor(c(0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0)) ref3 <- factor(c(0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1)) poids <- c(0.52, 1.43, 0.74, 1.60, 1.71, 0.08, 0.96, 1.62, 1.00, 0.83, 1.74, 0.82, 1.23, 1.04, 0.19, 1.63, 0.45, 0.08, 0.60, 1.73) gd <- factor(c("gp_2", "gp_1", "gp_2", "gp_1", "gp_2", "gp_1", "gp_1", "gp_1", "gp_2", "gp_2", "gp_1", "gp_1", "gp_2", "gp_1", "gp_1", "gp_2", "gp_2", "gp_1", "gp_2", "gp_2")) bth_2y <- factor(c(1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0)) db <- data.frame(gender, age, prema.5cl, meduc.4cl, mhealth, parttime, ref1, ref2, ref3, poids, gd, bth_2y) # 按gd分层拆分数据集 db_split <- split(db, db$gd) # 分别拟合两个分层的模型 reg_gp1 <- svyglm( bth_2y ~ gender + age + prema.5cl + meduc.4cl + mhealth + ref1 + ref2 + ref3, family = quasibinomial, design = svydesign(id=~1, data = db_split$gp_1, weights = ~poids)) reg_gp2 <- svyglm( bth_2y ~ gender + age + prema.5cl + meduc.4cl + mhealth + ref1 + ref2 + ref3, family = quasibinomial, design = svydesign(id=~1, data = db_split$gp_2, weights = ~poids)) # 生成各分层的结果表格并设置变量标签 tbl_gp1 <- tbl_regression(reg_gp1, label = list( gender ~ "性别", age ~ "年龄", prema.5cl ~ "孕前抑郁程度", meduc.4cl ~ "母亲教育水平", mhealth ~ "母亲健康状况", ref1 ~ "参考变量1", ref2 ~ "参考变量2", ref3 ~ "参考变量3" )) %>% modify_caption("**分层gp_1的回归结果**") tbl_gp2 <- tbl_regression(reg_gp2, label = list( gender ~ "性别", age ~ "年龄", prema.5cl ~ "孕前抑郁程度", meduc.4cl ~ "母亲教育水平", mhealth ~ "母亲健康状况", ref1 ~ "参考变量1", ref2 ~ "参考变量2", ref3 ~ "参考变量3" )) %>% modify_caption("**分层gp_2的回归结果**") # 合并两个分层表格 tbl_merge(list(tbl_gp1, tbl_gp2), tab_spanner = c("**gp_1**", "**gp_2**"))
方法2:用purrr批量处理(适配多分层场景)
如果分层数量较多,用map()函数批量完成模型拟合和表格生成:
# 批量拟合所有分层的模型 reg_list <- map(db_split, ~svyglm( bth_2y ~ gender + age + prema.5cl + meduc.4cl + mhealth + ref1 + ref2 + ref3, family = quasibinomial, design = svydesign(id=~1, data = ., weights = ~poids) )) # 批量生成各分层的结果表格 tbl_list <- map2(reg_list, names(reg_list), ~tbl_regression(.x, label = list( gender ~ "性别", age ~ "年龄", prema.5cl ~ "孕前抑郁程度", meduc.4cl ~ "母亲教育水平", mhealth ~ "母亲健康状况", ref1 ~ "参考变量1", ref2 ~ "参考变量2", ref3 ~ "参考变量3" )) %>% modify_caption(sprintf("**分层%s的回归结果**", .y))) # 合并所有分层表格 tbl_merge(tbl_list, tab_spanner = paste0("**", names(reg_list), "**"))
补充说明
- 若需检验分层间的系数差异,可在全局模型中加入自变量与分层变量的交互项(如
bth_2y ~ gd*(gender + age + prema.5cl + ...)),通过summary()查看交互项的显著性。 gtsummary的tbl_merge()支持自定义表格标题、列标签等格式,可根据需求进一步调整。
内容的提问来源于stack exchange,提问作者Curbice
相关产品推荐
相关产品推荐

