9M1. 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). Use ulam to estimate the posterior. Does the different prior have any detectible influence on the posterior distribution of sigma? Why or why not?

## Loading required package: rstan
## Loading required package: StanHeaders
## Loading required package: ggplot2
## rstan (Version 2.21.5, GitRev: 2e1f913d3ca3)
## 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)
## Loading required package: cmdstanr
## This is cmdstanr version 0.5.2
## - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
## - CmdStan path: /Users/madelhol/.cmdstanr/cmdstan-2.29.0
## - CmdStan version: 2.29.0
## 
## A newer version of CmdStan is available. See ?install_cmdstan() to install it.
## To disable this check set option or environment variable CMDSTANR_NO_VER_CHECK=TRUE.
## Loading required package: parallel
## rethinking (Version 2.21)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:rstan':
## 
##     stan
## The following object is masked from 'package:stats':
## 
##     rstudent
## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a56750c9c76.stan', line 4, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Running MCMC with 1 chain, with 1 thread(s) per chain...
## 
## 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 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/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a56750c9c76.stan', line 19, column 4 to column 39)
## 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 1
## Chain 1 finished in 0.1 seconds.
## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a5643cfb73e.stan', line 4, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Running MCMC with 1 chain, with 1 thread(s) per chain...
## 
## 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 finished in 0.1 seconds.
traceplot_ulam(m9.1)

traceplot_ulam(m9.2)

precis(m9.1, depth = 2)
##             mean          sd        5.5%       94.5%    n_eff     Rhat4
## a[1]   0.8860352 0.016328605  0.85903426  0.91151489 710.6986 0.9990631
## a[2]   1.0507788 0.009661327  1.03478125  1.06601215 930.0635 0.9985768
## b[1]   0.1287750 0.071676467  0.01806468  0.24914644 671.6128 0.9982858
## b[2]  -0.1419857 0.055834968 -0.23316861 -0.04869843 742.3259 0.9991191
## sigma  0.1114886 0.006428216  0.10211241  0.12252722 552.9765 0.9986288
precis(m9.2, depth = 2)
##             mean          sd         5.5%       94.5%    n_eff     Rhat4
## a[1]   0.8865255 0.016104062  0.860391665  0.91140409 365.2622 1.0003437
## a[2]   1.0508731 0.009953067  1.036038900  1.06684410 815.0648 0.9981757
## b[1]   0.1322356 0.077717577  0.002079288  0.25630276 810.7136 0.9991914
## b[2]  -0.1401623 0.055504702 -0.223950375 -0.05191299 593.4548 0.9990221
## sigma  0.1120133 0.006291819  0.102631815  0.12181047 738.9081 1.0003618
coeftab(m9.1, m9.2)
##       m9.1    m9.2   
## a[1]     0.89    0.89
## a[2]     1.05    1.05
## b[1]     0.13    0.13
## b[2]    -0.14   -0.14
## sigma    0.11    0.11
## nobs      170     170

while the different priors have an impact on n_eff, they do not change the posterior distributions of sigma.

9M2. 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?

## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a561fdf8c87.stan', line 4, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Running MCMC with 1 chain, with 1 thread(s) per chain...
## 
## 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 finished in 0.1 seconds.
precis(m9.2, depth = 2)
##             mean          sd         5.5%       94.5%    n_eff     Rhat4
## a[1]   0.8865255 0.016104062  0.860391665  0.91140409 365.2622 1.0003437
## a[2]   1.0508731 0.009953067  1.036038900  1.06684410 815.0648 0.9981757
## b[1]   0.1322356 0.077717577  0.002079288  0.25630276 810.7136 0.9991914
## b[2]  -0.1401623 0.055504702 -0.223950375 -0.05191299 593.4548 0.9990221
## sigma  0.1120133 0.006291819  0.102631815  0.12181047 738.9081 1.0003618
precis(m9.3, depth = 2)
##             mean          sd        5.5%      94.5%    n_eff     Rhat4
## a[1]  0.88861481 0.017025193 0.861793905 0.91417735 435.7818 0.9990284
## a[2]  1.04817558 0.011172757 1.029798950 1.06666665 462.2152 1.0054173
## b[1]  0.13835327 0.079944822 0.017581990 0.27472966 181.6327 1.0043075
## b[2]  0.01984004 0.016808417 0.001399216 0.05157125 464.3916 1.0029735
## sigma 0.11489991 0.006288271 0.104611895 0.12492265 487.4023 0.9997316
coeftab(m9.2, m9.3)
##       m9.2    m9.3   
## a[1]     0.89    0.89
## a[2]     1.05    1.05
## b[1]     0.13    0.14
## b[2]    -0.14    0.02
## sigma    0.11    0.11
## nobs      170     170
traceplot(m9.3)

When using an exponential prior for b, it restricts the estimation to positive values.Therefore, model n9.3 has an estimated value for b[2] of 0.02, while m9.2 has an estimated value of -0.14.

9M3. 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?

We are not sure how to solve this one. We could not figure out how to change the number or warmup itrations, only the number of chains.

m9.1 <- 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=2 )
## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a56661bf5d8.stan', line 4, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Running MCMC with 2 sequential chains, with 1 thread(s) per chain...
## 
## 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 finished in 0.0 seconds.
## Chain 2 Iteration:   1 / 1000 [  0%]  (Warmup) 
## Chain 2 Iteration: 100 / 1000 [ 10%]  (Warmup) 
## Chain 2 Iteration: 200 / 1000 [ 20%]  (Warmup) 
## Chain 2 Iteration: 300 / 1000 [ 30%]  (Warmup) 
## Chain 2 Iteration: 400 / 1000 [ 40%]  (Warmup) 
## Chain 2 Iteration: 500 / 1000 [ 50%]  (Warmup) 
## Chain 2 Iteration: 501 / 1000 [ 50%]  (Sampling) 
## Chain 2 Iteration: 600 / 1000 [ 60%]  (Sampling) 
## Chain 2 Iteration: 700 / 1000 [ 70%]  (Sampling) 
## Chain 2 Iteration: 800 / 1000 [ 80%]  (Sampling) 
## Chain 2 Iteration: 900 / 1000 [ 90%]  (Sampling) 
## Chain 2 Iteration: 1000 / 1000 [100%]  (Sampling)
## 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/37/pz72_81s6b57rfxw4l1030pw0000gp/T/Rtmpx67kOM/model-1a56661bf5d8.stan', line 19, column 4 to column 39)
## 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 2
## Chain 2 finished in 0.0 seconds.
## 
## Both chains finished successfully.
## Mean chain execution time: 0.0 seconds.
## Total execution time: 0.3 seconds.