我想使用ggplot2或sjPlot从R中的lme4或nlme绘制10条个体二次生长曲线的随机子集。我知道如何对线性线执行此操作,但对二次线则不行。显然,下面有105位参与者的情节太疯狂了。
我的模特:
growthquadsl <- lmer(count~time_point+I(time_point^2) + (1+time_point|ParticipantID),
REML = TRUE,
data = longfix)
summary(growthquadsl)
Linear mixed model fit by REML ['lmerMod']
Formula: count ~ time_point + I(time_point^2) + (1 + time_point |
ParticipantID)
Data: longfix
输出:
REML criterion at convergence: 23004.3
Scaled residuals:
Min 1Q Median 3Q Max
-3.3234 -0.6165 -0.0802 0.5312 4.2995
Random effects:
Groups Name Variance Std.Dev. Corr
ParticipantID (Intercept) 28228422 5313.0
time_point 209490 457.7 -0.40
Residual 18922159 4350.0
Number of obs: 1157, groups: ParticipantID, 107
Fixed effects:
Estimate Std. Error t value
(Intercept) 14242.57 605.82 23.510
time_point 874.18 157.42 5.553
I(time_point^2) -73.47 14.75 -4.979
Correlation of Fixed Effects:
(Intr) tm_pnt
time_point -0.510
I(tm_pnt^2) 0.355 -0.923
所有曲线:
ggplot(longfix, aes(x=time_point, y=count)) +
geom_line(aes(y = predict(growthquadsl, level=1, group=ParticipantID), colour = factor(ParticipantID)), size = 1)
输出:
答案 0 :(得分:0)
没有一些数据就无法测试,但是您可以对数据框进行采样并绘制该样本中的所有参与者。
# With `modelr::add_predictions`, if it works correctly for your type of model
longfix %>%
sample_n(100) %>%
modelr::add_predictions(fit) %>%
ggplot(aes(x = time_point, y = pred)) +
geom_line(aes(colour = factor(growthquadsl)), size = 1)
# Otherwise create a column with the predictions explicitly
longfix %>%
sample_n(100) %>%
mutate(pred = predict(fit, newdata = .)) %>%
ggplot(aes(x = time_point, y = pred)) +
geom_line(aes(colour = factor(growthquadsl)), size = 1)
答案 1 :(得分:0)
这可能有效:
library(ggeffects)
pr <- ggpredict(growthquadsl, c("time_point", "ParticipantID [sample=10]"), type = "re")
plot(pr)
有关示例的更全面说明,请参见this vignette,以及this vignette如何在特定值处绘制边际效应(例如,如上述示例中,对于n = 10的随机子样本)。