I have a model in R that includes a significant three-way interaction between two continuous independent variables IVContinuousA, IVContinuousB, IVCategorical and one categorical variable (with two levels: Control and Treatment). The dependent variable is continuous (DV).

model<-lm(DV ~ IVContinuousA * IVContinuousB * IVCategorical)

You can find the data here

I am trying to find out a way to visualise this in R to ease my interpretation of it (perhaps in ggplot2?).

I thought that I could dichotomise IVContinuousB into high and low values (so it would be a two-level factor itself: IVContinuousBHigh = mean of IVContinuousB + sd of IVContinuousB; IVContinuousBLow = mean of IVContinuousB - sd of IVContinuousB).

I then planned to plot the relationship between DV and IV ContinuousA and fit lines representing the slopes of this relationship for different combinations of IVCategorical and my new dichotomised IVContinuousB:

IVCategoricalControl and IVContinuousBHigh IVCategoricalControl and IVContinuousBLow IVCategoricalTreatment and IVContinuousBHigh IVCategoricalTreatment and IVContinuousBLow

My first question is - does this sound like a viable solution to producing an interpretable plot of this three-way-interaction? I want to avoid 3D plots if possible as I don't find them intuitive.

Secondly - does anyone have any idea on how to code for this in R via ggplot2? I appreciate this latter question may be more apt for Stack Overflow but given the problem I have is primarily a methodological one I thought I'd post it on Cross Validated as a primary choice - hope this is ok.

Thank you very much in advance for your thoughts.

Note that there are NAs (left as blanks) in the DV column and the design is unbalanced - with slightly different numbers of datapoints in the Control vs Treatment groups of the variable IVCategorical.

  • 1
    $\begingroup$ Maybe a scatterplot of the two continuous variables, and the categorical variable to encode color? $\endgroup$ – user2089357 Mar 2 '18 at 15:15
  • $\begingroup$ Thanks so much for your thoughts - I don't think that would work as I have one continuous dependent variable, two continuous independent dependent variables and one categorical independent, so unless I did a 3D plot (which I don't want to do) it wouldn't be possible to visualise... $\endgroup$ – Sarah Mar 3 '18 at 11:49
  • $\begingroup$ so only the effect of the interaction of the three variables was significant? $\endgroup$ – g3o2 Mar 4 '18 at 15:51

One can make a variety of charts that include all the variables, but it's another task to get meaningful information out, especially for a static view. Whatever you use, it's a good idea to try it on some simulated data that has interactions of various sizes so you can get a sense of how the interactions will show up in the views.

It's difficult to visualize the continuous interactions at the same time as the main effects because the latter are often more dominant. It can be useful to look at the interaction against the residuals after applying the main effects -- let's test it out.

With your data (thanks!), we can get a sense of the main effects by looking at one independent variable at a time. Further, we can color by the categorical input to see how that variable interacts with the others.

enter image description here

All the variables seem to make a difference (no zero slopes), but there are no obvious interactions with the categorical variable since the different-colored smoothers are roughly parallel. That is, the categorical variable moves the curve up or down, but doesn't obviously change its shape.

We can also see from these plots that the distribution of the dependent variable is skewed, which probably needs to be taken into account. With knowledge of the domain perhaps there this is a transformation that makes sense, or perhaps a more robust modeling technique is needed.

Skipping over that important detail for the sake of discussing interaction visualization, let's assume we accept what look like linear effects for these variables. Then can make a model and compute the residuals. Now we can look at the residuals against the products of the continuous variables (or their deltas) to get a sense of interaction.

Here is what it looks like for your data. Overlapping curves with near-zero slopes suggests no interactions.

enter image description here

If I modify the data to add an interaction between the two continuous variables the result is overlapping and close-to-parallel lines with non-zero slopes. That is, the interaction of these two variables has an effect, but it's not very different for each of the categorical variables.

enter image description here

If I add a 3-way interaction into the data, we see non-parallel and non-zero slopes. enter image description here

