【问题标题】:plotting interaction from mixed model lme4 with CI bands绘制混合模型 lme4 与 CI 波段的相互作用
【发布时间】:2017-04-19 14:51:06
【问题描述】:

我有以下混合效果模型:

p1 <- lmer(log(price) ~ year*loca + (1|author), data = df)

'年'是连续的 'loca' 是具有 2 个级别的分类变量

我正在尝试绘制此模型的重要交互。

以下代码(使用 visreg 包)绘制了来自两个 'loca' 中的每一个的线,但它不会产生 95% 的置信带:

visreg(p1, "year", by = "loca", overlay = T,
   line=list(lty = 1, col = c("grey", "black")), points=list(cex=1, pch=19, 
   col = c("grey", "black")), type="conditional", axes = T)

然后,我尝试使用以下代码绘制线条,但顶部没有数据点,也没有 CI:

visreg(p1, "year", by = "loca", overlay = T, 
   line=list(lty = 1, col = c("grey60", "black")), points=list(cex=1, 
   pch=19, col = c("grey", "black")),
   type="conditional", trans = exp, fill.par = list(col = c("grey80", 
   "grey70")))

当我使用 type = 'contrast' 而不是 'conditional' 时,我会得到 CI 波段。但是,当我尝试使用 trans = exp 对上述价格进行反向转换时,这不起作用。

总的来说,我需要能够绘制具有以下属性的交互:

  • 置信区间
  • 反向转换点
  • 预测行('loca' 的每个级别一个)

非常乐意尝试其他方法....但到目前为止我似乎找不到任何有效的方法。

非常感谢您的帮助!

【问题讨论】:

    标签: lme4 interaction mixed-models


    【解决方案1】:

    一种可能性是使用effects 包:

    library(effects)
    eff.p1 <- effect("year*loca", p1, KR=T)
    

    然后您可以直接使用包提供的内容绘制它并从那里自定义它:

    plot(eff.p1)
    

    或者采用产生的效果并用 ggplot 将其绘制在一个更好的图中:

    eff.p1 <- as.data.frame(eff.p1)
    ggplot(eff.p1, aes(year, linetype=factor(loca),
                                         color = factor(loca))) +
      geom_line(aes(y = fit, group=factor(loca)), size=1.2) +
      geom_line(aes(y = lower,
                    group=factor(loca)), linetype =3) +
      geom_line(aes(y = upper,
                    group=factor(loca)), linetype =3) +
      xlab("year") +
      ylab("Marginal Effects on Log Price") +
      scale_colour_discrete("") +
      scale_linetype_discrete("") +
      labs(color='loca') + theme_minimal()
    

    没有数据我无法真正尝试代码,但我认为它应该可以工作。

    【讨论】:

      【解决方案2】:

      这应该可以解决问题:

      install.packages(sjPlot)
      library(sjPlot)
      plot_model(p1, type = "int", terms = c(year,loca), ci.lvl = 0.95)
      

      虽然它带有一些关于标签的警告,对我的数据进行测试,但它会自动进行反向转换,并且似乎工作正常。自定义应该很容易,因为我相信 sjPlot 使用 ggplot。

      编辑:@Daniel 指出允许更多自定义的替代选项是 plot_model(type = "pred", ...) 或 plot_model(type = "eff", ...)

      【讨论】:

      • 是的,绘图基于 ggplot。允许更多自定义的替代选项是plot_model(type = "pred", ...)plot_model(type = "eff", ...)
      • 谢谢 - 我会把它添加到答案中
      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多