【问题标题】:Line plot of mixed models / lsmeans results (with ggplot?)混合模型/ lsmeans 结果的线图(使用ggplot?)
【发布时间】:2017-09-09 16:32:52
【问题描述】:

我对超过 4 个时间点的个人进行了纵向重复测量。在以时间作为固定效应和随机斜率的混合模型分析之后,我使用 lsmeans 来估计每个时间点的平均值以及 95% 置信区间。我现在想用 CI 绘制带有时间点 (x) 和结果变量 (y) 平均值的折线图。我可以使用例如ggplot 绘制我从 lsmeans 得到的结果?还是有另一种聪明的方法来绘制这个?

我从 lsmeans 获得的结果以及我想要绘制的结果(随着时间的推移 lsmean、lower.CL、upperCL)是:

$lsmeans
time    lsmean        SE df  lower.CL upper.CL
0    21.967213 0.5374422 60 20.892169 23.04226
1    16.069586 0.8392904 60 14.390755 17.74842
2    13.486802 0.8335159 60 11.819522 15.15408
3     9.495137 0.9854642 60  7.523915 11.46636

Confidence level used: 0.95 

【问题讨论】:

    标签: r plot ggplot2 mixed-models lsmeans


    【解决方案1】:

    这是你的意思吗?

    # To convert from lsmeans output (d <- lsmeans(paramaters))
    d <- summary(d)$lsmeans[c("lsmean", "lower.CL", "upper.CL")]
    
    library(ggplot2)
    ggplot(d, aes(time)) +
        geom_line(aes(y = lsmean)) +
        geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL),
                      width = 0.2) +
        geom_point(aes(y = lsmean), size = 3, 
                   shape = 21, fill = "white") +
        labs(x = "Time", y = "ls mean",
             title = "ls mean result over time") +
        theme_bw()
    

    【讨论】:

    • 是的,就是这样。在上面的代码中,如何将正确的数据放入 d 中?我已经尝试使用 d
    • 试试这个:library(broom); d &lt;- tidy(lsmeans(model, pairwise~time, adjust="tukey"))
    • tidy 命令给我一个错误信息,无法识别这个列表
    • 然后试试这个:tmp &lt;- summary(tidy(lsmeans(model, pairwise~time, adjust="tukey"))); d &lt;- tmp[c("lsmean", "lower.CL", "upper.CL")]
    • 同样的错误信息“没有识别此列表的整理方法”
    【解决方案2】:

    总而言之,将为您提供混合模型的估计和绘图的整个代码是:

    ## random slope model
    summary(model <- lme(outcome ~ time, random = ~1+time|ID, data = data, 
    na.action = na.exclude, method = "ML"))
    
    ## pairwise comparisons of timepoints
    install.packages("lsmeans")
    library(lsmeans)
    lsmeans(model, pairwise~time, adjust="tukey")
    
    ### Draw the picture
    d <- summary(lsmeans(model, ~time))
    
    library(ggplot2)
    ggplot(d, aes(time)) +
      geom_line(aes(y = lsmean, group = 1)) +
      geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = 0.2) +
      geom_point(aes(y = lsmean), size = 3, shape = 21, fill = "white") +
      labs(x = "Time", y = "ls mean", title = "ls mean result over time") +
      theme_bw()
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 2021-12-29
      • 2017-02-01
      • 2017-09-15
      • 2018-07-14
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2019-11-12
      相关资源
      最近更新 更多