From d89e3d22eb9dc383897b09e9c5aa1b57f65cdbf0 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 21 Feb 2019 14:34:45 +0100 Subject: Add the logistic model --- NEWS.md | 4 +- R/endpoints.R | 8 + R/logistic.solution.R | 4 + R/mkinfit.R | 9 +- R/mkinmod.R | 20 +- R/mkinpredict.R | 8 +- R/transform_odeparms.R | 11 +- README.html | 72 ++++++- _pkgdown.yml | 1 + docs/articles/FOCUS_D.html | 10 +- docs/articles/FOCUS_L.html | 58 +++--- docs/articles/mkin.html | 2 +- docs/articles/twa.html | 2 +- docs/articles/web_only/FOCUS_Z.html | 2 +- docs/articles/web_only/compiled_models.html | 14 +- docs/news/index.html | 7 +- docs/reference/Extract.mmkin.html | 12 +- docs/reference/index.html | 6 + docs/reference/logistic.solution-1.png | Bin 0 -> 63411 bytes docs/reference/logistic.solution-2.png | Bin 0 -> 29229 bytes docs/reference/logistic.solution.html | 295 ++++++++++++++++++++++++++++ docs/reference/mccall81_245T.html | 12 +- docs/reference/mkinfit.html | 46 ++--- docs/reference/mkinmod.html | 2 +- docs/reference/mkinpredict.html | 4 +- docs/reference/mmkin.html | 4 +- docs/reference/summary.mkinfit.html | 4 +- docs/reference/transform_odeparms.html | 24 +-- man/logistic.solution.Rd | 68 +++++++ test.log | 112 ++--------- tests/testthat/test_logistic.R | 47 +++++ 31 files changed, 653 insertions(+), 215 deletions(-) create mode 100644 R/logistic.solution.R create mode 100644 docs/reference/logistic.solution-1.png create mode 100644 docs/reference/logistic.solution-2.png create mode 100644 docs/reference/logistic.solution.html create mode 100644 man/logistic.solution.Rd create mode 100644 tests/testthat/test_logistic.R diff --git a/NEWS.md b/NEWS.md index 8ca0e89c..408eddda 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# mkin 0.9.47.6 (2019-01-31) +# mkin 0.9.48.1 (2019-02-21) - Add the function 'logLik.mkinfit' which makes it possible to calculate an AIC for mkinfit objects @@ -16,6 +16,8 @@ - Add the function 'CAKE_export' to facilitate cross-checking of results +- Implement the logistic model (only tested for parent fits) + # mkin 0.9.47.5 (2018-09-14) - Make the two-component error model stop in cases where it is inadequate to avoid nls crashes on windows diff --git a/R/endpoints.R b/R/endpoints.R index ac1e3e7c..80450185 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -159,6 +159,14 @@ endpoints <- function(fit) { ep$distimes[obs_var, c(paste("DT50", obs_var, "b1", sep = "_"))] = DT50_b1 ep$distimes[obs_var, c(paste("DT50", obs_var, "b2", sep = "_"))] = DT50_b2 } + if (type == "logistic") { + # FOCUS kinetics (2014) p. 67 + kmax = parms.all["kmax"] + k0 = parms.all["k0"] + r = parms.all["r"] + DT50 = (1/r) * log(1 - ((kmax/k0) * (1 - 2^(r/kmax)))) + DT90 = (1/r) * log(1 - ((kmax/k0) * (1 - 10^(r/kmax)))) + } ep$distimes[obs_var, c("DT50", "DT90")] = c(DT50, DT90) } return(ep) diff --git a/R/logistic.solution.R b/R/logistic.solution.R new file mode 100644 index 00000000..a3bddab3 --- /dev/null +++ b/R/logistic.solution.R @@ -0,0 +1,4 @@ +logistic.solution <- function(t, parent.0, kmax, k0, r) +{ + parent = parent.0 * (kmax / (kmax - k0 + k0 * exp (r * t))) ^(kmax/r) +} diff --git a/R/mkinfit.R b/R/mkinfit.R index b27f67b4..40413125 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -1,4 +1,4 @@ -# Copyright (C) 2010-2018 Johannes Ranke +# Copyright (C) 2010-2019 Johannes Ranke # Portions of this code are copyright (C) 2013 Eurofins Regulatory AG # Contact: jranke@uni-bremen.de # The summary function is an adapted and extended version of summary.modFit @@ -48,7 +48,7 @@ mkinfit <- function(mkinmod, observed, { # Check mkinmod and generate a model for the variable whith the highest value # if a suitable string is given - parent_models_available = c("SFO", "FOMC", "DFOP", "HS", "SFORB", "IORE") + parent_models_available = c("SFO", "FOMC", "DFOP", "HS", "SFORB", "IORE", "logistic") if (class(mkinmod) != "mkinmod") { presumed_parent_name = observed[which.max(observed$value), "name"] if (mkinmod[[1]] %in% parent_models_available) { @@ -153,6 +153,9 @@ mkinfit <- function(mkinmod, observed, if (parmname == "k2") parms.ini[parmname] = 0.01 if (parmname == "tb") parms.ini[parmname] = 5 if (parmname == "g") parms.ini[parmname] = 0.5 + if (parmname == "kmax") parms.ini[parmname] = 0.1 + if (parmname == "k0") parms.ini[parmname] = 0.0001 + if (parmname == "r") parms.ini[parmname] = 0.2 } # Default values for formation fractions in case they are present for (box in mod_vars) { @@ -376,7 +379,7 @@ mkinfit <- function(mkinmod, observed, lower[index_k] <- 0 index_k__iore <- grep("^k__iore_", names(lower)) lower[index_k__iore] <- 0 - other_rate_parms <- intersect(c("alpha", "beta", "k1", "k2", "tb"), names(lower)) + other_rate_parms <- intersect(c("alpha", "beta", "k1", "k2", "tb", "r"), names(lower)) lower[other_rate_parms] <- 0 } diff --git a/R/mkinmod.R b/R/mkinmod.R index 491b3d0a..2805ef54 100644 --- a/R/mkinmod.R +++ b/R/mkinmod.R @@ -1,4 +1,4 @@ -# Copyright (C) 2010-2015 Johannes Ranke {{{ +# Copyright (C) 2010-2015,2019 Johannes Ranke {{{ # Contact: jranke@uni-bremen.de # This file is part of the R package mkin @@ -42,8 +42,8 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb parms <- vector() # }}} - # Do not return a coefficient matrix mat when FOMC, IORE, DFOP or HS is used for the parent {{{ - if(spec[[1]]$type %in% c("FOMC", "IORE", "DFOP", "HS")) { + # Do not return a coefficient matrix mat when FOMC, IORE, DFOP, HS or logistic is used for the parent {{{ + if(spec[[1]]$type %in% c("FOMC", "IORE", "DFOP", "HS", "logistic")) { mat = FALSE } else mat = TRUE #}}} @@ -57,10 +57,10 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb # Check the type component of the compartment specification {{{ if(is.null(spec[[varname]]$type)) stop( "Every part of the model specification must be a list containing a type component") - if(!spec[[varname]]$type %in% c("SFO", "FOMC", "IORE", "DFOP", "HS", "SFORB")) stop( - "Available types are SFO, FOMC, IORE, DFOP, HS and SFORB only") - if(spec[[varname]]$type %in% c("FOMC", "DFOP", "HS") & match(varname, obs_vars) != 1) { - stop(paste("Types FOMC, DFOP and HS are only implemented for the first compartment,", + if(!spec[[varname]]$type %in% c("SFO", "FOMC", "IORE", "DFOP", "HS", "SFORB", "logistic")) stop( + "Available types are SFO, FOMC, IORE, DFOP, HS, SFORB and logistic only") + if(spec[[varname]]$type %in% c("FOMC", "DFOP", "HS", "logistic") & match(varname, obs_vars) != 1) { + stop(paste("Types FOMC, DFOP, HS and logistic are only implemented for the first compartment,", "which is assumed to be the source compartment")) } #}}} @@ -71,6 +71,7 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb IORE = varname, DFOP = varname, HS = varname, + logistic = varname, SFORB = paste(varname, c("free", "bound"), sep = "_") ) map[[varname]] <- new_boxes @@ -141,6 +142,11 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb decline_term <- paste(HS_decline, "*", box_1) parms <- c(parms, "k1", "k2", "tb") } #}}} + if(spec[[varname]]$type == "logistic") { # {{{ Add logistic decline term + # From p. 67 of the FOCUS kinetics report (2014) + decline_term <- paste("(k0 * kmax)/(k0 + (kmax - k0) * exp(-r * time)) *", box_1) + parms <- c(parms, "kmax", "k0", "r") + } #}}} # Add origin decline term to box 1 (usually the only box, unless type is SFORB)#{{{ diffs[[box_1]] <- paste(diffs[[box_1]], "-", decline_term)#}}} if(spec[[varname]]$type == "SFORB") { # {{{ Add SFORB reversible binding terms diff --git a/R/mkinpredict.R b/R/mkinpredict.R index 8e0823a8..c36d724a 100644 --- a/R/mkinpredict.R +++ b/R/mkinpredict.R @@ -1,4 +1,4 @@ -# Copyright (C) 2010-2016,2018 Johannes Ranke +# Copyright (C) 2010-2016,2018,2019 Johannes Ranke # Some lines in this code are copyright (C) 2013 Eurofins Regulatory AG # Contact: jranke@uni-bremen.de @@ -83,7 +83,11 @@ mkinpredict.mkinmod <- function(x, evalparse(parent.name), evalparse(paste("k", parent.name, "bound", sep="_")), evalparse(paste("k", sub("free", "bound", parent.name), "free", sep="_")), - evalparse(paste("k", parent.name, "sink", sep="_"))) + evalparse(paste("k", parent.name, "sink", sep="_"))), + logistic = logistic.solution(outtimes, + evalparse(parent.name), + evalparse("kmax"), evalparse("k0"), + evalparse("r")) ) out <- data.frame(outtimes, o) names(out) <- c("time", sub("_free", "", parent.name)) diff --git a/R/transform_odeparms.R b/R/transform_odeparms.R index c871c52a..f69f4ebd 100644 --- a/R/transform_odeparms.R +++ b/R/transform_odeparms.R @@ -1,4 +1,4 @@ -# Copyright (C) 2010-2014 Johannes Ranke +# Copyright (C) 2010-2014,2019 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package mkin @@ -71,8 +71,9 @@ transform_odeparms <- function(parms, mkinmod, } # Transform also FOMC parameters alpha and beta, DFOP and HS rates k1 and k2 - # and HS parameter tb if transformation of rates is requested - for (pname in c("alpha", "beta", "k1", "k2", "tb")) { + # and HS parameter tb as well as logistic model parameters kmax, k0 and r if + # transformation of rates is requested + for (pname in c("alpha", "beta", "k1", "k2", "tb", "kmax", "k0", "r")) { if (!is.na(parms[pname])) { if (transform_rates) { transparms[paste0("log_", pname)] <- log(parms[pname]) @@ -151,8 +152,8 @@ backtransform_odeparms <- function(transparms, mkinmod, } } - # Transform parameters also for FOMC, DFOP and HS models - for (pname in c("alpha", "beta", "k1", "k2", "tb")) { + # Transform parameters also for FOMC, DFOP, HS and logistic models + for (pname in c("alpha", "beta", "k1", "k2", "tb", "kmax", "k0", "r")) { if (transform_rates) { pname_trans = paste0("log_", pname) if (!is.na(transparms[pname_trans])) { diff --git a/README.html b/README.html index 411eea10..800ed3cb 100644 --- a/README.html +++ b/README.html @@ -11,7 +11,7 @@ -README.utf8 +README.utf8.md @@ -297,7 +364,6 @@ $(document).ready(function () { -

