Package 'bayesMeanScale'

Title: Bayesian Post-Estimation on the Mean Scale
Description: Computes Bayesian posterior distributions of predictions, marginal effects, and differences of marginal effects for various generalized linear models. Importantly, the posteriors are on the mean (response) scale, allowing for more natural interpretation than summaries on the link scale. Also, predictions and marginal effects of the count probabilities for Poisson and negative binomial models can be computed.
Authors: David M. Dalenberg [aut, cre]
Maintainer: David M. Dalenberg <[email protected]>
License: GPL (>= 3)
Version: 0.2.1
Built: 2025-03-09 05:44:20 UTC
Source: https://github.com/dalenbe2/bayesmeanscale

Help Index


Bayesian marginal effects on count probabilities.

Description

Computes Bayesian marginal effects on count probabilities. Models must have a family of "poisson"" or "neg_binomial_2" and be fit using the "rstanarm" package. Marginal effects can be averaged over the values of the covariates in the data (average marginal effects), or the covariates can be held at their means (marginal effects at the means). Also, "at" values can be specified to fix covariates at particular values. The marginal effects can be specified in terms of discrete changes or instantaneous rates of change.

Usage

bayesCountMargEffF(model,
                   counts,
                   marginal_effect,
                   start_value,
                   end_value,
                   n_draws         = 2000,
                   ci              = .95,
                   hdi_interval    = TRUE,
                   centrality      = 'mean',
                   digits          = 4,
                   at              = NULL,
                   at_means        = FALSE,
                   h               = .0001)

Arguments

model

A model object of class "stanreg."

counts

Vector of counts to get the predicted probabilities for.

marginal_effect

A character vector of terms that you want to estimate the marginal effect for.

start_value

The start value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

end_value

The end value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

n_draws

The number of draws to take from the joint posterior distribution.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at

Optional list of covariate values to estimate the marginal effect at.

at_means

If FALSE, the default, the marginal effects are averaged across the rows of the data. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

h

Step size for calculating the approximate instantaneous rate of change.

Details

Marginal effects on count probabilities give you useful information to complement post estimation summaries on the mean scale.

Value

A list of class "bayesmeanscale_marg" with the following components:

diffTable

summary table of the marginal effects

diffDraws

posterior draws of the marginal effects

Author(s)

David Dalenberg

References

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

Examples

if(require(rstanarm)){

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

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

# marginal effect holding covariates at their means #

bayesCountMargEffF(poissonModel,
                   counts          = 1,
                   marginal_effect = 'width',
                   start_value     = 25,
                   end_value       = 20,
                   at_means        = TRUE,
                   n_draws         = 500)
                   
}

Bayesian predictions of count probabilities.

Description

Computes Bayesian predictions for count probabilities. Models must have a family of "poisson"" or "neg_binomial_2" and be fit using the "rstanarm" package. Predictions can be averaged over the values of the covariates in the data (average marginal predictions), or the covariates can be held at their means (marginal predictions at the means). Also, "at" values must be specified to fix at least one covariate at particular values.

Usage

bayesCountPredsF(model,
                 counts,
                 at,
                 n_draws      = 2000,
                 ci           = .95,
                 hdi_interval = TRUE,
                 centrality   = 'mean',
                 digits       = 4,
                 at_means     = FALSE,
                 data_slice   = 'full')

Arguments

model

A model object of class "stanreg."

counts

Vector of counts to get the predicted probabilities for.

at

List of covariate values to estimate the predictions at.

n_draws

The number of draws to take from the joint posterior distribution.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at_means

If FALSE, the default, the predictions are averaged across the rows of the model data for each unique combination of "at" values. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

data_slice

The number of rows of data to average over for the predictions. Defaults to all rows. This can be useful for very large data sets.

Details

Predicted count probabilities give you useful information to complement post estimation summaries of the mean.

Value

A list of class "bayesmeanscale_pred" with the following components:

predTable

summary table of the predictions

predDraws

posterior draws of the predictions

Author(s)

David Dalenberg

References

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

Examples

if(require(rstanarm)){

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

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

bayesCountPredsF(poissonModel,
                 counts  = c(0,1),
                 at      = list(weight=c(2,3)),
                 n_draws = 500)
                 
}

Comparison of Bayesian marginal effects

Description

Tests the differences between all marginal effects in the "bayesmeanscale_marg" object that is passed to it. This is particularly useful for testing non-linear interaction effects.

Usage

bayesMargCompareF(marg_list,
                  ci           = 0.95,
                  hdi_interval = TRUE,
                  centrality   = 'mean',
                  digits       = 4)

Arguments

marg_list

