我正在分析一些重复测量的药物试验数据,并且不确定使用多面ggplots时如何绘制lmer结果。我已经从主数据集中绘制了单个斜率的初始图,但是我正在按性别分别进行lmer分析。
使用可公开获得的数据,该数据只有两个治疗组,而我只有四个,这是下面的可复制示例。它采用了reshape2
,lme4
和ggplot2
包。
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
有关数据集的信息在这里。
如何在两个方面根据lmer绘制来自lmer的正确汇总线Treatment
?
在我的现实生活示例中,我还将分析女性,因此每个方面将绘制两组线。
创建一个数据帧(例如lines.df
),并使用intercept(例如int
)和slope(slo
)变量,其中df的每一行对应一个面,然后在顶部绘制:
+ geom_abline(aes(intercept = int, slope = slo), data = lines.df)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句