This package has functions to calculate marginal effects
from brms
models ( http://paul-buerkner.github.io/brms/ ).
A central motivator is to calculate average marginal effects (AMEs)
for continuous and discrete predictors in fixed effects only and
mixed effects regression models including location scale models.
This table shows an overview of currently supported models / features where "X" indicates a specific model / feature is currently supported. The column 'Fixed' means fixed effects only models. The column 'Mixed' means mixed effects models.
Distribution / Feature | Fixed | Mixed |
---|---|---|
Gaussian / Normal | ✔️ | ✔️ |
Bernoulli (logistic) | ✔️ | ✔️ |
Poisson | ✔️ | ✔️ |
Negative Binomial | ✔️ | ✔️ |
Gamma | ✔️ | ✔️ |
Beta | ✔️ | ✔️ |
Multinomial logistic | ❌ | ❌ |
Multivariate models | ❌ | ❌ |
Gaussian location scale models | ✔️ | ✔️ |
Natural log / square root transformed outcomes | ✔️ | ✔️ |
Monotonic predictors | ✔️ | ✔️ |
Custom outcome transformations | ❌ | ❌ |
In general, any distribution supported by brms
that generates one and
only one predicted value (e.g., not multinomial logistic regression models)
should be supported for fixed effects only models.
Also note that currently, only Gaussian random effects are supported. This is not too
limiting as even for Bernoulli, Poisson, etc. outcomes, the random effects
are commonly assumed to have a Gaussian distribution.
Here is a quick syntax overview of how to use the main function,
brmsmargins()
.
h <- .001
ames <- brmsmargins(
object = model,
add = data.frame(x = c(0, h)),
contrasts = cbind("AME x" = c(-1 / h, 1 / h)),
effects = "fixedonly")
ames$ContrastSummary
ames <- brmsmargins(
object = model,
add = data.frame(x = c(0, 1)),
contrasts = cbind("AME x" = c(-1, 1)),
effects = "fixedonly")
ames$Summary
ames$ContrastSummary
h <- .001
ames <- brmsmargins(
object = model,
add = data.frame(x = c(0, h)),
contrasts = cbind("AME x" = c(-1 / h, 1 / h)),
effects = "integrateoutRE")
ames$ContrastSummary
ames <- brmsmargins(
object = model,
add = data.frame(x = c(0, 1)),
contrasts = cbind("AME x" = c(-1, 1)),
effects = "integrateoutRE")
ames$Summary
ames$ContrastSummary
h <- .001
ames <- brmsmargins(
object = model,
add = data.frame(x = c(0, h)),
contrasts = cbind("AME x" = c(-1 / h, 1 / h)),
dpar = "sigma",
effects = "integrateoutRE")
ames$ContrastSummary
ames <- brmsmargins(
object = model,
at = data.frame(x = c(0, 1)),
contrasts = cbind("AME x" = c(-1, 1)),
dpar = "sigma",
effects = "integrateoutRE")
ames$Summary
ames$ContrastSummary
Note that even on mixed effects models, it is possible to generate
predictions and marginal effects from the fixed effects only,
just by specifying effects = "fixedonly"
but this is
probably not a good idea generally so not shown by default.
Also note that for all of these examples ames$Summary
would
have a summary of the averaged predicted values. These often
are useful for discrete predictors. For continuous
predictors, if the focus is on marginal effects, they often are
not interesting. However, the at
argument can be used
with continuous predictors to generate interesting averaged
predicted values. For example, this would get predicted
values integrating out random effects for a range of ages
averaging (marginalizing) all other predictors / covariates.
ames <- brmsmargins(
object = model,
at = data.frame(age = c(20, 30, 40, 50, 60)),
effects = "integrateoutRE")
ames$Summary
You can install the package from CRAN by running this code:
install.packages("brmsmargins")
Alternately, for the latest, development version, run:
remotes::install_github("JWiley/brmsmargins")
There are three vignettes that introduce how to use the package for several scenarios.
- Fixed effects only models (also called single level models). This also is the best place to start learning about how to use the package. It includes a brief amount of motivation for why we would want to calculate marginal effects at all.
- Mixed effects models (also called multilevel models). This shows how to calculate marginal effects for mixed effects / multilevel models. There are runnable examples, but not much background.
- Location scale models.
Location scale models are models where both the location (e.g., mean) and
scale (e.g., variance / residual standard deviation) are explicitly
modeled as outcomes. These require use of distributional parameters
dpar
inbrms
. This vignette shows how to calculate marginal effects from location scale models for the scale part.