The posterior model
1. The posterior model
You now have the pieces in place to construct a posterior model of p, your underlying support in the upcoming election.2. Bayesian election model
First, the Beta(45,55) prior model suggested that your support hovered around 45%.3. Bayesian election model
Subsequently, you polled n = 10 voters and recorded the number X that support you. Conditioned on your support p, the likelihood model of X is Binomial. Upon observing X = 6, the corresponding likelihood function indicates that values of support p near 0.6 are the most compatible with your poll.4. Bayesian election model
The prior and likelihood, scaled here for comparison, don't completely agree. Yet both are valuable to a 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.5. Posterior model of p
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 prior. 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.6. Getting started with 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.7. Bayesian models in RJAGS: DEFINE
There are three essential steps to all RJAGS analyses: define, compile, and simulate. To begin, we *define* the Bayesian model by a model string and store this as `vote_model`. 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(n,p) distribution. Similarly, the `dbeta()` function specifies a Beta(a,b) prior model for p. If you’re familiar with the `dbinom()` 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.8. Bayesian models in RJAGS: COMPILE
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 `textConnection()` to the defined `vote_model` 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.9. Bayesian models in RJAGS: SIMULATE
Finally, we *simulate* the posterior, using `coda.samples()` to draw 10,000 approximate samples from the posterior. `coda.samples()` takes three arguments: `model` (your compiled `vote_jags` model), `variable.names` (here your parameter of interest p), and `n.iter` (your desired sample size or number of iterations). The results, stored in `vote_sim`, are an mcmc.list object.10. Bayesian models in RJAGS: SIMULATE
We can take a quick peak 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!11. Let's practice!
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.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.