Skip to content

Commit 76cd7ba

Browse files
committed
Minor cleaning in vignettes
[ci skip]
1 parent e308115 commit 76cd7ba

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

vignettes/plotting-mcmc-draws.Rmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ The `mcmc_hex` function creates a similar plot but using hexagonal binning, whic
147147
mcmc_hex(posterior, pars = c("(Intercept)", "wt"))
148148
```
149149

150-
In addition to `mcmc_scatter` and `mcmc_hex`, as of __bayesplot__ version 1.2.0
151-
an `mcmc_pairs` function for creating pairs plots with more than two parameters
152-
is available. Examples will eventually be included in this vignette. For now
153-
see `help("mcmc_pairs")`.
150+
<br>
151+
In addition to `mcmc_scatter` and `mcmc_hex`, __bayesplot__ now provides
152+
an `mcmc_pairs` function for creating pairs plots with more than two parameters.
153+
See the examples at `help("mcmc_pairs")`.
154154

155155
### Traceplots
156156

vignettes/visual-mcmc-diagnostics.Rmd

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ model {
132132
y ~ normal(theta, sigma);
133133
}
134134
```
135+
135136
The centered and non-centered are two parameterizations of the same statistical
136137
model, but they have very different practical implications for MCMC. Using the
137138
__bayesplot__ diagnostic plots, we'll see that, for this data, the NCP is
@@ -157,9 +158,9 @@ posterior_cp <- as.array(fit_cp)
157158
posterior_ncp <- as.array(fit_ncp)
158159
```
159160

160-
For now ignore any warnings about divergent transitions after warmup. We will
161-
come back to those later in the vignette in the [Diagnostics for the No-U-Turn
162-
Sampler](#diagnostics-for-the no-u-turn-sampler) section.
161+
For now ignore any warnings issued by the sampler. We will come back to them
162+
later in the [Diagnostics for the No-U-Turn Sampler](#diagnostics-for-the
163+
no-u-turn-sampler) section.
163164

164165

165166
### Rhat: potential scale reduction statistic
@@ -181,23 +182,24 @@ First we'll quickly fit one of the models above again, this time intentionally
181182
using too few MCMC iterations. This should lead to some high $\hat{R}$ values.
182183

183184
```{r, results='hide'}
184-
fit_cp_50iter <- sampling(schools_mod_cp, data = schools_dat, chains = 2, iter = 50)
185+
fit_cp_100iter <- sampling(schools_mod_cp, data = schools_dat,
186+
chains = 2, iter = 100)
185187
```
186188

187-
**bayesplot** also provides a generic `rhat` extractor function,
188-
currently with methods defined for models fit using the **rstan** and
189-
**rstanarm** packages. But regardless of how you fit your model, all
190-
**bayesplot** needs is a vector of $\hat{R}$ values.
189+
**bayesplot** provides a generic `rhat` extractor function, currently with
190+
methods defined for models fit using the **rstan** and **rstanarm** packages.
191+
But regardless of how you fit your model, all **bayesplot** needs is a vector of
192+
$\hat{R}$ values.
191193

192194
```{r print-rhats}
193195
library("bayesplot")
194-
rhats <- rhat(fit_cp_50iter)
196+
rhats <- rhat(fit_cp_100iter)
195197
print(rhats)
196198
```
197199

198200
We can visualize the $\hat{R}$ values with the `mcmc_rhat` function:
199201

200-
```{r mcmc_rhat}
202+
```{r mcmc_rhat-1}
201203
color_scheme_set("brightblue") # see help("color_scheme_set")
202204
mcmc_rhat(rhats)
203205
```
@@ -206,21 +208,21 @@ In the plot, the points representing the $\hat{R}$ values are colored based on
206208
whether they are less than $1.05$, between $1.05$ and $1.1$, or greater than
207209
$1.1$.
208210

209-
We can see the names of the parameters with the concerning $\hat{R}$ values by
210-
turning on the $y$-axis text using the `yaxis_text` convenience function:
211+
The axis $y$-axis text is off by default for this plot because it's only
212+
possible to see the labels clearly for models with very few parameters. We can
213+
see the names of the parameters with the concerning $\hat{R}$ values using the
214+
`yaxis_text` convenience function (which passes arguments like `hjust` to
215+
`ggplot2::element_text`):
211216

212217
```{r, mcmc_rhat-2}
213-
mcmc_rhat(rhats) + yaxis_text()
218+
mcmc_rhat(rhats) + yaxis_text(hjust = 1)
214219
```
215220

216-
The axis $y$-axis text is off by default for this plot because it's only
217-
possible to see the labels clearly for models with very few parameters.
218-
219221
If we look at the same model fit using longer Markov chains we should see all
220222
$\hat{R} < 1.1$, and all points in the plot the same (light) color:
221223

