从Lme或lmerMod对象中绘制R中各个二次增长曲线的随机子集?

问题描述 投票:0回答:2

我想使用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)

输出:

Graph all 105 participants

r ggplot2 lme4 nlme sjplot
2个回答
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)

0
投票

这可能有效:

library(ggeffects)
pr <- ggpredict(growthquadsl, c("time_point", "ParticipantID [sample=10]"), type = "re")
plot(pr)

有关示例,请参阅qazxsw poi以获得更全面的解释,并且qazxsw poi如何在特定值处绘制边际效应(例如,对于n = 10的随机子样本,如上例所示)。

© www.soinside.com 2019 - 2024. All rights reserved.