mod_levels: Create Levels of Moderators

View source: R/mod_levels.R

mod_levelsR Documentation

Create Levels of Moderators

Description

Create levels of moderators to be used by indirect_effect(), cond_indirect_effects(), and cond_indirect().

Usage

mod_levels(
  w,
  fit,
  w_type = c("auto", "numeric", "categorical"),
  w_method = c("sd", "percentile"),
  sd_from_mean = c(-1, 0, 1),
  percentiles = c(0.16, 0.5, 0.84),
  extract_gp_names = TRUE,
  prefix = NULL,
  values = NULL,
  reference_group_label = NULL,
  descending = TRUE
)

mod_levels_list(
  ...,
  fit,
  w_type = "auto",
  w_method = "sd",
  sd_from_mean = NULL,
  percentiles = NULL,
  extract_gp_names = TRUE,
  prefix = NULL,
  descending = TRUE,
  merge = FALSE
)

Arguments

w

Character. The names of the moderator. If the moderator is categorical with 3 or more groups, this is the vector of the indicator variables.

fit

The fit object. Can be a lavaan::lavaan object or a list of lm() outputs. It can also be a lavaan.mi object returned by semTools::runMI() or its wrapper, such as semTools::sem.mi(). If it is a single model fitted by lm(), it will be automatically converted to a list by lm2list().

w_type

Character. Whether the moderator is a "numeric" variable or a "categorical" variable. If "auto", the function will try to determine the type automatically.

w_method

Character, either "sd" or "percentile". If "sd", the levels are defined by the distance from the mean in terms of standard deviation. if "percentile", the levels are defined in percentiles.

sd_from_mean

A numeric vector. Specify the distance in standard deviation from the mean for each level. Default is c(-1, 0, 1) for mod_levels(). For mod_levels_list(), the default is c(-1, 0, 1) when there is only one moderator, and c(-1, 1) when there are more than one moderator. Ignored if w_method is not equal to "sd".

percentiles

A numeric vector. Specify the percentile (in proportion) for each level. Default is c(.16, .50, .84) for mod_levels(), corresponding approximately to one standard deviation below mean, mean, and one standard deviation above mean in a normal distribution. For mod_levels_list(), default is c(.16, .50, .84) if there is one moderator, and c(.16, .84) when there are more than one moderator. Ignored if w_method is not equal to "percentile".

extract_gp_names

Logical. If TRUE, the default, the function will try to determine the name of each group from the variable names.

prefix

Character. If extract_gp_names is TRUE and prefix is supplied, it will be removed from the variable names to create the group names. Default is NULL, and the function will try to determine the prefix automatically.

values

For numeric moderators, a numeric vector. These are the values to be used and will override other options. For categorical moderators, a named list of numeric vector, each vector has length equal to the number of indicator variables. If the vector is named, the names will be used to label the values. For example, if set to ⁠list(gp1 = c(0, 0), gp3 = c(0, 1)⁠, two levels will be returned, one named gp1 with the indicator variables equal to 0 and 0, the other named gp3 with the indicator variables equal to 0 and 1. Default is NULL.

reference_group_label

For categorical moderator, if the label for the reference group (group with all indicators equal to zero) cannot be determined, the default label is "Reference". To change it, set reference_group_label to the desired label. Ignored if values is set.

descending

If TRUE (default), the rows are sorted in descending order for numerical moderators: The highest value on the first row and the lowest values on the last row. For user supplied values, the first value is on the last row and the last value is on the first row. If FALSE, the rows are sorted in ascending order.

...

The names of moderators variables. For a categorical variable, it should be a vector of variable names.

merge

If TRUE, mod_levels_list() will call merge_mod_levels() and return the merged levels. Default is FALSE.

Details

It creates values of a moderator that can be used to compute conditional effect or conditional indirect effect. By default, for a numeric moderator, it uses one standard deviation below mean, mean, and one standard deviation above mean. The percentiles of these three levels in a normal distribution (16th, 50th, and 84th) can also be used. For categorical variable, it will simply collect the unique categories in the data.

The generated levels are then used by cond_indirect() and cond_indirect_effects().

If a model has more than one moderator, mod_levels_list() can be used to generate combinations of levels. The output can then passed to cond_indirect_effects() to compute the conditional effects or conditional indirect effects for all the combinations.

Value

mod_levels() returns a wlevels-class object which is a data frame with additional attributes about the levels.

mod_levels_list() returns a list of wlevels-class objects, or a wlevels-class object which is a data frame of the merged levels if merge = TRUE.

Functions

  • mod_levels(): Generate levels for one moderator.

  • mod_levels_list(): Generate levels for several moderators.

See Also

cond_indirect_effects() for computing conditional indiret effects; merge_mod_levels() for merging levels of moderators.

Examples


library(lavaan)
data(data_med_mod_ab)
dat <- data_med_mod_ab
# Form the levels from a list of lm() outputs
lm_m <- lm(m ~ x*w1 + c1 + c2, dat)
lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat)
lm_out <- lm2list(lm_m, lm_y)
w1_levels <- mod_levels(lm_out, w = "w1")
w1_levels
w2_levels <- mod_levels(lm_out, w = "w2")
w2_levels
# Indirect effect from x to y through m, at the first levels of w1 and w2
cond_indirect(x = "x", y = "y", m = "m",
              fit = lm_out,
              wvalues = c(w1 = w1_levels$w1[1],
                          w2 = w2_levels$w2[1]))
# Can form the levels based on percentiles
w1_levels2 <- mod_levels(lm_out, w = "w1", w_method = "percentile")
w1_levels2
# Form the levels from a lavaan output
# Compute the product terms before fitting the model
dat$mw2 <- dat$m * dat$w2
mod <-
"
m ~ x + w1 + x:w1 + c1 + c2
y ~ m + x + w1 + w2 + mw2 + c1 + c2
"
fit <- sem(mod, dat, fixed.x = FALSE)
cond_indirect(x = "x", y = "y", m = "m",
              fit = fit,
              wvalues = c(w1 = w1_levels$w1[1],
                          w2 = w2_levels$w2[1]))
# Can pass all levels to cond_indirect_effects()
# First merge the levels by merge_mod_levels()
w1w2_levels <- merge_mod_levels(w1_levels, w2_levels)
cond_indirect_effects(x = "x", y = "y", m = "m",
                      fit = fit,
                      wlevels = w1w2_levels)




# mod_levels_list() forms a combinations of levels in one call
# It returns a list, by default.
# Form the levels from a list of lm() outputs
# "merge = TRUE" is optional. cond_indirect_effects will merge the levels
# automatically.
w1w2_levels <- mod_levels_list("w1", "w2", fit = fit, merge = TRUE)
w1w2_levels
cond_indirect_effects(x = "x", y = "y", m = "m",
                      fit = fit, wlevels = w1w2_levels)
# Can work without merge = TRUE:
w1w2_levels <- mod_levels_list("w1", "w2", fit = fit)
w1w2_levels
cond_indirect_effects(x = "x", y = "y", m = "m",
                      fit = fit, wlevels = w1w2_levels)



manymome documentation built on Oct. 4, 2024, 5:10 p.m.