222-
```{r, results='hide'}
223-
mcmc_rhat(rhat = rhat(fit_cp)) + yaxis_text()
224+
```{r, mcmc_rhat-3}
225+
mcmc_rhat(rhat = rhat(fit_cp)) + yaxis_text(hjust = 0)
224226
```
225227

226228
We can see the same information shown by `mcmc_rhat` but in histogram form using
@@ -239,12 +241,13 @@ larger the ratio of $n_{eff}$ to $N$ the better.
239241
The **bayesplot** package provides a generic `neff_ratio` extractor function,
240242
currently with methods defined for models fit using the **rstan** and
241243
**rstanarm** packages. But regardless of how you fit your model, all
242-
**bayesplot** needs is a vector of $n_{eff}/N$ values. The `mcmc_neff` and `mcmc_neff_hist` can then be used to plot the ratios.
244+
**bayesplot** needs is a vector of $n_{eff}/N$ values. The `mcmc_neff` and
245+
`mcmc_neff_hist` can then be used to plot the ratios.
243246

244247
```{r print-neff-ratios}
245248
ratios_cp <- neff_ratio(fit_cp)
246249
print(ratios_cp)
247-
mcmc_neff(ratios_cp)
250+
mcmc_neff(ratios_cp, size = 2)
248251
```
249252

250253
In the plot, the points representing the values of $n_{eff}/N$ are colored based
@@ -261,13 +264,14 @@ much lower autocorrelations compared to draws obtained using other MCMC
261264
algorithms (e.g., Gibbs).
262265

263266
Even for models fit using **rstan** the parameterization can make a big
264-
difference. Here are the $n_{eff}/N$ plots for `fit_cp` and `fit_ncp` side by side.
267+
difference. Here are the $n_{eff}/N$ plots for `fit_cp` and `fit_ncp`
268+
side by side.
265269

266-
```{r mcmc_neff-compare, fig.width=7}
270+
```{r mcmc_neff-compare}
267271
# A function we'll use several times to plot comparisons of the centered
268272
# parameterization (cp) and the non-centered parameterization (ncp). See
269273
# help("bayesplot_grid") for details on the bayesplot_grid function used here.
270-
compare_cp_ncp <- function(cp_plot, ncp_plot, ncol = 1) {
274+
compare_cp_ncp <- function(cp_plot, ncp_plot, ncol = 2) {
271275
bayesplot_grid(
272276
cp_plot, ncp_plot,
273277
grid_args = list(ncol = ncol),
@@ -278,7 +282,7 @@ compare_cp_ncp <- function(cp_plot, ncp_plot, ncol = 1) {
278282
279283
neff_cp <- neff_ratio(fit_cp, pars = c("theta", "mu", "tau"))
280284
neff_ncp <- neff_ratio(fit_ncp, pars = c("theta", "mu", "tau"))
281-
compare_cp_ncp(mcmc_neff(neff_cp), mcmc_neff(neff_ncp))
285+
compare_cp_ncp(mcmc_neff(neff_cp), mcmc_neff(neff_ncp), ncol = 1)
282286
```
283287

284288
Because of the difference in parameterization, the effective sample sizes are
@@ -295,14 +299,13 @@ user-specified number of lags.
295299
Here we can again see a difference when comparing the two parameterizations of
296300
the same model. For model 1, $\theta_1$ is the primitive parameter for school 1,
297301
whereas for the non-centered parameterization in model 2 the primitive parameter
298-
is $\eta_1$ (and $\theta_1$ is later constructed from $\eta_1$, $\mu$, and
299-
$\tau$):
302+
is $\eta_1$ (and $\theta_1$ is later constructed from $\eta_1$, $\mu$,
303+
and $\tau$):
300304

301305
```{r mcmc_acf}
302306
compare_cp_ncp(
303307
mcmc_acf(posterior_cp, pars = "theta[1]", lags = 10),
304-
mcmc_acf(posterior_ncp, pars = "eta[1]", lags = 10),
305-
ncol = 2
308+
mcmc_acf(posterior_ncp, pars = "eta[1]", lags = 10)
306309
)
307310
```
308311

@@ -358,7 +361,7 @@ there is a cluster of many red marks:
358361
```{r, mcmc_trace}
359362
color_scheme_set("mix-brightblue-gray")
360363
mcmc_trace(posterior_cp, pars = "tau", divergences = np_cp) +
361-
xlab("Post-warmup Iteration")
364+
xlab("Post-warmup iteration")
362365
```
363366

364367
To look deeper at the information conveyed by the divergences we can use the
@@ -436,8 +439,7 @@ case for the non-centered parameterization (right):
436439
```{r, mcmc_nuts_energy-3, message=FALSE, fig.width=8}
437440
compare_cp_ncp(
438441
mcmc_nuts_energy(np_cp, binwidth = 1/2),
439-
mcmc_nuts_energy(np_ncp, binwidth = 1/2),
440-
ncol = 2
442+
mcmc_nuts_energy(np_ncp, binwidth = 1/2)
441443
)
442444
```
443445

@@ -448,10 +450,10 @@ posterior:
448450
```{r, mcmc_nuts_energy-4, message=FALSE, fig.width=8}
449451
np_cp_2 <- nuts_params(fit_cp_2)
450452
np_ncp_2 <- nuts_params(fit_ncp_2)
453+
451454
compare_cp_ncp(
452455
mcmc_nuts_energy(np_cp_2),
453-
mcmc_nuts_energy(np_ncp_2),
454-
ncol = 2
456+
mcmc_nuts_energy(np_ncp_2)
455457
)
456458
```
457459

0 commit comments

Comments
 (0)