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)
This is a part of the course
“Bayesian Modeling with RJAGS”
Exercise instructions
Apply
plot()
tosleep_sim
withdensity = FALSE
to construct trace plots for the \(m\) and \(s\) chains. NOTE: The 10,000 recordedIterations
start after a "burn-in" period in which samples are discarded. Thus theIterations
count doesn't start at 1!Apply
ggplot()
with ageom_line()
layer tosleep_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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use plot() to construct trace plots of the m and s chains
# Use ggplot() to construct a trace plot of the m chain
ggplot(___, aes(x = ___, y = ___)) +
geom_line()
# Trace plot the first 100 iterations of the m chain
ggplot(___, aes(x = ___, y = ___)) +
geom_line()