R Tutorial: The posterior model

Build a Bayesian posterior model in R using RJags. Start with a Beta prior centered at 45% and combine it with poll data from 10 voters where 6 support the candidate. The tutorial covers defining the model, compiling it with jags.model, and drawing 10,000 MCMC samples to visualize the posterior distribution.
Chapters
You now have the pieces in place to construct a posterior model of P your underlying support in the upcoming election. First, the Beta4555 prior model suggested that your support hovered around 45%. Subsequently, you polled N10 voters and recorded the number X that support you conditioned on your support P. The likelihood model of X is binomial. Upon observing x6, the corresponding likelihood function indicates that values of Support P near 0.6 are the most compatible with your pole. The prior and likelihood scaled here for comparison don't completely agree, yet both are valuable to Bayesian analysis. The prior contributes knowledge that you built prior to the most recent poll. The likelihood provides insight into the values of P that are most compatible with the current polling data. The posterior model combines the insights from the prior and likelihood. Here, the posterior reflects increased optimism about your election chances in light of the small but optimistic polling data. In the previous course, you learned that the exact specification of the posterior can be obtained through Bayes rule. Specifically, the posterior is proportional to the product of the likelihood and priority. However, in more sophisticated model settings, tidy closed form solutions to this formula might not exist. Thus, in this course we'll focus on approximating posterior models using RJags. RJags combines the power of R with the just another Gibbs sampler or JAGS engine. To get started, first download the JAGS program outside R. Then, within R, install the most recent version of the RJAGS package. There are three essential steps to all RJAGs define, compile, and simulate. To begin, we define the Bayesian model by a model string and store this as votemodel. The two lines of code within the curly brackets define the two important pieces of your model. The DBIN function specifies that the likelihood structure or the dependence of X on P is modeled by the binomial NP distribution. Similarly, the d beta function specifies a beta AB prior model for P. If you're familiar with the d binomial function in base R, you might think that there's a typo. In the DBIN call, the order of n and p are reversed. This isn't a typo. It's important to keep in mind that probability functions work differently in RJags than they do in base R. Next, we compile the model using the JAGS model function. Very loosely speaking, the goal here is to send information out to the JAGS program, which will then design an algorithm to sample from the posterior. In the first argument, we provide a text connection to the defined votemodel string. In the data argument, we supply the values of the A and b prior shape parameters, as well as the observed values of polling data x and n. The INITS argument ensures the reproducibility of our simulation results. We'll elaborate on this in chapter 2. Finally, we simulate the posterior using codasamples. To draw 10,000 approximate samples from the posterior codasamples takes three arguments. Model your compiled vote jags model variable names here your parameter of interest, p and niter your desired sample size or number of iterations. The results stored in votesim are an MCMC list object. We can take a quick peek at the distribution of the resulting 10,000 coda samples using the plot function. Importantly, this approximates the posterior model of your election support p. It's your turn to define, compile, and simulate. In the remaining Chapter 1 exercises, you'll play around with RJags while exploring the impact that different priors and different data can have on the posterior election model.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







