# Marginal Mediation

#### 2019-03-04

## 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`.

    library(furniture)
    data("nhanes_2010")
    names(nhanes_2010)

    ##  [1] "id"         "gen_health" "mod_active" "vig_active" "home_meals"
    ##  [6] "gender"     "age"        "marijuana"  "illicit"    "rehab"
    ## [11] "asthma"     "overweight" "cancer"     "low_int"    "down"
    ## [16] "sleeping"   "low_energy" "appetite"   "feel_bad"   "no_con"
    ## [21] "speak_move" "dead"       "difficulty" "active"

### `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.

    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)

    ##
    ## calculating a paths... b and c paths... Done.

    ## ┌───────────────────────────────┐
    ## │  Marginal Mediation Analysis  │
    ## └───────────────────────────────┘
    ## A marginal mediation model with:
    ##    1 mediators
    ##    3 indirect effects
    ##    3 direct effects
    ##    500 bootstrapped samples
    ##    95% confidence interval
    ##    n = 1417
    ##
    ## Formulas:
    ##    ◌ marijuana ~ home_meals + gender + age + asthma
    ##    ◌ home_meals ~ gender + age + asthma
    ##
    ## Regression Models:
    ##
    ##      marijuana ~
    ##                           Est      SE   Est/SE P-Value
    ##         (Intercept)  -0.39400 0.38028 -1.03608 0.30017
    ##         home_meals   -0.04062 0.01363 -2.98051 0.00288
    ##         genderFemale  0.43161 0.11723  3.68169 0.00023
    ##         age           0.00276 0.01470  0.18754 0.85123
    ##         asthmaNo     -0.00717 0.15004 -0.04778 0.96189
    ##
    ##      home_meals ~
    ##                           Est      SE   Est/SE P-Value
    ##         (Intercept)   6.56883 0.76462  8.59100 0.00000
    ##         genderFemale -1.34831 0.23910 -5.63913 0.00000
    ##         age          -0.05689 0.03017 -1.88565 0.05955
    ##         asthmaNo     -0.00428 0.31293 -0.01368 0.98909
    ##
    ## Unstandardized Mediated Effects:
    ##
    ##    Indirect Effects:
    ##
    ##      marijuana ~
    ##                                    Indirect    Lower   Upper
    ##         genderFemale => home_meals  0.01312  0.00481 0.02348
    ##         age => home_meals           0.00055  0.00001 0.00136
    ##         asthmaNo => home_meals      0.00004 -0.00699 0.00615
    ##
    ##    Direct Effects:
    ##
    ##      marijuana ~
    ##                        Direct    Lower   Upper
    ##         genderFemale  0.10430  0.05396 0.15653
    ##         age           0.00066 -0.00676 0.00876
    ##         asthmaNo     -0.00172 -0.08002 0.07095

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()`.

    frames(pathbc,
           boot = 100)

    ##
    ## Bootstrapping...Done.

    ## ┌────────────────────────────┐
    ## │  Average Marginal Effects  │
    ## └────────────────────────────┘
    ##
    ##              Estimate   Lower   Upper
    ## home_meals    -0.0097 -0.0176 -0.0035
    ## genderFemale   0.1043  0.0427  0.1540
    ## age            0.0007 -0.0072  0.0099
    ## asthmaNo      -0.0017 -0.0656  0.0596
    ## ────

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 `frames()` function.

    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).