2

In my dataset I have 3 groups and I would like to plot an interaction between group and x on y.

id <- c(1,1,1,2,2,2,3,3,3)
group <- c(0,0,0,1,1,1,2,2,2)
x <- c(20,50,30,50,65,80,20,50,60)
y <- c(120,130,150,200,210,180,160,170,120)

I already tried it with "interaction.plot" but it didn't work.

interaction.plot(x,group,y)

Does someone have a good R syntax to plot this interaction?

M.K.
  • 21
  • 2
  • Care to explain more specifically why it did not work? I enter this code and I do get a plot. What's wrong with it? – cgmil Sep 20 '16 at 14:04

1 Answers1

1

You have to change class of group into factor irrespective of what you want. Indeed interaction.plot() is for two-way combinations of factors and your x isn't factor. But if the one is continuous, interaction.plot() gives some help. In your case, the output says "it is a foolish idea to consider an interaction with such data".

But if you want to do (I supposed you wanted a linear model) :

df <- data.frame(id = id, x = x, y = y, group = as.factor(group))

## Base plot
model <- lm(y ~ x * group, data = df)
xpara <- 20:80

plot(y ~ x, data = df, col=c(2:4)[group], pch=19)
for(i in 1:3) lines(xpara, predict(model, data.frame(x = xpara, group = as.factor(i-1))), col = i+1)
legend("topleft",paste(c("group0","group1","group2")), pch=19, lty=1, col=c(2:4))

## ggplot2 (I plotted lines and confidence intervals to interpret)
library(ggplot2)
ggplot(df, aes(x = x, y = y, colour = group)) + 
  geom_point(size = 4) +
  geom_smooth(method = "lm", se = T, fullrange = T)

enter image description here

[Edited]

If class of the model supported by predict(), the way is basically the same.

df2 <- data.frame(id = as.factor(id), x = x, y = y, group = as.factor(group))
library(nlme)

# first; make model
lme.mod <- lme(y ~ x * group, random = ~ 1|id, data = df2)

# second; get predicted values
xpara <- 20:80   # make a vector for an independent variable you use as x.
y.g1 <- predict(lme.mod, data.frame(x = xpara, group = "0", id = "1"), type="response")
y.g2 <- predict(lme.mod, data.frame(x = xpara, group = "1", id = "1"), type="response")
y.g3 <- predict(lme.mod, data.frame(x = xpara, group = "2", id = "1"), type="response")

# third; draw
plot(y ~ x, df2, col=c(2:4)[group], pch=19)
lines(xpara, y.g1, col=2)
lines(xpara, y.g2, col=3)
lines(xpara, y.g3, col=4)

## Simplified version
lev <- levels(df$group)

plot(y ~ x, data = df2, col=c(2:4)[group], pch=19, ylab="y (id = "1")")
for(i in seq.int(length(lev))) 
  lines(xpara, predict(lme.mod, data.frame(x = xpara, group = lev[i], id = 1)), col = i+1)
legend("topleft",paste(c("group0","group1","group2")), pch=19, lty=1, col=c(2:4))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cuttlefish44
  • 6,586
  • 2
  • 17
  • 34
  • Thank you for your answer and syntax. However, because my dataset has a hierachical structure I would like to graph the interaction in a multilevel model so I would rather use the comand "lme" instead of "lm". Is this possible in a multilevel design? – M.K. Sep 21 '16 at 09:04