R/modmed_marginal_effect.R

Defines functions modmed_marginal_effect

Documented in modmed_marginal_effect

#' Compute the marginal effect of one variable on an other for all 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. If several values are provided, parallel paths will be estimated.
#' @param y            Character string. Dependent variable.
#' @param mod_values   Numeric vector. Values of the moderating variable
#' @return A tibble reporting marginal effects for all the relationships or paths.
#' @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 <- dataset[[z]]
#' # Compute the simple paths for the selected values of the moderator
#' marginal_effect <- modmed_marginal_effect(
#'   coefficients = mm$coefficients,
#'     x = x,
#'     m = m,
#'     y = y,
#'     mod_values = mod_values
#'  )
#' @seealso modmed()
#' @seealso modmed_marginal_graph()
#' @import dplyr
#' @import tibble
#' @import tidyr
#' @export


modmed_marginal_effect <- function(coefficients = NULL,
                                   x = NA,
                                   m = NA,
                                   y = NA,
                                   mod_values = NULL) {

  # Check entries
  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 the simple paths
  simplepaths <- modmed_simplepaths(
    coefficients = coefficients,
    x = x,
    m = m,
    y = y,
    mod_values = mod_values$mod_value
  )

  # Find the lower bound, mean, and upper bound of the coefficients for each value of the moderator and each draw
  lower_bound <- function(x) quantile(x, 0.025)
  upper_bound <- function(x) quantile(x, 0.975)

  # Compute the mean and 10% confidence interval.
  path_boundaries <- simplepaths %>%
    select(-draw) %>%
    group_by(mod_level, type, independent, mediator, dependent) %>%
    summarise(
      lower_bound = lower_bound(mod_coeff),
      coefficient = mean(mod_coeff),
      upper_bound = upper_bound(mod_coeff)
    ) %>%
    left_join(mod_values, by = "mod_level") %>%
    ungroup() %>%
    select(-mod_level)

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