#' Compute the moderated paths coefficients for analayses of moderated mediations.
#' @param coefficients Tibble. Output of the function compute_coefficients.
#' @param moderations Tibble. Output of the function retrieve_modval with option "allval" FALSE.
#' @param mediations List. List of vectors specifying the paths, from the first independent variable to the last dependent variable through the moderators in order.
#' @return A tibble gathering all (direct, indirect, and total) the moderated path coefficients.
#' @examples
#' # Prepare the specifications for subsequent analyses
#'
#' library(dplyr)
#' library(tibble)
#'
#' sample <- modestim::mm_edwards2007 %>%
#' mutate(
#' fbkc.x.cenc = fbkc * cenc,
#' satc.x.cenc = satc * cenc,
#' fbkc.x.gen = fbkc * gen,
#' satc.x.gen = satc * gen
#' )
#'
#' moderations <- modestim::retrieve_modval(
#' sample,
#' moderators = c("gen","cenc"),
#' types = c("discrete","continuous"),
#' allval = FALSE
#' )
#'
#' mediations <- list(
#' c("fbkc","satc","comc")
#' )
#'
#' # Replicate Edwards and Lambert's 2007 analysis of moderated mediation with gender
#'
#'
#' specgen <- tibble::tibble(
#' sample = rep(list(sample),2),
#' formula = list(
#' as.formula("satc ~ fbkc + gen + fbkc.x.gen"),
#' as.formula("comc ~ satc + fbkc + gen + satc.x.gen + fbkc.x.gen")
#' ),
#' method = "glm",
#' family = NA,
#' md_imputation = "deletion",
#' md_method = "deletion",
#' draws = 10,
#' size = NA,
#' cores = 2
#' )
#'
#' coeffgen <- modestim::compute_coefficients(specgen)
#'
#' pathanagen <- modestim::analyze_path(coeffgen, moderations, mediations)
#'
#'
#' # Replicate Edwards and Lambert's 2007 analysis of moderated mediation with centrality
#'
#' speccenc <- tibble::tibble(
#' sample = rep(list(sample),2),
#' formula = list(
#' as.formula("satc ~ fbkc + cenc + fbkc.x.cenc"),
#' as.formula("comc ~ satc + fbkc + cenc + satc.x.cenc + fbkc.x.cenc")
#' ),
#' method = "glm",
#' family = NA,
#' md_imputation = "deletion",
#' md_method = "deletion",
#' draws = 10,
#' size = NA,
#' cores = 2
#' )
#'
#' coeffcenc <- modestim::compute_coefficients(speccenc)
#'
#' pathanacenc <- modestim::analyze_path(coeffcenc, moderations, mediations)
#'
#' @references Edwards, J. R., & Lambert, L. S. (2007). Methods for integrating moderation and mediation: a general analytical framework using moderated path analysis. Psychological Methods, 12(1), 1–22.
#' @importFrom tibble tibble
#' @importFrom tibble is_tibble
#' @importFrom tidyr nest
#' @importFrom tidyr unnest
#' @importFrom tidyr replace_na
#' @importFrom tidyr spread
#' @importFrom tidyr gather
#' @importFrom dplyr group_by
#' @importFrom dplyr %>%
#' @importFrom dplyr n
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @importFrom purrr pmap
#' @importFrom purrr map
#' @importFrom stats sd
#' @importFrom stats pnorm
#' @export
analyze_path <- function(coefficients, moderations, mediations){
stopifnot(
tibble::is_tibble(coefficients),
tibble::is_tibble(moderations),
is.list(mediations) & length(mediations[[1]]) <= 3
)
# Bind variables
position <- NULL
coefficient <- NULL
relation <- NULL
path <- NULL
dependent <- NULL
independent <- NULL
moderator <- NULL
draw <- NULL
count <- NULL
estimate <- NULL
sdev <- NULL
p.value <- NULL
text <- NULL
coeff <- NULL
pathana <- direct_path(coefficients, moderations)
if (length(mediations[[1]] == 3)){
for (i in 1:length(mediations)) {
pathana <- dplyr::bind_rows(pathana, indirect_path(coefficients, moderations, mediations[[i]]))
}
}
pathana <- pathana %>%
tidyr::gather(position, coefficient, -relation, -path, -dependent, -independent, -moderator, -draw)
totals <- pathana %>%
dplyr::mutate(
relation = "total",
path = paste(independent, dependent, sep = "_")
) %>%
dplyr::group_by(relation, path, dependent, independent, moderator, position, draw) %>%
dplyr::summarise(coefficient = sum(coefficient), count = n()) %>%
dplyr::filter(count > 1) %>%
dplyr::select(-count)
pathana <- pathana %>%
dplyr::bind_rows(totals) %>%
dplyr::ungroup() %>%
dplyr::group_by(relation, path, dependent, independent, moderator, position) %>%
dplyr::select(-draw) %>%
dplyr::summarise(estimate = mean(coefficient), sdev = sd(coefficient)) %>%
dplyr::mutate(p.value = 2-2*pnorm(abs(estimate/sdev))) %>%
dplyr::ungroup() %>%
na.omit() %>%
dplyr::mutate(coeff = writer::stat_stars(estimate, p.value)) %>%
dplyr::filter(moderator != "base" | moderator == "base" & position == "low") %>%
dplyr::mutate(position = dplyr::case_when(moderator == "base" ~ "all", TRUE ~ position)) %>%
dplyr::mutate(moderator = dplyr::case_when(moderator == "base" ~ "", TRUE ~ moderator)) %>%
dplyr::mutate(
relation = factor(relation, levels = c("direct","indirect","total")),
position = factor(position, levels = c("all","low","high","diff"))
) %>%
dplyr::arrange(relation, path, position) %>%
dplyr::select(relation, path, dependent, independent, moderator, position, estimate, p.value, coeff)
return(pathana)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.