Session Ready
Exercise

Markov chain trace plots

A trace plot provides a visualization of a Markov chain's longitudinal behavior. Specifically, a trace plot for the \(m\) chain plots the observed chain value (y-axis) against the corresponding iteration number (x-axis).

You will construct trace plots of the \(m\) chain using two different approaches: by applying the built-in plot() function to the mcmc.list object sleep_sim and, for finer control over this graphic (and finer control over analyses in later chapters), by applying ggplot() to the data.frame object sleep_chains. Both sleep_sim and sleep_chains are in your workspace:

sleep_sim <- coda.samples(model = sleep_jags, variable.names = c("m", "s"), n.iter = 10000)
sleep_chains <- data.frame(sleep_sim[[1]], iter = 1:10000)
Instructions
100 XP
  • Apply plot() to sleep_sim with density = FALSE to construct trace plots for the \(m\) and \(s\) chains. NOTE: The 10,000 recorded Iterations start after a "burn-in" period in which samples are discarded. Thus the Iterations count doesn't start at 1!

  • Apply ggplot() with a geom_line() layer to sleep_chains to re-construct the trace plot of the \(m\) chain.

  • Zoom in: construct a ggplot() trace plot of the first 100 iterations of the \(m\) chain.