Bayesian Statistics Exercises in R: 20 Practice Problems

Twenty practice problems on Bayesian inference in R: priors, posteriors, MCMC, brms, rstanarm, posterior summaries. Hidden solutions.

library(loo)
library(rstan)
# library(brms); library(rstanarm); library(bayesplot); library(posterior)

Exercise 1: Conjugate prior for binomial

Difficulty: Intermediate.

Show solution
# Beta(a,b) prior + binomial likelihood -> Beta(a+x, b+n-x) posterior
a <- 2; b <- 2; n <- 10; x <- 7
post_a <- a + x; post_b <- b + n - x
qbeta(c(0.025, 0.975), post_a, post_b)

Exercise 2: Posterior mean (beta-binomial)

Difficulty: Intermediate.

Show solution
a <- 7; b <- 2 + 3
a / (a + b)

Exercise 3: brms simple linear regression

Difficulty: Advanced.

Show solution
# fit <- brms::brm(mpg ~ wt, data = mtcars, chains = 4)
# summary(fit)

Exercise 4: rstanarm linear

Difficulty: Advanced.

Show solution
# fit <- rstanarm::stan_glm(mpg ~ wt, data = mtcars)

Exercise 5: Set a normal prior

Difficulty: Advanced.

Show solution
# brms::brm(mpg ~ wt, data = mtcars,
#           prior = prior(normal(0, 10), class = "b"))

Exercise 6: Inspect chains

Difficulty: Advanced.

Show solution
# plot(fit)   # trace and density per parameter

Exercise 7: R-hat convergence

Difficulty: Advanced.

Show solution
# rstan::rhat(fit$fit) |> max()   # should be near 1.0

Exercise 8: Effective sample size

Difficulty: Advanced.

Show solution
# posterior::ess_bulk(as_draws(fit))

Exercise 9: Posterior CI

Difficulty: Intermediate.

Show solution
# posterior_interval(fit, prob = 0.95)

Exercise 10: Posterior predictive check

Difficulty: Advanced.

Show solution
# rstanarm::pp_check(fit, nreps = 50)

Exercise 11: Compare two models with LOO

Difficulty: Advanced.

Show solution
# l1 <- loo::loo(fit1); l2 <- loo::loo(fit2)
# loo::loo_compare(l1, l2)

Exercise 12: Bayes factor

Difficulty: Advanced.

Show solution
# library(bayestestR); bayesfactor(fit1, fit2)

Exercise 13: Posterior mean prediction

Difficulty: Intermediate.

Show solution
# posterior_predict(fit, newdata = data.frame(wt = 3)) |> colMeans()

Exercise 14: Hierarchical model

Difficulty: Advanced.

Show solution
# brms::brm(mpg ~ wt + (1 | cyl), data = mtcars)

Exercise 15: Logistic Bayesian

Difficulty: Advanced.

Show solution
# rstanarm::stan_glm(am ~ mpg, data = mtcars, family = binomial)

Exercise 16: Strong prior vs weak

Difficulty: Advanced.

Show solution
# Compare posterior with prior(normal(0, 0.1)) vs prior(normal(0, 10))

Exercise 17: Grid approximation

Difficulty: Advanced.

Show solution
p_grid <- seq(0, 1, length = 1000)
prior <- rep(1, 1000)
lik <- dbinom(7, 10, p_grid)
post <- (lik * prior) / sum(lik * prior)
sample(p_grid, 1e4, prob = post, replace = TRUE) |> mean()

Exercise 18: Plot posterior

Difficulty: Intermediate.

Show solution
# library(bayesplot); mcmc_areas(as.matrix(fit), prob = 0.95)

Exercise 19: Credible interval extraction

Difficulty: Intermediate.

Show solution
# bayestestR::hdi(fit)

Exercise 20: Posterior probability of effect

Difficulty: Advanced.

Show solution
# bayestestR::p_direction(fit)

What to do next

  • Hypothesis-Testing-Exercises (shipped), frequentist baseline.
  • Linear-Regression-Exercises (shipped), OLS baseline.

Ready to earn the Bayesian Statistics Certificate?

The quiz is concept-based and respects your time: pass it once and your verifiable certificate is yours to share on LinkedIn, your resume, or your portfolio. Take it when you feel comfortable with the material.

Attempt the quiz