Skip to content

A Bayesian model, start to finish

Fit a regression with priors and group effects in the Bayesian models studio, check the priors, the chains and the fit, and compare models by how well they predict new data.

Applied ~13 min10 steps

What runs in CodeSplash

You will be able to

  • Follow the workflow from priors to a reported model
  • Read a prior predictive check and a posterior predictive check
  • Say which convergence numbers must pass before you read an estimate
  • Compare two fits by expected log predictive density and its standard error

Read first

01

Why a whole workflow

Read

A Bayesian model gives a whole distribution for every parameter: not one slope, but every slope the data and the priorPriorWhat you believe about a parameter before seeing the data, written as a probability distribution. leave plausible, with their probabilities. That's worth having only when three things hold:

  1. The priors say something sensible.
  2. The sampler actually explored the posteriorPosteriorYour belief about a parameter after combining the prior with the data..
  3. The model describes the data well enough to trust what it says about them.

The Bayesian models studio walks through those checks in order: Data → Model → Fit → Diagnose → Check → Compare → Report. Every fit runs on StanStanA language and sampler for Bayesian models. CodeSplash runs precompiled Stan programs, so no compiler is needed. programs that CodeSplash compiled ahead of time, from Python or R. Both engines run the same program on the same data file, so the same seed gives the same draws.

02

Priors before data

Read

A prior on a regression slope of normal(0, 1000) sounds harmless: "I don't know." But on the outcome's scale it may allow effects a thousand times larger than anything possible. A prior predictive checkPrior predictive checkSimulate data from the priors alone, before using the outcome, to see whether the priors allow sensible values. makes this visible. It simulates outcomes from the priors alone, before the outcome is used.

03

Predict: what the prior allows

Predict

Predict first

You model chick weights (grams) with very wide normal(0, 1000) priors on every coefficient and run the prior predictive check. What do the simulated weights look like?

Values close to the observed chick weightsValues spread from huge negative to huge positive weightsOnly zeros
Reveal

They spread from huge negative to huge positive values: a chick of −3,000 g is as plausible to this prior as one of 150 g. Weakly informative priors, scaled to the data, rule out the absurd without favoring an answer. The studio's defaults are weakly informative, and its Model step lists them.

04

Did the chains converge?

Read

Stan runs several chains from different starting points. Their draws are only trustworthy when they agree and have explored the whole posterior. Read the Diagnose step before any estimate:

  • R-hat compares the chains. At most 1.011.01 means they agree.
  • Effective sample size says how many independent draws the correlated draws are worth. Aim for at least 400, in the bulk and in the tails.
  • Divergent transitions mean the sampler met curvature it couldn't follow, so it may have missed part of the posterior. Raising adapt_delta takes smaller steps; a non-centered model for group effects often removes them.

The studio says which checks failed and what to try. A failed check means: don't read the estimates yet.

05

Check: did it converge?

Checkpoint

Check yourself

A fit reports R-hat of 1.08 for one group's standard deviation and no divergent transitions. Everything else looks fine. What should you do?

Show the answer

Answer: b

R-hat well above 1.011.01 means the chains disagree about that parameter, so its draws don't describe one posterior yet. More iterations often fix it; when they don't, the data may not identify the parameter, and a more informative prior on the group scale or a simpler model helps.

06

Does the model fit?

Read

Convergence says the sampler did its job, not that the model is right. A posterior predictive checkPosterior predictive checkCompare data simulated from the fitted model with the observed data; a systematic gap shows what the model misses. simulates new data sets from the fitted model and overlays them on the real data. Too few zeros, tails that are too thin, or a shifted peak show what the model misses: a different family, a missing predictor or a group effect.

The Check step also lists each observation's Pareto k̂ from PSIS-LOOPSIS-LOOEstimates how well a model predicts each observation left out, from one fit, by reweighting the posterior draws.. An observation above 0.70.7 sways the fit on its own, and the leave-one-out estimate for it is unreliable.

07

Compare fits by prediction

Read

"New fit from this one" copies a fit, so you can change one thing, such as the family or a group effect, and compare. The Compare step ranks fits by expected log predictive densityExpected log predictive densityHow well a model is expected to predict new data, summed over observations. Higher is better; compare with its SE. (elpd): how well each is expected to predict new data. Higher is better.

08

Check: comparing fits

Checkpoint

Check yourself

Fit A has elpd −1,204.3 and fit B −1,206.1. The difference is −1.8 with a standard error of 2.4. What does the comparison say?

Show the answer

Answer: b

The difference is smaller than its standard error, and well under the rule of thumb of about 4. The data don't separate the fits, so prefer the simpler or more interpretable one, or average them with stacking weights.

09

Try it

In the app

In Studios, open Bayesian models and start a model. On Data, try chick weights: 50 chicks on four diets, weighed over three weeks. On Model, use weight as the outcome, Time and Diet as predictors, Chick as the grouping factor and Time as a varying slope. Run Check priors, then fit. Read Diagnose and Check before the estimates, then make a new fit without the group effect and compare the two.

10

Recap

Recap
  • Check the priors before the data, with a prior predictive check.
  • Read convergence first: R-hat at most 1.011.01, enough effective draws, no divergences.
  • Convergence isn't fit: check that simulated data look like the real data.
  • Compare fits by elpd, and treat a difference smaller than about twice its standard error as no real difference.

Concepts in this lesson