2017-04-13 79 views
1

我對4個時間點的個體進行了縱向重複測量。按照固定效應和隨機斜率的時間混合模型分析,我已經使用平均值來估計每個時間點的平均值以及95%的置信區間。我現在想繪製帶有時間點(x)和結果變量(y)與CI的平均值的線圖。我可以使用例如ggplot來繪製我從lsmeans得到的結果?還是有另一種巧妙的方式來繪製這個?混合模型/ lsmeans結果的線圖(使用ggplot?)

,我從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 

回答

0

這是你的意思?

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

Dirty solution

+0

是的,這也正是它。如何在上面的代碼中將正確的數據轉化爲d?我已經嘗試了 d < - summary(lsmeans(model,pairwise〜time,adjust =「tukey」)) 但是這會返回到ggplot無法使用的列表。 –

+0

試試這個:'library(broom); d < - tidy(lsmeans(model,pairwise〜time,adjust =「tukey」))' – PoGibas

+0

整潔的命令給我一個錯誤信息,無法識別這個列表 –

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