简体   繁体   English

如何绘制 lmer 模型的一些术语

[英]How to plot some terms of a lmer model

I am trying to plot only a few terms of random effects from a lmer object.我试图从 lmer 对象中仅绘制几个随机效应项。 I will borrow the dataset posted by oshun here .我将在这里借用oshun发布的数据集。

Make up data:组成数据:

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
)

Make up some response data组成一些响应数据

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)

I can plot random effects by using type = "re" as follows:我可以使用 type = "re" 绘制随机效应,如下所示:

plot_model(model, type = "re")

I would like to show only A and E so I add the 'terms' argument as follows:我只想显示 A 和 E,因此我添加了“条款”参数,如下所示:

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

But this does not work.但这不起作用。 Any guidance on how I can show "A" and "E" only??关于如何仅显示“A”和“E”的任何指导?

For some reason, the terms option doesn't work.出于某种原因,条款选项不起作用。 Should be reflected to the authors of sjplot.应该反映给 sjplot 的作者。 Here's two workarounds:这里有两个解决方法:

1) manually define the terms using ggplot2: 1) 使用 ggplot2 手动定义术语:

library(ggplot2)
library(sjPlot)

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

在此处输入图片说明

You get the following warnings because you throw out data您会收到以下警告,因为您丢弃了数据

Scale for 'x' is already present. 'x' 的比例已经存在。 Adding another scale for 'x', which will replace the existing scale.为“x”添加另一个比例,这将替换现有比例。 Warning messages: 1: Removed 3 rows containing missing values (geom_point).警告消息:1:删除了 3 行包含缺失值 (geom_point)。 2: Removed 3 rows containing missing values (geom_errorbar). 2:删除了包含缺失值的 3 行(geom_errorbar)。

2) Plot it from get_model() 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")

在此处输入图片说明

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

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