I'm trying to visualize a three-way interaction from a rather complex linear mixed model in R (lmer function from the lme4 package; the model has a complex random-effects structure). The interaction consists of two continuous variables and one categorical variable (two experimental conditions).

So far, I have graphed the interaction via two 3D-surface plots using visreg2d from the visreg package (see image). But my reviewers found these plots confusing and asked for a different illustration, such as conditional coefficient plots (i.e., plots of the strength of coefficient 1 as coefficient 2 increases).

I've tried to find a package that allows me to create these kind of plots, but failed. The existing packages only allow coefficient plots for two-way interactions (for instance the interplot package; https://cran.r-project.org/web/packages/interplot/vignettes/interplot-vignette.html). That means I only get a conditional coefficient plot of the two-way interaction, collapsed across both levels of the categorical variable.

Is there a package for my case? If not, I probably have to manually extract fitted values from my model (e.g., using broom) and somehow plot them in ggplot2. But I don't really know how to do this, whether or not to take into account random effects (and how), etc. Any ideas would be much appreciated...

enter image description here

  • 1
    $\begingroup$ You might get better responses on the R list dedicated to mixed models $\endgroup$ – mdewey Apr 11 '17 at 11:56
  • 1
    $\begingroup$ I have written a package, to be submitted to CRAN in a few days, which is currently available only from GitHub. You could install it and try the ggpredict()-function. In the help (?ggpredict), there is an example of how to plot 3-way-interactions. $\endgroup$ – Daniel Apr 11 '17 at 12:04
  • $\begingroup$ Thanks Daniel, that's a neat package. However, when I try to run the 3-way code (adapted for my data) I get the following error: Error in dimnames(reMatrix)[[3]] <- alllvl : attempt to set an attribute on NULL. Sounds like a problem of input data format? However, all entered variables are numeric/vectors... (PS Running the code on the example dataset works fine) $\endgroup$ – Klemens Apr 11 '17 at 16:09

Your Answer

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

Browse other questions tagged or ask your own question.