---
title: "Marginal Mediation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Marginal Mediation}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
date: "`r Sys.Date()`"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(furniture)
library(MarginalMediation)
```

## What is "Marginal Mediation"

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.

## How to Use `MarginalMediation`

To demonstrate the main functions in `MarginalMediation`, we are going to use a data set from the `furniture` package called `nhanes_2010`.

```{r}
library(furniture)
data("nhanes_2010")
names(nhanes_2010)
```

### `mma()`

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.

```{r}
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:

1. information about the overall model,
2. the individual regression models,
3. the indirect effect (`a x b`) with its confidence interval, and
4. the direct effect with its confidence interval.

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.

### `frames()`

This function provides the average marginal effects of a GLM model's parameters. Its syntax is similar to that of `mma()`.

```{r}
frames(pathbc,
       boot = 100)
```

We can compare these results to those from the remarkable `margins` package found at [https://github.com/leeper/margins](https://github.com/leeper/margins) from Thomas Leeper. We won't show them here, but the estimates are the same as those from the `frames()` function. 
```{r, eval=FALSE}
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 `MarginalMediation`.


### Conclusions

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 (<t.barrett@aggiemail.usu.edu>).

