Skip to content

Commit e53b503

Browse files
committed
revert vignette formatting hacks
closes #803
1 parent 323c14d commit e53b503

2 files changed

Lines changed: 15 additions & 101 deletions

File tree

vignettes/cmdstanr.Rmd

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ first argument specifies the variables to summarize and any arguments
187187
after that are passed on to `posterior::summarise_draws()` to specify
188188
which summaries to compute, whether to use multiple cores, etc.
189189

190-
```{r summary, eval=FALSE}
190+
```{r summary}
191191
fit$summary()
192192
fit$summary(variables = c("theta", "lp__"), "mean", "sd")
193193
@@ -202,24 +202,6 @@ fit$summary(
202202
)
203203
```
204204

205-
```{r, echo=FALSE}
206-
# NOTE: the hack of using print.data.frame in chunks with echo=FALSE
207-
# is used because the pillar formatting of posterior draws_summary objects
208-
# isn't playing nicely with pkgdown::build_articles().
209-
options(digits = 2)
210-
print.data.frame(fit$summary())
211-
212-
print.data.frame(fit$summary(variables = c("theta", "lp__"), "mean", "sd"))
213-
214-
print.data.frame(fit$summary("theta", pr_lt_half = ~ mean(. <= 0.5)))
215-
216-
print.data.frame(fit$summary(
217-
variables = NULL,
218-
posterior::default_summary_measures(),
219-
extra_quantiles = ~posterior::quantile2(., probs = c(.0275, .975))
220-
))
221-
```
222-
223205
#### CmdStan's stansummary utility
224206

225207
CmdStan itself provides a `stansummary` utility that can be called using the
@@ -351,20 +333,11 @@ the `$sample()` method demonstrated above.
351333

352334
We can find the (penalized) maximum likelihood estimate (MLE) using [`$optimize()`](https://mc-stan.org/cmdstanr/reference/model-method-optimize.html).
353335

354-
```{r optimize, eval=FALSE}
336+
```{r optimize}
355337
fit_mle <- mod$optimize(data = data_list, seed = 123)
356338
fit_mle$summary() # includes lp__ (log prob calculated by Stan program)
357339
fit_mle$mle("theta")
358340
```
359-
```{r, echo=FALSE}
360-
# NOTE: the hack of using print.data.frame in chunks with echo=FALSE
361-
# is used because the pillar formatting of posterior draws_summary objects
362-
# isn't playing nicely with pkgdown::build_articles().
363-
options(digits = 2)
364-
fit_mle <- mod$optimize(data = data_list, seed = 123)
365-
print.data.frame(fit_mle$summary()) # includes lp__ (log prob calculated by Stan program)
366-
fit_mle$mle("theta")
367-
```
368341

