Save fitted model object to a file — fit-method-save_object • cmdstanr
Description: 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.

# Save fitted model object to a file

Source: `R/fit.R`
`fit-method-save_object.Rd`

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.

    save_object(file, ...)

## Arguments

file

  (string) Path where the file should be saved.

...

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

## See also

`CmdStanMCMC`, `CmdStanMLE`, `CmdStanVB`, `CmdStanGQ`

## 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>        <num>   <num>  <num>  <num>    <num>    <num> <num>    <num>
    #>  1 lp__       -66.0   -65.6   1.45   1.24   -68.8    -64.3     1.00    2217.
    #>  2 alpha        0.386   0.382 0.217  0.215    0.0345   0.754   1.00    4227.
    #>  3 beta[1]     -0.659  -0.650 0.246  0.246   -1.07    -0.268   1.00    3920.
    #>  4 beta[2]     -0.275  -0.277 0.226  0.226   -0.654    0.0937  1.00    4340.
    #>  5 beta[3]      0.675   0.665 0.275  0.267    0.236    1.15    1.00    3661.
    #>  6 log_lik[1]  -0.514  -0.508 0.0974 0.0967  -0.683   -0.365   1.00    4222.
    #>  7 log_lik[2]  -0.410  -0.389 0.154  0.150   -0.690   -0.198   1.00    4320.
    #>  8 log_lik[3]  -0.500  -0.469 0.218  0.206   -0.908   -0.207   1.00    4270.
    #>  9 log_lik[4]  -0.453  -0.436 0.152  0.146   -0.729   -0.238   1.00    3898.
    #> 10 log_lik[5]  -1.18   -1.16  0.287  0.285   -1.68    -0.760   1.00    3941.
    #> # ℹ 95 more rows
    #> # ℹ 1 more variable: ess_tail <num>
    # }