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 |
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.
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)
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)
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. |
Marginal effects on count probabilities give you useful information to complement post estimation summaries on the mean scale.
A list of class "bayesmeanscale_marg" with the following components:
diffTable |
summary table of the marginal effects |
diffDraws |
posterior draws of the marginal effects |
David Dalenberg
Long, J. Scott and Jeremy Freese. 2001. "Predicted Probabilities for Count Models." Stata Journal 1(1): 51-57.
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) }
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) }
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.
bayesCountPredsF(model, counts, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
bayesCountPredsF(model, counts, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
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. |
Predicted count probabilities give you useful information to complement post estimation summaries of the mean.
A list of class "bayesmeanscale_pred" with the following components:
predTable |
summary table of the predictions |
predDraws |
posterior draws of the predictions |
David Dalenberg
Long, J. Scott and Jeremy Freese. 2001. "Predicted Probabilities for Count Models." Stata Journal 1(1): 51-57.
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) }
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) }
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.
bayesMargCompareF(marg_list, ci = 0.95, hdi_interval = TRUE, centrality = 'mean', digits = 4)
bayesMargCompareF(marg_list, ci = 0.95, hdi_interval = TRUE, centrality = 'mean', digits = 4)
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. |
Differences of marginal effects contained in the "bayesmeanscale_marg" object are computed.
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 |
David Dalenberg
Mize, Trenton D. 2019. "Best Practices for Estimating, Interpreting, and Presenting Non-linear Interaction Effects." Sociological Science 6: 81-117.
## 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) }
## 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) }
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.
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)
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)
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. |
The following families of fixed-effect models fit using "rstanarm" are supported: 'beta', 'binomial', 'Gamma', 'gaussian', 'neg_binomial_2', and 'poisson.'
A list of class "bayesmeanscale_marg" with the following components:
diffTable |
summary table of the marginal effects |
diffDraws |
posterior draws of the marginal effects |
David Dalenberg
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.
## 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) }
## 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) }
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.
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)
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)
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. |
The following links for fixed-effect ordinal models fit using "rstanarm" are supported: logit, probit, and clogclog.
A list of class "bayesmeanscale_marg" with the following components:
diffTable |
summary table of the marginal effects |
diffDraws |
posterior draws of the marginal effects |
David Dalenberg
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.
## 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) }
## 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) }
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.
bayesOrdinalPredsF(model, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
bayesOrdinalPredsF(model, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
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. |
The following links for fixed-effect ordinal models fit using "rstanarm" are supported: logit, probit, and clogclog.
A list of class "bayesmeanscale_pred" with the following components:
predTable |
summary table of the predictions |
predDraws |
posterior draws of the predictions |
David Dalenberg
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.
## 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) }
## 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) }
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.
bayesPredsF(model, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
bayesPredsF(model, at, n_draws = 2000, ci = .95, hdi_interval = TRUE, centrality = 'mean', digits = 4, at_means = FALSE, data_slice = 'full')
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. |
The following families of fixed-effect models fit using "rstanarm" are supported: 'beta', 'binomial', 'Gamma', 'gaussian', 'neg_binomial_2', and 'poisson.'
A list of class "bayesmeanscale_pred" with the following components:
predTable |
summary table of the predictions |
predDraws |
posterior draws of the predictions |
David Dalenberg
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.
## 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) }
## 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) }