[R-sig-ME] Plotting interactions from lme with ggplot

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Jun 14 10:05:52 CEST 2012


Something like this?

newdata <- expand.grid(
  time = pretty(dataset$time, 20),
  predictor = qnorm(pnorm(c(-1, 0, 1)), mean = mean(dataset$predictor, na.rm = TRUE), sd = sd(dataset$predictor, na.rm = TRUE)),
  moderator = qnorm(pnorm(c(-1, 0, 1)), mean = mean(dataset$moderator, na.rm = TRUE), sd = sd(dataset$moderator, na.rm = TRUE))
)
newdata$outcome <- predict(model, newdata = newdata, level = 0)
newdata$fPredictor <- factor(newdata$predictor, labels = c("LP", "MP", "HP"))
newdata$fModerator <- factor(newdata$moderator, labels = c("LM", "MM", "HM"))
newdata$Combination <- interaction(newdata$fPredict, newdata$fModerator)
library(ggplot2)
ggplot(newdata, aes(x = time, y = outcome, colour = fPredictor, linetype = fModerator)) + geom_line()
ggplot(newdata, aes(x = time, y = outcome, colour = Combination)) + geom_line()



ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-sig-mixed-models-bounces op r-project.org [mailto:r-sig-mixed-models-bounces op r-project.org] Namens Isaac Petersen
Verzonden: woensdag 13 juni 2012 21:50
Aan: R-sig-mixed-models
Onderwerp: [R-sig-ME] Plotting interactions from lme with ggplot

I'm fitting a lme growth curve model with two predictors and their interaction as predictors. The multilevel model is nested so that level 1 is time within the individual, and level 2 is the individual.  I would like to plot the mean group-level trajectories at plus and minus 1 SD from the mean of the main effects composing the interaction term using ggplot.
 Thus, the plot should have 4 lines (mean trajectories) representing all combinations of the two predictors (where LO = -1sd from its mean, and HI =
+1sd from its mean):
-LO predictor, LO moderator
-LO predictor, HI moderator
-HI predictor, LO moderator
-HI predictor, HI moderator

They are growth curves, so the plot should have 'time' on the x-axis and 'outcome' on the y-axis.  Here's a sample model:

library(nlme)

set.seed(123)

dataset <- NULL
dataset$id <- rep(1:100, each=10)
dataset$time <- rep(1:10,100)
dataset$outcome <- rnorm(1000, mean=100, sd=15) dataset$predictor <- rnorm(1000, mean=10, sd=3) dataset$moderator <- rnorm(1000, mean=5, sd=2)

dataset <- as.data.frame(dataset)
dataset[sample(1:1000, 400, replace=F),"outcome"] <- NA dataset[sample(1:1000, 400, replace=F),"predictor"] <- NA dataset[sample(1:1000, 400, replace=F),"moderator"] <- NA

model <- lme(outcome~ time + predictor + moderator + predictor*moderator,
random=~1 + time + predictor|id, data=dataset, na.action=na.omit)

How can I plot the interaction on the mean group-level trajectories using ggplot?  I'm new to ggplot, so any help would be greatly appreciated.  Thanks in advance!

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-mixed-models op r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-sig-mixed-models mailing list