summary(m.L2.DFOP, data = FALSE)
-
## mkin version used for fitting:    0.9.47.5 
+
## mkin version used for fitting:    0.9.47.6 
 ## R version used for fitting:       3.5.2 
-## Date of fit:     Thu Jan 31 16:52:43 2019 
-## Date of summary: Thu Jan 31 16:52:43 2019 
+## Date of fit:     Thu Feb 21 14:32:09 2019 
+## Date of summary: Thu Feb 21 14:32:09 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) *
@@ -378,7 +378,7 @@
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 336 model solutions performed in 0.839 s
+## Fitted with method Port using 336 model solutions performed in 0.86 s
 ## 
 ## Weighting: none
 ## 
@@ -458,10 +458,10 @@
 

The objects returned by mmkin are arranged like a matrix, with models as a row index and datasets as a column index.

We can extract the summary and plot for e.g. the DFOP fit, using square brackets for indexing which will result in the use of the summary and plot functions working on mkinfit objects.

summary(mm.L3[["DFOP", 1]])
-
## mkin version used for fitting:    0.9.47.5 
+
## mkin version used for fitting:    0.9.47.6 
 ## R version used for fitting:       3.5.2 
-## Date of fit:     Thu Jan 31 16:52:44 2019 
-## Date of summary: Thu Jan 31 16:52:44 2019 
+## Date of fit:     Thu Feb 21 14:32:10 2019 
+## Date of summary: Thu Feb 21 14:32:11 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) *
@@ -470,7 +470,7 @@
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 137 model solutions performed in 0.341 s
+## Fitted with method Port using 137 model solutions performed in 0.351 s
 ## 
 ## Weighting: none
 ## 
