@@ -153,8 +153,8 @@ schools_mod_ncp <- stan_model("schools_mod_ncp.stan")
153
153
We then fit the model by calling Stan's MCMC algorithm using the ` sampling `
154
154
function (the increased ` adapt_delta ` param is to make the sampler a bit more "careful" and avoid false positive divergences),
155
155
``` {r, fit-models-hidden, results='hide', message=FALSE}
156
- fit_cp <- sampling(schools_mod_cp, data = schools_dat, seed = 803214054 , control = list(adapt_delta = 0.95 ))
157
- fit_ncp <- sampling(schools_mod_ncp, data = schools_dat, seed = 457721433, control = list(adapt_delta = 0.95 ))
156
+ fit_cp <- sampling(schools_mod_cp, data = schools_dat, seed = 803214053 , control = list(adapt_delta = 0.9 ))
157
+ fit_ncp <- sampling(schools_mod_ncp, data = schools_dat, seed = 457721433, control = list(adapt_delta = 0.9 ))
158
158
```
159
159
and extract a ` iterations x chains x parameters ` array of posterior draws with
160
160
` as.array ` ,
@@ -289,7 +289,8 @@ Let's look at how `tau` interacts with other variables, using only one of the
289
289
` theta ` s to keep the plot readable:
290
290
291
291
``` {r, mcmc_pairs}
292
- mcmc_pairs(posterior_cp, np = np_cp, pars = c("mu","tau","theta[1]"))
292
+ mcmc_pairs(posterior_cp, np = np_cp, pars = c("mu","tau","theta[1]"),
293
+ off_diag_args = list(size = 0.75))
293
294
```
294
295
295
296
Note that each bivariate plot is present twice -- by default each of those
@@ -325,7 +326,8 @@ scatter_theta_cp <- mcmc_scatter(
325
326
posterior_cp,
326
327
pars = c("theta[1]", "tau"),
327
328
transform = list(tau = "log"), # can abbrev. 'transformations'
328
- np = np_cp
329
+ np = np_cp,
330
+ size = 1
329
331
)
330
332
scatter_theta_cp
331
333
```
@@ -353,7 +355,8 @@ scatter_eta_ncp <- mcmc_scatter(
353
355
posterior_ncp,
354
356
pars = c("eta[1]", "tau"),
355
357
transform = list(tau = "log"),
356
- np = np_ncp
358
+ np = np_ncp,
359
+ size = 1
357
360
)
358
361
scatter_eta_ncp
359
362
```
@@ -391,10 +394,11 @@ scatter_theta_ncp <- mcmc_scatter(
391
394
posterior_ncp,
392
395
pars = c("theta[1]", "tau"),
393
396
transform = list(tau = "log"),
394
- np = np_ncp
397
+ np = np_ncp,
398
+ size = 1
395
399
)
396
400
397
- compare_cp_ncp(scatter_theta_cp, scatter_theta_ncp, ylim = c(-9 , 4))
401
+ compare_cp_ncp(scatter_theta_cp, scatter_theta_ncp, ylim = c(-8 , 4))
398
402
```
399
403
400
404
Once we transform the ` eta ` values into ` theta ` values we actually see an even
@@ -427,11 +431,11 @@ mcmc_trace(posterior_cp, pars = "tau", np = np_cp) +
427
431
The first thing to note is that all chains seem to be exploring the same region
428
432
of parameter values, which is a good sign. But the plot is too crowded to help
429
433
us diagnose divergences. We may however zoom in to investigate, using the
430
- ` window ` parameter :
434
+ ` window ` argument :
431
435
432
436
``` {r echo=FALSE}
433
437
#A check that the chosen window still relevant
434
- n_divergent_in_window <- np_cp %>% filter(Parameter == "divergent__" & Value == 1 & Iteration >= 350 & Iteration <= 500 ) %>% nrow()
438
+ n_divergent_in_window <- np_cp %>% filter(Parameter == "divergent__" & Value == 1 & Iteration >= 50 & Iteration <= 200 ) %>% nrow()
435
439
436
440
if(n_divergent_in_window < 6) {
437
441
divergences <- np_cp %>% filter(Parameter == "divergent__" & Value == 1) %>% select(Iteration) %>% get("Iteration", .) %>% sort() %>% paste(collapse = ",")
@@ -440,7 +444,7 @@ if(n_divergent_in_window < 6) {
440
444
```
441
445
442
446
``` {r, mcmc_trace_zoom}
443
- mcmc_trace(posterior_cp, pars = "tau", np = np_cp, window = c(400,550 )) +
447
+ mcmc_trace(posterior_cp, pars = "tau", np = np_cp, window = c(50,200 )) +
444
448
xlab("Post-warmup iteration")
445
449
```
446
450
@@ -672,7 +676,7 @@ sample size, $n_{eff}$, is usually smaller than the total sample size, $N$
672
676
(although it may be larger in some cases[ ^ 1 ] ). The larger the ratio of $n_ {eff}$ to
673
677
$N$ the better (see Gelman et al. 2013, Stan Development Team 2018 for more details) .
674
678
675
- [ ^ 1 ] : $n_ {eff} > N$` indicates that the mean estimate of the parameter computed from Stan
679
+ [ ^ 1 ] : $n_ {eff} > N$ indicates that the mean estimate of the parameter computed from Stan
676
680
draws approaches the true mean faster than the mean estimate computed from independent
677
681
samples from the true posterior
678
682
(the estimate from Stan has smaller variance). This is possible when the draws are
@@ -789,8 +793,8 @@ path lengths in Hamiltonian Monte Carlo. *Journal of Machine Learning Research*.
789
793
Rubin, D. B. (1981). Estimation in Parallel Randomized Experiments. * Journal of
790
794
Educational and Behavioral Statistics* . 6:377--401.
791
795
792
- Stan Development Team. (2018). * Stan Modeling Language Users
793
- Guide and Reference Manual * . https://mc-stan.org/users/documentation/
796
+ Stan Development Team. _ Stan Modeling Language Users
797
+ Guide and Reference Manual _ . https://mc-stan.org/users/documentation/
794
798
795
799
Stan Development Team. (2018). RStan: the R interface to Stan. R package version 2.17.3.
796
800
https://mc-stan.org/rstan
0 commit comments