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 -66 -67 -65 -67
#> 2 -65 -65 -64 -67
#> 3 -66 -66 -65 -64
#> 4 -65 -65 -65 -66
#> 5 -67 -67 -66 -65
#>
#> , , variable = alpha
#>
#> chain
#> iteration 1 2 3 4
#> 1 0.37 0.0232 0.30 0.19
#> 2 0.35 0.1359 0.50 0.56
#> 3 0.39 0.0021 0.22 0.42
#> 4 0.42 0.2264 0.22 0.21
#> 5 0.30 0.6367 0.19 0.24
#>
#> , , variable = beta[1]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.21 -0.70 -0.87 -0.93
#> 2 -0.42 -0.81 -0.58 -0.38
#> 3 -1.01 -0.83 -0.81 -0.49
#> 4 -0.95 -0.45 -0.67 -1.03
#> 5 -0.16 -0.96 -0.73 -0.89
#>
#> , , variable = beta[2]
#>
#> chain
#> iteration 1 2 3 4
#> 1 -0.3841 0.08 -0.574 -0.73
#> 2 -0.2843 -0.27 -0.376 0.12
#> 3 -0.0223 -0.13 -0.065 -0.11
#> 4 0.0016 -0.28 -0.100 -0.29
#> 5 -0.5327 -0.66 -0.644 -0.16
#>
#> # ... 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.66 ± 0.25 -0.27 ± 0.23 0.67 ± 0.26
#>
#> $log_lik: rvar<1000,4>[100] mean ± sd:
#> [1] -0.518 ± 0.099 -0.405 ± 0.147 -0.501 ± 0.220 -0.451 ± 0.153
#> [5] -1.173 ± 0.276 -0.592 ± 0.185 -0.640 ± 0.123 -0.280 ± 0.135
#> [9] -0.693 ± 0.163 -0.740 ± 0.234 -0.283 ± 0.124 -0.500 ± 0.241
#> [13] -0.655 ± 0.206 -0.364 ± 0.175 -0.282 ± 0.106 -0.278 ± 0.087
#> [17] -1.582 ± 0.288 -0.481 ± 0.107 -0.235 ± 0.076 -0.115 ± 0.079
#> [21] -0.215 ± 0.088 -0.570 ± 0.144 -0.332 ± 0.140 -0.139 ± 0.067
#> [25] -0.456 ± 0.121 -1.513 ± 0.345 -0.309 ± 0.120 -0.447 ± 0.083
#> [29] -0.722 ± 0.227 -0.694 ± 0.189 -0.489 ± 0.157 -0.427 ± 0.106
#> [33] -0.412 ± 0.126 -0.065 ± 0.052 -0.585 ± 0.184 -0.327 ± 0.135
#> [37] -0.700 ± 0.227 -0.314 ± 0.150 -0.182 ± 0.110 -0.681 ± 0.126
#> [41] -1.124 ± 0.248 -0.928 ± 0.195 -0.412 ± 0.267 -1.169 ± 0.185
#> [45] -0.362 ± 0.119 -0.583 ± 0.128 -0.307 ± 0.129 -0.327 ± 0.084
#> [49] -0.322 ± 0.079 -1.286 ± 0.334 -0.291 ± 0.096 -0.833 ± 0.144
#> [53] -0.404 ± 0.127 -0.373 ± 0.143 -0.388 ± 0.134 -0.322 ± 0.195
#> [57] -0.658 ± 0.117 -0.951 ± 0.353 -1.351 ± 0.337 -0.975 ± 0.159
#> [61] -0.542 ± 0.099 -0.876 ± 0.314 -0.119 ± 0.075 -0.901 ± 0.247
#> [65] -1.995 ± 0.581 -0.511 ± 0.134 -0.279 ± 0.083 -1.057 ± 0.233
#> [69] -0.438 ± 0.083 -0.638 ± 0.235 -0.609 ± 0.207 -0.463 ± 0.169
#> [73] -1.476 ± 0.363 -0.944 ± 0.194 -1.139 ± 0.389 -0.376 ± 0.140
#> [77] -0.875 ± 0.136 -0.489 ± 0.174 -0.764 ± 0.190 -0.545 ± 0.196
#> [81] -0.165 ± 0.100 -0.226 ± 0.138 -0.346 ± 0.081 -0.278 ± 0.091
#> [85] -0.132 ± 0.075 -1.120 ± 0.315 -0.822 ± 0.127 -0.773 ± 0.237
#> [89] -1.270 ± 0.314 -0.263 ± 0.136 -0.388 ± 0.129 -1.484 ± 0.335
#> [93] -0.738 ± 0.218 -0.321 ± 0.088 -0.391 ± 0.110 -1.562 ± 0.280
#> [97] -0.433 ± 0.102 -1.052 ± 0.372 -0.693 ± 0.139 -0.393 ± 0.098
#>
posterior::as_draws_list(fit)
#> # A draws_list: 1000 iterations, 4 chains, and 105 variables
#>
#> [chain = 1]
#> $lp__
#> [1] -66 -65 -66 -65 -67 -65 -65 -64 -67 -69
#>
#> $alpha
#> [1] 0.373 0.355 0.389 0.419 0.300 0.178 0.629 0.288 0.016 -0.075
#>
#> $`beta[1]`
#> [1] -0.21 -0.42 -1.01 -0.95 -0.16 -0.51 -0.69 -0.67 -0.74 -0.71
#>
#> $`beta[2]`
#> [1] -0.3841 -0.2843 -0.0223 0.0016 -0.5327 -0.3040 -0.3392 -0.2792 0.0976
#> [10] 0.1916
#>
#>
#> [chain = 2]
#> $lp__
#> [1] -67 -65 -66 -65 -67 -67 -67 -64 -68 -66
#>
#> $alpha
#> [1] 0.0232 0.1359 0.0021 0.2264 0.6367 0.7152 0.4707 0.3940 0.0634 0.4544
#>
#> $`beta[1]`
#> [1] -0.70 -0.81 -0.83 -0.45 -0.96 -0.34 -1.17 -0.57 -0.62 -0.62
#>
#> $`beta[2]`
#> [1] 0.08 -0.27 -0.13 -0.28 -0.66 -0.57 -0.21 -0.20 -0.47 -0.67
#>
#> # ... with 990 more iterations, and 2 more chains, and 101 more variables
# }