@@ -380,12 +380,9 @@ these links to their documentation pages.
380380
381381## Saving fitted model objects
382382
383- In order to save a fitted model object to disk and ensure that all of the
384- contents are available when reading the object back into R, we recommend using the
385- [ ` $save_object() ` ] ( http://mc-stan.org/cmdstanr/reference/fit-method-save_object.html )
386- method provided by CmdStanR. The reason for this is discussed in detail in the vignette
387- [ _ How does CmdStanR work?_ ] ( http://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html ) ,
388- so here we just demonstrate how to use the method.
383+ The [ ` $save_object() ` ] ( http://mc-stan.org/cmdstanr/reference/fit-method-save_object.html )
384+ method provided by CmdStanR is the most convenient way to save a fitted model object
385+ to disk and ensure that all of the contents are available when reading the object back into R.
389386
390387``` {r save_object, eval=FALSE}
391388fit$save_object(file = "fit.RDS")
@@ -394,6 +391,48 @@ fit$save_object(file = "fit.RDS")
394391fit2 <- readRDS("fit.RDS")
395392```
396393
394+ But if your model object is large, then
395+ [ ` $save_object() ` ] ( http://mc-stan.org/cmdstanr/reference/fit-method-save_object.html )
396+ could take a long time.
397+ [ ` $save_object() ` ] ( http://mc-stan.org/cmdstanr/reference/fit-method-save_object.html )
398+ reads the CmdStan results files into memory, stores them in the model object,
399+ and saves the object with ` saveRDS() ` . To speed up the process, you can emulate
400+ [ ` $save_object() ` ] ( http://mc-stan.org/cmdstanr/reference/fit-method-save_object.html )
401+ and replace ` saveRDS ` with the much faster ` qsave() ` function from the
402+ [ ` qs ` ] ( https://github.com/traversc/qs ) package.
403+
404+ ``` {r save_object_qs_full, eval = FALSE}
405+ # Load CmdStan output files into the fitted model object.
406+ fit$draws() # Load posterior draws into the object.
407+ try(fit$sampler_diagnostics(), silent = TRUE) # Load sampler diagnostics.
408+ try(fit$init(), silent = TRUE) # Load user-defined initial values.
409+ try(fit$profiles(), silent = TRUE) # Load profiling samples.
410+
411+ # Save the object to a file.
412+ qs::qsave(x = fit, file = "fit.qs")
413+
414+ # Read the object.
415+ fit2 <- qs::qread("fit.qs")
416+ ```
417+
418+ Storage is even faster if you discard results you do not need to save.
419+ The following example saves only posterior draws and discards
420+ sampler diagnostics, user-specified initial values, and profiling data.
421+
422+ ``` {r save_object_qs_small, eval = FALSE}
423+ # Load posterior draws into the fitted model object and omit other output.
424+ fit$draws()
425+
426+ # Save the object to a file.
427+ qs::qsave(x = fit, file = "fit.qs")
428+
429+ # Read the object.
430+ fit2 <- qs::qread("fit.qs")
431+ ```
432+
433+ See the vignette [ _ How does CmdStanR work?_ ] ( http://mc-stan.org/cmdstanr/articles/cmdstanr-internals.html )
434+ for more information about the composition of CmdStanR objects.
435+
397436## Comparison with RStan
398437
399438``` {r child="children/comparison-with-rstan.md"}
0 commit comments