This vignette provides an overview of the bayesMeanScale package, which is designed to compute model predictions, marginal effects, and comparisons of marginal effects for several different fixed-effect generalized linear models fit using the rstanarm package (https://mc-stan.org/rstanarm/). In particular, these statistics are computed on the mean scale rather than the link scale for easier interpretation. For example, rather than working on the log-odds scale for a logistic regression, we focus on the probability scale.

To get to the mean scale, bayesMeanScale takes a random sample with replacement from the joint posterior distribution. Then, this matrix is multiplied by the adjusted data matrix (adjusted according to your values of interest). Finally, the inverse link function is applied to transform the predictions to the mean scale.

Predictions are computed by holding one or more explanatory variables fixed at particular values and either averaging over the rows of the data (average marginal predictions) or holding all other covariates at their means (marginal predictions at the mean). Marginal effects can also be calculated by averaging over the data (average marginal effect) or holding covariates at their means (marginal effect at the mean).

Currently, the effects can only be specified in terms of discrete changes. For a continuous variable, this might mean looking at the difference between the mean and the mean plus 1 standard deviation. In statistical applications, this sort of strategy is often very useful for summarizing a model.

The third workhorse function of the package compares marginal effects against each other. This is particularly useful for testing non-linear interaction effects such as those that appear in generalized linear models that do not use the identity link.

Predictions

Average marginal predictions

The examples in this vignette use the wells data from the rstanarm package. Use ?rstanarm::wells to view the documentation on this dataset.

For average marginal predictions, the goal is to get predictions at important settings of one or more of the model explanatory variables. These predictions are then averaged over the rows of the data.

lapply(c('bayesMeanScale', 'dplyr', 'rstanarm', 'bayestestR', 'flextable'), function(x) base::library(x, character.only=T))
# Simulate the data #

data(wells)

modelData <- wells %>%
  mutate(assoc = if_else(assoc==1, 'Y', 'N'))

binomialModel <- stan_glm(switch ~ dist*educ + arsenic + I(arsenic^2) + assoc, 
                          data    = modelData, 
                          family  = binomial, 
                          refresh = 0)
bayesPredsF(binomialModel, 
            at = list(arsenic = c(.82, 1.3, 2.2), assoc=c("Y", "N")))
##  arsenic assoc   mean  lower  upper
##     0.82     Y 0.4532 0.4201 0.4841
##     1.30     Y 0.5344 0.5056 0.5623
##     2.20     Y 0.6559 0.6212 0.6844
##     0.82     N 0.4839 0.4553 0.5107
##     1.30     N 0.5649 0.5416 0.5878
##     2.20     N 0.6830 0.6584 0.7105

The output contains the unique values for the “at” variables, the posterior means, and the lower and upper bounds of the credible intervals.

Marginal predictions at the mean

For marginal predictions at the mean, the goal is essentially the same except that we want to hold the covariates at their means. In the example below, all explanatory variables except for “arsenic” are held at their means for the computation. Since “assoc” is a discrete variable, we hold it at the proportion of cases that equaled “Y”.

bayesPredsF(binomialModel, 
            at       = list(arsenic = c(.82, 1.3, 2.2), assoc=c("Y", "N")), 
            at_means = T)
##  arsenic assoc   mean  lower  upper
##     0.82     Y 0.4486 0.4168 0.4835
##     1.30     Y 0.5334 0.5044 0.5631
##     2.20     Y 0.6605 0.6263 0.6902
##     0.82     N 0.4806 0.4512 0.5102
##     1.30     N 0.5653 0.5405 0.5903
##     2.20     N 0.6888 0.6630 0.7171

The results are slightly different than the average marginal predictions. From a computational standpoint, setting “at_means” to “TRUE” makes for a substantially faster computation. For relatively small models, this speed advantage is likely trivial, but it can make a noticeable difference when working with big data models.

Average marginal predictions for count probabilities

You can also get the predictions for the count probabilities from a poisson or negative binomial model. Here, rather than looking at the rate, or mean, parameter, we investigate the probabilities of particular counts. This is an effective approach for summarizing count models in more depth.

crabs <- read.table("https://users.stat.ufl.edu/~aa/cat/data/Crabs.dat", header=T)

poissonModel  <- stan_glm(sat ~ weight + width, 
                          data    = crabs, 
                          family  = poisson, 
                          refresh = 0)

bayesCountPredsF(poissonModel,
                 counts = c(0,1,2),
                 at     = list(weight=c(2,3,4)))
##  weight count   mean  lower  upper
##       2     0 0.1097 0.0750 0.1476
##       3     0 0.0344 0.0134 0.0610
##       4     0 0.0094 0.0000 0.0334
##       2     1 0.2361 0.1852 0.2811
##       3     1 0.1099 0.0621 0.1618
##       4     1 0.0364 0.0003 0.1028
##       2     2 0.2596 0.2382 0.2706
##       3     2 0.1819 0.1358 0.2204
##       4     2 0.0765 0.0020 0.1678

Marginal effects

Average marginal effects

The concept of average marginal effects is straightforward. We simply take two posterior distributions of average marginal predictions and subtract one from the other. In the example below, we see that the average expected probability of switching wells for families that had an arsenic level of 2.2 is roughly 27 percentage points greater than for a family that had an arsenic level of .82.

binomialAME <- bayesMargEffF(binomialModel,
                             marginal_effect = 'arsenic',
                             start_value     = 2.2,
                             end_value       = .82)

binomialAME
##  marg_effect start_val end_val  mean  lower  upper
##      arsenic       2.2    0.82 0.268 0.2168 0.3164
head(binomialAME$diffDraws)
##         diff   comparison marg_effect
##        <num>       <char>      <char>
## 1: 0.2566236 2.2 vs. 0.82     arsenic
## 2: 0.3137333 2.2 vs. 0.82     arsenic
## 3: 0.2867327 2.2 vs. 0.82     arsenic
## 4: 0.2857021 2.2 vs. 0.82     arsenic
## 5: 0.2575260 2.2 vs. 0.82     arsenic
## 6: 0.3009130 2.2 vs. 0.82     arsenic

Also note that we can access the posterior distribution of the marginal effects. This can be useful for graphing purposes and to describe the marginal effect distributions in more detail.

We can also compute multiple marginal effects. When doing so, it is necessary to specify the start and end values in a list.

bayesMargEffF(binomialModel,
              marginal_effect = c('arsenic', 'dist'),
              start_value     = list(2.2, 64.041),
              end_value       = list(.82, 21.117))
##  marg_effect start_val end_val    mean   lower   upper
##      arsenic     2.200   0.820  0.2684  0.2142  0.3133
##         dist    64.041  21.117 -0.0963 -0.1151 -0.0762

The “at” argument allows the user to specify particular values for one or more covariates, and they must be specified in a list. The example below specifies at values for “educ” given that we have an interaction between “dist” and “educ” in the model. If there is an interaction effect on the mean (probability) scale, we would expect the marginal effect of “dist” to be different at various levels of “educ.” The final section will cover how to test these marginal effects against each other.

binomialAMEInteraction <- bayesMargEffF(binomialModel,
                                        marginal_effect = 'dist',
                                        start_value     = 64.041,
                                        end_value       = 21.117,
                                        at              = list(educ=c(0, 5, 8)))

binomialAMEInteraction
##  educ marg_effect start_val end_val    mean   lower   upper
##     0        dist    64.041  21.117 -0.1430 -0.1740 -0.1091
##     5        dist    64.041  21.117 -0.0942 -0.1139 -0.0747
##     8        dist    64.041  21.117 -0.0651 -0.0887 -0.0420

Average marginal effects for count probabilities

You can also get the marginal effects for the count probabilities from a poisson or negative binomial model.

countMarg <- bayesCountMargEffF(poissonModel,
                                counts          = c(0,1,2),
                                marginal_effect = 'width',
                                start_value     = 25,
                                end_value       = 20,
                                at              = list(weight=c(2,3,4)))

countMarg
##  weight count marg_effect start_val end_val    mean   lower  upper
##       2     0       width        25      20 -0.0757 -0.2092 0.0648
##       3     0       width        25      20 -0.0564 -0.1807 0.0111
##       4     0       width        25      20 -0.0348 -0.1499 0.0009
##       2     1       width        25      20 -0.0503 -0.1240 0.0536
##       3     1       width        25      20 -0.0699 -0.1551 0.0308
##       4     1       width        25      20 -0.0615 -0.1707 0.0053
##       2     2       width        25      20  0.0191 -0.0084 0.0711
##       3     2       width        25      20 -0.0212 -0.0674 0.0531
##       4     2       width        25      20 -0.0512 -0.1163 0.0092

Marginal effects at the mean

Marginal effects at the mean compute the differences while holding the covariates at their means. Like the marginal predictions at the mean, specifying “at_means=TRUE” allows for a much faster computation than specifying “at_means=F”.

binomialMEMInteraction <- bayesMargEffF(binomialModel,
                                        marginal_effect = 'dist',
                                        start_value     = 64.041,
                                        end_value       = 21.117,
                                        at              = list(educ=c(0, 5, 8)),
                                        at_means        = T)

binomialMEMInteraction
##  educ marg_effect start_val end_val    mean   lower   upper
##     0        dist    64.041  21.117 -0.1525 -0.1875 -0.1189
##     5        dist    64.041  21.117 -0.1001 -0.1210 -0.0785
##     8        dist    64.041  21.117 -0.0691 -0.0938 -0.0450

Comparing marginal effects

After computing multiple marginal effects for a model, you might like to compare them against one another. The “bayesMargCompareF” function calculates tests for all the unique pairs of marginal effects that you computed with “bayesMargEffF”. In the example below, we are able to investigate the interaction effect between “dist” and “educ”. We see that the marginal effect of “dist” is meaningfully different at different levels of “educ”.

bayesMargCompareF(binomialAMEInteraction)
##  educ1       comparison1 marg_effect1 educ2       comparison2 marg_effect2
##      5 64.041 vs. 21.117         dist     0 64.041 vs. 21.117         dist
##      8 64.041 vs. 21.117         dist     0 64.041 vs. 21.117         dist
##      8 64.041 vs. 21.117         dist     5 64.041 vs. 21.117         dist
##    mean  lower  upper
##  0.0488 0.0255 0.0747
##  0.0779 0.0411 0.1189
##  0.0291 0.0151 0.0438

You can also compare the marginal effects on count probabilities, shown in the example below.

bayesMargCompareF(countMarg)
##  weight1 count1 comparison1 marg_effect1 weight2 count2 comparison2
##        3      0   25 vs. 20        width       2      0   25 vs. 20
##        4      0   25 vs. 20        width       2      0   25 vs. 20
##        4      0   25 vs. 20        width       3      0   25 vs. 20
##        2      1   25 vs. 20        width       2      0   25 vs. 20
##        2      1   25 vs. 20        width       3      0   25 vs. 20
##        2      1   25 vs. 20        width       4      0   25 vs. 20
##        3      1   25 vs. 20        width       2      0   25 vs. 20
##        3      1   25 vs. 20        width       3      0   25 vs. 20
##        3      1   25 vs. 20        width       4      0   25 vs. 20
##        3      1   25 vs. 20        width       2      1   25 vs. 20
##        4      1   25 vs. 20        width       2      0   25 vs. 20
##        4      1   25 vs. 20        width       3      0   25 vs. 20
##        4      1   25 vs. 20        width       4      0   25 vs. 20
##        4      1   25 vs. 20        width       2      1   25 vs. 20
##        4      1   25 vs. 20        width       3      1   25 vs. 20
##        2      2   25 vs. 20        width       2      0   25 vs. 20
##        2      2   25 vs. 20        width       3      0   25 vs. 20
##        2      2   25 vs. 20        width       4      0   25 vs. 20
##        2      2   25 vs. 20        width       2      1   25 vs. 20
##        2      2   25 vs. 20        width       3      1   25 vs. 20
##        2      2   25 vs. 20        width       4      1   25 vs. 20
##        3      2   25 vs. 20        width       2      0   25 vs. 20
##        3      2   25 vs. 20        width       3      0   25 vs. 20
##        3      2   25 vs. 20        width       4      0   25 vs. 20
##        3      2   25 vs. 20        width       2      1   25 vs. 20
##        3      2   25 vs. 20        width       3      1   25 vs. 20
##        3      2   25 vs. 20        width       4      1   25 vs. 20
##        3      2   25 vs. 20        width       2      2   25 vs. 20
##        4      2   25 vs. 20        width       2      0   25 vs. 20
##        4      2   25 vs. 20        width       3      0   25 vs. 20
##        4      2   25 vs. 20        width       4      0   25 vs. 20
##        4      2   25 vs. 20        width       2      1   25 vs. 20
##        4      2   25 vs. 20        width       3      1   25 vs. 20
##        4      2   25 vs. 20        width       4      1   25 vs. 20
##        4      2   25 vs. 20        width       2      2   25 vs. 20
##        4      2   25 vs. 20        width       3      2   25 vs. 20
##  marg_effect2    mean   lower  upper
##         width  0.0193 -0.0284 0.0517
##         width  0.0410 -0.0399 0.1022
##         width  0.0217 -0.0093 0.0487
##         width  0.0255 -0.0074 0.1039
##         width  0.0062 -0.0358 0.0858
##         width -0.0155 -0.0636 0.0768
##         width  0.0058 -0.0226 0.0731
##         width -0.0135 -0.0469 0.0349
##         width -0.0352 -0.0872 0.0381
##         width -0.0197 -0.0535 0.0037
##         width  0.0142 -0.0493 0.0533
##         width -0.0051 -0.0325 0.0084
##         width -0.0268 -0.0704 0.0035
##         width -0.0113 -0.0763 0.0316
##         width  0.0084 -0.0323 0.0406
##         width  0.0948 -0.0410 0.2771
##         width  0.0755 -0.0063 0.2517
##         width  0.0538 -0.0087 0.2173
##         width  0.0694 -0.0489 0.1824
##         width  0.0890 -0.0207 0.2210
##         width  0.0806 -0.0019 0.2411
##         width  0.0546 -0.0058 0.2336
##         width  0.0353 -0.0291 0.2062
##         width  0.0136 -0.0556 0.1681
##         width  0.0291 -0.0229 0.1356
##         width  0.0488 -0.0049 0.1679
##         width  0.0404 -0.0335 0.1971
##         width -0.0402 -0.0865 0.0349
##         width  0.0245 -0.0621 0.1801
##         width  0.0052 -0.0410 0.1407
##         width -0.0165 -0.0911 0.1151
##         width -0.0010 -0.0652 0.1019
##         width  0.0187 -0.0347 0.1133
##         width  0.0103 -0.0358 0.1363
##         width -0.0703 -0.1553 0.0054
##         width -0.0301 -0.0793 0.0157

List of models currently supported


Class

Family

Links

stanreg

beta

logit; probit; cloglog

stanreg

binomial

logit; probit; cloglog

stanreg

Gamma

inverse; log; identity

stanreg

gaussian

identity

stanreg

neg_binomial_2

identity; log; sqrt

stanreg

poisson

identity; log; sqrt

References

Agresti, Alan. 2013. Categorical Data Analysis. Third Edition. New York: Wiley

Long, J. Scott and Jeremy Freese. 2001. “Predicted Probabilities for Count Models.” Stata Journal 1(1): 51-57.

Long, J. Scott and Sarah A. Mustillo. 2018. “Using Predictions and Marginal Effects to Compare Groups in Regression Models for Binary Outcomes.” Sociological Methods & Research 50(3): 1284-1320.

Mize, Trenton D. 2019. “Best Practices for Estimating, Interpreting, and Presenting Non-linear Interaction Effects.” Sociological Science 6: 81-117.