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 CmdStanVB
as_draws(x, ...)
# S3 method for CmdStanGQ
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 -66 -65 -65 -64
#> 2 -66 -65 -64 -66
#> 3 -65 -66 -65 -66
#> 4 -65 -65 -66 -65
#> 5 -67 -66 -66 -67
#>
#> , , variable = alpha
#>
#> chain
#> iteration 1 2 3 4
#> 1 0.37 0.21 0.66 0.314
#> 2 0.34 0.35 0.28 0.748
#> 3 0.58 0.25 0.41 0.023
#> 4 0.20 0.37 0.45 0.297
#> 5 0.24 0.37 0.72 0.328
#>
#> , , variable = beta[1]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.54 -0.69 -0.71 -0.55
#> 2 -0.98 -0.91 -0.67 -0.70
#> 3 -0.77 -0.30 -0.82 -0.63
#> 4 -0.41 -0.66 -1.07 -0.75
#> 5 -0.26 -0.78 -0.72 -0.45
#>
#> , , variable = beta[2]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.738 -0.427 -0.183 -0.256
#> 2 -0.545 -0.076 -0.220 -0.433
#> 3 -0.168 -0.494 -0.400 -0.201
#> 4 -0.281 -0.029 -0.388 0.072
#> 5 0.054 -0.460 -0.027 0.122
#>
#> # ... 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.4
#>
#> $alpha: rvar<1000,4>[1] mean ± sd:
#> [1] 0.37 ± 0.22
#>
#> $beta: rvar<1000,4>[3] mean ± sd:
#> [1] -0.67 ± 0.25 -0.27 ± 0.22 0.67 ± 0.27
#>
#> $log_lik: rvar<1000,4>[100] mean ± sd:
#> [1] -0.517 ± 0.101 -0.404 ± 0.151 -0.497 ± 0.215 -0.451 ± 0.152
#> [5] -1.177 ± 0.282 -0.595 ± 0.194 -0.639 ± 0.124 -0.281 ± 0.134
#> [9] -0.696 ± 0.170 -0.736 ± 0.229 -0.284 ± 0.127 -0.503 ± 0.249
#> [13] -0.654 ± 0.209 -0.361 ± 0.173 -0.281 ± 0.106 -0.278 ± 0.087
#> [17] -1.585 ± 0.286 -0.482 ± 0.109 -0.234 ± 0.076 -0.114 ± 0.078
#> [21] -0.214 ± 0.088 -0.572 ± 0.150 -0.333 ± 0.139 -0.137 ± 0.067
#> [25] -0.455 ± 0.121 -1.516 ± 0.346 -0.310 ± 0.123 -0.447 ± 0.085
#> [29] -0.723 ± 0.222 -0.697 ± 0.195 -0.492 ± 0.165 -0.426 ± 0.108
#> [33] -0.410 ± 0.128 -0.064 ± 0.050 -0.584 ± 0.182 -0.327 ± 0.132
#> [37] -0.697 ± 0.220 -0.313 ± 0.145 -0.180 ± 0.109 -0.682 ± 0.131
#> [41] -1.124 ± 0.254 -0.931 ± 0.201 -0.413 ± 0.269 -1.172 ± 0.186
#> [45] -0.360 ± 0.119 -0.582 ± 0.132 -0.307 ± 0.130 -0.326 ± 0.084
#> [49] -0.321 ± 0.080 -1.289 ± 0.340 -0.290 ± 0.095 -0.834 ± 0.146
#> [53] -0.405 ± 0.132 -0.371 ± 0.144 -0.387 ± 0.137 -0.322 ± 0.189
#> [57] -0.657 ± 0.118 -0.947 ± 0.345 -1.358 ± 0.345 -0.977 ± 0.161
#> [61] -0.542 ± 0.101 -0.881 ± 0.324 -0.118 ± 0.073 -0.899 ± 0.249
#> [65] -1.999 ± 0.597 -0.509 ± 0.135 -0.278 ± 0.082 -1.056 ± 0.235
#> [69] -0.437 ± 0.085 -0.641 ± 0.239 -0.608 ± 0.208 -0.462 ± 0.171
#> [73] -1.479 ± 0.361 -0.947 ± 0.192 -1.146 ± 0.401 -0.373 ± 0.139
#> [77] -0.875 ± 0.138 -0.489 ± 0.178 -0.762 ± 0.186 -0.544 ± 0.202
#> [81] -0.164 ± 0.099 -0.225 ± 0.139 -0.345 ± 0.082 -0.277 ± 0.091
#> [85] -0.131 ± 0.075 -1.126 ± 0.328 -0.822 ± 0.130 -0.773 ± 0.243
#> [89] -1.276 ± 0.323 -0.261 ± 0.132 -0.386 ± 0.129 -1.490 ± 0.344
#> [93] -0.740 ± 0.216 -0.319 ± 0.088 -0.391 ± 0.113 -1.566 ± 0.280
#> [97] -0.433 ± 0.103 -1.050 ± 0.364 -0.693 ± 0.143 -0.392 ± 0.099
#>
posterior::as_draws_list(fit)
#> # A draws_list: 1000 iterations, 4 chains, and 105 variables
#>
#> [chain = 1]
#> $lp__
#> [1] -66 -66 -65 -65 -67 -65 -64 -64 -66 -64
#>
#> $alpha
#> [1] 0.37 0.34 0.58 0.20 0.24 0.48 0.36 0.35 0.21 0.35
#>
#> $`beta[1]`
#> [1] -0.54 -0.98 -0.77 -0.41 -0.26 -0.80 -0.62 -0.74 -0.55 -0.51
#>
#> $`beta[2]`
#> [1] -0.738 -0.545 -0.168 -0.281 0.054 -0.315 -0.204 -0.103 0.067 -0.446
#>
#>
#> [chain = 2]
#> $lp__
#> [1] -65 -65 -66 -65 -66 -68 -66 -68 -65 -65
#>
#> $alpha
#> [1] 0.211 0.348 0.245 0.371 0.366 0.050 0.135 -0.055 0.417 0.247
#>
#> $`beta[1]`
#> [1] -0.69 -0.91 -0.30 -0.66 -0.78 -0.47 -0.86 -0.85 -0.32 -0.72
#>
#> $`beta[2]`
#> [1] -0.427 -0.076 -0.494 -0.029 -0.460 0.199 -0.272 0.096 -0.201 -0.249
#>
#> # ... with 990 more iterations, and 2 more chains, and 101 more variables
# }