[R] Plot interaction in multilevel model
William Revelle
lists at revelle.net
Sat Mar 6 23:55:20 CET 2010
I think both graphs will follow the same technique. This is partly
laid out in how to do graphs of anova output at
http://personality-project.org/r/r.plotregressions.html (which is
part of the short (and out of date) guide to R for psychologists)
http://personality-project.org/r/
At 6:39 PM -0800 3/5/10, dadrivr wrote:
>I am trying to plot an interaction in a multilevel model. Here is some
>sample data. In the following example, it is longitudinal (i.e., repeated
>measures), so the outcome, score (at each of the three time points), is
>nested within the individual. I am interested in the interaction between
>gender and happiness predicting score.
>
>id <- c(1,1,1,2,2,2,3,3,3)
>age <- c(10,15,20,10,15,20,10,15,20)
>gender <- c(1,1,1,0,0,0,1,1,1)
>happiness <- c(50,30,25,70,65,80,70,40,60)
>score <- c(180,140,110,240,220,280,150,140,130)
>mydata <- data.frame(id,age,gender,happiness,score)
>
>I am looking to create two plots:
>
>1. A plot with score on the y-axis, happiness on the x-axis, gender as the
>moderating variable, and a linear best-fit line for each level of gender
>(male & female). Here is my attempt, but I don't know how to make it into
>linear best-fit lines:
>with(mydata,interaction.plot(happiness,gender,score))
color <- c("red","blue")
plot(happiness,score,col=color[gender+1])
by(mydata,gender,function(x) abline(lm(x$score~x$happiness)))
>
>2. A plot with score on the y-axis, age on the x-axis, and 4 different best
>fit lines representing the following levels of gender and happiness (male hi
>happy, male lo happy, female hi happy, female lo happy). Here is my
>attempt, but I don't know how to create the 4 different best-fit lines
>representing the 4 different interaction levels:
>with(mydata,interaction.plot(age,gender,score))
by(mydata,gender*age,function(x) abline(lm(x$score~x$happiness)))
>
>Any ideas? Any help would be greatly appreciated with these two plots.
>Thanks so much!
I hope this does what you want.
Bill
>--
>View this message in context:
>http://n4.nabble.com/Plot-interaction-in-multilevel-model-tp1580370p1580370.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
William Revelle http://revelle.net/revelle.html
2815 Lakeside Court http://revelle.net/lakeside
Evanston, Illinois
It is 6 minutes to midnight http://www.thebulletin.org
More information about the R-help
mailing list