0

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

输出:

绘制所有 105 名参与者的图表

4

2 回答 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)
于 2019-03-09T18:39:26.860 回答
0

这可能有效:

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

有关示例的更全面解释,请参阅此小插图以及如何在特定值处绘制边际效应(例如,对于 n=10 的随机子样本,如上例所示)。

于 2019-03-18T23:13:29.680 回答