0

I'm analyzing some longitudinal data using lme4 package (lmer function) with 3 Levels: measurement points nested in individuals nested in households. I'm interested in linear and non-linear change curves surrounding a specific life event. My model has many time predictors (indicating linear change before and after the event occurs and indicating non-linear change (i.e., squared time variables) before and after the event occurs). Additionally, I have several Level-2 predictors that do not vary with time (i.e., personality traits) and some control variables (e.g., age, gender). So far I did not include any random slopes or cross-level interactions.

This is my model code:

model.RI <- lmer(outcome ~ time + female_c + age_c + age_c2 + preLin + preLin.sq + postLin + postLin.sq + per1.c + per2.c + per3.c + per4.c + per5.c + (1 | ID) +  (1 | House))

outcome = my dependent variable

time = year 1, year 2, year 3 ... (until year 9); this variable symbolizes something like a testing effect

female_c = gender centered

age_c = age centered

age_c2 = age squared centered

preLin = time variable indicating time to the event (this variable is 0 after the event has occurred and is -1 e.g. one year ahead of the event, -2 two years ahead of the event etc.)

preLin.sq = squared values of preLin

postLin = time variable indicating time after the event (this variable is 0 before the event and increases after the event has occurred; e.g. is +1 one year after the event)

postLin.sq = squared values of postLin

per1.c until per5.c = personality traits on Level 2 (centered)

ID = indicating the individuum

House = indicating the household

I was wondering how I could plot the predicted values of this lmer model (e.g., using ggplot2?). I've plotted change curves using the method=gam in R. This is a rather data-driven method to inspect the data without pre-defining if the curve is linear or quadratic or whatever. I would now like to check whether my parametric lmer model is comparable to that data-driven gam-plot I already have. Do you have any advise how to do this?

I would be more than happy to get some help on this! Please also feel free to ask if I was not precise enough on my explanation of what I would like to do!

Thanks a lot!

Follow this link: This is how my gam-plot looks like and I hope to get something similar when plotting the predicted values of my lmer model!

4

1 回答 1

1

您可以使用ggeffects-packageggpredict()中的-function 。如果要绘制时间 ( ) 的预测值,只需编写:preLin

ggpredict(model.RI, "preLin")

该函数返回一个数据框(参见文章),您可以在 ggplot 中使用它,但您也可以直接绘制结果:

ggpredict(model.RI, "preLin") %>% plot()

或者

p <- ggpredict(model.RI, "preLin")
plot(p)

您也可以使用sjPlot-package,但是,对于边际效应/预测值,sjPlot::plot_model()-function 内部只调用ggeffects::ggpredict(),因此结果基本相同。

模型的另一个注意事项:如果您有纵向数据,您还应该将时间变量包括为随机斜率。我不确定如何postLin准确地引用preLin,但是如果preLin捕获了所有测量值,您至少应该像这样编写模型:

model.RI <- lmer(
  outcome ~ time + female_c + age_c + age_c2 + preLin + preLin.sq + 
  postLin + postLin.sq + per1.c + per2.c + per3.c + per4.c + per5.c + 
  (1 + preLin | ID) +  (1 + preLin | House)
)

如果您还假设每个人的二次趋势 ( ID),您甚至可以将平方项添加为随机斜率。

正如您的图形示例建议使用样条曲线,您也可以尝试以下操作:

library(splines)
model.RI <- lmer(
  outcome ~ time + female_c + age_c + age_c2 + bs(preLin) 
  postLin + postLin.sq + per1.c + per2.c + per3.c + per4.c + per5.c + 
  (1 + preLin | ID) +  (1 + preLin | House)
)
p <- ggpredict(model.RI, "preLin")
plot(p)

我上面提到的网站上也演示了样条曲线的示例。

编辑: 另一个注释与嵌套有关:您当前正在建模一个完全交叉或交叉分类的模型。如果它完全嵌套,随机部分将如下所示:

... + (1 + preLin | House / ID)

(另见这个小代码示例)。

于 2018-06-11T13:26:04.530 回答