An object of class "bayesmeanscale_marg."

ci

The level of the credible interval.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

Details

Differences of marginal effects contained in the "bayesmeanscale_marg" object are computed.

Value

A list of class "bayesmeanscale_margcompare" with the following components:

diffTable

summary table of the differences of the marginal effects

diffDraws

posterior draws of the differences of the marginal effects

Author(s)

David Dalenberg

References

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

Examples

## Logit model ##

if(require(rstanarm)){

m1 <- stan_glm(switch ~ dist + educ + arsenic + assoc, 
               data    = rstanarm::wells, 
               family  = binomial, 
               refresh = 0,
               iter    = 500)

m1Marg <- bayesMargEffF(m1, 
                        marginal_effect = 'arsenic', 
                        start_value     = 2.2, 
                        end_value       = .82, 
                        at              = list(educ=c(0, 5)),
                        n_draws         = 500)

bayesMargCompareF(m1Marg)

}

Bayesian marginal effects on the mean scale.

Description

Computes Bayesian marginal effects on the mean scale for models fit using the package "rstanarm". Marginal effects can be averaged over the values of the covariates in the data (average marginal effects), or the covariates can be held at their means (marginal effects at the means). Also, "at" values can be specified to fix covariates at particular values. The marginal effects can be specified in terms of discrete changes or instantaneous rates of change.

Usage

bayesMargEffF(model,
              n_draws         = 2000,
              marginal_effect,
              start_value,
              end_value,
              ci              = .95,
              hdi_interval    = TRUE,
              centrality      = 'mean',
              digits          = 4,
              at              = NULL,
              at_means        = FALSE,
              h               = .0001)

Arguments

model

A model object of class "stanreg."

n_draws

The number of draws to take from the joint posterior distribution.

marginal_effect

A character vector of terms that you want to estimate the marginal effect for.

start_value

The start value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

end_value

The end value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at

Optional list of covariate values to estimate the marginal effect at.

at_means

If FALSE, the default, the marginal effects are averaged across the rows of the data. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

h

Step size for calculating the approximate instantaneous rate of change.

Details

The following families of fixed-effect models fit using "rstanarm" are supported: 'beta', 'binomial', 'Gamma', 'gaussian', 'neg_binomial_2', and 'poisson.'

Value

A list of class "bayesmeanscale_marg" with the following components:

diffTable

summary table of the marginal effects

diffDraws

posterior draws of the marginal effects

Author(s)

David Dalenberg

References

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

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.

Examples

## Logit model ##

if(require(rstanarm)){

m1 <- stan_glm(switch ~ dist + educ + arsenic + assoc, 
               data    = rstanarm::wells, 
               family  = binomial, 
               refresh = 0, 
               iter    = 500)

# marginal effect averaging over sample values of covariates #

bayesMargEffF(m1, 
              marginal_effect = 'arsenic', 
              start_value     = 2.2, 
              end_value       = .82,
              n_draws         = 500)
              
}

Bayesian marginal effects on the mean scale.

Description

Computes Bayesian marginal effects on the probability of outcomes for cumulative link models with a proportional odds structure fit using the package "rstanarm". Marginal effects can be averaged over the values of the covariates in the data (average marginal effects), or the covariates can be held at their means (marginal effects at the means). Also, "at" values can be specified to fix covariates at particular values. The marginal effects can be specified in terms of discrete changes or instantaneous rates of change.

Usage

bayesOrdinalMargEffF(model,
                     n_draws         = 2000,
                     marginal_effect,
                     start_value,
                     end_value,
                     ci              = .95,
                     hdi_interval    = TRUE,
                     centrality      = 'mean',
                     digits          = 4,
                     at              = NULL,
                     at_means        = FALSE,
                     h               = .0001)

Arguments

model

A model object of class "stanreg" and "polr."

n_draws

The number of draws to take from the joint posterior distribution.

marginal_effect

A character vector of terms that you want to estimate the marginal effect for.

start_value

The start value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

end_value

The end value(s) for the marginal effect(s). Must be a list when specifying multiple values. Specify "instantaneous" when you want to calculate an approximate instantaneous rate of change.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at

Optional list of covariate values to estimate the marginal effect at.

at_means

If FALSE, the default, the marginal effects are averaged across the rows of the data. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

h

Step size for calculating the approximate instantaneous rate of change.

Details

The following links for fixed-effect ordinal models fit using "rstanarm" are supported: logit, probit, and clogclog.

Value

A list of class "bayesmeanscale_marg" with the following components:

diffTable

summary table of the marginal effects

diffDraws

posterior draws of the marginal effects

Author(s)

David Dalenberg

