Stats 102C - Lecture 23 - JAGS Models
Overview
JAGS, or Just Another Gibbs Sampler, automates posterior sampling from hierarchical models specified in the BUGS language, avoiding the need to derive every conditional distribution by hand. The lecture shows how to connect JAGS to R with rjags and apply it to baseball batting averages, coffee-filling machines, and document clustering, including convergence checks and label switching.
Key takeaways
- JAGS replaces manual derivation of Gibbs conditional distributions with BUGS declarations that describe stochastic and deterministic relationships.
- BUGS normal distributions require precision rather than variance, so a variance of 16 must be entered as precision 1/16.
- In the baseball hierarchy, players with more at-bats have more data-driven, tighter batting-average posteriors, while players with sparse data are pulled more toward the shared beta prior.
- Four chains of 3,500 iterations with the first 1,000 discarded provide the lecture's example workflow for posterior summaries and trace-plot inspection.
- In the coffee example, the machine observed through eight bags has lower posterior uncertainty than the machine observed through two bags.
- Mixture-model cluster labels are interchangeable across chains, so raw averages of cluster numbers are not meaningful without correcting for label switching.
Chapters
- JAGS automates Gibbs sampling, which is useful when deriving conditional distributions manually becomes difficult.
- Homework 5 asks students to perform a simpler Gibbs sampler manually in R.
- JAGS is a standalone C++ program; its name means Just Another Gibbs Sampler.
- JAGS is available for Windows and Mac through SourceForge, and macOS users can also install it with Homebrew using `brew install jags`.
- The lecture cautions users to choose the JAGS download links and avoid SourceForge advertisements.
- R communicates with the JAGS engine through the `rjags` package, installed with `install.packages("rjags")`.
- BUGS model code can be stored in a `.bugs` text file or as text inside an R function, but it is not executable R code.
- The tilde `~` declares a stochastic relationship, while `<-` defines a deterministic quantity.
- BUGS uses precision, equal to 1 divided by variance, for normal distributions; its binomial parameters are ordered probability then trial count.
- The lecture also reviews BUGS distributions including Bernoulli, beta, uniform, gamma, categorical, Dirichlet, and multinomial.
- The example models batting averages for 10 baseball players from their observed hits and at-bats.
- Each player has a separate batting probability theta, drawn from a shared beta distribution with shape parameters alpha and beta.
- A hyperparameter Omega has a Beta(3, 7) prior, while concentration K is fixed at 300.
- The model converts Omega and K into beta shapes using alpha = 1 + Omega(K - 2) and beta = 1 + (1 - Omega)(K - 2).
- The R data list contains observed hits H, at-bats N, the player count S = 10, and fixed K = 300.
- Names in the data list must match the variable names used in the BUGS model.
- The model declares Omega as random, alpha and beta as deterministic functions of Omega and K, and each player's theta as beta-distributed.
- Each observed hit count is modeled as Binomial(theta for that player, that player's at-bats).
- The demonstration runs four chains for 3,500 iterations each and discards the first 1,000 iterations as burn-in.
- Trace plots show sampled values for Omega and each player's theta; posterior summaries include means, standard deviations, and equal-tail 95% credible intervals.
- The player with 250 at-bats has a tighter posterior than the player with 10 at-bats, whose estimate is pulled more toward the shared prior.
- R-hat compares between-chain and within-chain variation; the lecture gives below 1.01 as a target and notes values above 1.05 can indicate inadequate mixing.
- The coffee example models 18 bag-weight offsets from four machines, with each machine having its own mean offset.
- Machine means are centered on a shared production-line offset mu, and individual bags are centered on their machine's mean.
- The model uses known standard deviations of 4 for the line prior, 2 for machine offsets, and 4 for bag measurements.
- Because BUGS normal distributions use precision, the corresponding variances 16 and 4 are entered as precisions 1/16 and 1/4.
- The R data list includes the 18 bag offsets, the number of machines, and a machine-index vector identifying each bag's source.
- The BUGS model samples mu, samples four machine offsets theta around mu, and models each bag with the theta for its machine.
- The posterior mean for the line offset is about 4.5; machine 1's estimate is based on two bags, while machine 2's is based on eight.
- Machine 1 has greater posterior uncertainty than machine 2, with reported standard deviations of about 1.8 and 1.23.
- The document-clustering example uses three children's books split into 29 passages and represented with counts for 93 words.
- A Dirichlet prior models the mixing proportions across three clusters, and Dirichlet distributions model each cluster's word probabilities.
- Each passage receives a latent cluster assignment, which connects its word-count vector to the cluster-specific word distribution.
- Unsupervised mixture-model clusters have no fixed names, so different chains can assign the same book to different cluster numbers.
- One chain may label Green Eggs and Ham as cluster 1, while another labels it as cluster 3 despite recovering the same groups.
- Averaging cluster-number assignments across chains can therefore produce misleading results; cluster membership must be compared in a way that accounts for label switching.
Transcript excerpt
0:00 this. And I believe my screen is shared. Okay. All right. Well, yeah, happy Wednesday, happy 8:00 a.m., happy day before Thanksgiving. And uh I hope you guys are all doing well. Thanks for joining me. Um so today we will um take a look at uh using JAGS. JAGS is a software program that allows you to perform Gibbs sampling. So oh, I just posted uh homework five. And in that assignment um uh part of your assignment is to do some Gibbs sampling. And you'll you'll do it kind of manually. I
0:46 only have an R version for homework five. I apologize, but um you'll only um but you'll do some uh Gibbs sampling in a in you in a bit of a manual fashion. And you'll see that it there's a you know getting the conditional distribution can be sometimes a challenge to figure out. Uh you know sometimes it's easy. And the example um uh assigned in the homework is on the relatively easier side. But um um I guess easier, not necessarily easy easy. But um in [clears throat] in a lot of other
1:32 cases figuring out the conditional distribution can be uh quite challenging. And so JAGS um and there's other software solutions that try to make that part um easier for you. So um this today's lecture covers uh using that software. And um and we'll do that. So um so JAGS is actually a bit of a a standalone program. It's written in uh C++ and it's based off of uh an older um kind of an older system called bugs. So I don't know maybe
2:21 25 years ago or so um Gibbs sampling was performed on the computer using a um software called uh bugs uh which stood for I think Bayesian
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.