@@ -559,17 +559,17 @@
 

The \(\chi^2\) error level of 3.3% as well as the plot suggest that the SFO model fits very well. The error level at which the \(\chi^2\) test passes is slightly lower for the FOMC model. However, the difference appears negligible.

summary(mm.L4[["SFO", 1]], data = FALSE)
-
## mkin version used for fitting:    0.9.47.5 
+
## mkin version used for fitting:    0.9.47.6 
 ## R version used for fitting:       3.5.2 
-## Date of fit:     Thu Jan 31 16:52:44 2019 
-## Date of summary: Thu Jan 31 16:52:45 2019 
+## Date of fit:     Thu Feb 21 14:32:11 2019 
+## Date of summary: Thu Feb 21 14:32:11 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 46 model solutions performed in 0.11 s
+## Fitted with method Port using 46 model solutions performed in 0.113 s
 ## 
 ## Weighting: none
 ## 
@@ -619,17 +619,17 @@
 ##        DT50 DT90
 ## parent  106  352
summary(mm.L4[["FOMC", 1]], data = FALSE)
-
## mkin version used for fitting:    0.9.47.5 
+
## mkin version used for fitting:    0.9.47.6 
 ## R version used for fitting:       3.5.2 
-## Date of fit:     Thu Jan 31 16:52:45 2019 
-## Date of summary: Thu Jan 31 16:52:45 2019 
+## Date of fit:     Thu Feb 21 14:32:11 2019 
+## Date of summary: Thu Feb 21 14:32:11 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 66 model solutions performed in 0.162 s
+## Fitted with method Port using 66 model solutions performed in 0.168 s
 ## 
 ## Weighting: none
 ## 
diff --git a/docs/articles/mkin.html b/docs/articles/mkin.html
index 488ca302..c5b0d9c1 100644
--- a/docs/articles/mkin.html
+++ b/docs/articles/mkin.html
@@ -85,7 +85,7 @@
       

Introduction to mkin

Johannes Ranke

-

2019-01-31

+

2019-02-21

diff --git a/docs/articles/twa.html b/docs/articles/twa.html index d50b23ba..8a1f82a8 100644 --- a/docs/articles/twa.html +++ b/docs/articles/twa.html @@ -85,7 +85,7 @@

Calculation of time weighted average concentrations with mkin

Johannes Ranke

-

2019-01-31

+

2019-02-21

diff --git a/docs/articles/web_only/FOCUS_Z.html b/docs/articles/web_only/FOCUS_Z.html index 84bd971f..c55abdbc 100644 --- a/docs/articles/web_only/FOCUS_Z.html +++ b/docs/articles/web_only/FOCUS_Z.html @@ -85,7 +85,7 @@

Example evaluation of FOCUS dataset Z

Johannes Ranke

-

2019-01-31

+

2019-02-21

diff --git a/docs/articles/web_only/compiled_models.html b/docs/articles/web_only/compiled_models.html index 08bb9b44..3a37d114 100644 --- a/docs/articles/web_only/compiled_models.html +++ b/docs/articles/web_only/compiled_models.html @@ -85,7 +85,7 @@

Performance benefit by using compiled model definitions in mkin

Johannes Ranke

-

2019-01-31

+

2019-02-21

@@ -126,9 +126,9 @@ }
## Lade nötiges Paket: rbenchmark
##                    test replications elapsed relative user.self sys.self
-## 3     deSolve, compiled            3   2.353    1.000     2.352        0
-## 1 deSolve, not compiled            3  17.619    7.488    17.609        0
-## 2      Eigenvalue based            3   2.899    1.232     2.898        0
+## 3     deSolve, compiled            3   2.429    1.000     2.427        0
+## 1 deSolve, not compiled            3  17.826    7.339    17.815        0
+## 2      Eigenvalue based            3   2.968    1.222     2.967        0
 ##   user.child sys.child
 ## 3          0         0
 ## 1          0         0
@@ -157,13 +157,13 @@
 }
## Successfully compiled differential equation model from auto-generated C code.
##                    test replications elapsed relative user.self sys.self
-## 2     deSolve, compiled            3   4.180    1.000     4.177        0
-## 1 deSolve, not compiled            3  37.331    8.931    37.312        0
+## 2     deSolve, compiled            3   4.234    1.000     4.232        0
+## 1 deSolve, not compiled            3  37.807    8.929    37.785        0
 ##   user.child sys.child
 ## 2          0         0
 ## 1          0         0

Here we get a performance benefit of a factor of 9 using the version of the differential equation model compiled from C code!

-

This vignette was built with mkin 0.9.47.5 on

+

This vignette was built with mkin 0.9.47.6 on

## R version 3.5.2 (2018-12-20)
 ## Platform: x86_64-pc-linux-gnu (64-bit)
 ## Running under: Debian GNU/Linux 9 (stretch)
diff --git a/docs/news/index.html b/docs/news/index.html index 7736a28d..4a4d71fe 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -119,9 +119,9 @@ -
+

