From 37cb2b4b793fa699d033632158e3604c37678c20 Mon Sep 17 00:00:00 2001
From: Johannes Ranke
Helper functions to create nlme models from mmkin row objects
Fit nonlinear mixed models with SAEM
Retrieve a degradation function from the mmkin namespace
Helper functions to create nlme models from mmkin row objects
Create saemix models from mmkin row objects
Retrieve a degradation function from the mmkin namespace
R/mkinpredict.R
+ Source: R/mkinpredict.R
mkinpredict.Rd
mkinmod
, u
kinetic parameters and initial values for the state variables.
mkinpredict( - x, - odeparms, - odeini, - outtimes = seq(0, 120, by = 0.1), - solution_type = "deSolve", - use_compiled = "auto", - method.ode = "lsoda", - atol = 1e-08, - rtol = 1e-10, - map_output = TRUE, - ... -) +mkinpredict(x, odeparms, odeini, outtimes, ...) # S3 method for mkinmod -mkinpredict( - x, - odeparms = c(k_parent_sink = 0.1), - odeini = c(parent = 100), - outtimes = seq(0, 120, by = 0.1), - solution_type = "deSolve", - use_compiled = "auto", - method.ode = "lsoda", - atol = 1e-08, - rtol = 1e-10, - map_output = TRUE, - ... -) +mkinpredict( + x, + odeparms = c(k_parent_sink = 0.1), + odeini = c(parent = 100), + outtimes = seq(0, 120, by = 0.1), + solution_type = "deSolve", + use_compiled = "auto", + method.ode = "lsoda", + atol = 1e-08, + rtol = 1e-10, + map_output = TRUE, + na_stop = TRUE, + ... +) # S3 method for mkinfit -mkinpredict( - x, - odeparms = x$bparms.ode, - odeini = x$bparms.state, - outtimes = seq(0, 120, by = 0.1), - solution_type = "deSolve", - use_compiled = "auto", - method.ode = "lsoda", - atol = 1e-08, - rtol = 1e-10, - map_output = TRUE, - ... -)+mkinpredict( + x, + odeparms = x$bparms.ode, + odeini = x$bparms.state, + outtimes = seq(0, 120, by = 0.1), + solution_type = "deSolve", + use_compiled = "auto", + method.ode = "lsoda", + atol = 1e-08, + rtol = 1e-10, + map_output = TRUE, + ... +)
outtimes | A numeric vector specifying the time points for which model predictions should be generated. |
+
+ ||
---|---|---|---|
... | +Further arguments passed to the ode solver in case such a +solver is used. |
||
solution_type | @@ -258,22 +252,25 @@ FALSE). Setting this to FALSE has no effect for analytical solutions, as these always return mapped output.|||
... | -Further arguments passed to the ode solver in case such a -solver is used. |
+ na_stop | +Should it be an error if deSolve::ode returns NaN values |
A matrix with the numeric solution in wide format
+Johannes Ranke
@@ -432,7 +441,7 @@ solver is used. diff --git a/docs/dev/reference/plot.nlme.mmkin-1.png b/docs/dev/reference/plot.nlme.mmkin-1.png index 91e3ca52..5cb33214 100644 Binary files a/docs/dev/reference/plot.nlme.mmkin-1.png and b/docs/dev/reference/plot.nlme.mmkin-1.png differ diff --git a/docs/dev/reference/plot.nlme.mmkin-2.png b/docs/dev/reference/plot.nlme.mmkin-2.png index 7b59b947..c0d67204 100644 Binary files a/docs/dev/reference/plot.nlme.mmkin-2.png and b/docs/dev/reference/plot.nlme.mmkin-2.png differ diff --git a/docs/dev/reference/saem.html b/docs/dev/reference/saem.html new file mode 100644 index 00000000..f883eb11 --- /dev/null +++ b/docs/dev/reference/saem.html @@ -0,0 +1,316 @@ + + + + + + + + +-SFO <- mkinmod(degradinol = mkinsub("SFO")) +SFO <- mkinmod(degradinol = mkinsub("SFO")) # Compare solution types -mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, - solution_type = "analytical")#> time degradinol +mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, + solution_type = "analytical") +#> time degradinol #> 0 0 100.0000000 #> 1 1 74.0818221 #> 2 2 54.8811636 @@ -294,8 +291,9 @@ solver is used. #> 17 17 0.6096747 #> 18 18 0.4516581 #> 19 19 0.3345965 -#> 20 20 0.2478752#> time degradinol +#> 20 20 0.2478752#> time degradinol #> 0 0 100.0000000 #> 1 1 74.0818221 #> 2 2 54.8811636 @@ -316,8 +314,9 @@ solver is used. #> 17 17 0.6096747 #> 18 18 0.4516581 #> 19 19 0.3345965 -#> 20 20 0.2478752mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, - solution_type = "deSolve", use_compiled = FALSE)#> time degradinol +#> 20 20 0.2478752mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, + solution_type = "deSolve", use_compiled = FALSE) +#> time degradinol #> 0 0 100.0000000 #> 1 1 74.0818221 #> 2 2 54.8811636 @@ -338,8 +337,9 @@ solver is used. #> 17 17 0.6096747 #> 18 18 0.4516581 #> 19 19 0.3345965 -#> 20 20 0.2478752#> time degradinol +#> 20 20 0.2478752#> time degradinol #> 0 0 100.0000000 #> 1 1 74.0818221 #> 2 2 54.8811636 @@ -362,59 +362,68 @@ solver is used. #> 19 19 0.3345965 #> 20 20 0.2478752# Compare integration methods to analytical solution -mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, - solution_type = "analytical")[21,]#> time degradinol -#> 20.0000000 0.2478752#> time degradinol -#> 20.0000000 0.2478752#> time degradinol -#> 20.0000000 0.2478752#> time degradinol +mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), 0:20, + solution_type = "analytical")[21,] +#> time degradinol +#> 20.0000000 0.2478752#> time degradinol +#> 20.0000000 0.2478752#> time degradinol +#> 20.0000000 0.2478752#> time degradinol #> 20.0000000 0.2480043# rk4 is not as precise here # The number of output times used to make a lot of difference until the # default for atol was adjusted -mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), - seq(0, 20, by = 0.1))[201,]#> time degradinol -#> 20.0000000 0.2478752#> time degradinol +mkinpredict(SFO, c(k_degradinol = 0.3), c(degradinol = 100), + seq(0, 20, by = 0.1))[201,] +#> time degradinol +#> 20.0000000 0.2478752#> time degradinol #> 20.0000000 0.2478752# Comparison of the performance of solution types -SFO_SFO = mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max")#> Successfully compiled differential equation model from auto-generated C code.if(require(rbenchmark)) { - benchmark(replications = 10, order = "relative", columns = c("test", "relative", "elapsed"), - eigen = mkinpredict(SFO_SFO, - c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), - c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), - solution_type = "eigen")[201,], - deSolve_compiled = mkinpredict(SFO_SFO, - c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), - c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), - solution_type = "deSolve")[201,], - deSolve = mkinpredict(SFO_SFO, - c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), - c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), - solution_type = "deSolve", use_compiled = FALSE)[201,], - analytical = mkinpredict(SFO_SFO, - c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), - c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), - solution_type = "analytical", use_compiled = FALSE)[201,]) -}#> test relative elapsed +SFO_SFO = mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "max") +#> Successfully compiled differential equation model from auto-generated C code.if(require(rbenchmark)) { + benchmark(replications = 10, order = "relative", columns = c("test", "relative", "elapsed"), + eigen = mkinpredict(SFO_SFO, + c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), + c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), + solution_type = "eigen")[201,], + deSolve_compiled = mkinpredict(SFO_SFO, + c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), + c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), + solution_type = "deSolve")[201,], + deSolve = mkinpredict(SFO_SFO, + c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), + c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), + solution_type = "deSolve", use_compiled = FALSE)[201,], + analytical = mkinpredict(SFO_SFO, + c(k_parent = 0.15, f_parent_to_m1 = 0.5, k_m1 = 0.01), + c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), + solution_type = "analytical", use_compiled = FALSE)[201,]) +} +#> Loading required package: rbenchmark#> test relative elapsed #> 2 deSolve_compiled 1.0 0.005 -#> 4 analytical 1.8 0.009 #> 1 eigen 4.0 0.020 -#> 3 deSolve 44.6 0.223+#> 4 analytical 4.0 0.020 +#> 3 deSolve 45.6 0.228# \dontrun{ # Predict from a fitted model - f <- mkinfit(SFO_SFO, FOCUS_2006_C, quiet = TRUE) - f <- mkinfit(SFO_SFO, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve") - head(mkinpredict(f))#> time parent m1 + f <- mkinfit(SFO_SFO, FOCUS_2006_C, quiet = TRUE) + f <- mkinfit(SFO_SFO, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve") + head(mkinpredict(f)) +#> time parent m1 #> 0 0.0 82.49216 0.000000 -#> 0.1 0.1 80.00562 1.236198 -#> 0.2 0.2 77.59404 2.422818 -#> 0.3 0.3 75.25514 3.561476 -#> 0.4 0.4 72.98675 4.653740 -#> 0.5 0.5 70.78673 5.701130# } +#> 0.1 0.1 80.00562 1.236394 +#> 0.2 0.2 77.59404 2.423201 +#> 0.3 0.3 75.25514 3.562040 +#> 0.4 0.4 72.98675 4.654478 +#> 0.5 0.5 70.78673 5.702033# }
This function uses saemix::saemix()
as a backend for fitting nonlinear mixed
+effects models created from mmkin row objects using the stochastic approximation
+to the expectation maximisation algorithm (SAEM).
saem(object, control, ...) + +# S3 method for mmkin +saem( + object, + control = list(displayProgress = FALSE, print = FALSE, save = FALSE, save.graphs = + FALSE), + cores = 1, + verbose = FALSE, + suppressPlot = TRUE, + ... +) + +saemix_model(object, cores = 1, verbose = FALSE, ...) + +saemix_data(object, verbose = FALSE, ...)+ +
object | +An mmkin row object containing several fits of the same +mkinmod model to different datasets |
+
---|---|
control | +Passed to saemix::saemix |
+
... | +Further parameters passed to saemix::saemixData +and saemix::saemixModel. |
+
cores | +The number of cores to be used for multicore processing using
+ |
+
verbose | +Should we print information about created objects? |
+
suppressPlot | +Should we suppress any plotting that is done +by the saemix function? |
+
An saemix::SaemixModel object.
+An saemix::SaemixData object.
+An mmkin row object is essentially a list of mkinfit objects that have been +obtained by fitting the same model to a list of datasets using mkinfit.
+Starting values for the fixed effects (population mean parameters, argument
+psi0 of saemix::saemixModel()
are the mean values of the parameters found
+using mmkin.
+# \dontrun{ +ds <- lapply(experimental_data_for_UBA_2019[6:10], + function(x) subset(x$data[c("name", "time", "value")])) +names(ds) <- paste("Dataset", 6:10) +f_mmkin_parent_p0_fixed <- mmkin("FOMC", ds, cores = 1, + state.ini = c(parent = 100), fixed_initials = "parent", quiet = TRUE) +f_saem_p0_fixed <- saem(f_mmkin_parent_p0_fixed) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:14:50 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:14:52 2020"+f_mmkin_parent <- mmkin(c("SFO", "FOMC", "DFOP"), ds, quiet = TRUE) +f_saem_sfo <- saem(f_mmkin_parent["SFO", ]) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:14:53 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:14:55 2020"f_saem_fomc <- saem(f_mmkin_parent["FOMC", ]) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:14:55 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:14:57 2020"f_saem_dfop <- saem(f_mmkin_parent["DFOP", ]) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:14:57 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:15:00 2020"+# The returned saem.mmkin object contains an SaemixObject, we can use +# functions from saemix +library(saemix) +#> Package saemix, version 3.1.9000 +#> please direct bugs, questions and feedback to emmanuelle.comets@inserm.fr#> Likelihoods computed by importance sampling#> AIC BIC +#> 1 624.2428 622.2900 +#> 2 467.7644 465.0305 +#> 3 491.3541 487.8391+f_mmkin_parent_tc <- update(f_mmkin_parent, error_model = "tc") +f_saem_fomc_tc <- saem(f_mmkin_parent_tc["FOMC", ]) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:15:02 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:15:07 2020"#> Likelihoods computed by importance sampling#> AIC BIC +#> 1 467.7644 465.0305 +#> 2 469.4862 466.3617#> Successfully compiled differential equation model from auto-generated C code.f_mmkin <- mmkin(list("DFOP-SFO" = dfop_sfo), ds, quiet = TRUE, solution_type = "analytical") +# This takes about 4 minutes on my system +f_saem <- saem(f_mmkin) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:15:08 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:19:07 2020"+f_mmkin_des <- mmkin(list("DFOP-SFO" = dfop_sfo), ds, quiet = TRUE, solution_type = "deSolve") +# Using a single core, the following takes about 6 minutes, using 10 cores +# it is slower instead of faster +f_saem_des <- saem(f_mmkin_des, cores = 1) +#> Running main SAEM algorithm +#> [1] "Sat Nov 7 13:19:26 2020" +#> .... +#> Minimisation finished +#> [1] "Sat Nov 7 13:27:33 2020"#> Error in compare.saemix(list(f_saemix$so, f_saemix_des$so)): object 'f_saemix' not found# } +
R/summary.mkinfit.R
+ Source: R/summary.mkinfit.R
summary.mkinfit.Rd
# S3 method for mkinfit -summary(object, data = TRUE, distimes = TRUE, alpha = 0.05, ...) +summary(object, data = TRUE, distimes = TRUE, alpha = 0.05, ...) # S3 method for summary.mkinfit -print(x, digits = max(3, getOption("digits") - 3), ...)+print(x, digits = max(3, getOption("digits") - 3), ...)