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.

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 #> digraph { #> #> graph [layout = 'neato', #> outputorder = 'edgesfirst', #> bgcolor = 'white', rankdir=LR,] #> #> node [fontname = 'Helvetica', #> fontsize = '10', #> shape = 'circle', #> fixedsize = 'true', #> width = '0.5', #> style = 'filled', #> fillcolor = 'white', #> color = 'black', #> fontcolor = 'black'] #> #> 'X' [label = <Training>, color = 'black', shape = 'rectangle', height = '0.5', width = '1.5', pos = '0,0!'] #> 'Y' [label = <Performance>, color = 'black', shape = 'rectangle', height = '0.5', width = '1.5', pos = '5,0!'] #> 'M' [label = <Self-Efficacy>, color = 'black', shape = 'rectangle', height = '0.5', width = '1.5', pos = '2.5,1!'] #> 'a' [label = <+> color = 'black', fillcolor='transparent', shape = 'plaintext', pos = '1.0,0.6!'] #> 'amod' [label = <+> color = 'black', fillcolor='transparent', shape = 'plaintext', pos = '1.45,-0.25!'] #> 'b' [label = <+>, color = 'black', fillcolor='transparent', shape = 'plaintext', pos = '3.8,0.65!'] #> 'c' [label = <+>, color = 'black', fillcolor='transparent', shape = 'plaintext', pos = '2.45,-0.15!'] #> 'W' [label = <Motivation>, color = 'black', shape = 'rectangle', height = '0.5', width = '1.5', pos = '1.8,-0.7!'] #> 'MW' [style = invis, pos = '1.3,0.5!', height = '0', width = '0'] #> 'XW' [style = invis, pos = '1.8,0!', height = '0', width = '0'] #> 'cmod' [label = <+>, color = 'black', shape = 'plaintext', fillcolor='transparent', pos = '1.9,-0.25!'] #> #> 'CV' [label = <Age, Gender>, color = 'black', shape = 'rectangle', height = '0.4', width = '1.5', pos = '5,-0.7!'] #> #> edge [fontname = 'Helvetica', #> fontsize = '10', #> len = '1.5', #> color = 'black', #> arrowsize = '0.5'] #> #> X->MW [arrowhead=none] #> MW -> M #> M->Y #> W->MW #> X->XW [arrowhead=none] #> XW -> Y #> W->XW #> #> CV->Y [style=dashed] #> #> } # To create the graph again (e.g., after you edit its code) DiagrammeR::grViz(plot$code)