如何在具有多个预测变量的混合模型中绘制随机截距和斜率? [英] How to plot random intercept and slope in a mixed model with multiple predictors?

查看:564
本文介绍了如何在具有多个预测变量的混合模型中绘制随机截距和斜率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当混合模型具有多个预测变量时,是否可以绘制随机模型的随机截距或斜率?

Is it possible to plot the random intercept or slope of a mixed model when it has more than one predictor?

有了一个预测变量,我会这样:

With one predictor I would do like this:

#generate one response, two predictors and one factor (random effect)
resp<-runif(100,1, 100)
pred1<-c(resp[1:50]+rnorm(50, -10, 10),resp[1:50]+rnorm(50, 20, 5))
pred2<-resp+rnorm(100, -10, 10)
RF1<-gl(2, 50)

#gamm
library(mgcv)
mod<-gamm(resp ~ pred1, random=list(RF1=~1))
plot(pred1, resp, type="n")
for (i in ranef(mod$lme)[[1]]) {
abline(fixef(mod$lme)[1]+i, fixef(mod$lme)[2])
}

#lmer
library(lme4)
mod<-lmer(resp ~ pred1 + (1|RF1))
plot(pred1, resp, type="n")
for (i in ranef(mod)[[1]][,1]) {
abline(fixef(mod)[1]+i, fixef(mod)[2])
}

但是如果我有这样的模型怎么办?:

But what if I have a model like this instead?:

mod<-gamm(resp ~ pred1 + pred2, random=list(RF1=~1))

或者使用lmer

mod<-lmer(resp ~ pred1 + pred2 + (1|RF1))

我应该考虑所有系数还是仅考虑要绘制的变量中的一个?

Should I consider all the coefficients or only the ones of the variable that I'm plotting?

谢谢

推荐答案

## generate one response, two predictors and one factor (random effect)
set.seed(101)
resp <- runif(100,1,100)
pred1<- rnorm(100, 
           mean=rep(resp[1:50],2)+rep(c(-10,20),each=50),
           sd=rep(c(10,5),each=50))
pred2<- rnorm(100, resp-10, 10)

注意,您可能尝试随机拟合 仅具有两个级别的分组变量的效果-这将 几乎总是导致估计的随机效应方差为零, 依次将您的预测行放在每个行的顶部 其他-我正在从gl(2,50)切换到gl(10,10) ...

NOTE that you should probably not be trying to fit a random effect for an grouping variable with only two levels -- this will almost invariably result in an estimated random-effect variance of zero, which will in turn put your predicted lines right on top of each other -- I'm switching from gl(2,50) to gl(10,10) ...

RF1<-gl(10,10)
d <- data.frame(resp,pred1,pred2,RF1)

#lmer
library(lme4)
mod <- lmer(resp ~ pred1 + pred2 + (1|RF1),data=d)

lme4的开发版本具有predict()功能 这使这变得容易一些...

The development version of lme4 has a predict() function that makes this a little easier ...

  • 预测pred1的范围,其中pred2等于其平均值, 反之亦然.这一切都比它需要的要聪明 因为它会生成两个焦点预测变量的所有值 并用ggplot一次性绘制它们...
  • Predict for a range of pred1 with pred2 equal to its mean, and vice versa. This is all a little bit cleverer than it needs to be, since it generates all the values for both focal predictors and plots them with ggplot in one go ...

()

nd <- with(d,
           rbind(data.frame(expand.grid(RF1=levels(RF1),
                      pred1=seq(min(pred1),max(pred1),length=51)),
                      pred2=mean(pred2),focus="pred1"),
                 data.frame(expand.grid(RF1=levels(RF1),
                      pred2=seq(min(pred2),max(pred2),length=51)),
                      pred1=mean(pred1),focus="pred2")))
nd$val <- with(nd,pred1[focus=="pred1"],pred2[focus=="pred2"])
pframe <- data.frame(nd,resp=predict(mod,newdata=nd))
library(ggplot2)
ggplot(pframe,aes(x=val,y=resp,colour=RF1))+geom_line()+
         facet_wrap(~focus,scale="free")

  • 或者,仅关注pred1并为pred2值的(小/离散)范围生成预测...
    • Alternatively, focusing just on pred1 and generating predictions for a (small/discrete) range of pred2 values ...
    • ()

      nd <- with(d,
                 data.frame(expand.grid(RF1=levels(RF1),
                            pred1=seq(min(pred1),max(pred1),length=51),
                            pred2=seq(-20,100,by=40))))
      pframe <- data.frame(nd,resp=predict(mod,newdata=nd))
      ggplot(pframe,aes(x=pred1,y=resp,colour=RF1))+geom_line()+
               facet_wrap(~pred2,nrow=1)
      

      您可能想在最后一个facet_wrap()中设置scale="free" ...或 使用facet_grid(~pred2,labeller=label_both)

      You might want to set scale="free" in the last facet_wrap() ... or use facet_grid(~pred2,labeller=label_both)

      对于演示,您可能需要替换colour美学, 使用group,如果您要做的就是区分组 (即绘制单独的线)而不是识别它们...

      For presentation you might want to replace the colour aesthetic, with group, if all you want to do is distinguish among groups (i.e. plot separate lines) rather than identify them ...

      这篇关于如何在具有多个预测变量的混合模型中绘制随机截距和斜率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