References

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

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.

Examples

## Proportional odds logistic regression ##

if(require(rstanarm) & require(MASS)){

m1 <- stan_polr(Sat ~ Infl + Type, 
                data    = MASS::housing, 
                prior   = rstanarm::R2(0.2, 'mean'), 
                refresh = 0, 
                iter    = 500)

# marginal effects holding covariates at means #

bayesOrdinalMargEffF(m1,
                     marginal_effect = "Infl",
                     start_value     = "Low",
                     end_value       = "High",
                     at              = list(Type = c("Tower", "Apartment")), 
                     at_means        = TRUE,
                     n_draws         = 500)
                     
}

Bayesian predictions for ordinal models on the mean scale.

Description

Computes Bayesian outcome predictions for cumulative link models with a proportional odds structure fit using the package "rstanarm". Predictions can be averaged over the values of the covariates in the data (average marginal predictions), or the covariates can be held at their means (marginal predictions at the means). Also, "at" values must be specified to fix at least one covariate at particular values.

Usage

bayesOrdinalPredsF(model,
                   at,
                   n_draws      = 2000,
                   ci           = .95,
                   hdi_interval = TRUE,
                   centrality   = 'mean',
                   digits       = 4,
                   at_means     = FALSE,
                   data_slice   = 'full')

Arguments

model

A model object of class "stanreg" and "polr."

at

List of covariate values to estimate the predictions at.

n_draws

The number of draws to take from the joint posterior distribution.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at_means

If FALSE, the default, the predictions are averaged across the rows of the model data for each unique combination of "at" values. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

data_slice

The number of rows of data to average over for the predictions. Defaults to all rows. This can be useful for very large data sets.

Details

The following links for fixed-effect ordinal models fit using "rstanarm" are supported: logit, probit, and clogclog.

Value

A list of class "bayesmeanscale_pred" with the following components:

predTable

summary table of the predictions

predDraws

posterior draws of the predictions

Author(s)

David Dalenberg

References

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

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.

Examples

## Proportional odds logistic regression ##

if(require(rstanarm) & require(MASS)){

m1 <- stan_polr(Sat ~ Infl + Type, 
                data    = MASS::housing, 
                prior   = rstanarm::R2(0.2, 'mean'), 
                refresh = 0, 
                iter    = 500)

# marginal predictions holding covariates at means #

bayesOrdinalPredsF(m1, 
                   at       = list(Type = c("Tower", "Apartment")), 
                   at_means = TRUE,
                   n_draws  = 500)
 
}

Bayesian predictions on the mean scale.

Description

Computes Bayesian predictions on the mean scale for models fit using the package "rstanarm". Predictions can be averaged over the values of the covariates in the data (average marginal predictions), or the covariates can be held at their means (marginal predictions at the means). Also, "at" values must be specified to fix at least one covariate at particular values.

Usage

bayesPredsF(model,
            at,
            n_draws      = 2000,
            ci           = .95,
            hdi_interval = TRUE,
            centrality   = 'mean',
            digits       = 4,
            at_means     = FALSE,
            data_slice   = 'full')

Arguments

model

A model object of class "stanreg."

at

List of covariate values to estimate the predictions at.

n_draws

The number of draws to take from the joint posterior distribution.

ci

The level for the credible intervals.

hdi_interval

If TRUE, the default, computes the highest density credible interval. If FALSE, computes the equal-tailed interval.

centrality

Centrality measure for the posterior distribution. Options are "mean" or "median".

digits

The number of digits to report in the summary table.

at_means

If FALSE, the default, the predictions are averaged across the rows of the model data for each unique combination of "at" values. If TRUE, the covariate values that are not specified in the "at" argument are held at their means.

data_slice

The number of rows of data to average over for the predictions. Defaults to all rows. This can be useful for very large data sets.

Details

The following families of fixed-effect models fit using "rstanarm" are supported: 'beta', 'binomial', 'Gamma', 'gaussian', 'neg_binomial_2', and 'poisson.'

Value

A list of class "bayesmeanscale_pred" with the following components:

predTable

summary table of the predictions

predDraws

posterior draws of the predictions

Author(s)

David Dalenberg

References

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

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.

Examples

## Logit model ##

if(require(rstanarm)){

m1 <- stan_glm(switch ~ dist + educ + arsenic + assoc, 
               data    = rstanarm::wells, 
               family  = binomial, 
               refresh = 0, 
               iter    = 500)

# marginal predictions holding covariates at means #

bayesPredsF(m1, 
            at       = list(arsenic = c(.82, 1.3)), 
            at_means = TRUE,
            n_draws  = 500)
         
}