The $summary()
method runs
summarise_draws()
from the posterior
package and returns the output. For MCMC, only post-warmup draws are
included in the summary.
There is also a $print()
method that prints the same summary stats but
removes the extra formatting used for printing tibbles and returns the
fitted model object itself. The $print()
method may also be faster than
$summary()
because it is designed to only compute the summary statistics
for the variables that will actually fit in the printed output whereas
$summary()
will compute them for all of the specified variables in order
to be able to return them to the user. See Examples.
summary(variables = NULL, ...)
(character vector) The variables to include.
Optional arguments to pass to posterior::summarise_draws()
.
The $summary()
method returns the tibble data frame created by
posterior::summarise_draws()
.
The $print()
method returns the fitted model object itself (invisibly),
which is the standard behavior for print methods in R.
# \dontrun{
fit <- cmdstanr_example("logistic")
fit$summary()
#> # A tibble: 105 × 10
#> variable mean median sd mad q5 q95 rhat ess_bulk
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 lp__ -66.0 -65.6 1.45 1.21 -68.7 -64.3 1.00 2233.
#> 2 alpha 0.382 0.380 0.214 0.217 0.0308 0.730 1.00 4473.
#> 3 beta[1] -0.667 -0.665 0.248 0.254 -1.08 -0.261 1.00 4454.
#> 4 beta[2] -0.274 -0.271 0.233 0.232 -0.649 0.103 1.00 4166.
#> 5 beta[3] 0.680 0.672 0.264 0.258 0.254 1.12 1.00 3555.
#> 6 log_lik[1] -0.514 -0.506 0.0973 0.0977 -0.684 -0.368 1.00 4272.
#> 7 log_lik[2] -0.403 -0.383 0.147 0.137 -0.677 -0.199 1.00 4474.
#> 8 log_lik[3] -0.497 -0.465 0.217 0.204 -0.897 -0.202 1.00 4228.
#> 9 log_lik[4] -0.450 -0.434 0.152 0.148 -0.717 -0.238 1.00 3834.
#> 10 log_lik[5] -1.18 -1.16 0.278 0.275 -1.67 -0.760 1.00 4261.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>
fit$print()
#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail
#> lp__ -65.96 -65.62 1.45 1.21 -68.75 -64.29 1.00 2232 2919
#> alpha 0.38 0.38 0.21 0.22 0.03 0.73 1.00 4473 3190
#> beta[1] -0.67 -0.66 0.25 0.25 -1.08 -0.26 1.00 4454 3056
#> beta[2] -0.27 -0.27 0.23 0.23 -0.65 0.10 1.00 4166 2823
#> beta[3] 0.68 0.67 0.26 0.26 0.25 1.12 1.00 3555 3140
#> log_lik[1] -0.51 -0.51 0.10 0.10 -0.68 -0.37 1.00 4272 3239
#> log_lik[2] -0.40 -0.38 0.15 0.14 -0.68 -0.20 1.00 4474 3106
#> log_lik[3] -0.50 -0.46 0.22 0.20 -0.90 -0.20 1.00 4228 3323
#> log_lik[4] -0.45 -0.43 0.15 0.15 -0.72 -0.24 1.00 3833 2800
#> log_lik[5] -1.18 -1.16 0.28 0.28 -1.67 -0.76 1.00 4260 3060
#>
#> # showing 10 of 105 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)
fit$print(max_rows = 2) # same as print(fit, max_rows = 2)
#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail
#> lp__ -65.96 -65.62 1.45 1.21 -68.75 -64.29 1.00 2232 2919
#> alpha 0.38 0.38 0.21 0.22 0.03 0.73 1.00 4473 3190
#>
#> # showing 2 of 105 rows (change via 'max_rows' argument or 'cmdstanr_max_rows' option)
# include only certain variables
fit$summary("beta")
#> # A tibble: 3 × 10
#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 beta[1] -0.667 -0.665 0.248 0.254 -1.08 -0.261 1.00 4454. 3056.
#> 2 beta[2] -0.274 -0.271 0.233 0.232 -0.649 0.103 1.00 4166. 2824.
#> 3 beta[3] 0.680 0.672 0.264 0.258 0.254 1.12 1.00 3555. 3140.
fit$print(c("alpha", "beta[2]"))
#> variable mean median sd mad q5 q95 rhat ess_bulk ess_tail
#> alpha 0.38 0.38 0.21 0.22 0.03 0.73 1.00 4473 3190
#> beta[2] -0.27 -0.27 0.23 0.23 -0.65 0.10 1.00 4166 2823
# include all variables but only certain summaries
fit$summary(NULL, c("mean", "sd"))
#> # A tibble: 105 × 3
#> variable mean sd
#> <chr> <dbl> <dbl>
#> 1 lp__ -66.0 1.45
#> 2 alpha 0.382 0.214
#> 3 beta[1] -0.667 0.248
#> 4 beta[2] -0.274 0.233
#> 5 beta[3] 0.680 0.264
#> 6 log_lik[1] -0.514 0.0973
#> 7 log_lik[2] -0.403 0.147
#> 8 log_lik[3] -0.497 0.217
#> 9 log_lik[4] -0.450 0.152
#> 10 log_lik[5] -1.18 0.278
#> # ℹ 95 more rows
# can use functions created from formulas
# for example, calculate Pr(beta > 0)
fit$summary("beta", prob_gt_0 = ~ mean(. > 0))
#> # A tibble: 3 × 2
#> variable prob_gt_0
#> <chr> <dbl>
#> 1 beta[1] 0.0015
#> 2 beta[2] 0.118
#> 3 beta[3] 0.994
# can combine user-specified functions with
# the default summary functions
fit$summary(variables = c("alpha", "beta"),
posterior::default_summary_measures()[1:4],
quantiles = ~ quantile2(., probs = c(0.025, 0.975)),
posterior::default_convergence_measures()
)
#> # A tibble: 4 × 10
#> variable mean median sd mad q2.5 q97.5 rhat ess_bulk ess_tail
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 alpha 0.382 0.380 0.214 0.217 -0.0460 0.804 1.00 4473. 3191.
#> 2 beta[1] -0.667 -0.665 0.248 0.254 -1.16 -0.189 1.00 4454. 3056.
#> 3 beta[2] -0.274 -0.271 0.233 0.232 -0.727 0.182 1.00 4166. 2824.
#> 4 beta[3] 0.680 0.672 0.264 0.258 0.172 1.21 1.00 3555. 3140.
# the functions need to calculate the appropriate
# value for a matrix input
fit$summary(variables = "alpha", dim)
#> # A tibble: 1 × 3
#> variable dim.1 dim.2
#> <chr> <int> <int>
#> 1 alpha 1000 4
# the usual [stats::var()] is therefore not directly suitable as it
# will produce a covariance matrix unless the data is converted to a vector
fit$print(c("alpha", "beta"), var2 = ~var(as.vector(.x)))
#> variable var2
#> alpha 0.05
#> beta[1] 0.06
#> beta[2] 0.05
#> beta[3] 0.07
# }