如何预测和绘制lmer或glmer中的非线性变化斜率?

时间:2014-04-27 23:00:18

标签: r ggplot2 lme4 mixed-models

我的目标是使用R中lme4包的lmerglmer函数,从变截距,变斜率多级模型计算预测值。并且清楚,我在这里展示了一个玩具示例,其中包括" mtcars"数据集:

以下是我通常如何通过变截距,变斜率多级模型创建预测值(此代码应该可以正常工作):

# loading in-built cars dataset
data(mtcars)

# the "gear" column will be the group-level factor, so we'll have cars nested 
# within "gear" type
mtcars$gear <- as.factor(mtcars$gear)

# fitting varying-slope, varying-intercept model
m <- lmer(mpg ~ 1 + wt + hp + (1 + wt|gear), data=mtcars)

# creating the prediction frame
newdata <- with(mtcars, expand.grid(wt=unique(wt),
                              gear=unique(gear),
                              hp=mean(hp)))

# calculating predictions
newdata$pred <- predict(m, newdata, re.form=~(1 + wt|gear))

# quick ggplot2 graph
p <- ggplot(newdata, aes(x=wt, y=pred, colour=gear))
p + geom_line() + ggtitle("Varying Slopes")

predicted values

上面的R代码应该可以工作,但是如果我想从非线性变量截距,变化斜率创建和绘制预测,那么它显然会失败。为了简单和可重复,这里是使用&#34; mtcars&#34;的绊脚石。数据集:

# key question: how to create predictions if I want to examine a non-linear 
# varying slope?

# creating a squared term for a non-linear relationship
# NB: usually I use the `poly` function
mtcars$wtsq <- (mtcars$wt)^2

# fitting varying-slope, varying-intercept model with a non-linear trend
m <- lmer(mpg ~ 1 + wt + wtsq + hp + (1 + wt + wtsq|gear), data=mtcars)

# creating the prediction frame
newdata <- with(mtcars, expand.grid(wt=unique(wt),
                                wtsq=unique(wtsq),
                                gear=unique(gear),
                                hp=mean(hp)))

# calculating predictions
newdata$pred <- predict(m, newdata, re.form=~(1 + wt + wtsq|gear))

# quick ggplot2 graph 
# clearly not correct (see the graph below)
p <- ggplot(newdata, aes(x=wt, y=pred, colour=gear))
p + geom_line() + ggtitle("Varying Slopes")

predicted values

显然,预测帧未正确设置。在R中拟合非线性变截距,变斜率多级模型时,如何创建和绘制预测值的任何想法?谢谢!

1 个答案:

答案 0 :(得分:4)

问题是,当expand.gridwt同时使用wt^2时,您会创建wtwt^2的所有可能组合。您对代码的修改有效:

newdata <- with(mtcars, expand.grid(wt=unique(wt),
                                gear=unique(gear),
                                hp=mean(hp)))
newdata$wtsq <- newdata$wt^2

newdata$pred <- predict(m, newdata)

p <- ggplot(newdata, aes(x=wt, y=pred, colour=gear, group=gear))
p + geom_line() + ggtitle("Varying Slopes")