| name | bayesian-modeling |
| description | Bayesian modeling in R with brms, rstanarm, priors, diagnostics, posterior checks, and model comparison. |
Bayesian Modeling in R
Overview
Comprehensive Bayesian statistical modeling using Stan-based packages (brms, rstanarm), covering prior specification, posterior analysis, model comparison, and Bayesian workflow best practices.
brms: Bayesian Regression Models
Basic Models
library(brms)
fit <- brm(
formula = y ~ x1 + x2,
data = df,
family = gaussian(),
seed = 123
)
fit_logit <- brm(
y ~ x1 + x2,
data = df,
family = bernoulli(link = "logit")
)
fit_pois <- brm(
count ~ x1 + x2 + offset(log(exposure)),
data = df,
family = poisson()
)
Prior Specification
get_prior(y ~ x1 + x2, data = df, family = gaussian())
custom_priors <- c(
prior(normal(0, 10), class = "Intercept"),
prior(normal(0, 2), class = "b"),
prior(normal(0, 1), class = "b", coef = "x1"),
prior(exponential(1), class = "sigma")
)
fit <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = custom_priors,
seed = 123
)
Prior Predictive Checks
fit_prior <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = custom_priors,
sample_prior = "only",
seed = 123
)
pp_check(fit_prior, type = "dens_overlay", ndraws = 100)
Mixed Effects Models
fit_mixed <- brm(
y ~ x1 + x2 + (1 | group),
data = df,
family = gaussian()
)
fit_mixed <- brm(
y ~ x1 + x2 + (1 + x1 | group),
data = df,
family = gaussian()
)
fit_mixed <- brm(
y ~ x1 + (1 | subject) + (1 | item),
data = df,
family = gaussian()
)
Control Parameters
fit <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
chains = 4,
iter = 4000,
warmup = 2000,
cores = 4,
seed = 123,
control = list(
adapt_delta = 0.95,
max_treedepth = 15
)
)
rstanarm: Applied Regression
library(rstanarm)
fit <- stan_glm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = normal(0, 2.5),
prior_intercept = normal(0, 10),
seed = 123
)
fit_mixed <- stan_lmer(
y ~ x1 + x2 + (1 | group),
data = df,
seed = 123
)
fit_glmer <- stan_glmer(
y ~ x1 + x2 + (1 | group),
data = df,
family = binomial(),
seed = 123
)
Posterior Analysis
Summary and Inference
summary(fit)
posterior <- as_draws_df(fit)
posterior_summary(fit)
fixef(fit)
ranef(fit)
posterior_interval(fit, prob = 0.95)
Hypothesis Testing
hypothesis(fit, "x1 > 0")
hypothesis(fit, "x1 > x2")
hypothesis(fit, "x1 + x2 > 0")
hypothesis(fit, c("x1 > 0", "x2 > 0", "x1 > x2"))
Posterior Predictive Checks
library(bayesplot)
pp_check(fit, type = "dens_overlay", ndraws = 50)
pp_check(fit, type = "hist", ndraws = 8)
pp_check(fit, type = "error_scatter_avg")
pp_check(fit, type = "intervals")
pp_check(fit, type = "stat", stat = "mean")
pp_check(fit, type = "stat_2d", stat = c("mean", "sd"))
MCMC Diagnostics
library(bayesplot)
mcmc_trace(fit)
rhat(fit)
mcmc_rhat(rhat(fit))
neff_ratio(fit)
mcmc_neff(neff_ratio(fit))
mcmc_pairs(fit, pars = c("b_x1", "b_x2", "sigma"))
mcmc_nuts_energy(nuts_params(fit))
Model Comparison
LOO Cross-Validation
loo_fit1 <- loo(fit1)
loo_fit2 <- loo(fit2)
loo_compare(loo_fit1, loo_fit2)
plot(loo_fit1)
WAIC
waic_fit1 <- waic(fit1)
waic_fit2 <- waic(fit2)
loo_compare(waic_fit1, waic_fit2)
Bayes Factors
library(bridgesampling)
bridge_fit1 <- bridge_sampler(fit1)
bridge_fit2 <- bridge_sampler(fit2)
bayes_factor(bridge_fit1, bridge_fit2)
Model Stacking
library(loo)
model_weights <- loo_model_weights(
list(fit1, fit2, fit3),
method = "stacking"
)
Predictions
Posterior Predictions
fitted(fit, newdata = new_data)
predict(fit, newdata = new_data)
pp_draws <- posterior_predict(fit, newdata = new_data)
Marginal Effects
conditional_effects(fit)
conditional_effects(fit, effects = "x1")
conditional_effects(fit, effects = "x1:x2")
plot(conditional_effects(fit, effects = "x1"), points = TRUE)
Marginal Means
library(emmeans)
emmeans(fit, ~ treatment)
emmeans(fit, pairwise ~ treatment)
Advanced Topics
Non-Linear Models
fit_nl <- brm(
bf(y ~ a * exp(-b * x), a ~ 1, b ~ 1, nl = TRUE),
data = df,
prior = c(
prior(normal(10, 5), nlpar = "a"),
prior(normal(0.5, 0.2), nlpar = "b")
),
family = gaussian()
)
Distributional Models
fit_dist <- brm(
bf(y ~ x1 + x2, sigma ~ x1),
data = df,
family = gaussian()
)
Ordinal Regression
fit_ordinal <- brm(
rating ~ x1 + x2,
data = df,
family = cumulative("logit")
)
Zero-Inflated Models
fit_zi <- brm(
count ~ x1 + x2,
data = df,
family = zero_inflated_poisson()
)
fit_zi <- brm(
bf(count ~ x1 + x2, zi ~ x3),
data = df,
family = zero_inflated_poisson()
)
Survival Models
fit_surv <- brm(
time | cens(censored) ~ x1 + x2,
data = df,
family = cox()
)
fit_weibull <- brm(
time | cens(censored) ~ x1 + x2,
data = df,
family = weibull()
)
Bayesian Workflow
Complete Workflow Example
library(brms)
library(bayesplot)
fit_prior <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = c(
prior(normal(0, 10), class = "Intercept"),
prior(normal(0, 2), class = "b"),
prior(exponential(1), class = "sigma")
),
sample_prior = "only",
seed = 123
)
pp_check(fit_prior, ndraws = 50)
fit <- update(fit_prior, sample_prior = "no")
summary(fit)
mcmc_trace(fit)
pp_check(fit, ndraws = 50)
fit_alt <- brm(y ~ x1, data = df, family = gaussian())
loo_compare(loo(fit), loo(fit_alt))
fixef(fit)
hypothesis(fit, "x1 > 0")
conditional_effects(fit)
Key Packages Summary
| Package | Purpose |
|---|
| brms | General Bayesian regression |
| rstanarm | Applied regression models |
| bayesplot | MCMC visualization |
| loo | Model comparison |
| bridgesampling | Bayes factors |
| tidybayes | Tidy Bayesian analysis |
| posterior | Posterior manipulation |