knitr::opts_chunk$set(echo = TRUE) library(furniture) library(MarginalMediation)
Marginal Mediation is the general method of interpreting indirect and direct effects in terms of the average marginal effects. When the mediator(s) and outcome are continuous, this is the same as the normal
a x b method. When either the mediator(s) or outcome is dichotomous, then it allows a simple transformation from the log-odds scale to probability.
For example, when the mediator is dichotomous and the outcome is continuous, we have a probability times a value in the unit of the outcome. Simply, the resulting indirect or total effect is in the units of the outcome.
To demonstrate the main functions in
MarginalMediation, we are going to use a data set from the
furniture package called
library(furniture) data("nhanes_2010") names(nhanes_2010)
This is the main function of the package. It takes data, two (or more in the future) formulas relating to the outcome and mediators, the family of the generalized linear model (GLM) regression functions, the indirect effects to compute, and the number of bootstrapped samples.
pathbc <- glm(marijuana ~ home_meals + gender + age + asthma, data = nhanes_2010, family = "binomial") patha <- glm(home_meals ~ gender + age + asthma, data = nhanes_2010, family = "gaussian") mma(pathbc, patha, ind_effects = c("genderFemale-home_meals", "age-home_meals", "asthmaNo-home_meals"), boot = 500)
The print method of MMA shows:
a x b) with its confidence interval, and
These estimates are all average marginal effects, meaning each are interpreted in terms of the corresponding endogenous variable's original units. In this example, since marijuana is binary, its original units are probabilities. That means that the indirect and direct effects are risks (or probabilities) of using marijuana.
This function provides the average marginal effects of a GLM model's parameters. Its syntax is similar to that of
frames(pathbc, boot = 100)
We can compare these results to those from the remarkable
margins package found at https://github.com/leeper/margins from Thomas Leeper. We won't show them here, but the estimates are the same as those from the
remotes::install_github("leeper/margins") library(margins) summary(margins(pathbc))
The differences are mainly in the confidence intervals since they are based on two different methods. I highly recommend looking into both the delta method (as used in
margins and Stata) as well as the bootstrapping method as used in
This is currently beta but I am excited to provide an initial working release. Let me know if you find any bugs or want to discuss the method (firstname.lastname@example.org).
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.