繁体   English   中英

在ggplot2中创建平滑线和置信区间形成lmer模型的新手问题

[英]Newbie problems with creating smooth lines and confidence intervals form lmer model in ggplot2

对不起,这是一个愚蠢的简单问题,但我已经尝试了我在网上找到的所有解决方案都无济于事。 这也是我在这里的第一篇文章,我试图遵循有关格式的规则。 可笑的是,我已经完全实现了我想要的,将绘图保存为 png,然后当我几周后返回代码时它不起作用,现在我无法复制我所拥有的。

我试图在这里给出一些示例数据(从这个网站借用了一些虚构的数据——我希望没问题)。

tempEf <- data.frame(
  N = rep(c("1", "2","1", "2","1", "2","1"), each=5, times=11),
  Myc = rep(c("1", "2", "3", "4", "5"), each=1, times=77),
  TRTYEAR = runif(385, 1, 15),
  site = rep(c(1:77), each=5, times=1),#77 sites
  Asp = runif(385, 1, 5)
)

# Make up some response data
tempEf$r <- 2*tempEf$TRTYEAR +                   
  -8*as.numeric(tempEf$Myc=="1") +
  4*as.numeric(tempEf$N=="1") +
  0.1*tempEf$TRTYEAR * as.numeric(tempEf$N=="1") +
  0.2*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="1") +
  -11*as.numeric(tempEf$Myc=="1")*as.numeric(tempEf$N=="1")+ 
  0.5*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="1")*as.numeric(tempEf$N=="1")+ 
  as.numeric(tempEf$site) +  #Random intercepts; intercepts will increase by 1
  tempEf$TRTYEAR/10*rnorm(385, mean=0, sd=2)    #Add some noise
#fit model
library(lme4)
model <- lmer(r ~ Myc * N + TRTYEAR + Asp + (1|site), data=tempEf)
tempEf$fit <- predict(model)   #Add model fits to dataframe

我的目标是:

  1. 从 lmer 模型计算拟合值和 95% 置信区间

  2. 分别针对“Myc”的 2 个水平绘制拟合值(“fit”)与我的因变量(“r”),根据 Myc 着色。 为了这个图的目的,我想忽略 N 和 Asp(在我的真实数据中,这些是控制变量,在模型中很重要但不感兴趣)

  3. 将我的 95% 置信区间添加到这两行

所有这一切看起来都很简单,只是它出错了!

我在这里获得了我的拟合值和 95% CI,这给了我 fit、upr 和 lwr:

predicted_EF<-predictInterval(model, tempEf)

然后我将它们添加到我的原始数据框中:

tempEf<-cbind(tempEf,predicted_EF)

然后我这样做:

ggplot(tempEf,aes(TRTYEAR, r, group=Myc, col=Myc )) + 
  geom_line(aes(y=fit, lty=Myc), size=0.8) +
  geom_point(alpha = 0.3) + 
  theme_bw()

这给了我锯齿状的线条,如下所示: crappy graph

我可以使用 geom_smooth 而不是 geom_line,它提供平滑的线条,但我相信这是将线条拟合到原始数据,而不是模型拟合值? 我还可以使用 geom_abline 为 Myc 的每个级别拟合单独的回归线(使用 fit 变量),但也不确定这是否正确。

ggplot(tempEf,aes(TRTYEAR, r, group=Myc, col=Myc, fill = Myc)) + 
  geom_smooth(method="lm",se = FALSE)+
  geom_point(alpha = 0.3)+
  theme_bw()

然后尝试使用我的 upr 和 lwr 变量添加 95% 置信区间会导致锯齿状的置信带:

ggplot(tempEf,aes(TRTYEAR, r, group=Myc, col=Myc, fill = Myc)) + 
  geom_smooth(method="lm",se = FALSE)+
  geom_point(alpha = 0.3) +
  geom_ribbon(alpha=0.1,
              aes(ymin=lwr,ymax=upr,fill = Myc, colour = NA))+
  theme_bw()

如何获得具有平滑置信区间的平滑线? 我做错了什么(很多,我确定!)。

感谢您的帮助。

我认为这是 effecs 图(或估计的边际均值)的“经典”任务。 您可以使用ggeffects -package轻松完成此操作,网站上有很多示例。

根据您的数据,您只需调用ggpredict(model, c("TRTYEAR", "Myc"))

library(ggeffects)
pred <- ggpredict(model, c("TRTYEAR", "Myc"))
pred
#> 
#> # Predicted values of r
#> # x = TRTYEAR
#> 
#> # Myc = AM
#>   x predicted std.error conf.low conf.high
#>   0     0.797     0.737   -0.647     2.241
#>   2     5.361     0.727    3.936     6.786
#>   6    14.489     0.716   13.085    15.892
#>   8    19.052     0.715   17.652    20.453
#>  10    23.616     0.716   22.213    25.020
#>  16    37.308     0.737   35.863    38.752
#> 
#> # Myc = ECM
#>   x predicted std.error conf.low conf.high
#>   0    -5.575     0.737   -7.019    -4.130
#>   2    -1.011     0.727   -2.436     0.415
#>   6     8.117     0.716    6.713     9.520
#>   8    12.681     0.715   11.280    14.081
#>  10    17.244     0.716   15.841    18.648
#>  16    30.936     0.737   29.492    32.380
#> 
#> Adjusted for:
#> *    N = Nhigh
#> *  Asp =  2.99
#> * site = 0 (population-level)

plot(pred)
#> Loading required namespace: ggplot2

plot(pred, add.data = TRUE)

reprex 包(v0.3.0) 于 2019 年 12 月 11 日创建

ggeffects包看起来超级ggeffects ,非常值得一试。 为了回答您关于为 Myc 的每个级别分别放置多行的问题, ggplot(aes(group = ))调用中的interaction函数始终是快速执行此操作的便捷工具。 在您的情况下,您包含了四个分类变量,其中一个用颜色编码。 将其他三个拆分为每个(在每个子组下)给出直线和色带:

ggplot(tempEf, aes(TRTYEAR, r, group = interaction(site, N, Myc), col=Myc, fill = Myc)) +
  geom_point(alpha = 0.3) +
  geom_line(aes(y = fit)) +
  geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.1, colour = NA)

给出一组代表每个Myc x site x N分组的线条和色带。 我认为根据您的要求,它是您想要的其他输出(来自ggeffects ),但如果这是一个有用的工具:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM