Skip to content

Commit 42a42e0

Browse files
authored
Merge pull request #955 from stan-dev/document-structured-draws-via-draws_of
Document the `draws_of` trick to emulate `rstan::extract()`
2 parents 4a865bb + d79ffd8 commit 42a42e0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

vignettes/cmdstanr.Rmd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ In general, converting to a different draws format in this way will be slower
258258
than just setting the appropriate format initially in the call to the `$draws()`
259259
method, but in most cases the speed difference will be minor.
260260

261+
The vignette
262+
[Working with Posteriors](https://mc-stan.org/cmdstanr/articles/posterior.html)
263+
has more details on posterior draws, including how to reproduce the structured
264+
output RStan users are accustomed to getting from `rstan::extract()`.
265+
261266
#### Plotting draws
262267

263268
Plotting posterior distributions is as easy as passing the object returned by

vignettes/posterior.Rmd

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ To convert an existing draws object to a different format use the
121121
`posterior::as_draws_*()` functions.
122122

123123
To manipulate the `draws` objects use the various methods described in the
124-
posterior package [vignettes](https://mc-stan.org/posterior/articles/index.html)
124+
**posterior** package [vignettes](https://mc-stan.org/posterior/articles/index.html)
125125
and [documentation](https://mc-stan.org/posterior/reference/index.html).
126126

127+
### Structured draws similar to `rstan::extract()`
128+
129+
The **posterior** package's `rvar` format provides a multidimensional,
130+
sample-based representation of random variables. See
131+
https://mc-stan.org/posterior/articles/rvar.html for details.
132+
In addition to being useful in its own right, this format also allows CmdStanR
133+
users to obtain draws in a similar format to `rstan::extract()`.
134+
135+
Suppose we have a parameter `matrix[2,3] x`. The `rvar` format lets you
136+
interact with `x` as if it's a `2 x 3` matrix and automatically applies operations
137+
over the many posterior draws of `x`. To instead directly access the draws of `x`
138+
while maintaining the structure of the matrix use `posterior::draws_of()`.
139+
For example:
140+
141+
```{r structured-draws, eval = FALSE}
142+
draws <- posterior::as_draws_rvars(fit$draws())
143+
x_rvar <- draws$x
144+
x_array <- posterior::draws_of(draws$x)
145+
```
146+
147+
The object `x_rvar` will be an `rvar` that can be used like a `2 x 3` matrix,
148+
with the draws handled behind the scenes. The object `x_array` will be a
149+
`4000 x 2 x 3` array (assuming `4000` posterior draws), which is the same as it
150+
would be after being extracted from the list returned by `rstan::extract()`.

0 commit comments

Comments
 (0)