2

I did a regression analysis with the following variables: Predictor = dummy variable, dependent Variable = metric, moderator variable = metric. I now want to show my results in a figure. The interaction should be shown by three regression lines. One for moderator = Mean (0), one for moderator = -1 SD, and one for moderator = + 1 SD.

How can I do this in R?

gung - Reinstate Monica
  • 132,789
  • 81
  • 357
  • 650
  • possible duplicate of [How can one plot continuous by continuous interactions in ggplot2?](http://stats.stackexchange.com/questions/6557/how-can-one-plot-continuous-by-continuous-interactions-in-ggplot2) – smillig Jan 28 '15 at 15:51
  • For future reference, be aware that questions only asking for code, are off-topic here (but can be on-topic on [SO] w/ a [reproducible example](http://stackoverflow.com/q/5963269/1217536) for people to work with). – gung - Reinstate Monica Jan 28 '15 at 17:07
  • @smillig, I think the question merits discussion of how best to plot data like that (see my answer below). I think that aspect makes it on-topic & sufficiently distinct from the linked thread. – gung - Reinstate Monica Jan 28 '15 at 17:08
  • 1
    @smillig, it is phrased as 'how... in R?', but my opinion has always been that the determination should be based on *what the OP needs to understand*. If the required concepts are strictly about programming / R-usage, it's off-topic, but if they're statistical (etc) ideas, it's on-topic, no matter how the Q is phrased. (NB, this is my opinion; others seem to disagree.) B/c what the OP needs to understand here are principles of data vis (& that are distinct from what is explained in the linked thread), I think this can stay here / open. Of course, you are not obliged to agree w/ me. – gung - Reinstate Monica Jan 28 '15 at 17:33
  • Regarding whether the study is experimental in nature, that is orthogonal to both the data vis principles & the coding required. Instead of control vs experimental, it could just as well be male vs female, & everything else would be the same. – gung - Reinstate Monica Jan 28 '15 at 17:35

1 Answers1

2

Your suggestion would not display most of the information you have. Instead, display two lines: one for the control group and one for the experimental group. Then you can see all of the information you have, rather than just six points / bars. To see how much more informative the former can be, let's make some pretend data:

set.seed(4255)                          # this makes the example exactly reproducible

predictor = rep(c("C", "E"), each=50)   # here I'm generating the data & fitting the 
moderator = rnorm(100, mean=20, sd=12)  #  model
dependent = 13 + 5*I(predictor=="E") + 1*moderator + 
            -1*I(predictor=="E")*moderator + rnorm(100, mean=0, sd=4)
model     = lm(dependent~predictor+moderator+predictor:moderator)
summary(model)
# ...
# Coefficients:
#                      Estimate Std. Error t value Pr(>|t|)    
# (Intercept)           13.2640     1.5817   8.386  4.3e-13 ***
# predictorE             2.9518     1.9279   1.531    0.129    
# moderator              0.9869     0.0680  14.513  < 2e-16 ***
# predictorE:moderator  -0.9512     0.0866 -10.983  < 2e-16 ***
# ...

windows()            # this plots the data w/ different symbols & colors
  plot(  moderator[predictor=="C"], dependent[predictor=="C"], 
         col="gray40", xlim=c(-15,50), ylim=c(5,60), xlab="moderator",
         ylab="predictor")
  points(moderator[predictor=="E"], dependent[predictor=="E"], 
         col="cornflowerblue", pch=2)
    # below I calculate the predicted values & plot them as lines over the data
  new.df = data.frame(moderator=-15:50, predictor="C")  
  lines(-15:50, predict(model, new.df))
  new.df = data.frame(moderator=-15:50, predictor="E")
  lines(-15:50, predict(model, new.df), col="darkblue", lty=2)
  legend("topleft", legend=c("C", "E"), col=c("black", "darkblue"), pch=1:2, lty=1:2)

enter image description here

Now let's make a barplot:

mm     = mean(moderator)  # [1] 19.20544
sdm    = sd(moderator)    # [1] 10.67953
new.df = data.frame(moderator=rep(c(mm-sdm, mm, mm+sdm), times=2), 
                    predictor=rep(c("C","E"), each=3)             )
tab = matrix(predict(model, new.df), ncol=2)
colnames(tab) = c("C","E")

windows()
  barplot(tab, beside=T, col=c("gray25","gray50","gray75"))
  legend("topright", legend=c("-1 SD", "mean", "+1 SD"), pch=15,
         col=c("gray25","gray50","gray75"))

enter image description here

gung - Reinstate Monica
  • 132,789
  • 81
  • 357
  • 650