-mkin 0.9.47.6 (2019-01-31) Unreleased +mkin 0.9.48.1 (2019-02-21) Unreleased

  • Add the function ‘logLik.mkinfit’ which makes it possible to calculate an AIC for mkinfit objects

  • @@ -132,6 +132,7 @@
  • ‘mkinfit’: Improve the correctness of the fitted two component error model by fitting the mean absolute deviance at each observation against the observed values, weighting with the current two-component error model

  • ‘tests/testthat/test_irls.R’: Test if the components of the error model used to generate the data can be reproduced with moderate accuracy

  • Add the function ‘CAKE_export’ to facilitate cross-checking of results

  • +
  • Implement the logistic model (only tested for parent fits)

@@ -675,7 +676,7 @@

Contents

#> Successfully compiled differential equation model from auto-generated C code.
fit.1 <- mkinfit(SFO_SFO_SFO, subset(mccall81_245T, soil == "Commerce"), quiet = TRUE) summary(fit.1, data = FALSE)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:05 2019 -#> Date of summary: Thu Jan 31 16:51:05 2019 +#> Date of fit: Thu Feb 21 14:30:31 2019 +#> Date of summary: Thu Feb 21 14:30:31 2019 #> #> Equations: #> d_T245/dt = - k_T245_sink * T245 - k_T245_phenol * T245 @@ -171,7 +171,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 574 model solutions performed in 3.87 s +#> Fitted with method Port using 574 model solutions performed in 3.822 s #> #> Weighting: none #> @@ -249,8 +249,8 @@ fixed_parms = "k_phenol_sink", quiet = TRUE) summary(fit.2, data = FALSE)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:07 2019 -#> Date of summary: Thu Jan 31 16:51:07 2019 +#> Date of fit: Thu Feb 21 14:30:32 2019 +#> Date of summary: Thu Feb 21 14:30:32 2019 #> #> Equations: #> d_T245/dt = - k_T245_sink * T245 - k_T245_phenol * T245 @@ -260,7 +260,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 246 model solutions performed in 1.618 s +#> Fitted with method Port using 246 model solutions performed in 1.601 s #> #> Weighting: none #> diff --git a/docs/reference/mkinfit.html b/docs/reference/mkinfit.html index 42845d68..d09b9232 100644 --- a/docs/reference/mkinfit.html +++ b/docs/reference/mkinfit.html @@ -438,15 +438,15 @@ fit <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE) summary(fit)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:10 2019 -#> Date of summary: Thu Jan 31 16:51:10 2019 +#> Date of fit: Thu Feb 21 14:30:35 2019 +#> Date of summary: Thu Feb 21 14:30:35 2019 #> #> Equations: #> d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent #> #> Model predictions using solution type analytical #> -#> Fitted with method Port using 64 model solutions performed in 0.162 s +#> Fitted with method Port using 64 model solutions performed in 0.16 s #> #> Weighting: none #> @@ -515,7 +515,7 @@ m1 = mkinsub("SFO"))
#> Successfully compiled differential equation model from auto-generated C code.
# Fit the model to the FOCUS example dataset D using defaults print(system.time(fit <- mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet = TRUE)))
#> User System verstrichen -#> 1.022 0.000 1.025
coef(fit)
#> parent_0 log_k_parent_sink log_k_parent_m1 log_k_m1_sink +#> 1.017 0.004 1.022
coef(fit)
#> parent_0 log_k_parent_sink log_k_parent_m1 log_k_m1_sink #> 99.59848 -3.03822 -2.98030 -5.24750
#> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 @@ -590,7 +590,7 @@ #> Model cost at call 146 : 371.2134 #> Optimisation by method Port successfully terminated. #> User System verstrichen -#> 0.823 0.000 0.823
coef(fit.deSolve)
#> parent_0 log_k_parent_sink log_k_parent_m1 log_k_m1_sink +#> 0.845 0.000 0.846
coef(fit.deSolve)
#> parent_0 log_k_parent_sink log_k_parent_m1 log_k_m1_sink #> 99.59848 -3.03822 -2.98030 -5.24750
endpoints(fit.deSolve)
#> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 @@ -628,8 +628,8 @@ m1 = mkinsub("SFO"), use_of_ff = "max")
#> Successfully compiled differential equation model from auto-generated C code.
f.noweight <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE) summary(f.noweight)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:22 2019 -#> Date of summary: Thu Jan 31 16:51:22 2019 +#> Date of fit: Thu Feb 21 14:30:47 2019 +#> Date of summary: Thu Feb 21 14:30:47 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -637,7 +637,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 186 model solutions performed in 0.872 s +#> Fitted with method Port using 186 model solutions performed in 0.86 s #> #> Weighting: none #> @@ -745,8 +745,8 @@ #> 120 m1 33.31 28.78984 4.520e+00
f.irls <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, reweight.method = "obs", quiet = TRUE) summary(f.irls)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:24 2019 -#> Date of summary: Thu Jan 31 16:51:24 2019 +#> Date of fit: Thu Feb 21 14:30:50 2019 +#> Date of summary: Thu Feb 21 14:30:50 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -754,7 +754,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 551 model solutions performed in 2.558 s +#> Fitted with method Port using 551 model solutions performed in 2.529 s #> #> Weighting: none #> @@ -867,8 +867,8 @@ #> 120 m1 33.31 28.80898 4.501e+00 2.722
f.w.mean <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, weight = "mean", quiet = TRUE) summary(f.w.mean)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:25 2019 -#> Date of summary: Thu Jan 31 16:51:25 2019 +#> Date of fit: Thu Feb 21 14:30:51 2019 +#> Date of summary: Thu Feb 21 14:30:51 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -876,7 +876,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 155 model solutions performed in 0.711 s +#> Fitted with method Port using 155 model solutions performed in 0.707 s #> #> Weighting: mean #> @@ -985,8 +985,8 @@ quiet = TRUE) summary(f.w.value)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:26 2019 -#> Date of summary: Thu Jan 31 16:51:26 2019 +#> Date of fit: Thu Feb 21 14:30:52 2019 +#> Date of summary: Thu Feb 21 14:30:52 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -994,7 +994,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 174 model solutions performed in 0.807 s +#> Fitted with method Port using 174 model solutions performed in 0.792 s #> #> Weighting: manual #> @@ -1105,8 +1105,8 @@ f.w.man <- mkinfit(SFO_SFO.ff, dw, err = "err.man", quiet = TRUE) summary(f.w.man)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:28 2019 -#> Date of summary: Thu Jan 31 16:51:28 2019 +#> Date of fit: Thu Feb 21 14:30:53 2019 +#> Date of summary: Thu Feb 21 14:30:53 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -1114,7 +1114,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 270 model solutions performed in 1.257 s +#> Fitted with method Port using 270 model solutions performed in 1.243 s #> #> Weighting: manual #> @@ -1223,8 +1223,8 @@ reweight.method = "obs") summary(f.w.man.irls)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:51:31 2019 -#> Date of summary: Thu Jan 31 16:51:31 2019 +#> Date of fit: Thu Feb 21 14:30:57 2019 +#> Date of summary: Thu Feb 21 14:30:57 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -1232,7 +1232,7 @@ #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 692 model solutions performed in 3.3 s +#> Fitted with method Port using 692 model solutions performed in 3.27 s #> #> Weighting: manual #> diff --git a/docs/reference/mkinmod.html b/docs/reference/mkinmod.html index 84b335f7..0710e6de 100644 --- a/docs/reference/mkinmod.html +++ b/docs/reference/mkinmod.html @@ -231,7 +231,7 @@ For the definition of model types and their parameters, the equations given SFO_SFO <- mkinmod( parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"), verbose = TRUE)
#> Compilation argument: -#> /usr/lib/R/bin/R CMD SHLIB file165a4bc987b7.c 2> file165a4bc987b7.c.err.txt +#> /usr/lib/R/bin/R CMD SHLIB file33652d0fd552.c 2> file33652d0fd552.c.err.txt #> Program source: #> 1: #include <R.h> #> 2: diff --git a/docs/reference/mkinpredict.html b/docs/reference/mkinpredict.html index c34da511..8c5bf22b 100644 --- a/docs/reference/mkinpredict.html +++ b/docs/reference/mkinpredict.html @@ -325,7 +325,7 @@ c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "eigen")[201,]))
#> time parent m1 #> 201 20 4.978707 27.46227
#> User System verstrichen -#> 0.003 0.000 0.003
system.time( +#> 0.004 0.000 0.004
system.time( print(mkinpredict(SFO_SFO, c(k_parent_m1 = 0.05, k_parent_sink = 0.1, k_m1_sink = 0.01), c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "deSolve")[201,]))
#> time parent m1 @@ -335,7 +335,7 @@ c(parent = 100, m1 = 0), seq(0, 20, by = 0.1), solution_type = "deSolve", use_compiled = FALSE)[201,]))
#> time parent m1 #> 201 20 4.978707 27.46227
#> User System verstrichen -#> 0.042 0.000 0.042
+#> 0.047 0.000 0.047
# Predict from a fitted model f <- mkinfit(SFO_SFO, FOCUS_2006_C)
#> Model cost at call 1 : 552.5739 #> Model cost at call 3 : 552.5739 diff --git a/docs/reference/mmkin.html b/docs/reference/mmkin.html index 3fa5c9a5..75da28c7 100644 --- a/docs/reference/mmkin.html +++ b/docs/reference/mmkin.html @@ -191,8 +191,8 @@ time_1 <- system.time(fits.4 <- mmkin(models, datasets, cores = 1, quiet = TRUE)) time_default
#> User System verstrichen -#> 0.045 0.036 7.264
time_1
#> User System verstrichen -#> 22.905 0.000 22.919
+#> 0.039 0.040 7.082
time_1
#> User System verstrichen +#> 22.650 0.004 22.666
endpoints(fits.0[["SFO_lin", 2]])
#> $ff #> parent_M1 parent_sink M1_M2 M1_sink #> 0.7340480 0.2659520 0.7505686 0.2494314 diff --git a/docs/reference/summary.mkinfit.html b/docs/reference/summary.mkinfit.html index 2815eccb..242d9f63 100644 --- a/docs/reference/summary.mkinfit.html +++ b/docs/reference/summary.mkinfit.html @@ -208,8 +208,8 @@

Examples

summary(mkinfit(mkinmod(parent = mkinsub("SFO")), FOCUS_2006_A, quiet = TRUE))
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:52:23 2019 -#> Date of summary: Thu Jan 31 16:52:23 2019 +#> Date of fit: Thu Feb 21 14:31:48 2019 +#> Date of summary: Thu Feb 21 14:31:48 2019 #> #> Equations: #> d_parent/dt = - k_parent_sink * parent diff --git a/docs/reference/transform_odeparms.html b/docs/reference/transform_odeparms.html index d3c20ed8..939fa7b4 100644 --- a/docs/reference/transform_odeparms.html +++ b/docs/reference/transform_odeparms.html @@ -202,8 +202,8 @@ The transformation of sets of formation fractions is fragile, as it supposes fit <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE) summary(fit, data=FALSE) # See transformed and backtransformed parameters
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:52:31 2019 -#> Date of summary: Thu Jan 31 16:52:31 2019 +#> Date of fit: Thu Feb 21 14:31:57 2019 +#> Date of summary: Thu Feb 21 14:31:57 2019 #> #> Equations: #> d_parent/dt = - k_parent_sink * parent - k_parent_m1 * parent @@ -211,7 +211,7 @@ The transformation of sets of formation fractions is fragile, as it supposes #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 153 model solutions performed in 0.703 s +#> Fitted with method Port using 153 model solutions performed in 0.849 s #> #> Weighting: none #> @@ -278,8 +278,8 @@ The transformation of sets of formation fractions is fragile, as it supposes
fit.2 <- mkinfit(SFO_SFO, FOCUS_2006_D, transform_rates = FALSE, quiet = TRUE) summary(fit.2, data=FALSE)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:52:33 2019 -#> Date of summary: Thu Jan 31 16:52:33 2019 +#> Date of fit: Thu Feb 21 14:31:59 2019 +#> Date of summary: Thu Feb 21 14:31:59 2019 #> #> Equations: #> d_parent/dt = - k_parent_sink * parent - k_parent_m1 * parent @@ -287,7 +287,7 @@ The transformation of sets of formation fractions is fragile, as it supposes #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 350 model solutions performed in 1.597 s +#> Fitted with method Port using 350 model solutions performed in 1.649 s #> #> Weighting: none #> @@ -366,8 +366,8 @@ The transformation of sets of formation fractions is fragile, as it supposes fit.ff <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE) summary(fit.ff, data = FALSE)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:52:34 2019 -#> Date of summary: Thu Jan 31 16:52:34 2019 +#> Date of fit: Thu Feb 21 14:32:00 2019 +#> Date of summary: Thu Feb 21 14:32:00 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -375,7 +375,7 @@ The transformation of sets of formation fractions is fragile, as it supposes #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 186 model solutions performed in 0.898 s +#> Fitted with method Port using 186 model solutions performed in 0.976 s #> #> Weighting: none #> @@ -450,8 +450,8 @@ The transformation of sets of formation fractions is fragile, as it supposes fit.ff.2 <- mkinfit(SFO_SFO.ff.2, FOCUS_2006_D, quiet = TRUE) summary(fit.ff.2, data = FALSE)
#> mkin version used for fitting: 0.9.47.6 #> R version used for fitting: 3.5.2 -#> Date of fit: Thu Jan 31 16:52:35 2019 -#> Date of summary: Thu Jan 31 16:52:35 2019 +#> Date of fit: Thu Feb 21 14:32:01 2019 +#> Date of summary: Thu Feb 21 14:32:01 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -459,7 +459,7 @@ The transformation of sets of formation fractions is fragile, as it supposes #> #> Model predictions using solution type deSolve #> -#> Fitted with method Port using 104 model solutions performed in 0.482 s +#> Fitted with method Port using 104 model solutions performed in 0.512 s #> #> Weighting: none #> diff --git a/man/logistic.solution.Rd b/man/logistic.solution.Rd new file mode 100644 index 00000000..798e78d1 --- /dev/null +++ b/man/logistic.solution.Rd @@ -0,0 +1,68 @@ +\name{logistic.solution} +\alias{logistic.solution} +\title{ Logistic kinetics } +\description{ + Function describing exponential decline from a defined starting value, with + an increasing rate constant, supposedly caused by microbial growth +} +\usage{ +logistic.solution(t, parent.0, kmax, k0, r) +} +\arguments{ + \item{t}{ Time. } + \item{parent.0}{ Starting value for the response variable at time zero. } + \item{kmax}{ Maximum rate constant. } + \item{k0}{ Minumum rate constant effective at time zero. } + \item{r}{ Growth rate of the increase in the rate constant. } +} +\note{ + The solution of the logistic model reduces to the + \code{\link{SFO.solution}} if \code{k0} is equal to + \code{kmax}. +} +\value{ + The value of the response variable at time \code{t}. +} +\references{ + FOCUS (2014) \dQuote{Generic guidance for Estimating Persistence and + Degradation Kinetics from Environmental Fate Studies on Pesticides in EU + Registration} Report of the FOCUS Work Group on Degradation Kinetics, + Version 1.1, 18 December 2014 + \url{http://esdac.jrc.ec.europa.eu/projects/degradation-kinetics} +} +\examples{ + # Reproduce the plot on page 57 of FOCUS (2014) + plot(function(x) logistic.solution(x, 100, 0.08, 0.0001, 0.2), + from = 0, to = 100, ylim = c(0, 100), + xlab = "Time", ylab = "Residue") + plot(function(x) logistic.solution(x, 100, 0.08, 0.0001, 0.4), + from = 0, to = 100, add = TRUE, lty = 2, col = 2) + plot(function(x) logistic.solution(x, 100, 0.08, 0.0001, 0.8), + from = 0, to = 100, add = TRUE, lty = 3, col = 3) + plot(function(x) logistic.solution(x, 100, 0.08, 0.001, 0.2), + from = 0, to = 100, add = TRUE, lty = 4, col = 4) + plot(function(x) logistic.solution(x, 100, 0.08, 0.08, 0.2), + from = 0, to = 100, add = TRUE, lty = 5, col = 5) + legend("topright", inset = 0.05, + legend = paste0("k0 = ", c(0.0001, 0.0001, 0.0001, 0.001, 0.08), + ", r = ", c(0.2, 0.4, 0.8, 0.2, 0.2)), + lty = 1:5, col = 1:5) + + # Fit with synthetic data + logistic <- mkinmod(parent = mkinsub("logistic")) + + sampling_times = c(0, 1, 3, 7, 14, 28, 60, 90, 120) + parms_logistic <- c(kmax = 0.08, k0 = 0.0001, r = 0.2) + parms_logistic_optim <- c(parent_0 = 100, parms_logistic) + d_logistic <- mkinpredict(logistic, + parms_logistic, c(parent = 100), + sampling_times) + d_2_1 <- add_err(d_logistic, + sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07), + n = 1, reps = 2, digits = 5, LOD = 0.1, seed = 123456)[[1]] + + m <- mkinfit("logistic", d_2_1) + plot_sep(m) + summary(m)$bpar +} +\keyword{ manip } diff --git a/test.log b/test.log index 7754d6ad..5a71534f 100644 --- a/test.log +++ b/test.log @@ -1,102 +1,28 @@ Loading mkin -Loading required package: testthat Testing mkin ✔ | OK F W S | Context - ⠏ | 0 | Calculation of FOCUS chi2 error levels ⠋ | 1 | Calculation of FOCUS chi2 error levels ⠙ | 2 | Calculation of FOCUS chi2 error levels ✔ | 2 | Calculation of FOCUS chi2 error levels [2.2 s] - ⠏ | 0 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠋ | 1 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠙ | 2 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠹ | 3 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠸ | 4 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠼ | 5 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠴ | 6 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠦ | 7 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠧ | 8 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ✔ | 8 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [6.5 s] - ⠏ | 0 | Iteratively reweighted least squares (IRLS) fitting ⠋ | 0 1 | Iteratively reweighted least squares (IRLS) fitting ⠙ | 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠸ | 1 1 2 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠸ | 1 1 2 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠼ | 1 1 3 | Iteratively reweighted least squares (IRLS) fitting ⠴ | 1 1 4 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 1 1 1 | Iteratively reweighted least squares (IRLS) fitting ⠹ | 2 1 | Iteratively reweighted least squares (IRLS) fitting ⠸ | 3 1 | Iteratively reweighted least squares (IRLS) fitting ⠼ | 4 1 | Iteratively reweighted least squares (IRLS) fitting ⠴ | 5 1 | Iteratively reweighted least squares (IRLS) fitting ⠦ | 6 1 | Iteratively reweighted least squares (IRLS) fitting ⠧ | 7 1 | Iteratively reweighted least squares (IRLS) fitting ✖ | 7 1 | Iteratively reweighted least squares (IRLS) fitting [172.0 s] + ⠏ | 0 | Calculation of FOCUS chi2 error levels ⠋ | 1 | Calculation of FOCUS chi2 error levels ⠙ | 2 | Calculation of FOCUS chi2 error levels ✔ | 2 | Calculation of FOCUS chi2 error levels [2.5 s] + ⠏ | 0 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠋ | 1 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠙ | 2 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠹ | 3 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠸ | 4 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠼ | 5 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠴ | 6 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠦ | 7 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ⠧ | 8 | Results for FOCUS D established in expertise for UBA (Ranke 2014) ✔ | 8 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [7.2 s] + ⠏ | 0 | Iteratively reweighted least squares (IRLS) fitting ⠋ | 1 | Iteratively reweighted least squares (IRLS) fitting ⠙ | 1 1 | Iteratively reweighted least squares (IRLS) fitting ✔ | 1 1 | Iteratively reweighted least squares (IRLS) fitting [9.0 s] ──────────────────────────────────────────────────────────────────────────────── -test_irls.R:38: error: Reweighting method 'obs' works -Objekt 'tc_fit' nicht gefunden -1: mkinfit(m_synth_SFO_lin, SFO_lin_a, reweight.method = "obs", quiet = TRUE) at /home/jranke/git/mkin/tests/testthat/test_irls.R:38 -2: system.time({ - fit <- modFit(cost, c(state.ini.optim, transparms.optim), method = method.modFit, - control = control.modFit, lower = lower, upper = upper, ...) - if (!is.null(reweight.method)) { - if (!reweight.method %in% c("obs", "tc")) - stop("Only reweighting methods 'obs' and 'tc' are implemented") - if (reweight.method == "obs") { - if (!quiet) { - cat("IRLS based on variance estimates for each observed variable\n") - cat("Initial variance estimates are:\n") - print(signif(fit$var_ms_unweighted, 8)) - } - } - if (reweight.method == "tc") { - tc_fit <- fit_error_model_mad_obs(cost(fit$par)$residuals, tc, 0) - if (is.character(tc_fit)) { - if (!quiet) { - cat(tc_fit, ".\n", "No reweighting will be performed.") - } - tc_fitted <- c(sigma_low = NA, rsd_high = NA) - } - else { - tc_fitted <- coef(tc_fit) - if (!quiet) { - cat("IRLS based on variance estimates according to the two component error model\n") - cat("Initial variance components are:\n") - print(signif(tc_fitted)) - } - } - } - reweight.diff = 1 - n.iter <- 0 - if (!is.null(err)) - observed$err.ini <- observed[[err]] - err = "err.irls" - while (reweight.diff > reweight.tol & n.iter < reweight.max.iter & !is.character(tc_fit)) { - n.iter <- n.iter + 1 - if (reweight.method == "obs") { - sr_old <- fit$var_ms_unweighted - observed[err] <- sqrt(fit$var_ms_unweighted[as.character(observed$name)]) - } - if (reweight.method == "tc") { - sr_old <- tc_fitted - tmp_predicted <- mkin_wide_to_long(out_predicted, time = "time") - tmp_data <- suppressMessages(join(observed, tmp_predicted, by = c("time", - "name"))) - observed[err] <- predict(tc_fit, newdata = data.frame(obs = observed$value)) - } - fit <- modFit(cost, fit$par, method = method.modFit, control = control.modFit, - lower = lower, upper = upper, ...) - if (reweight.method == "obs") { - sr_new <- fit$var_ms_unweighted - } - if (reweight.method == "tc") { - tc_fit <- fit_error_model_mad_obs(cost(fit$par)$residuals, tc_fitted, - n.iter) - if (is.character(tc_fit)) { - if (!quiet) { - cat(tc_fit, ".\n") - } - break - } - else { - tc_fitted <- coef(tc_fit) - sr_new <- tc_fitted - } - } - reweight.diff = sum((sr_new - sr_old)^2) - if (!quiet) { - cat("Iteration", n.iter, "yields variance estimates:\n") - print(signif(sr_new, 8)) - cat("Sum of squared differences to last variance (component) estimates:", - signif(reweight.diff, 2), "\n") - } - } - } - }) at /home/jranke/git/mkin/R/mkinfit.R:396 +test_irls.R:44: skip: Reweighting method 'tc' works +Too much trouble with datasets that are randomly generated ──────────────────────────────────────────────────────────────────────────────── - ⠏ | 0 | Model predictions with mkinpredict ⠋ | 1 | Model predictions with mkinpredict ⠙ | 2 | Model predictions with mkinpredict ⠹ | 3 | Model predictions with mkinpredict ✔ | 3 | Model predictions with mkinpredict [0.4 s] - ⠏ | 0 | Fitting of parent only models ⠋ | 1 | Fitting of parent only models ⠙ | 2 | Fitting of parent only models ⠹ | 3 | Fitting of parent only models ⠸ | 4 | Fitting of parent only models ⠼ | 5 | Fitting of parent only models ⠴ | 6 | Fitting of parent only models ⠦ | 7 | Fitting of parent only models ⠧ | 8 | Fitting of parent only models ⠇ | 9 | Fitting of parent only models ⠏ | 10 | Fitting of parent only models ⠋ | 11 | Fitting of parent only models ⠙ | 12 | Fitting of parent only models ⠹ | 13 | Fitting of parent only models ⠸ | 14 | Fitting of parent only models ⠼ | 15 | Fitting of parent only models ⠴ | 16 | Fitting of parent only models ⠦ | 17 | Fitting of parent only models ⠧ | 18 | Fitting of parent only models ⠇ | 19 | Fitting of parent only models ⠏ | 20 | Fitting of parent only models ⠋ | 21 | Fitting of parent only models ✔ | 21 | Fitting of parent only models [22.1 s] - ⠏ | 0 | Complex test case from Schaefer et al. (2007) Piacenza paper ⠋ | 1 | Complex test case from Schaefer et al. (2007) Piacenza paper ⠙ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper ✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.2 s] - ⠏ | 0 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠋ | 1 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠙ | 2 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠹ | 3 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠸ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) ✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [6.5 s] - ⠏ | 0 | Calculation of maximum time weighted average concentrations (TWAs) ⠋ | 1 | Calculation of maximum time weighted average concentrations (TWAs) ⠙ | 2 | Calculation of maximum time weighted average concentrations (TWAs) ⠹ | 3 | Calculation of maximum time weighted average concentrations (TWAs) ⠸ | 4 | Calculation of maximum time weighted average concentrations (TWAs) ⠼ | 5 | Calculation of maximum time weighted average concentrations (TWAs) ⠴ | 6 | Calculation of maximum time weighted average concentrations (TWAs) ⠦ | 7 | Calculation of maximum time weighted average concentrations (TWAs) ⠧ | 8 | Calculation of maximum time weighted average concentrations (TWAs) ✔ | 8 | Calculation of maximum time weighted average concentrations (TWAs) [7.6 s] + ⠏ | 0 | Fitting the logistic model ⠋ | 1 | Fitting the logistic model ⠙ | 1 1 | Fitting the logistic model ✔ | 1 1 | Fitting the logistic model [0.5 s] +──────────────────────────────────────────────────────────────────────────────── +test_logistic.R:42: skip: The logistic fit can be done via differential equation +Skip slow fit of logistic model using deSolve without compilation +──────────────────────────────────────────────────────────────────────────────── + ⠏ | 0 | Model predictions with mkinpredict ⠋ | 1 | Model predictions with mkinpredict ⠙ | 2 | Model predictions with mkinpredict ⠹ | 3 | Model predictions with mkinpredict ✔ | 3 | Model predictions with mkinpredict [0.3 s] + ⠏ | 0 | Fitting of parent only models ⠋ | 1 | Fitting of parent only models ⠙ | 2 | Fitting of parent only models ⠹ | 3 | Fitting of parent only models ⠸ | 4 | Fitting of parent only models ⠼ | 5 | Fitting of parent only models ⠴ | 6 | Fitting of parent only models ⠦ | 7 | Fitting of parent only models ⠧ | 8 | Fitting of parent only models ⠇ | 9 | Fitting of parent only models ⠏ | 10 | Fitting of parent only models ⠋ | 11 | Fitting of parent only models ⠙ | 12 | Fitting of parent only models ⠹ | 13 | Fitting of parent only models ⠸ | 14 | Fitting of parent only models ⠼ | 15 | Fitting of parent only models ⠴ | 16 | Fitting of parent only models ⠦ | 17 | Fitting of parent only models ⠧ | 18 | Fitting of parent only models ⠇ | 19 | Fitting of parent only models ⠏ | 20 | Fitting of parent only models ⠋ | 21 | Fitting of parent only models ✔ | 21 | Fitting of parent only models [23.9 s] + ⠏ | 0 | Complex test case from Schaefer et al. (2007) Piacenza paper ⠋ | 1 | Complex test case from Schaefer et al. (2007) Piacenza paper ⠙ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper ✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [6.0 s] + ⠏ | 0 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠋ | 1 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠙ | 2 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠹ | 3 | Results for synthetic data established in expertise for UBA (Ranke 2014) ⠸ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) ✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.5 s] + ⠏ | 0 | Calculation of maximum time weighted average concentrations (TWAs) ⠋ | 1 | Calculation of maximum time weighted average concentrations (TWAs) ⠙ | 2 | Calculation of maximum time weighted average concentrations (TWAs) ⠹ | 3 | Calculation of maximum time weighted average concentrations (TWAs) ⠸ | 4 | Calculation of maximum time weighted average concentrations (TWAs) ⠼ | 5 | Calculation of maximum time weighted average concentrations (TWAs) ⠴ | 6 | Calculation of maximum time weighted average concentrations (TWAs) ⠦ | 7 | Calculation of maximum time weighted average concentrations (TWAs) ⠧ | 8 | Calculation of maximum time weighted average concentrations (TWAs) ✔ | 8 | Calculation of maximum time weighted average concentrations (TWAs) [8.4 s] ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 222.6 s +Duration: 65.5 s -OK: 55 -Failed: 1 +OK: 50 +Failed: 0 Warnings: 0 -Skipped: 0 +Skipped: 2 diff --git a/tests/testthat/test_logistic.R b/tests/testthat/test_logistic.R new file mode 100644 index 00000000..1ea1013b --- /dev/null +++ b/tests/testthat/test_logistic.R @@ -0,0 +1,47 @@ +# Copyright (C) 2019 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +context("Fitting the logistic model") + +logistic <- mkinmod(parent = mkinsub("logistic")) + +sampling_times = c(0, 1, 3, 7, 14, 28, 60, 90, 120) +parms_logistic <- c(kmax = 0.08, k0 = 0.0001, r = 0.2) +parms_logistic_optim <- c(parent_0 = 100, parms_logistic) +d_logistic <- mkinpredict(logistic, + parms_logistic, c(parent = 100), + sampling_times) +d_2_1 <- add_err(d_logistic, + sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07), + n = 1, reps = 2, digits = 5, LOD = 0.1, seed = 123456) + +test_that("The logistic model fit is reproducible", { + m <- mkinfit("logistic", d_2_1[[1]], quiet = TRUE) + dtx <- endpoints(m)$distimes["parent", ] + expect_equivalent(dtx, c(36.86533, 62.41511), tolerance = 0.00001) +}) + +test_that("The logistic fit can be done via differential equation", { + # This is slow as we did not implement conversion to C + # because it is unlikely we will use the logistic model with metabolites + skip("Skip slow fit of logistic model using deSolve without compilation") + m_2 <- mkinfit("logistic", d_2_1[[1]], solution_type = "deSolve", + quiet = TRUE) + dtx_2 <- endpoints(m_2)$distimes["parent", ] + expect_equivalent(dtx_2, c(36.86533, 62.41511), tolerance = 0.00001) +}) -- cgit v1.2.1