| Title: | Save 'nlmixr2' Fits in a Format Readable Outside 'nlmixr2' |
|---|---|
| Description: | Provides tools to save 'nlmixr2' fitted models in a portable format readable outside of 'nlmixr2' and independent of the package version. 'nlmixr2' fits and compares nonlinear mixed-effects models in differential equations with flexible dosing information commonly seen in pharmacokinetics and pharmacodynamics (Almquist, Leander, and Jirstrand 2015 <doi:10.1007/s10928-015-9409-1>). Differential equation solving uses compiled C code from the 'rxode2' package (Wang, Hallow, and James 2015 <doi:10.1002/psp4.12052>). |
| Authors: | Matthew Fidler [aut, cre] (ORCID: <https://orcid.org/0000-0001-8538-6691>) |
| Maintainer: | Matthew Fidler <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-18 09:08:16 UTC |
| Source: | https://github.com/cran/nlmixr2save |
By default it is equivalent to the standard assignment operator <-, but
it is a S3 generic so it can have other behaviors for specific classes.
x |
the name of the object to assign the value to |
value |
the value to assign to the object, because R can use
non-standard evaluation, this expression may not be evaluated
when passed to the function. In the case of the |
For example, when used with a nlmixr2 call, say:
fit := nlmixr2(one.cmt, theo_sd, est="focei")
the := operator will assign the result of the nlmixr2 call to
fit, but it will also save the fit to a file named "fit.zip" in
the current working directory.
If the "fit.zip" file already exists, it will be loaded instead of running the possibly expensive fitting process (as long as the sha1 hash of the arguments are the same).
This allows for easy saving and loading of fitted models without having to explicitly call a save function.
This S3 generic can be extended to other classes as needed, allowing for custom behaviors when assigning values to objects of those classes.
When trying to save expensive evaluations like the output of a
nlmixr2() fit, the s3 dispach would be to :=.assign_nlmixr2(x, value) or whatever function is used in the call. This allows
checking the arguments to see if there can be a cache that will be
loaded.
Otherwise, the default s3 method would be :=.class where class
instead. Unlike the un-evaluated function dispach there is no way
to check the arguments for a cache, so loading from cache is not possible.
the value that was assigned to the object, invisibly. It also has the side effect of assigning the value to the parent environment.
Matthew L. Fidler
saveFit() for saving fitted model objects to files,
loadFit() for loading fitted model objects from files, and
.assignParent() for getting or setting the environment used in
the := operator.
if (requireNamespace("nlmixr2est", quietly=TRUE) && requireNamespace("withr")) { library(nlmixr2est) library(nlmixr2data) withr::with_tempdir({ one.cmt <- function() { ini({ tka <- 0.45 tcl <- log(c(0, 2.7, 100)) tv <- 3.45 eta.ka ~ 0.6 eta.cl ~ 0.3 eta.v ~ 0.1 add.sd <- 0.7 }) model({ ka <- exp(tka + eta.ka) cl <- exp(tcl + eta.cl) v <- exp(tv + eta.v) linCmt() ~ add(add.sd) }) } # First fit creates fit.zip fit := nlmixr2(one.cmt, theo_sd, est="focei") # Second fit loads from fit.zip since it had the same options fit := nlmixr2(one.cmt, theo_sd, est="focei") # Third fit refits since the options are different fit := nlmixr2(one.cmt, theo_sd, est="saem") }) }if (requireNamespace("nlmixr2est", quietly=TRUE) && requireNamespace("withr")) { library(nlmixr2est) library(nlmixr2data) withr::with_tempdir({ one.cmt <- function() { ini({ tka <- 0.45 tcl <- log(c(0, 2.7, 100)) tv <- 3.45 eta.ka ~ 0.6 eta.cl ~ 0.3 eta.v ~ 0.1 add.sd <- 0.7 }) model({ ka <- exp(tka + eta.ka) cl <- exp(tcl + eta.cl) v <- exp(tv + eta.v) linCmt() ~ add(add.sd) }) } # First fit creates fit.zip fit := nlmixr2(one.cmt, theo_sd, est="focei") # Second fit loads from fit.zip since it had the same options fit := nlmixr2(one.cmt, theo_sd, est="focei") # Third fit refits since the options are different fit := nlmixr2(one.cmt, theo_sd, est="saem") }) }
:= assignment was actually a restore from a
fileReturn if the last := assignment was actually a restore from a
file
.assignRestore().assignRestore()
TRUE if the last := assignment was a restore from a file, FALSE otherwise
Matthew L. Fidler
.assignRestore().assignRestore()
Load a fitted model object from a file
loadFit(file)loadFit(file)
file |
the base name of the files to load the fit from. |
the fitted model object
This function is typically not needed by end users.
nlmixrDataSimplify(data, object, table = list())nlmixrDataSimplify(data, object, table = list())
data |
nlmixr data |
object |
an nlmixr_ui object (e.g. the output of running
|
table |
The output table control object (like 'tableControl()') |
The standardization keeps columns that rxode2 and nlmixr2 use along with the covariates. Column order is standardized (rxode2 then nlmixr2 then alphabetically sorted covariates), and rxode2 and nlmixr2 column names are converted to lower case.
The data with the nlmixr2 column lower case and on the left and the covariate columns on the right and alphabetically sorted.
William S. Denney with minor modifications from Matt Fidler
Save a fitted model object to a series of files
saveFit(fit, file, zip = TRUE) ## S3 method for class 'nlmixr2FitCore' saveFit(fit, file, zip = TRUE) ## S3 method for class 'nlmixr2FitData' saveFit(fit, file, zip = TRUE) ## Default S3 method: saveFit(fit, file, zip = TRUE)saveFit(fit, file, zip = TRUE) ## S3 method for class 'nlmixr2FitCore' saveFit(fit, file, zip = TRUE) ## S3 method for class 'nlmixr2FitData' saveFit(fit, file, zip = TRUE) ## Default S3 method: saveFit(fit, file, zip = TRUE)
fit |
the fitted model object |
file |
the base name of the files to save the fit to. |
zip |
Boolean indicating if the files should be zipped. |
nothing, called for side effects
Matthew L. Fidler
if (requireNamespace("nlmixr2est", quietly=TRUE) && requireNamespace("withr")) { library(nlmixr2est) library(nlmixr2data) withr::with_tempdir({ one.cmt <- function() { ini({ tka <- 0.45 tcl <- log(c(0, 2.7, 100)) tv <- 3.45 eta.ka ~ 0.6 eta.cl ~ 0.3 eta.v ~ 0.1 add.sd <- 0.7 }) model({ ka <- exp(tka + eta.ka) cl <- exp(tcl + eta.cl) v <- exp(tv + eta.v) linCmt() ~ add(add.sd) }) } fit <- nlmixr2(one.cmt, theo_sd, est="focei") saveFit(fit) # saved to fit.zip fit2 <- loadFit(fit) # load fit.zip if (file.exists("fit.zip")) { unlink("fit.zip") } print(fit2) }) }if (requireNamespace("nlmixr2est", quietly=TRUE) && requireNamespace("withr")) { library(nlmixr2est) library(nlmixr2data) withr::with_tempdir({ one.cmt <- function() { ini({ tka <- 0.45 tcl <- log(c(0, 2.7, 100)) tv <- 3.45 eta.ka ~ 0.6 eta.cl ~ 0.3 eta.v ~ 0.1 add.sd <- 0.7 }) model({ ka <- exp(tka + eta.ka) cl <- exp(tcl + eta.cl) v <- exp(tv + eta.v) linCmt() ~ add(add.sd) }) } fit <- nlmixr2(one.cmt, theo_sd, est="focei") saveFit(fit) # saved to fit.zip fit2 <- loadFit(fit) # load fit.zip if (file.exists("fit.zip")) { unlink("fit.zip") } print(fit2) }) }
:=
Functions registered here are saved with random-state metadata so cached restores behave like the original call was run again.
saveFitRandom(fun = NULL, remove = FALSE)saveFitRandom(fun = NULL, remove = FALSE)
fun |
Function name(s) to add or remove. If |
remove |
Boolean indicating if |
Character vector of registered function names.
Matthew L. Fidler
saveFitRandom() saveFitRandom("myRandomFun") saveFitRandom("myRandomFun", remove=TRUE)saveFitRandom() saveFitRandom("myRandomFun") saveFitRandom("myRandomFun", remove=TRUE)