369342
Here's a plot comparing the penalized MLE to the posterior distribution of
370343
`theta`.
@@ -380,18 +353,10 @@ We can run Stan's experimental variational Bayes algorithm (ADVI) using the
380353
[`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html)
381354
method.
382355

383-
```{r variational, eval=FALSE}
356+
```{r variational}
384357
fit_vb <- mod$variational(data = data_list, seed = 123, output_samples = 4000)
385358
fit_vb$summary("theta")
386359
```
387-
```{r, echo=FALSE}
388-
# NOTE: the hack of using print.data.frame in chunks with echo=FALSE
389-
# is used because the pillar formatting of posterior draws_summary objects
390-
# isn't playing nicely with pkgdown::build_articles().
391-
options(digits = 2)
392-
fit_vb <- mod$variational(data = data_list, seed = 123, output_samples = 4000)
393-
print.data.frame(fit_vb$summary("theta"))
394-
```
395360

396361
The `$draws()` method can be used to access the approximate posterior draws.
397362
Let's extract the draws, make the same plot we made after MCMC, and compare the

vignettes/posterior.Rmd

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,38 @@ vignette: >
1515
```{r child="children/_settings-knitr.Rmd"}
1616
```
1717

18-
<!--
19-
NOTE: the hack below of using print.data.frame in chunks with echo=FALSE
20-
is used because the pillar formatting of posterior draws_summary objects
21-
isn't playing nicely with pkgdown::build_articles(). When that is fixed
22-
using options(digits=2) also won't be necessary anymore.
23-
-->
24-
```{r, include=FALSE}
25-
options(digits=2)
26-
```
27-
2818
## Summary statistics
2919

30-
We can easily customise the summary statistics reported by `$summary()` and `$print()`.
20+
We can easily customize the summary statistics reported by `$summary()` and `$print()`.
3121

32-
```{r eval=FALSE}
22+
```{r}
3323
fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
3424
fit$summary()
3525
```
36-
```{r echo=FALSE}
37-
fit <- cmdstanr::cmdstanr_example("schools", method = "sample")
38-
print.data.frame(fit$summary())
39-
```
4026

4127
By default all variables are summaries with the follow functions:
4228
```{r}
4329
posterior::default_summary_measures()
4430
```
4531

46-
To change the variables summarised, we use the variables argument
47-
```{r eval=FALSE}
32+
To change the variables summarized, we use the variables argument
33+
```{r}
4834
fit$summary(variables = c("mu", "tau"))
4935
```
50-
```{r echo=FALSE}
51-
print.data.frame(fit$summary(variables = c("mu", "tau")))
52-
```
5336

5437
We can additionally change which functions are used
55-
```{r eval=FALSE}
38+
```{r}
5639
fit$summary(variables = c("mu", "tau"), mean, sd)
5740
```
58-
```{r echo=FALSE}
59-
print.data.frame(fit$summary(variables = c("mu", "tau"), mean, sd))
60-
```
6141

62-
To summarise all variables with non-default functions, it is necessary to set explicitly set the variables argument, either to `NULL` or the full vector of variable names.
63-
```{r eval=FALSE}
42+
To summarize all variables with non-default functions, it is necessary to set explicitly set the variables argument, either to `NULL` or the full vector of variable names.
43+
```{r}
6444
fit$metadata()$model_params
6545
fit$summary(variables = NULL, "mean", "median")
6646
```
67-
```{r echo=FALSE}
68-
fit$metadata()$model_params
69-
print.data.frame(fit$summary(variables = NULL, "mean", "median"))
70-
```
7147

7248
Summary functions can be specified by character string, function, or using a formula (or anything else supported by [rlang::as_function]). If these arguments are named, those names will be used in the tibble output. If the summary results are named they will take precedence.
73-
```{r eval=FALSE}
49+
```{r}
7450
my_sd <- function(x) c(My_SD = sd(x))
7551
fit$summary(
7652
c("mu", "tau"),
@@ -81,58 +57,31 @@ fit$summary(
8157
Minimum = function(x) min(x)
8258
)
8359
```
84-
```{r echo=FALSE}
85-
my_sd <- function(x) c(My_SD = sd(x))
86-
print.data.frame(fit$summary(
87-
c("mu", "tau"),
88-
MEAN = mean,
89-
"median",
90-
my_sd,
91-
~quantile(.x, probs = c(0.1, 0.9)),
92-
Minimum = function(x) min(x)
93-
))
94-
```
95-
9660

9761
Arguments to all summary functions can also be specified with `.args`.
98-
```{r eval=FALSE}
62+
```{r}
9963
fit$summary(c("mu", "tau"), quantile, .args = list(probs = c(0.025, .05, .95, .975)))
10064
```
101-
```{r echo=FALSE}
102-
print.data.frame(fit$summary(c("mu", "tau"), quantile, .args = list(probs = c(0.025, .05, .95, .975))))
103-
```
10465

10566
The summary functions are applied to the array of sample values, with dimension `iter_sampling`x`chains`.
106-
```{r eval=FALSE}
67+
```{r}
10768
fit$summary(variables = NULL, dim, colMeans)
10869
```
109-
```{r echo=FALSE}
110-
print.data.frame(fit$summary(variables = NULL, dim, colMeans))
111-
```
11270

11371

11472
For this reason users may have unexpected results if they use `stats::var()` directly, as it will return a covariance matrix. An alternative is the `distributional::variance()` function,
11573
which can also be accessed via `posterior::variance()`.
116-
```{r eval=FALSE}
74+
```{r}
11775
fit$summary(c("mu", "tau"), posterior::variance, ~var(as.vector(.x)))
11876
```
119-
```{r echo=FALSE}
120-
print.data.frame(fit$summary(c("mu", "tau"), posterior::variance, ~var(as.vector(.x))))
121-
```
122-
12377

12478
Summary functions need not be numeric, but these won't work with `$print()`.
12579

126-
```{r eval=FALSE}
80+
```{r}
12781
strict_pos <- function(x) if (all(x > 0)) "yes" else "no"
12882
fit$summary(variables = NULL, "Strictly Positive" = strict_pos)
12983
# fit$print(variables = NULL, "Strictly Positive" = strict_pos)
13084
```
131-
```{r echo=FALSE}
132-
strict_pos <- function(x) if (all(x > 0)) "yes" else "no"
133-
print.data.frame(fit$summary(variables = NULL, "Strictly Positive" = strict_pos))
134-
# fit$print(variables = NULL, "Strictly Positive" = strict_pos)
135-
```
13685

13786
For more information, see `posterior::summarise_draws()`, which is called by `$summary()`.
13887

0 commit comments

Comments
 (0)