E2. Gibbs sampling is considered more efficient than the Metropolis algorithm because it updates each parameter one at a time, based on its conditional distribution. This helps it to explore the parameter space faster, as each update depends on the current values of other parameters. However, a downside is that it only works well if we know the conditional distributions for each parameter, which isn’t always the case in more complex models. Additionally, if there’s high correlation between parameters, Gibbs sampling can become less effective.
E3 Hamiltonian Monte Carlo can’t handle discrete parameters well because it relies on gradients to explore the parameter space. For HMC to work properly, it needs a continuous and differentiable energy function.
E4. The effective number of samples (neff) tells us how many independent samples we’d need to get the same information as our correlated MCMC samples. Because of autocorrelation (where samples aren’t fully independent), the effective sample size is often lower than the actual number of samples collected. So, if our neffis much smaller than our actual sample size, it means there’s a lot of autocorrelation, and we might need more samples to get a reliable result.
E5. For a correctly sampling Markov chain, Rhat should approach 1. This indicates that the chains have converged and are sampling the target distribution well, with minimal variance between chains.
E6. A good trace plot for a Markov chain looks like a fuzzy caterpillar—it should show consistent, random-looking movements around a horizontal band, indicating the chain is exploring the posterior distribution fully and evenly. A plot like this shows good mixing and suggests the chain has converged.
E7. I don’t think there is an E7
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(tibble)library(ggplot2)library(ggdag)
Attaching package: 'ggdag'
The following object is masked from 'package:stats':
filter
library(rethinking)
Loading required package: cmdstanr
This is cmdstanr version 0.8.1
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
The following objects are masked from 'package:stats':
mad, sd, var
The following objects are masked from 'package:base':
%in%, match
Loading required package: parallel
rethinking (Version 2.42)
Attaching package: 'rethinking'
The following object is masked from 'package:stats':
rstudent
library(tidyr)library(stringr)library(rstan)
Loading required package: StanHeaders
rstan version 2.32.6 (Stan version 2.32.2)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions,
change `threads_per_chain` option:
rstan_options(threads_per_chain = 1)
Attaching package: 'rstan'
The following object is masked from 'package:tidyr':
extract
The following objects are masked from 'package:rethinking':
stan, traceplot
The following objects are masked from 'package:posterior':
ess_bulk, ess_tail
library(tidybayes)
Warning: package 'tidybayes' was built under R version 4.4.1
data(WaffleDivorce)d <- WaffleDivorced$D <-standardize(d$Divorce)d$M <-standardize(d$Marriage)d$A <-standardize(d$MedianAgeMarriage)d_trim <-list(D = d$D, M = d$M, A = d$A)m5.1_stan <-ulam(alist( D ~dnorm(mu, sigma), mu <- a + bA * A, a ~dnorm(0, 0.2), bA ~dnorm(0, 0.5), sigma ~dexp(1) ),data = d_trim,chains =4, cores =4,log_lik =TRUE)
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/3f/x462l48n5b53prbc0t037zkm0000gn/T/RtmpMTffBA/model-669d195db590.stan', line 19, column 4 to column 29)
Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 2 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/3f/x462l48n5b53prbc0t037zkm0000gn/T/RtmpMTffBA/model-669d195db590.stan', line 19, column 4 to column 29)
Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 3 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/3f/x462l48n5b53prbc0t037zkm0000gn/T/RtmpMTffBA/model-669d195db590.stan', line 19, column 4 to column 29)
Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
m5.3_stan <-ulam(alist( D ~dnorm(mu, sigma), mu <- a + bA * A + bM * M, a ~dnorm(0, 0.2), bA ~dnorm(0, 0.5), bM ~dnorm(0, 0.5), sigma ~dexp(1) ),data = d_trim,chains =4, cores =4,log_lik =TRUE)
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: normal_lpdf: Scale parameter is 0, but must be positive! (in '/var/folders/3f/x462l48n5b53prbc0t037zkm0000gn/T/RtmpMTffBA/model-669d43c97671.stan', line 21, column 4 to column 29)
Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
data(cherry_blossoms)data <- cherry_blossoms[!is.na(cherry_blossoms$doy) &!is.na(cherry_blossoms$temp), ]m_cherry <-ulam(alist( doy ~dnorm(mu, sigma), mu <- a + b * temp, a ~dnorm(100, 10), b ~dnorm(0, 10), sigma ~dexp(1) ), data = data, log_lik =TRUE)
Some Pareto k values are high (>0.5). Set pointwise=TRUE to inspect individual points.
Some Pareto k values are high (>0.5). Set pointwise=TRUE to inspect individual points.
Some Pareto k values are high (>0.5). Set pointwise=TRUE to inspect individual points.