bayesian-modelinglisted
Install: claude install-skill choxos/BiostatAgent
# 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
```r
library(brms)
# Linear regression
fit <- brm(
formula = y ~ x1 + x2,
data = df,
family = gaussian(),
seed = 123
)
# Logistic regression
fit_logit <- brm(
y ~ x1 + x2,
data = df,
family = bernoulli(link = "logit")
)
# Poisson regression
fit_pois <- brm(
count ~ x1 + x2 + offset(log(exposure)),
data = df,
family = poisson()
)
```
### Prior Specification
```r
# View default priors
get_prior(y ~ x1 + x2, data = df, family = gaussian())
# Set custom priors
custom_priors <- c(
prior(normal(0, 10), class = "Intercept"),
prior(normal(0, 2), class = "b"), # All regression coefficients
prior(normal(0, 1), class = "b", coef = "x1"), # Specific coefficient
prior(exponential(1), class = "sigma") # Error SD
)
fit <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = custom_priors,
seed = 123
)
```
### Prior Predictive Checks
```r
# Sample from prior only
fit_prior <- brm(
y ~ x1 + x2,
data = df,
family = gaussian(),
prior = custom_priors,
sample_prior = "only", # Prior predictive
seed = 123
)
# Visualize prior predictions
pp_check(fit_prior, type = "dens_overlay", ndraws = 100)
```
### Mixed Effects Models
```r
# Random intercept