Skip to contents

Getting started

Let’s first load the demo data. This data set comes with base R (meaning you have it too and can directly type this command into your R console).

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Load the rempsyc package:

Note: If you haven’t installed this package yet, you will need to install it via the following command: install.packages("rempsyc").

For moderations and simple slopes, we usually want to standardize (or at least center) our variables.

mtcars2 <- lapply(mtcars, scale) |> as.data.frame()

Simple moderation: nice_mod

nice_mod(data = mtcars2,
         response = "mpg",
         predictor = "gear",
         moderator = "wt") -> moderations
moderations
##   Dependent Variable Predictor df           b          t            p
## 1                mpg      gear 28 -0.08718042 -0.7982999 4.314156e-01
## 2                mpg        wt 28 -0.94959988 -8.6037724 2.383144e-09
## 3                mpg   gear:wt 28 -0.23559962 -2.1551077 3.989970e-02
##           sr2
## 1 0.004805465
## 2 0.558188818
## 3 0.035022025

If we want it to look nice

(my_table <- nice_table(moderations, highlight = TRUE))

Note: The sr2 (semi-partial correlation squared, also known as delta R-square) allows us to quantify the unique contribution (proportion of variance explained) of an independent variable on the dependent variable, over and above the other variables in the model. sr2 is often considered a better indicator of the practical relevance of a variable.

Save table to Word

Let’s save it to word for use in a publication (optional).

save_as_docx(my_table, path = "moderations.docx")

Simple slopes: nice_slopes

Let’s extract the simple slopes now, including the sr2.

nice_slopes(data = mtcars2,
            response = "mpg",
            predictor = "gear",
            moderator = "wt") -> slopes
slopes
##   Dependent Variable Predictor (+/-1 SD) df           b          t          p
## 1                mpg       gear (LOW-wt) 28  0.14841920  1.0767040 0.29080233
## 2                mpg      gear (MEAN-wt) 28 -0.08718042 -0.7982999 0.43141565
## 3                mpg      gear (HIGH-wt) 28 -0.32278004 -1.9035367 0.06729622
##           sr2
## 1 0.008741702
## 2 0.004805465
## 3 0.027322839
nice_table(slopes, highlight = TRUE)

In this specific case, the interaction is significant but none of the simple slopes. This means that although the two slopes are significantly different from each other, taken individually, the slopes aren’t significantly different from a straight line.

The neat thing is that you can add as many dependent variables at once as you want.

# Moderations
nice_mod(data = mtcars2,
         response = c("mpg", "disp", "hp"),
         predictor = "gear",
         moderator = "wt") |>
  nice_table(highlight = TRUE)
# Simple slopes
nice_slopes(data = mtcars2,
            response = c("mpg", "disp", "hp"),
            predictor = "gear",
            moderator = "wt") |> 
  nice_table(highlight = TRUE)

Pro tip: Both the nice_mod() and nice_slopes() functions take the same argument, so you can just copy-paste the first and change the function call to save time!

Special cases

Covariates

You can also have more complicated models, like with added covariates.

Moderations

nice_mod(data = mtcars2,
         response = "mpg",
         predictor = "gear",
         moderator = "wt",
         covariates = c("am", "vs")) |> 
  nice_table(highlight = TRUE)

Simple slopes

nice_slopes(data = mtcars2,
            response = "mpg",
            predictor = "gear",
            moderator = "wt",
            covariates = c("am", "vs")) |> 
  nice_table(highlight = TRUE)

In this case, only the third row is significant, which means that those who are high on the wt variable (above one standard deviation) have significantly lower mpg the higher their gear. We can plot this in the more traditional way:

# First need to define model for plot function
mod <- lm(mpg ~ gear * wt + am + vs, data = mtcars2)

# Plot the model
library(interactions)
interact_plot(mod, pred = "gear", modx = "wt", interval = TRUE)

Note: If you haven’t installed this package yet, you will need to install it via the following command: install.packages(interactions). Furthermore, know that this plot can be heavily customized with available arguments for publication purposes, but I won’t be going into these details here.

Three-way interaction

Let’s make a three-way interaction for example.

Note that for the simple slopes, for now, the second moderator needs to be a dichotomic variable (and the first moderator a continuous variable). We’ll reset the am variable for this purpose for now.

mtcars2$am <- mtcars$am

Moderations

nice_mod(response = "mpg",
         predictor = "gear",
         moderator = "disp",
         moderator2 = "am",
         data = mtcars2) |> 
  nice_table(highlight = TRUE)

Simple slopes

nice_slopes(data = mtcars2,
            response = "mpg",
            predictor = "gear",
            moderator = "disp",
            moderator2 = "am") |> 
  nice_table(highlight = TRUE)

Complex models: nice_lm

For more complicated models not supported by nice_mod, one can define the model in the traditional way and feed it to nice_lm and nice_lm_slopes instead. They support multiple lm models as well.

nice_lm

model1 <- lm(mpg ~ cyl + wt * hp, mtcars2)
model2 <- lm(qsec ~ disp + drat * carb, mtcars2)
my.models <- list(model1, model2)
nice_lm(my.models) |> 
  nice_table(highlight = TRUE)

The same applies to simple slopes, this time we use the nice_lm_slopes function. It supports multiple lm models as well, but the predictor and moderator need to be the same for these models (the dependent variable can change).

nice_lm_slopes

model1 <- lm(mpg ~ gear * wt, mtcars2)
model2 <- lm(disp ~ gear * wt, mtcars2)
my.models <- list(model1, model2)
nice_lm_slopes(my.models, predictor = "gear", moderator = "wt") |> 
  nice_table(highlight = TRUE)

Thanks for checking in

Make sure to check out this page again if you use the code after a time or if you encounter errors, as I periodically update or improve the code. Feel free to contact me for comments, questions, or requests to improve this function at https://github.com/rempsyc/rempsyc/issues. See all tutorials here: https://remi-theriault.com/tutorials.