This method is a wrapper around base::saveRDS() that ensures that all posterior draws and diagnostics are saved when saving a fitted model object. Because the contents of the CmdStan output CSV files are only read into R lazily (i.e., as needed), the $save_object() method is the safest way to guarantee that everything has been read in before saving.

See the "Saving fitted model objects" section of the Getting started with CmdStanR vignette for some suggestions on faster model saving for large models.

save_object(file, ...)

Arguments

file

(string) Path where the file should be saved.

...

Other arguments to pass to base::saveRDS() besides object and file.

Examples

# \dontrun{
fit <- cmdstanr_example("logistic")

temp_rds_file <- tempfile(fileext = ".RDS")
fit$save_object(file = temp_rds_file)
rm(fit)

fit <- readRDS(temp_rds_file)
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__       -65.9   -65.6   1.42   1.21   -68.6    -64.3     1.00    2184.
#>  2 alpha        0.376   0.374 0.216  0.221    0.0219   0.730   1.00    4114.
#>  3 beta[1]     -0.661  -0.659 0.244  0.246   -1.08    -0.269   1.00    4410.
#>  4 beta[2]     -0.274  -0.272 0.227  0.228   -0.639    0.0940  1.00    3599.
#>  5 beta[3]      0.675   0.668 0.265  0.261    0.256    1.11    1.00    3811.
#>  6 log_lik[1]  -0.517  -0.507 0.0995 0.0970  -0.692   -0.369   1.00    4022.
#>  7 log_lik[2]  -0.404  -0.387 0.144  0.134   -0.663   -0.199   1.00    4613.
#>  8 log_lik[3]  -0.501  -0.465 0.219  0.208   -0.909   -0.215   1.00    3919.
#>  9 log_lik[4]  -0.450  -0.432 0.152  0.148   -0.723   -0.233   1.00    3378.
#> 10 log_lik[5]  -1.18   -1.15  0.277  0.272   -1.68    -0.758   1.00    4267.
#> # ℹ 95 more rows
#> # ℹ 1 more variable: ess_tail <dbl>
# }