在多面ggplot中绘制lmer结果

plot lmer result in faceted ggplot

我正在分析一些重复测量的药物试验数据,但我不确定在使用分面 ggplots 时如何绘制 lmer 结果。我已经从主数据集中绘制了单个斜率的初始图,但我正在按性别分别进行 lmer 分析。

使用公开可用的数据,与我拥有的四个治疗组相比,只有两个治疗组,这是下面可复制的示例。它使用 reshape2lme4ggplot2 包。

CatAnx <- read.fwf(file=("http://www.stat.ufl.edu/~winner/data/cats_anxiety1.dat"),
               widths=c(-6,2,-5,3,-5,3,-7,1,-7,1,-7,1,-7,1,-7,1,-6,2,-6,2,-6,2,-6,2,-6,2))
colnames(CatAnx) <- c('ID','Weight','Age_Months','Gender','Environment','Origin','Treatment','Result','EmoTime1','EmoTime2',
                  'EmoTime3','EmoTime4','EmoTime5')
library("reshape2")
CatAnxRM <- melt(CatAnx, id.vars=c("ID", "Gender", "Treatment"), measure.vars=c("EmoTime1", "EmoTime2", "EmoTime3",
                                                                            "EmoTime4", "EmoTime5"))
CatAnxRM$Sex <- with(CatAnxRM, ifelse(Gender==1, "Neut Female", ifelse(Gender==2, "Neut Male", "Whole Female")))
CatAnxRM$Time <- with(CatAnxRM, ifelse(variable=="EmoTime1", 1, ifelse(variable=="EmoTime2", 2, ifelse(variable=="EmoTime3", 3,
                                  ifelse(variable=="EmoTime4", 4,5)))))
CatAnxRM.Male <- subset(CatAnxRM, Gender=="2")
library("lme4")
Male.lmer <- lmer(value ~ Treatment * Time + (Time + 1|ID), data=CatAnxRM.Male)
library("ggplot2")
AnxScores<-ggplot(CatAnxRM, aes(Time, value, colour=Sex))+
geom_line(aes(group = ID))+
labs(x="Time Anxiety Measured", y="Anxiety Score", title="Effect of Zylkene on Anxiety")+ 
facet_grid(. ~ Treatment)
AnxScores

关于数据集的信息is here

如何在两个方面绘制 lmer 的正确摘要线,这两个方面因 Treatment 而不同?

在我的真实生活示例中,我还将分析女性,因此每个面将绘制两组线。

'correct summary line' 的意思并不完全清楚,但我已根据您的数据调整了 this answer 中的代码。这是您需要的输出吗?

newdata <- with(CatAnxRM.Male, expand.grid(Treatment=unique(Treatment),
                                           Time=unique(Time), Sex = unique(Sex),
                                           ID=unique(ID)))

newdata$pred <- predict(Male.lmer, newdata)

p <- ggplot(newdata, aes(x=Time, y=pred, colour=Sex, group=ID))
p + geom_line() + ggtitle("Varying Slopes") + 
    facet_grid(. ~ Treatment)

并得到如下输出

创建一个带有截距(例如int)和斜率(slo)变量的数据框(例如lines.df),其中df的每一行对应一个方面,然后绘制在顶部:

+ geom_abline(aes(intercept = int, slope = slo), data = lines.df)