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

4
我的目标是使用R语言中的和函数,通过lme4包中的变截距、变斜率多层模型计算预测值。为了使内容更加具体明确,我在这里提供了一个玩具示例,使用"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")

预测值

以上 R 代码应该是可行的,但如果我想要创建和绘制来自非线性变化截距、变化斜率的预测,则显然会失败。为了简单和可重复性,这里使用“mtcars”数据集来说明问题:

# 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")

预测值

很明显,预测框架并未正确设置。有没有关于如何在使用R拟合非线性变截距、变斜率多层模型时创建和绘制预测值的想法呢?谢谢!

1个回答

4
问题在于当您同时使用wtwt^2来使用expand.grid时,您会创建所有可能的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")

太好了,谢谢!我知道答案很简单 :-) - statsRus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接