plotmod: Plot the moderation effect in a path model

Description Usage Arguments Details Value Examples

View source: R/plotmod.R

Description

Plot the moderation effect in a path model

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
plotmod(
  fit,
  y,
  x,
  w,
  xw,
  x_label,
  w_label,
  y_label,
  title,
  a_shift = 0,
  expansion = 0.1,
  standardized = FALSE,
  digits = 3,
  x_from_mean_in_sd = 1,
  w_from_mean_in_sd = 1,
  w_method = "sd",
  w_percentiles = c(0.16, 0.84),
  x_method = "sd",
  x_percentiles = c(0.16, 0.84),
  w_sd_to_percentiles,
  x_sd_to_percentiles,
  plot_x_vlines,
  x_vlines_unit = "sd"
)

Arguments

fit

The output from lavaan::lavaan or its wrapper, such as lavaan::sem.

y

The name of the outcome variable as in the data set in fit. It can be the name of the variable, with or without quotes.

x

The name of the focal variable as in the data set in It can be the name of the variable, with or without quotes.fit.

w

The name of the moderator as in the data set in fit. It can be the name of the variable, with or without quotes.

xw

The name of the product term, x * w. If not supplied, The function will try to find it in the data set. It can be the name of the variable, with or without quotes.

x_label

The label for the X-axis. Default is the vlaues of x.

w_label

The label for the legend for the lines. Default is the value ofw.

y_label

The label for the Y-axis. Default is the value of y.

title

The title of the graph. If not supplied, will be generated from the variable names.

a_shift

Default is 0. Can be ignored for now.

expansion

How much tha lower and upper limits of the axis will be adjusted.

standardized

Logical. Plot the moderation effect in standardized metric. All three variables, x, w, and y will be standardized. Default is FALSE

digits

Number of decimal digits to print. Default is 3.

x_from_mean_in_sd

How many SD from mean is used to define "low" and "high" for the focal variable. Default is 1.

w_from_mean_in_sd

How many SD from mean is used to define "low" and "high" for the moderator. Default is 1.

w_method

How to define "high" and "low" for the moderator levels. Default is in terms of the standard deviation of the moderator, "sd". If equal to "percentile", then percentiles of the moderator in the dataset is used.

w_percentiles

If w_method is "percentile", then this argument specifies the two percentiles to be used, divided by 100. It must be a vector of two numbers. The default is c(.16, .84), the 16th and 84th percentiles, which corresponds approximately to one SD below and above mean for a normal distributoin, respectively.

x_method

How to define "high" and "low" for the focal variable levels. Default is in terms of the standard deviation of the focal variable, "sd". If equal to "percentile", then percentiles of the focal variable in the dataset is used.

x_percentiles

If x_method is "percentile", then this argument specifies the two percentiles to be used, divided by 100. It must be a vector of two numbers. The default is c(.16, .84), the 16th and 84th percentiles, which corresponds approximately to one SD below and above mean for a normal distributoin, respectively.

w_sd_to_percentiles

If w_method is "percentile" and this argument is set to a number, this number will be used to to determine the percentiles to be used. The lower percentile is the percentile in a normal distribution that is w_sd_to_percentiles SD below the mean. The upper percentile is the percentil in a normal distribution that is w_sd_to_percentiles SD above the mean. Therefore, if w_sd_to_percentiles is set to 1, then the lower and upper percentiles are 16th and 84th, respectively.

x_sd_to_percentiles

If x_method is "percentile" and this argument is set to a number, this number will be used to to determine the percentiles to be used. The lower percentile is the percentile in a normal distribution that is x_sd_to_percentiles SD below the mean. The upper percentile is the percentil in a normal distribution that is x_sd_to_percentiles SD above the mean. Therefore, if x_sd_to_percentiles is set to 1, then the lower and upper percentiles are 16th and 84th, respectively.

plot_x_vlines

If supplied, vertical lines to indicate the levels of the focal variable will be plotted. This should be a vector of numbers, indicating the levels to be plotted. How these numbers are interpreted depends on x_vlines_unit.

x_vlines_unit

If equal to "sd", then the values of plot_x_vlines will be interpreted as the deviation from the mean. For example, 1 is 1 SD above mean, and -1 is 1 SD below mean. If equal to "percentile", then the numbers, mulipltied by 100, are the precentiles. For example, .25 is the 25th percentile, and .75 is the 75th percentile.

Details

This function extracts the information stored in the lavaan fit object to plot a two-line graph, one for the relation between the focal variale (x) and the outcome variable (y) when the moderator (w) is one stanard deviation below mean, and one when the moderator is one standard deviation above mean.

Value

A ggplot2 graph.

Examples

1
2
3
4
## Not run: 
# To be prepared

## End(Not run)

sfcheung/plotmodsem documentation built on Dec. 23, 2021, 12:21 a.m.