如何提取二项式GLMM(lme4包glmer模型)随机项的参数估计值及统计量
GLMM随机截距参数提取方案
你要提取的是(1 | group)语法定义的每个组水平的随机截距估计值及对应统计量,lme4默认的summary()仅输出随机效应的方差/标准差组分,不会输出单个组水平的参数,可通过以下两种方案实现:
方案1:配合arm包手动计算
- 先在拟合模型时无需额外调整,提取时需要先加载
arm包,开启随机效应的条件方差计算即可获取SE - 示例代码贴合你的模型场景:
library(lme4) library(arm) # 你的模型拟合代码(已拟合可跳过) mod <- glmer(pid ~ age + age2 + (1 | cohort) + (1| period), family = binomial, data = 你的数据集名) # 提取随机效应点估计,condVar = TRUE开启条件方差计算 re_est <- ranef(mod, condVar = TRUE) # 提取随机效应对应标准误 re_se <- se.ranef(mod) # 拼接cohort维度的估计值+SE,计算t值、p值(正态近似) cohort_res <- cbind(re_est$cohort, re_se$cohort) colnames(cohort_res) <- c("estimate", "std.error") cohort_res$t_value <- cohort_res$estimate / cohort_res$std.error cohort_res$p_value <- 2 * pnorm(-abs(cohort_res$t_value)) print(cohort_res) # period维度结果同理提取 period_res <- cbind(re_est$period, re_se$period) colnames(period_res) <- c("estimate", "std.error") period_res$t_value <- period_res$estimate / period_res$std.error period_res$p_value <- 2 * pnorm(-abs(period_res$t_value)) print(period_res)
方案2:配合broom.mixed包一键输出结构化结果
- 该方案可以直接输出所有随机效应的点估计、标准误、置信区间,无需手动拼接,适合需要批量处理的场景
- 示例代码:
library(lme4) library(broom.mixed) # 模型拟合步骤同上 mod <- glmer(pid ~ age + age2 + (1 | cohort) + (1| period), family = binomial, data = 你的数据集名) # 一键提取所有随机组水平的参数 re_all <- tidy(mod, effects = "ran_vals", conf.int = TRUE) # 按随机项类别筛选结果 cohort_res <- re_all[re_all$group == "cohort", ] period_res <- re_all[re_all$group == "period", ]
注意事项
- 上述p值采用正态近似计算,GLMM的随机效应参数p值本身存在自由度争议,如果需要更严谨的结果可采用参数bootstrap法实现
- 如果你需要提取的是随机效应的方差组分(而非单个组水平的截距)的SE、p值,可使用
as.data.frame(VarCorr(mod))获取方差组分的标准误,配合lmerTest包的rand()函数获取随机效应显著性检验的p值
内容的提问来源于stack exchange,提问作者andreafg
相关产品推荐
相关产品推荐

