moderation_plot: Interaction plot for continuous moderators

Description Usage Arguments Value Examples

View source: R/moderation_plot.r

Description

This functions creates a conditional effect plot that illustrates how an effect of x on y changes with changes in a continuous moderator variable.

Usage

1
2
3
4
5
6
7
8
9
moderation_plot(
  object,
  x = NULL,
  m = NULL,
  x_lab = "Moderator",
  y_lab = "Conditional effect of x on y",
  quantil = 100,
  mod_hist = TRUE
)

Arguments

object

An object of class lm or lmer that includes an interaction.

x

The name of the predictor variable.

m

The name of the continuous moderator variable.

x_lab

A character value specifying the x label in the final plot.

y_lab

A character value specifying the y label in the final plot.

quantil

Number of quantiles that should be calculated.

mod_hist

A logical value indicating whether a histogram of the moderator variable should be added above the moderation plot.

Value

If mod_hist = TRUE, this function returns a "ggExtraPlot" (i.e. a "gtable"). If mod_hist = FALSE, it returns a ggplot object that can be further customized using standard ggplot elements.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Simulate an interaction
set.seed(10)
x <- rnorm(500, 2, 1)
z <- rnorm(500, 2, 1)
y <- 0.5*x + 1.5*(z*x) + rnorm(500, 0, 3.5)

# Estimate linear model
mod.lm <- lm(y ~ x + z + x:z)
summary(mod.lm)

# Plot model
moderation_plot(mod.lm, x = "x", m = "z")

masurp/pmstats documentation built on Oct. 6, 2020, 9:24 p.m.