2

我对超过 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 
4

2 回答 2

2

这是你的意思吗?

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

脏溶液

于 2017-04-13T14:02:46.890 回答
0

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

## 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()
于 2017-04-14T09:34:05.627 回答