On this page

Save a fit with its inputs, settings and estimates to an RDS file, so that it can be read, summarized, plotted and refitted in another session.

Usage

save_assessment(fit, file, x = NULL, overwrite = FALSE)

Arguments

ArgumentDefaultDescription
fitrequired

An ACL or ALSCL fit, or a saved fit read with read_assessment().

filerequired

Path of the RDS file to write; its parent directory must exist.

xNULL

Complete input bundle, only for a fit that does not hold its inputs and settings (no fit$recipe); giving x for another fit is an error. The biology and settings of the original fit cannot be checked from x and are recorded as not checked.

overwriteFALSE

Logical. Whether to replace an existing file. Default FALSE.

Value

The normalized path of the file, invisibly. The fit is not changed.

Details

The file holds the inputs, map, bounds, fitting settings, software versions, estimates and reports of the fit. It does not hold the TMB object (obj), which is valid only in the session that fitted the model. read_assessment() reads the file in any session, and summaries, extractors and the plots that use the report work on the result; refit_model() fits the model again and returns a fit with a TMB object. If writing fails, an existing file is kept unchanged. The file uses R serialization version 3, which records the character encoding of length labels and column names. Read files only from trusted sources.

When x is given for a fit that does not hold its inputs and settings, the result fields that the fit lacks (observations, time labels, ages, length bins, time step and F structure) are filled from x, so that residuals, estimates and plots remain available without the TMB object. When the fit still has its TMB object, its observations are compared with those of x, and a mismatch is an error. recipe$provenance lists the fields that came from x.

recipe$implementation records the source revision when it is available, MD5 hashes of the model template and the shared header, hashes of the R source files, and the platform, BLAS, LAPACK and dependency versions; a source revision does not show whether the working tree had uncommitted changes. refit_model() warns when the recorded software differs from the installed software. The same records do not guarantee the same numbers on another platform.

When fits are saved in parallel, give each its own file name. If two sessions save to the same new path with the default overwrite = FALSE, only the first succeeds. If an interrupted session leaves a lock error, confirm that its writing process has stopped, then remove the lock directory named in the error.

Additional details from R help

The file excludes the TMB object of the session and stores a schema version, fitted estimates, input tables, map, bounds, settings and software versions. New fits also record available source revisions, MD5 hashes of the model template and shared header, and platform, BLAS, LAPACK and dependency information in recipe$implementation. These identify the recorded source files; a checkout revision alone does not establish that the working tree was clean. R source-file hashes are also recorded when available in a source tree; ordinary installed packages need not retain those files. Missing source metadata stays unavailable, and no source revision is inferred for a fit without a recipe supplied with x. For such a fit, result fields that it lacks (observations, time labels, ages, length bins, time step and F structure) are filled from x, so that residuals, estimates and plots remain available after the live TMB object is dropped. Observations are compared with the data in the live TMB object when it is present, and a mismatch is an error. recipe$provenance lists the fields that came from x. The file uses R serialization version 3, which records the character encoding of length labels and column names. Concurrent saves to the same path use a temporary lock directory during the final rename. With overwrite = FALSE, only the first save writes the file. If an interrupted process leaves a lock, confirm that the writer has stopped before removing the lock directory named in the error. A new session can summarize and plot the saved fit without a TMB object. refit_model rebuilds the TMB object; a refit with other software versions can differ numerically. Read saved fits only from trusted sources.

Examples

Run the setup before the example. Fitting examples require an R-compatible C++ toolchain; simulation and export examples may write files.

Example setup
library(ALSCL)
library(ggplot2)
library(patchwork)
data("YTF_example")
x <- YTF_example
inputs <- x[c("data.CatL", "data.wgt", "data.mat")]
dat <- x$data.CatL
a <- run_acl(x, silent = TRUE)
b <- run_alscl(x, silent = TRUE)
path <- tempfile(fileext = ".rds")
save_assessment(b, path)
saved <- read_assessment(path)
summary(saved)

See also

Search documentation