For the sake of comparison, here is another way of visualization the interaction: two paneled, smoothed contour plots of the residuals, one for the original data and one the first augmentation. There is a difference--the interaction has a saddle shape in 2-space, but it's harder to distinguish for random variation. The diverging color scale encode the positive and negative residuals.

Original data: enter image description here

Augumented with an interaction: enter image description here

In all the plots you have to keep in mind that the fit is less trustworthy where the data is sparse, and I think it's harder to do that with the contour plots.

These techniques are independent of the statistical tool, which is mostly what this forum is about. I used JMP for these examples, but I'm sure the techniques can be done with R.

| cite | improve this answer | |
  • $\begingroup$ Thank you very much indeed! For the first panel of the first plot, do you know the equivalent function in R to get such a figure? Perhaps ggplot2 boxplot with geom_point overlaid? $\endgroup$ – Sarah Mar 4 '18 at 14:02
  • 1
    $\begingroup$ Aha - just found a post that details how to do this: stackoverflow.com/questions/23675735/… :-) $\endgroup$ – Sarah Mar 4 '18 at 14:14
  • 1
    $\begingroup$ Thanks for adding that. SO is better for the coding details than CV. $\endgroup$ – xan Mar 4 '18 at 14:27
  • $\begingroup$ Can I also ask - You mentioned the skew in my distribution - I was concerned about that myself. Can you advise further on suitable transformations and the more robust modelling methods you suggest? Thank you so very much again! $\endgroup$ – Sarah Mar 4 '18 at 14:52
  • 1
    $\begingroup$ I wasn't more specific because it's not my area of expertise, but I was thinking of something like quantile regression which models the medians instead of means. And sometimes linear modeling tools have options to specify a non-normal residual distribution. For transformations, some apply power transformations to normalize the variation. $\endgroup$ – xan Mar 4 '18 at 15:31

A scatter plot matrix (SPloM) is an elegant solution to the problem. Combined with the brushing interaction technique you will be able to see what is going on in your data. In your case, you may probably want to encode the categorical variable through color in each scatter plot forming the splom.

As a tool, I would suggest vega-lite but there are also dedicated R packages for this special purpose, for example the recent DataExplorer package.

Find here a link to an actionable vega-lite example.

| cite | improve this answer | |

Scatter Plot

  • Y-Axis

one continuous dependent variable

  • X-Axis

two continuous independent dependent variables

  1. one categorical independent encode symbol type

  1. IVContinuousA: encode symbol size
  2. IVContinuousB: encode symbol color
| cite | improve this answer | |
  • $\begingroup$ Thank you so much. What package would you use for this? Maybe ggplot2? Is there any chance you could advise me as to how to code this up? I can't quite visualise it in my head and can't work out the code to try it out in R. Any help would be very, very much appreciated!!! $\endgroup$ – Sarah Mar 3 '18 at 13:41
  • $\begingroup$ So i've managed to code up the two-way interaction: A<-ggplot(data=data,aes(x=IVContinuousA,y=DV,group=IVCategorical,shape=IVCategorical,col = IVCategorical,))+geom_point(size = 3)+geom_smooth(method='lm',formula=y~x) plot(A) But I am not sure if the method you suggested will allow me to visualise that 3 way interaction properly. If you do have any further thoughts i'd very much appreciate hearing them :-) $\endgroup$ – Sarah Mar 3 '18 at 14:26
  • $\begingroup$ @Sarah what do you mean " visualise that 3 way interaction properly". It seems like you got the idea. $\endgroup$ – grldsndrs Mar 3 '18 at 16:00
  • $\begingroup$ Sorry, I meant that I am not sure whether that coding shows what I want it to show... perhaps this question would be better for stack overflow... $\endgroup$ – Sarah Mar 3 '18 at 16:36
  • $\begingroup$ @Sarah Not sure why you just don't try it: see what you get. I did something similiar here: line 288, and shown here. In this example I put 7 dimensions onto a 2D graph $\endgroup$ – grldsndrs Mar 3 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.