【问题标题】:Plotting random subset of individual quadratic growth curves in R from lme or lmerMod object?从 lme 或 lmerMod 对象绘制 R 中单个二次增长曲线的随机子集?
【发布时间】:2019-03-09 17:47:17
【问题描述】:

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


    【解决方案1】:

    在没有一些数据的情况下无法对此进行测试,但您可以对数据框进行采样并绘制该样本中的所有参与者。

    # 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)
    

    【讨论】:

      【解决方案2】:

      这可能有效:

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

      请参阅 this vignette 以获得更全面的示例说明,以及 this vignette 如何绘制特定值的边际效应(例如,对于 n=10 的随机子样本,如上例所示)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-09
        • 2021-04-12
        • 2017-02-09
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        相关资源
        最近更新 更多