plot_moderated_mediation: Plot a moderated mediation model

View source: R/med_mod_plots.R

plot_moderated_mediationR Documentation

Plot a moderated mediation model

Description

This function helps to plot a moderated mediation model - with a single moderator and a single mediator. Labels for each path need to be specified manually. By default, covariates are only related to the outcome variable - if they are also used to estimate the mediator, the graph code needs to be adjusted manually.

Usage

plot_moderated_mediation(
  X,
  M,
  W,
  Y,
  CV = NULL,
  mod_direct_path = TRUE,
  labels = list(a = "+", b = "+", c = "+", a_mod = "+", c_mod = "+"),
  filename = NULL
)

Arguments

X

The name of the predictor variable

M

The name of the mediator variable

W

The name of the moderator variable

Y

The name of the outcome variable

CV

The name of covariates

mod_direct_path

Logical. Should a direct path from X to Y be included?

labels

A list of labels for the paths. See the example for the required names.

filename

The filename to save the plot to (should end in .svg). When NULL, the graph is not saved but simply returned.

Value

A named list with the code and the graph

Examples

plot <- plot_moderated_mediation(X = "Training", M = "Self-Efficacy", 
  W = "Motivation", Y = "Performance", CV = "Age, Gender", 
  mod_direct_path = TRUE, labels = list(a = "+", b = "+", c = "+", 
                                        a_mod = "+", c_mod = "+"), 
                                        filename = NULL)

# Show the graph
plot$graph

# Show the code 
plot$code

# To create the graph again (e.g., after you edit its code)
DiagrammeR::grViz(plot$code)


LukasWallrich/timesaveR documentation built on Nov. 29, 2024, 4:47 a.m.