本文介绍了如何预测和绘制lmer或glmer中的非线性变化斜率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的目标是使用 lmer 和 glmer lme4 包。为了使这个具体和明确,我在这里给出一个玩具的例子与mtcars数据集: 这是我通常从变截距,变斜率多级模型创建预测值(这段代码应该可以正常工作): #loading内置车数据集数据(mtcars) #齿轮列将是组级别因子,因此我们将在齿轮箱内嵌套# 类型 mtcars $齿轮< - as.factor(mtcars $ gear) #拟合变化斜率,变截距模型m #创建预测帧 newdata< - with(mtcars,expand.grid(wt =独特(wt),齿轮=唯一(齿轮), hp =平均(hp))) #计算预测新数据$ pred #quick ggplot2 graph p + geom_line()+ ggtitle(Varying Slopes) 工作,但如果我想从非线性变截距,变斜率创建和图形预测,那么它显然会失败。为了简单和可重复性,下面是使用mtcars数据集的绊脚石: #关键问题:如何创建预测if我想检查一个非线性的#变化斜率? #为非线性关系创建平方项#NB:通常我使用`poly`函数 mtcars $ wtsq< - (mtcars $ wt)^ 2 #拟合变斜率,具有非线性趋势的变截距模型m -lmer(mpg〜1 + wt + wtsq + hp +(1 + wt + wtsq data(mtcars)) #创建预测帧 newdata wtsq =独特(wtsq),齿轮=唯一(齿轮), hp =平均(hp))) #计算预测新数据$ pred #quick ggplot2 graph #显然不正确(见下图)p < - ggplot(newdata,aes(x = wt,y = pred,color = gear))p + geom_line()+ ggtitle(Varying Slopes) / pre> 很明显,预测帧设置不正确。在拟合R中的非线性变截距,变斜率多级模型时如何创建和绘制预测值的任何想法?感谢!解决方案问题是,当您使用 expand.grid 使用 wt 和 wt ^ 2 ,您可以创建 wt 和 wt ^ 2 。您的代码的这种修改工作: $ p $ newdata gear = unique(gear), hp = mean(hp))) newdata $ wtsq< - newdata $ wt ^ 2 newdata $ pred< ; - 预测(m,newdata) p p + geom_line + ggtitle(Varying Slopes) My goal is to calculate predicted values from a varying-intercept, varying-slope multilevel model using the lmer and glmer functions of the lme4 package in R. To make this concrete and clear, I present here a toy example with the "mtcars" data set:Here's how I usually create predicted values from a varying-intercept, varying-slope multilevel model (this code should work just fine):# loading in-built cars datasetdata(mtcars)# the "gear" column will be the group-level factor, so we'll have cars nested # within "gear" typemtcars$gear <- as.factor(mtcars$gear)# fitting varying-slope, varying-intercept modelm <- lmer(mpg ~ 1 + wt + hp + (1 + wt|gear), data=mtcars)# creating the prediction framenewdata <- with(mtcars, expand.grid(wt=unique(wt), gear=unique(gear), hp=mean(hp)))# calculating predictionsnewdata$pred <- predict(m, newdata, re.form=~(1 + wt|gear))# quick ggplot2 graphp <- ggplot(newdata, aes(x=wt, y=pred, colour=gear))p + geom_line() + ggtitle("Varying Slopes")The above R code should work, but if I want to create and graph predictions from a non-linear varying-intercept, varying-slope then it clearly fails. For simplicity and reproducibility, here's the stumbling block using the "mtcars" data set:# 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` functionmtcars$wtsq <- (mtcars$wt)^2# fitting varying-slope, varying-intercept model with a non-linear trendm <- lmer(mpg ~ 1 + wt + wtsq + hp + (1 + wt + wtsq|gear), data=mtcars)# creating the prediction framenewdata <- with(mtcars, expand.grid(wt=unique(wt), wtsq=unique(wtsq), gear=unique(gear), hp=mean(hp)))# calculating predictionsnewdata$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")Clearly the prediction frame is not set up correctly. Any ideas on how to create and graph predicted values when fitting a non-linear varying-intercept, varying-slope multilevel model in R? Thanks! 解决方案 The issue is that when you use expand.grid with both wt and wt^2, you create all possible combinations of wt and wt^2. This modification of your code works:newdata <- with(mtcars, expand.grid(wt=unique(wt), gear=unique(gear), hp=mean(hp)))newdata$wtsq <- newdata$wt^2newdata$pred <- predict(m, newdata)p <- ggplot(newdata, aes(x=wt, y=pred, colour=gear, group=gear))p + geom_line() + ggtitle("Varying Slopes") 这篇关于如何预测和绘制lmer或glmer中的非线性变化斜率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 13:00