Get Started

Posterior credible intervals

Let's focus on slope parameter \(b\), the rate of change in weight over height. The posterior mean of \(b\) reflects the trend in the posterior model of the slope. In contrast, a posterior credible interval provides a range of posterior plausible slope values, thus reflects posterior uncertainty about \(b\). For example, the 95% credible interval for \(b\) ranges from the 2.5th to the 97.5th quantile of the \(b\) posterior. Thus there's a 95% (posterior) chance that \(b\) is in this range.

You will use RJAGS simulation output to approximate credible intervals for \(b\). The 100,000 iteration RJAGS simulation of the posterior, weight_sim_big, is in your workspace along with a data frame of the Markov chain output, weight_chains.

This is a part of the course

“Bayesian Modeling with RJAGS”

View Course

Exercise instructions

  • Obtain summary() statistics of the weight_sim_big chains.
  • The 2.5% and 97.5% posterior quantiles for \(b\) are reported in Table 2 of the summary(). Apply quantile() to the raw weight_chains to verify these calculations. Save this as ci_95 and print it.
  • Similarly, use the weight_chains data to construct a 90% credible interval for \(b\). Save this as ci_90 and print it.
  • Construct a density plot of the \(b\) Markov chain values. Superimpose vertical lines representing the 90% credible interval for \(b\) using geom_vline() with xintercept = ci_90.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Summarize the posterior Markov chains


# Calculate the 95% posterior credible interval for b
ci_95 <- quantile(___, probs = c(___, ___))
ci_95

# Calculate the 90% posterior credible interval for b
ci_90 <- ___
ci_90

# Mark the 90% credible interval 
ggplot(___, aes(x = ___)) + 
    geom_density() + 
    geom_vline(xintercept = ___, color = "red")
Edit and Run Code

This exercise is part of the course

Bayesian Modeling with RJAGS

AdvancedSkill Level
4.7+
7 reviews

In this course, you'll learn how to implement more advanced Bayesian models using RJAGS.

In this chapter, you will extend the Normal-Normal model to a simple Bayesian regression model. Within this context, you will explore how to use rjags simulation output to conduct posterior inference. Specifically, you will construct posterior estimates of regression parameters using posterior means & credible intervals, you will test hypotheses using posterior probabilities, and you will construct posterior predictive distributions for new observations.

Exercise 1: A simple Bayesian regression modelExercise 2: Regression priorsExercise 3: Visualizing the regression priorsExercise 4: Weight & height dataExercise 5: Bayesian regression in RJAGSExercise 6: Define, compile, & simulate the regression modelExercise 7: Regression Markov chainsExercise 8: Posterior estimation & inferenceExercise 9: Posterior point estimatesExercise 10: Posterior credible intervals
Exercise 11: Posterior probabilitiesExercise 12: Posterior predictionExercise 13: Inference for the posterior trendExercise 14: Calculating posterior predictionsExercise 15: Posterior predictive distribution

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free