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)) 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)) Any ideas? Any help would be greatly appreciated with these two plots. Thanks so much! -- 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.
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
## dadrivr <dadrivr@gmail.com> ## I suggest a lattice plot of all two-way interactions on the ## off-diagonals and all marginal main effects on the main diagonal. ## Please install HH and then ?interaction2wt for some examples. ## install.packages("HH") ## if you don't already have it library(HH) ?interaction2wt mydata <- data.frame(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)) with(mydata,interaction.plot(happiness,gender,score)) ## your first plot with(mydata,interaction.plot(age,gender,score)) ## your second plot ## You didn't say if male is 1 or 0. I am arbitrarily setting 0 to male mydata$genderMF <- factor(mydata$gender, labels=c("M", "F")) ## The first plot you produced appears in the ## score ~ age | genderMF ## panel interaction2wt(score ~ age + genderMF, data=mydata) ## Happiness looks more like a response variable then a conditioning ## factor. Therefore I need more information before I can make any ## suggestions about how to graph happiness. ## Rich [[alternative HTML version deleted]]