knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
nlmixr2save can cache more than nlmixr2 fits. If your package has its own
simulation or stochastic estimation functions, you can register them so the
:= operator restores cached results without breaking reproducibility.
Use saveFitRandom() to tell nlmixr2save that a function should be treated as
random-state-aware.
library(nlmixr2save) saveFitRandom("mySimulate")
After registration, calls of the form below are cached in sim.rds together
with the starting and ending random state:
sim := mySimulate(model, n = 1000)
On restore, nlmixr2save checks:
If those checks pass, the cached value is loaded and the random seed is advanced to the same state the original run would have produced.
If your package always wants a function treated as stochastic, register it in
.onLoad():
.onLoad <- function(libname, pkgname) { nlmixr2save::saveFitRandom("mySimulate") }
You can also unregister it later:
saveFitRandom("mySimulate", remove = TRUE)
nlmixr2save handles nlmixr2(...) calls differently from ordinary function
calls:
deterministic estimation methods are cached as saved fits (.zip bundles),
stochastic estimation methods are cached with seed metadata (.rds files).
To place your estimation method into the seed-aware group, define the
nlmixr2Est.<method> method with a random attribute set to TRUE or to a
function that inspects the control object.
nlmixr2Est.myMethod <- function(object, data, control, ...) { # run stochastic estimation here } attr(nlmixr2Est.myMethod, "random") <- TRUE
If the stochastic behavior depends on control settings, the attribute can be a function:
attr(nlmixr2Est.myMethod, "random") <- function(control) { isTRUE(control$stochastic) }
With that in place, this call uses the seed-aware cache path automatically:
fit := nlmixr2(model, data, est = "myMethod", control = list(stochastic = TRUE))
Use saveFitRandom() when:
you are caching a regular function such as a simulation helper,
the result should be stored as a generic .rds, and
reproducibility depends on restoring the random seed state.
Use the random attribute on nlmixr2Est.<method> when:
the function is an nlmixr2 estimation method,
deterministic runs should still use the fit-saving .zip path, and
stochastic runs should move into the seed-aware .rds path.
The same limitations apply here as in the main usage vignette:
:= needs to see the expensive call directly on the right-hand side.
Seed-aware restores only replay safely when the starting random state matches.
Generic registered functions are restored from .rds, not from the
text-and-csv fit format used for deterministic nlmixr2 fits.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.