【问题标题】:How to plot some terms of a lmer model如何绘制 lmer 模型的一些项
【发布时间】:2019-11-25 07:57:45
【问题描述】:

我试图仅从 lmer 对象中绘制几个随机效应项。我会借用oshunhere发的数据集。

补资料:

tempEf <- data.frame(
  N = rep(c("Nlow", "Nhigh"), each=300),
  Myc = rep(c("AM", "ECM"), each=150, times=2),
  TRTYEAR = runif(600, 1, 15),
  site = rep(c("A","B","C","D","E"), each=10, times=12)   #5 sites
)

补一些响应数据

tempEf$r <- 2*tempEf$TRTYEAR +                   
  -8*as.numeric(tempEf$Myc=="ECM") +
  4*as.numeric(tempEf$N=="Nlow") +
  0.1*tempEf$TRTYEAR * as.numeric(tempEf$N=="Nlow") +
  0.2*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="ECM") +
  -11*as.numeric(tempEf$Myc=="ECM")*as.numeric(tempEf$N=="Nlow")+ 
  0.5*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="ECM")*as.numeric(tempEf$N=="Nlow")+ 
  as.numeric(tempEf$site) +  #Random intercepts; intercepts will increase by 1
  tempEf$TRTYEAR/10*rnorm(600, mean=0, sd=2)    #Add some noise

library(lme4)
model <- lmer(r ~ Myc * N * TRTYEAR + (1|site), data=tempEf)

我可以使用 type = "re" 来绘制随机效应,如下所示:

plot_model(model, type = "re")

我只想显示 A 和 E,所以我添加了 'terms' 参数,如下所示:

plot_model(model, type = "re", terms = c("A", "E"))

但这不起作用。关于如何仅显示“A”和“E”的任何指导?

【问题讨论】:

  • 你不妨看看merTools包

标签: r plot random lme4 effects


【解决方案1】:

由于某种原因,条款选项不起作用。应该反映给 sjplot 的作者。这里有两种解决方法:

1) 使用 ggplot2 手动定义术语:

library(ggplot2)
library(sjPlot)

plot_model(model, type = "re") + scale_x_discrete(limits=c("A","D"))

您收到以下警告是因为您丢弃了数据

“x”的比例已经存在。为“x”添加另一个比例,即 将取代现有的规模。警告消息:1:删除 3 行 包含缺失值(geom_point)。 2:删除了 3 行包含 缺失值(geom_errorbar)。

2) 从 get_model() 绘制它

    df = get_model_data(model,type="re")
    df = subset(df,term %in% c("A","D"))
    ggplot(df,aes(x=term,y=estimate,col=group)) +
    geom_point(show.legend=FALSE) + 
    geom_segment(aes(xend=term,y=conf.low,yend=conf.high),show.legend=FALSE)+
    scale_colour_brewer(palette = "Set1")+
    coord_flip()+ggtitle("Random Effects")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    相关资源
    最近更新 更多