我试图查看如何为我一直在运行的增长曲线模型绘制R中的二次增长。 型号:
m1 <- lmer(score ~ Time + Group + Time_Sqaure +
(1 + School | Subject), data=df, REML = FALSE)
tab_model(m1)
时间(B = 9.58,p <.01)和时间平方(B =-0.51,p <.01)以及组(B = 2.77,p <.01)的差异都很大。
如果我使用plot_model,它会为我提供适合每个组的最佳拟合线。
plot_model(m1, type = "pred", terms = c("Time", "Group"))
有没有一种方法可以绘制拟合曲线或二次增长曲线,以显示增长速度随着时间的推移而变慢?
谢谢!
答案 0 :(得分:3)
要让sjPlot::plot_model
理解发生了什么,您必须将Time_Square
输入I(Time^2)
而不是单独的预测变量。
鉴于df$Time_Square <- df$Time^2
,以下两个模型应为您提供相同的结果:
m1 <- lmer(score ~ Time + Group + Time_Square +
(1 + School | Subject), data=df, REML = FALSE)
m2 <- lmer(score ~ Time + Group + I(Time^2) +
(1 + School | Subject), data=df, REML = FALSE)
但是,在第二个模型中,很明显两次输入了预测变量Time
,因此在使用sjPlot::plot_model(...)
进行绘制时可以考虑该变量。
为了确保这一点,我使用以下模拟数据进行了测试:
library(dplyr)
grps <- 2 #number of groups
subj <- 100 #number of subjects within group
obs <- 10 #number of observations/times per subjects
b_0 <- 0 #overall intercept
b_1 <- 9.58 #linear time effect
b_2 <- -0.51 #quadratic time effect
sd_b0 <- 0.4 #SD of random intercept per subject
sd_b1 <- 3 #SD of random slope per subject
sd_b3 <- 1 #SD of group effect (you can simulate more than 2 groups)
sd_resid <- 10 #SD of residuals
df <- list(Group = factor(rep(letters[1:grps], each=obs*subj)),
Subject = factor(rep(1:subj, times=grps, each=obs)),
Time = rep(1:obs, times=subj*grps)
) %>% as.data.frame()
df$TimeSq <- df$Time^2
subj_b0 <- rnorm(subj, b_0, sd_b0) %>% rep(times=grps, each=obs)
subj_b1 <- rnorm(subj, b_1, sd_b1) %>% rep(times=grps, each=obs)
grp_m <- rnorm(grps, 0, sd_b3) %>% rep(times=, each=subj*obs)
df$Score <- with(df, subj_b0 + Time*subj_b1 + (Time^2)*b_2 + grp_m + rnorm(grps*subj*obs, 0, sd_resid))
fit1 <- lme4::lmer(Score ~ Time + I(Time^2) + Group + (Time | Subject), data=df)
sjPlot::plot_model(fit1, type="pred", terms=c("Time"))