R/modmed_simplepaths.R

Defines functions modmed_simplepaths

Documented in modmed_simplepaths

#' Compute the simple paths from the coefficients returned by the function modmed() for the different values of the moderating variable.
#' @param coefficients Tibble. Coefficients returned by the function modmed().
#' @param x            Character string. Independent variable.
#' @param m            Character vector. Mediating variables.
#' @param y            Character string. Dependent variable.
#' @param mod_values   Numeric vector. Values of the moderating variable.
#' @return A tibble reporting simple paths for various levels of the moderaing variable.
#' @examples
#' library(modelest)
#' data("edwards2007")
#' dataset <- edwards2007
#' draws = 10
#' x <- "fbkc"
#' m <- "satc"
#' y <- "comc"
#' z <- "gen"
#' mm <- modmed(dataset = dataset,
#'              draws = draws,
#'              x = x,
#'              m = m,
#'              y = y,
#'              z = z,
#'              ctrl_s1 = NA,
#'              ctrl_s2 = NA,
#'              inst_x = NA,
#'              inst_m = NA,
#'              model = "DFS",
#'              method = "OLS")
#' # Prepare 2 hypothetical values of the moderator
#' mod_values <- c(-sd(dataset[[z]]), sd(dataset[[z]]))
#' # Compute the simple paths for the selected values of the moderator
#' simplepaths <- modmed_simplepaths(
#'   coefficients = mm$coefficients,
#'     x = x,
#'     m = m,
#'     y = y,
#'     mod_values = mod_values
#'  )
#' @seealso modmed()
#' @import dplyr
#' @import tibble
#' @import tidyr
#' @export


modmed_simplepaths <- function(coefficients = NULL,
                               x = NA,
                               m = NA,
                               y = NA,
                               mod_values = NA) {
  stopifnot(
    is_tibble(coefficients),
    !is.na(x),
    !is.na(m),
    !is.na(y),
    is.numeric(mod_values)
  )
  
  # Gather and give an id to the values of the moderating variable.
  mod_values <- sort(mod_values)
  if (length(mod_values) == 2) {
    mod_values <- tibble(
      mod_level = c("low", "high"),
      mod_value = mod_values
    )
  } else {
    mod_values <- tibble(
      mod_level = paste0("V", 1:length(mod_values)),
      mod_value = mod_values
    )
  }
  
  # Compute direct paths
  simplepaths <- coefficients %>%
    mutate(mod_value = replicate(nrow(.), list(mod_values))) %>%
    unnest() %>%
    mutate(
      mod_coeff = case_when(
        is.na(moderator) ~ coefficient,
        TRUE ~ coefficient * mod_value
      )
    ) %>%
    select(draw, independent, dependent, mod_level, mod_value, mod_coeff) %>%
    group_by(draw, independent, dependent, mod_level, mod_value) %>%
    summarise(mod_coeff = sum(mod_coeff)) %>%
    mutate(type = "direct") %>%
    ungroup()

  # Compute indirect paths
  simplepaths <- simplepaths %>%
    left_join(
      select(., draw, mod_level, dependent = independent, final = dependent, mod_coeff2 = mod_coeff),
      by = c("draw", "mod_level", "dependent")
    ) %>%
    select(-type) %>%
    na.omit() %>%
    mutate(
      full_coeff = mod_coeff * mod_coeff2
    ) %>%
    select(draw, mod_level, independent, mediator = dependent, dependent = final, mod_coeff = full_coeff) %>%
    mutate(type = "indirect") %>%
    bind_rows(simplepaths) %>%
    select(draw, mod_level, type, independent, mediator, dependent, mod_coeff)
  
  # Compute total effect (direct and indirect)
  total_effect <- simplepaths %>%
    select(-mediator) %>%
    mutate(type = "full") %>%
    filter(independent == x, dependent == y) %>%
    group_by(draw, mod_level, type, independent, dependent) %>%
    summarise_all(sum)
  
  # Gather all effectss.
  simplepaths <- simplepaths %>%
    bind_rows(total_effect)

  return(simplepaths)
}
NicolasJBM/model documentation built on Feb. 15, 2018, 11:12 p.m.