Create a draws
object supported by the posterior package. These
methods are just wrappers around CmdStanR's $draws()
method provided for convenience.
# S3 method for CmdStanMCMC
as_draws(x, ...)
# S3 method for CmdStanMLE
as_draws(x, ...)
# S3 method for CmdStanLaplace
as_draws(x, ...)
# S3 method for CmdStanVB
as_draws(x, ...)
# S3 method for CmdStanGQ
as_draws(x, ...)
# S3 method for CmdStanPathfinder
as_draws(x, ...)
A CmdStanR fitted model object.
Optional arguments passed to the $draws()
method (e.g., variables
, inc_warmup
, etc.).
To subset iterations, chains, or draws, use the
posterior::subset_draws()
method after creating the draws
object.
# \dontrun{
fit <- cmdstanr_example()
as_draws(fit)
#> # A draws_array: 1000 iterations, 4 chains, and 105 variables
#> , , variable = lp__
#>
#> chain
#> iteration 1 2 3 4
#> 1 -65 -67 -65 -66
#> 2 -66 -65 -65 -67
#> 3 -65 -65 -65 -65
#> 4 -65 -66 -65 -65
#> 5 -66 -69 -71 -66
#>
#> , , variable = alpha
#>
#> chain
#> iteration 1 2 3 4
#> 1 0.448 0.19 0.36 0.34
#> 2 0.343 0.57 0.28 0.53
#> 3 0.441 0.54 0.56 0.45
#> 4 0.095 0.75 0.19 0.57
#> 5 0.089 0.81 0.57 0.20
#>
#> , , variable = beta[1]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.40 -0.13 -0.86 -0.87
#> 2 -0.60 -0.75 -0.39 -0.66
#> 3 -0.46 -1.00 -0.90 -0.61
#> 4 -0.74 -0.72 -0.90 -0.87
#> 5 -0.45 -0.80 -0.71 -0.99
#>
#> , , variable = beta[2]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.29 -0.25 -0.041 -0.62
#> 2 -0.70 -0.35 -0.376 0.26
#> 3 -0.12 -0.21 -0.190 -0.23
#> 4 -0.48 -0.34 -0.117 -0.20
#> 5 -0.44 -0.10 0.289 -0.28
#>
#> # ... with 995 more iterations, and 101 more variables
# posterior's as_draws_*() methods will also work
posterior::as_draws_rvars(fit)
#> # A draws_rvars: 1000 iterations, 4 chains, and 4 variables
#> $lp__: rvar<1000,4>[1] mean ± sd:
#> [1] -66 ± 1.5
#>
#> $alpha: rvar<1000,4>[1] mean ± sd:
#> [1] 0.38 ± 0.22
#>
#> $beta: rvar<1000,4>[3] mean ± sd:
#> [1] -0.67 ± 0.25 -0.28 ± 0.23 0.69 ± 0.27
#>
#> $log_lik: rvar<1000,4>[100] mean ± sd:
#> [1] -0.517 ± 0.101 -0.399 ± 0.145 -0.501 ± 0.225 -0.446 ± 0.153
#> [5] -1.185 ± 0.288 -0.589 ± 0.186 -0.639 ± 0.122 -0.277 ± 0.138
#> [9] -0.692 ± 0.164 -0.743 ± 0.239 -0.278 ± 0.126 -0.492 ± 0.239
#> [13] -0.656 ± 0.204 -0.362 ± 0.176 -0.278 ± 0.108 -0.274 ± 0.089
#> [17] -1.598 ± 0.297 -0.478 ± 0.109 -0.232 ± 0.077 -0.113 ± 0.080
#> [21] -0.211 ± 0.089 -0.567 ± 0.147 -0.329 ± 0.142 -0.136 ± 0.067
#> [25] -0.452 ± 0.119 -1.523 ± 0.345 -0.305 ± 0.123 -0.445 ± 0.085
#> [29] -0.723 ± 0.230 -0.694 ± 0.192 -0.485 ± 0.161 -0.423 ± 0.108
#> [33] -0.407 ± 0.124 -0.063 ± 0.050 -0.584 ± 0.183 -0.324 ± 0.134
#> [37] -0.702 ± 0.231 -0.309 ± 0.148 -0.179 ± 0.111 -0.681 ± 0.127
#> [41] -1.135 ± 0.256 -0.932 ± 0.194 -0.410 ± 0.262 -1.177 ± 0.192
#> [45] -0.359 ± 0.120 -0.580 ± 0.127 -0.301 ± 0.127 -0.324 ± 0.084
#> [49] -0.318 ± 0.082 -1.290 ± 0.332 -0.287 ± 0.095 -0.833 ± 0.145
#> [53] -0.400 ± 0.130 -0.372 ± 0.143 -0.382 ± 0.133 -0.319 ± 0.194
#> [57] -0.658 ± 0.119 -0.953 ± 0.354 -1.365 ± 0.337 -0.978 ± 0.163
#> [61] -0.541 ± 0.099 -0.872 ± 0.313 -0.116 ± 0.075 -0.903 ± 0.248
#> [65] -2.029 ± 0.603 -0.508 ± 0.136 -0.276 ± 0.083 -1.064 ± 0.237
#> [69] -0.435 ± 0.086 -0.639 ± 0.237 -0.609 ± 0.205 -0.461 ± 0.167
#> [73] -1.492 ± 0.362 -0.948 ± 0.200 -1.139 ± 0.388 -0.374 ± 0.142
#> [77] -0.878 ± 0.139 -0.489 ± 0.172 -0.766 ± 0.193 -0.539 ± 0.194
#> [81] -0.161 ± 0.098 -0.220 ± 0.134 -0.343 ± 0.083 -0.275 ± 0.093
#> [85] -0.130 ± 0.076 -1.131 ± 0.318 -0.823 ± 0.128 -0.777 ± 0.238
#> [89] -1.284 ± 0.315 -0.258 ± 0.133 -0.384 ± 0.127 -1.503 ± 0.353
#> [93] -0.736 ± 0.221 -0.318 ± 0.090 -0.387 ± 0.113 -1.578 ± 0.293
#> [97] -0.431 ± 0.102 -1.055 ± 0.371 -0.693 ± 0.139 -0.391 ± 0.098
#>
posterior::as_draws_list(fit)
#> # A draws_list: 1000 iterations, 4 chains, and 105 variables
#>
#> [chain = 1]
#> $lp__
#> [1] -65 -66 -65 -65 -66 -66 -65 -64 -65 -65
#>
#> $alpha
#> [1] 0.448 0.343 0.441 0.095 0.089 0.680 0.373 0.327 0.441 0.215
#>
#> $`beta[1]`
#> [1] -0.40 -0.60 -0.46 -0.74 -0.45 -0.80 -0.65 -0.85 -0.39 -0.36
#>
#> $`beta[2]`
#> [1] -0.29 -0.70 -0.12 -0.48 -0.44 -0.14 -0.50 -0.33 -0.25 -0.33
#>
#>
#> [chain = 2]
#> $lp__
#> [1] -67 -65 -65 -66 -69 -68 -67 -67 -68 -69
#>
#> $alpha
#> [1] 0.186 0.574 0.536 0.753 0.813 -0.010 0.027 0.027 0.628 0.271
#>
#> $`beta[1]`
#> [1] -0.126 -0.754 -1.000 -0.716 -0.801 -0.503 -0.628 -0.628 -0.985 -0.052
#>
#> $`beta[2]`
#> [1] -0.25 -0.35 -0.21 -0.34 -0.10 -0.34 -0.17 -0.17 -0.70 -0.21
#>
#> # ... with 990 more iterations, and 2 more chains, and 101 more variables
# }