You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

emmeans计算线性混合效应模型调整均值结果异常的问题咨询

R语言emmeans包计算混合模型调整均值异常的解决方案

我使用emmeans包计算线性混合效应回归模型的调整均值,但得到的结果不符合预期。我想要绘制模型拟合结果以及单个数据点的调整值,最终输出的绘图结果存在异常:
ggplot

估计得到的调整均值在Course A组明显过高、Course C组明显过低。我构建的线性混合效应回归模型以post.diff为响应变量,pre.diff为协变量,纳入group和course的主效应及交互项;由于course存在重复测量、测试场景存在差异,我为bib和school添加了随机截距。使用emmeans得到的估计结果如下:

# 模型拟合
CI_post <- lmer(
  post.diff ~ 
    pre.diff +
    group * course 
  + (1|bib) 
  + (1|school), 
  data = dat, 
  REML = FALSE)

# 估计调整均值
emmeans(CI_post, specs = c("course", "group"),lmer.df = "satterthwaite")

# 输出结果
 course group       emmean    SE   df lower.CL upper.CL
 A      blocked      0.311 0.191 6.65  -0.1452    0.768
 B      blocked      0.649 0.180 5.38   0.1954    1.102
 C      blocked      1.141 0.195 7.28   0.6847    1.598
 A      interleaved  0.189 0.194 7.15  -0.2666    0.645
 B      interleaved  0.497 0.179 5.31   0.0451    0.949
 C      interleaved  1.046 0.191 6.72   0.5907    1.502

上述结果就是我绘图使用的数据,我认为该估计结果存在错误,请问如何操作才能得到正确的调整均值估计值?
阅读emmeans官方基础文档后,我猜测误差的来源是pre.diff被默认设置为了固定均值?

ref_grid(CI_post)

# 输出结果
'emmGrid' object with variables:
    pre.diff = 1.5065
    group = blocked, interleaved
    course = A, B, C

编辑

参考Lenth的建议,我尝试使用公式post.diff.adj = post.diff + b * (1.506 - pre.diff)计算调整值,得到的绘图结果如下:
adjusted

调整后的结果看起来更符合预期,我使用的是模型输出的固定效应回归系数:

Fixed effects:
                          Estimate Std. Error        df t value             Pr(>|t|)    
(Intercept)               -0.66087    0.18158   5.58701  -3.639             0.012280 *  
pre.diff                   0.64544    0.06178 130.60667  10.448 < 0.0000000000000002 ***
groupinterleaved          -0.12209    0.15189  65.38709  -0.804             0.424431    
courseB                    0.33714    0.09703 131.63603   3.475             0.000693 ***
courseC                    0.82993    0.16318 151.09201   5.086           0.00000107 ***
groupinterleaved:courseB  -0.02922    0.11777 101.47596  -0.248             0.804563    
groupinterleaved:courseC   0.02692    0.11763 100.29319   0.229             0.819435 

随后我在tibble中按如下方式计算调整值:

dat <- dat %>%
  mutate(adjustedMean = (post.diff) + (0.6454358 * (1.506 - pre.diff)))

之后我使用ggplot绘制可视化图形,代码如下:

CI_post_plot <- ggplot(dat, aes(x = interaction(group, course), y = adjustedMean)) +
  geom_point(aes(color=group), size=1.5, position=position_jitter(width=0.1), alpha=0.7)+
  scale_y_continuous(name = "Time substracted from straight gliding time (sec.)", breaks = seq(-2, 6, 1)) +
  theme_pubr()+
  theme(legend.position="none",
        axis.title.x=element_blank()) +
  geom_hline(aes(yintercept=0), linetype = "dashed", size=0.2) + 
  scale_x_discrete(labels = c("Blocked\nCourse A", "Interleaved\nCourse A", "Blocked\nCourse B", "Interleaved\nCourse B", "Blocked\nCourse C",  "Interleaved\nCourse C")) 

CI_post_plot <- CI_post_plot + 
  geom_point(data = estmarg_mean, aes(x=interaction(group, course), y=emmean, group=group), size=2.5) +
  geom_errorbar(data = estmarg_mean, aes(x= interaction(group, course), y = emmean, ymin = lower.CL,ymax = upper.CL), width=0.1)

内容的提问来源于stack exchange,提问作者Cmagelssen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 07:15:05