dat <- data_med_mod_a
lm_m <- lm(m ~ x*w + c1 + c2, dat)
lm_y <- lm(y ~ m + x + c1 + c2, dat)
fit_lm <- lm2list(lm_m, lm_y)
# Set R to a large value in real research.
boot_out_lm <- do_boot(fit_lm,
R = 50,
seed = 54532,
parallel = FALSE,
progress = FALSE)
# Compute the conditional indirect effects
# from 2 SD below mean to 2 SD above mean of the moderator,
# by setting sd_from_mean of cond_indirect_effects().
# Set length.out to a larger number for a smooth graph.
out_lm <- cond_indirect_effects(wlevels = "w",
x = "x",
y = "y",
m = "m",
fit = fit_lm,
sd_from_mean = seq(-2, 2, length.out = 10),
boot_ci = TRUE,
boot_out = boot_out_lm)
p <- plot_effect_vs_w(out_lm)
p
# The output is a ggplot2 graph and so can be further customized
library(ggplot2)
# Add the line for the mean of w, the moderator
p2 <- p + geom_vline(xintercept = mean(dat$w),
color = "red")
p2
# Use fill_wlevels to add moderator levels:
dat <- data_med_mod_a
lm_m <- lm(m ~ x*w + c1 + c2, dat)
lm_y <- lm(y ~ m + x + c1 + c2, dat)
fit_lm <- lm2list(lm_m, lm_y)
wlevels <- mod_levels(w = "w",
sd_from_mean = c(-3, 0, 3),
fit = fit_lm)
wlevels
cond_out <- cond_indirect_effects(wlevels = wlevels,
x = "x",
y = "m",
fit = fit_lm)
cond_out
# Only 3 points
p1 <- plot_effect_vs_w(cond_out)
p1
# Increase the number of levels to 15
cond_out_filled <- fill_wlevels(cond_out,
k = 15)
cond_out_filled
p2 <- plot_effect_vs_w(cond_out_filled)
p2
Run the code above in your browser using DataLab