Removes objects from a fitted model that sdmTMB rebuilds as needed, so the
model takes less space when saved with saveRDS(). The stripped model
works the same as one read back from disk: its objective function is taped
again the first time a method needs it.
Arguments
- object
A model fit with
sdmTMB().- strip_joint_precision
Also remove the joint precision matrix of the fixed and random effects? This usually gives the largest saving, but makes some methods slower. See Details.
Details
This removes the handle to the in-memory RTMB tape, which doesn't survive a save and reload and can be the largest part of a saved model, and the cached sparse Cholesky factor of the random-effects Hessian, which sdmTMB rebuilds cheaply when needed.
The joint precision matrix is often the largest part of what remains, but
removing it (strip_joint_precision = TRUE) has a cost later:
Simulation-based methods (e.g.,
predict(..., nsim = ),get_index_sims(),spread_sims(),gather_sims(), andproject()) recompute it withRTMB::sdreport()each time they are called. It isn't stored back in the model, so every call costs about as much as thesdreport()step of the original fit, which can take minutes for large spatiotemporal models.get_index(),get_cog(), andget_eao()can't use a shortcut based on the matrix and instead build a new objective function and runRTMB::sdreport()on it.
Other methods (e.g., predict() without nsim, tidy(), and
residuals()) are unaffected, as are models fit without the matrix (see
get_joint_precision in sdmTMBcontrol()).
The original object is not changed.
Examples
mesh <- make_mesh(pcod_2011, c("X", "Y"), cutoff = 20)
fit <- sdmTMB(density ~ 1, data = pcod_2011, mesh = mesh,
family = tweedie())
fit_small <- strip_sdmTMB(fit)
object.size(serialize(fit, NULL))
#> 4271816 bytes
object.size(serialize(fit_small, NULL))
#> 1099664 bytes
p <- predict(fit_small)
