This chapter has been an informal introduction to Markov chain Monte Carlo (MCMC) estimation. The goal has been to introduce the purpose and approach MCMC algorithms. The major algorithms introduced were the Metropolis, Gibbs sampling, and Hamiltonian Monte Carlo algorithms. Each has its advantages and disadvantages. The ulam function in the rethinking package was introduced. It uses the Stan (mc-stan.org) Hamiltonian Monte Carlo engine to fit models as they are defined in this book. General advice about diagnosing poor MCMC fits was introduced by the use of a couple of pathological examples.
Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them.
Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.
9-1. Re-estimate the terrain ruggedness model from the chapter, but now using a uniform prior for the standard deviation, sigma. The uniform prior should be dunif(0,1). Visualize the priors. Use ulam to estimate the posterior. Visualize the posteriors for both models. Does the different prior have any detectible influence on the posterior distribution of sigma? Why or why not?
library(rethinking)
data(rugged)
data= rugged
data$log_gdp = log(data$rgdppc_2000)
dd = data[ complete.cases(data$rgdppc_2000) , ]
dd$log_gdp_std = dd$log_gdp/ mean(dd$log_gdp)
dd$rugged_std = dd$rugged/max(dd$rugged)
dd$cid=ifelse(dd$cont_africa==1,1,2)
dat_slim = list(
log_gdp_std = dd$log_gdp_std,
rugged_std = dd$rugged_std,
cid = as.integer( dd$cid )
)
model1 = ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , chains=4, cores = 4)
pairs(model1)
model2 = ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dunif(0, 1 )
), data=dat_slim , chains=4, cores = 4 )
pairs(model2)
# Does not have detectible influence on the posterior distribution of sigma
9-2. Modify the terrain ruggedness model again. This time, change the prior for b[cid] to dexp(0.3). What does this do to the posterior distribution? Can you explain it?
model3 = ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dexp(0.3) ,
sigma ~ dexp( 1 )
), data=dat_slim , chains=4, cores = 4 )
pairs(model3)
# No differences in the posterior distribution.
9-3. Re-estimate one of the Stan models from the chapter, but at different numbers of warmup iterations. Be sure to use the same number of sampling iterations in each case. Compare the n_eff values. How much warmup is enough?
Stan_model_100 = map2stan(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , warmup = 100, iter = 4000, chains=4, cores = 4 )
precis(Stan_model_100,depth=2)
## mean sd 5.5% 94.5% n_eff Rhat4
## a[1] 0.8864360 0.016009758 0.86042661 0.91189060 17689.904 0.9999447
## a[2] 1.0505370 0.010233922 1.03406911 1.06691688 18361.395 0.9998459
## b[1] 0.1322801 0.074209753 0.01313206 0.25203836 6925.833 1.0004600
## b[2] -0.1428735 0.055883402 -0.23196063 -0.05268639 8670.730 1.0005885
## sigma 0.1115831 0.006168167 0.10216618 0.12182635 9331.127 0.9999996
Stan_model_200 = map2stan(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , warmup = 200, iter = 4000, chains=4, cores = 4 )
Stan_model_300 = map2stan(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , warmup = 300, iter = 4000, chains=4, cores = 4 )
Stan_model_400 = map2stan(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , warmup = 400, iter = 4000, chains=4, cores = 4 )
Stan_model_500 = map2stan(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
), data=dat_slim , warmup = 500, iter = 4000, chains=4, cores = 4 )
#As warmup is increased,n_eff got closer to number of iterations. 400 warmup iterations are enough
9-4. Run the model below and then inspect the posterior distribution and explain what it is accomplishing. Compare the samples for the parameters a and b. Can you explain the different trace plots? If you are unfamiliar with the Cauchy distribution, you should look it up. The key feature to attend to is that it has no expected value. Can you connect this fact to the trace plot?
mp = ulam(
alist(
a ~ dnorm(0,1),
b ~ dcauchy(0,1)
), data=list(y=1) , chains=1 )
##
## SAMPLING FOR MODEL 'bcf56ee89f6cf2a4224a4139ff01c7d4' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 0 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 1000 [ 0%] (Warmup)
## Chain 1: Iteration: 100 / 1000 [ 10%] (Warmup)
## Chain 1: Iteration: 200 / 1000 [ 20%] (Warmup)
## Chain 1: Iteration: 300 / 1000 [ 30%] (Warmup)
## Chain 1: Iteration: 400 / 1000 [ 40%] (Warmup)
## Chain 1: Iteration: 500 / 1000 [ 50%] (Warmup)
## Chain 1: Iteration: 501 / 1000 [ 50%] (Sampling)
## Chain 1: Iteration: 600 / 1000 [ 60%] (Sampling)
## Chain 1: Iteration: 700 / 1000 [ 70%] (Sampling)
## Chain 1: Iteration: 800 / 1000 [ 80%] (Sampling)
## Chain 1: Iteration: 900 / 1000 [ 90%] (Sampling)
## Chain 1: Iteration: 1000 / 1000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.028 seconds (Warm-up)
## Chain 1: 0.039 seconds (Sampling)
## Chain 1: 0.067 seconds (Total)
## Chain 1:
p=precis(mp)
traceplot(mp)
## [1] 1000
## [1] 1
## [1] 1000
#From the plot we can see plot a should be a normal distribution as the prior is aroung 0 and spread in between 2 and -2. Plot b is Cauchy distribution which contains some extreme value go up to over 30 and -50.
9-5. Recall the divorce rate example from Chapter 5. Repeat that analysis, using ulam this time, fitting models m5.1, m5.2, and m5.3. Use compare to compare the models on the basis of WAIC or PSIS. To use WAIC or PSIS with ulam, you need add the argument log_log=TRUE. Explain the model comparison results.
library(tidybayes)
data(WaffleDivorce)
data = WaffleDivorce
data$Divorce_sd=standardize(data$Divorce)
data$Marriage_sd=standardize(data$Marriage)
data$MedianAgeMarriage_sd=standardize(data$MedianAgeMarriage)
d_trim = list(D = data$Divorce_sd, M = data$Marriage_sd, A = data$MedianAgeMarriage_sd)
m5.1 = 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
)
m5.2 = ulam(
alist(
D ~ dnorm(mu, sigma),
mu <- a + bM * M,
a ~ dnorm(0, 0.2),
bM ~ dnorm(0, 0.5),
sigma ~ dexp(1)
),
data = d_trim,
chains = 4,
cores = 4,
log_lik = TRUE
)
m5.3 = 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
)
set.seed(77)
compare( m5.1 , m5.2 , m5.3 , func=WAIC )
## WAIC SE dWAIC dSE pWAIC weight
## m5.1 125.5562 12.524005 0.000000 NA 3.507706 0.6862041442
## m5.3 127.1258 12.529783 1.569606 0.7473222 4.484837 0.3130529326
## m5.2 139.2129 9.834348 13.656676 9.0943745 2.926459 0.0007429232
# Model m5.1 with only median age at marriage as a predictor performs best, but is not really distinguishable from model m5.3. However, the model with marriage rate only, m5.2 clearly performs worse than both.