2

I'm analysing some repeated measures drug trials data and I'm not sure how to plot the lmer results when using faceted ggplots. I have made an initial plot of the individual slopes from the master dataset, but I'm doing the lmer analyses separately by sex.

Using publicly available data, which has only 2 treatment groups compared to the four I have, this is the replicable example below. It uses the reshape2, lme4, and ggplot2 packages.

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

Information about the dataset is here.

How do I plot the correct summary line from lmer in both facets, which differ on the basis of Treatment?

In my real life example, I'll also be analysing the females so there will be two sets of lines to plot per facet.

Michelle
  • 1,281
  • 2
  • 16
  • 31

2 Answers2

1

Create a data frame (e.g. lines.df) with intercept (e.g. int) and slope (slo) variables where each line of the df corresponds to one facet, then plot over the top:

+ geom_abline(aes(intercept = int, slope = slo), data = lines.df)
Scransom
  • 3,175
  • 3
  • 31
  • 51
  • As I will have two lines per facet, do I need to add a Sex indicator to `lines.df` so I can get the correct number of lines per facet? – Michelle Apr 27 '15 at 01:17
  • You could either add `sex` to `lines.df` and have two lines for each facet with one sex for each, and add e.g. `colour=sex` of `linetype=sex` to `aes` or add a second data frame and call it too with an extra line of code – Scransom Apr 27 '15 at 01:27
  • I'm getting all four lines to plot in every facet, how do I instruct ggplot which row in lines.df to plot per facet? This is four lines, and not two, because I've implemented in my real work and not the example. – Michelle Apr 27 '15 at 03:57
  • 1
    I think I fixed it by adding in the value of the Treatment, and now it knows in which facet to draw each line. – Michelle Apr 27 '15 at 04:07
0

It isn't entirely clear what you mean by 'correct summary line', but I've adapted the code in this answer to your data. Is this the output you need?

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)

And get the following output

Random slopes plot

Community
  • 1
  • 1
r.bot
  • 5,309
  • 1
  • 34
  • 45
  • Hi r.bot, what I'm after is the original facets plot, with a mean line from the lmer results overplotted on each facet. I'm trying to visual essentially the group averages (sex with treatment) on top of the raw lines so I can get a sense of overall change. So, if it was an OLS regression, the overplot would be the line-of-best fit, on top of the individual slopes. – Michelle Apr 26 '15 at 10:14