From cfa1340c41fe77117c3c2481ddbb25579f196bdd Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 30 Mar 2020 19:17:13 +0200 Subject: Don't check for gcc on the path in mkinmod Roxygen update -> formatting changes in Rd files --- R/mkinmod.R | 117 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 57 insertions(+), 60 deletions(-) (limited to 'R') diff --git a/R/mkinmod.R b/R/mkinmod.R index a1ae0021..cf618525 100644 --- a/R/mkinmod.R +++ b/R/mkinmod.R @@ -364,79 +364,76 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb model$coefmat <- m }#}}} - # Try to create a function compiled from C code if more than one observed {{{ - # variable and gcc is available + # Try to create a function compiled from C code if >1 observed variable {{{ if (length(obs_vars) > 1) { - if (Sys.which("gcc") != "") { - # Translate the R code for the derivatives to C code - diffs.C <- paste(diffs, collapse = ";\n") - diffs.C <- paste0(diffs.C, ";") + # Translate the R code for the derivatives to C code + diffs.C <- paste(diffs, collapse = ";\n") + diffs.C <- paste0(diffs.C, ";") - # HS - diffs.C <- gsub(HS_decline, "(time <= tb ? k1 : k2)", diffs.C, fixed = TRUE) + # HS + diffs.C <- gsub(HS_decline, "(time <= tb ? k1 : k2)", diffs.C, fixed = TRUE) - for (i in seq_along(diffs)) { - state_var <- names(diffs)[i] + for (i in seq_along(diffs)) { + state_var <- names(diffs)[i] - # IORE - if (state_var %in% obs_vars) { - if (spec[[state_var]]$type == "IORE") { - diffs.C <- gsub(paste0(state_var, "^N_", state_var), - paste0("pow(y[", i - 1, "], N_", state_var, ")"), - diffs.C, fixed = TRUE) - } + # IORE + if (state_var %in% obs_vars) { + if (spec[[state_var]]$type == "IORE") { + diffs.C <- gsub(paste0(state_var, "^N_", state_var), + paste0("pow(y[", i - 1, "], N_", state_var, ")"), + diffs.C, fixed = TRUE) } + } - # Replace d_... terms by f[i-1] - # First line - pattern <- paste0("^d_", state_var) - replacement <- paste0("\nf[", i - 1, "]") - diffs.C <- gsub(pattern, replacement, diffs.C) - # Other lines - pattern <- paste0("\\nd_", state_var) - replacement <- paste0("\nf[", i - 1, "]") - diffs.C <- gsub(pattern, replacement, diffs.C) + # Replace d_... terms by f[i-1] + # First line + pattern <- paste0("^d_", state_var) + replacement <- paste0("\nf[", i - 1, "]") + diffs.C <- gsub(pattern, replacement, diffs.C) + # Other lines + pattern <- paste0("\\nd_", state_var) + replacement <- paste0("\nf[", i - 1, "]") + diffs.C <- gsub(pattern, replacement, diffs.C) - # Replace names of observed variables by y[i], - # making the implicit assumption that the observed variables only occur after "* " - pattern <- paste0("\\* ", state_var) - replacement <- paste0("* y[", i - 1, "]") - diffs.C <- gsub(pattern, replacement, diffs.C) - } + # Replace names of observed variables by y[i], + # making the implicit assumption that the observed variables only occur after "* " + pattern <- paste0("\\* ", state_var) + replacement <- paste0("* y[", i - 1, "]") + diffs.C <- gsub(pattern, replacement, diffs.C) + } - derivs_sig <- signature(n = "integer", t = "numeric", y = "numeric", - f = "numeric", rpar = "numeric", ipar = "integer") + derivs_sig <- signature(n = "integer", t = "numeric", y = "numeric", + f = "numeric", rpar = "numeric", ipar = "integer") - # Declare the time variable in the body of the function if it is used - derivs_code <- if (spec[[1]]$type %in% c("FOMC", "DFOP", "HS")) { - paste0("double time = *t;\n", diffs.C) - } else { - diffs.C - } + # Declare the time variable in the body of the function if it is used + derivs_code <- if (spec[[1]]$type %in% c("FOMC", "DFOP", "HS")) { + paste0("double time = *t;\n", diffs.C) + } else { + diffs.C + } - # Define the function initializing the parameters - npar <- length(parms) - initpar_code <- paste0( - "static double parms [", npar, "];\n", - paste0("#define ", parms, " parms[", 0:(npar - 1), "]\n", collapse = ""), - "\n", - "void initpar(void (* odeparms)(int *, double *)) {\n", - " int N = ", npar, ";\n", - " odeparms(&N, parms);\n", - "}\n\n") + # Define the function initializing the parameters + npar <- length(parms) + initpar_code <- paste0( + "static double parms [", npar, "];\n", + paste0("#define ", parms, " parms[", 0:(npar - 1), "]\n", collapse = ""), + "\n", + "void initpar(void (* odeparms)(int *, double *)) {\n", + " int N = ", npar, ";\n", + " odeparms(&N, parms);\n", + "}\n\n") - # Try to build a shared library - cf <- try(cfunction(list(func = derivs_sig), derivs_code, - otherdefs = initpar_code, - verbose = verbose, - convention = ".C", language = "C"), - silent = TRUE) + # Try to build a shared library + cf <- try(cfunction(list(func = derivs_sig), derivs_code, + otherdefs = initpar_code, + verbose = verbose, + convention = ".C", language = "C"), + silent = TRUE) - if (!inherits(cf, "try-error")) { - if (!quiet) message("Successfully compiled differential equation model from auto-generated C code.") - model$cf <- cf - } + if (!inherits(cf, "try-error")) { + if (!quiet) message("Successfully compiled differential equation model from auto-generated C code.") + model$cf <- cf } } # }}} -- cgit v1.2.3 From f9e0c46378f090a04fe95c70bb7977a686679895 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Tue, 31 Mar 2020 09:24:14 +0200 Subject: Static documentation rebuilt by pkgdown --- DESCRIPTION | 4 +- NAMESPACE | 1 + R/mkinmod.R | 6 ++- check.log | 15 +++++-- test.log | 51 ++++++++++++++-------- .../plotting/plot-errmod-with-sfo-lin-a-obs.svg | 6 +-- tests/testthat/DFOP_FOCUS_C_messages.txt | 4 +- tests/testthat/FOCUS_2006_D.csf | 2 +- 8 files changed, 57 insertions(+), 32 deletions(-) (limited to 'R') diff --git a/DESCRIPTION b/DESCRIPTION index f631a541..5576e5bf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: mkin Type: Package Title: Kinetic Evaluation of Chemical Degradation Data Version: 0.9.49.9 -Date: 2020-03-20 +Date: 2020-03-31 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de", comment = c(ORCID = "0000-0003-4371-6538")), @@ -17,7 +17,7 @@ Description: Calculation routines based on the FOCUS Kinetics Report (2006, note that no warranty is implied for correctness of results or fitness for a particular purpose. Imports: stats, graphics, methods, deSolve, R6, inline, parallel, numDeriv, - lmtest + lmtest, pkgbuild Suggests: knitr, rbenchmark, tikzDevice, testthat, rmarkdown, covr, vdiffr, benchmarkme, tibble, stats4 License: GPL diff --git a/NAMESPACE b/NAMESPACE index 26995055..6f509cd7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ importFrom(methods,signature) importFrom(parallel,detectCores) importFrom(parallel,mclapply) importFrom(parallel,parLapply) +importFrom(pkgbuild,has_compiler) importFrom(stats,AIC) importFrom(stats,BIC) importFrom(stats,aggregate) diff --git a/R/mkinmod.R b/R/mkinmod.R index cf618525..cfd40504 100644 --- a/R/mkinmod.R +++ b/R/mkinmod.R @@ -33,6 +33,7 @@ #' @param verbose If \code{TRUE}, passed to \code{\link{cfunction}} if #' applicable to give detailed information about the C function being built. #' @importFrom methods signature +#' @importFrom pkgbuild has_compiler #' @importFrom inline cfunction #' @return A list of class \code{mkinmod} for use with \code{\link{mkinfit}}, #' containing, among others, @@ -364,8 +365,9 @@ mkinmod <- function(..., use_of_ff = "min", speclist = NULL, quiet = FALSE, verb model$coefmat <- m }#}}} - # Try to create a function compiled from C code if >1 observed variable {{{ - if (length(obs_vars) > 1) { + # Try to create a function compiled from C code there is more than one observed variable {{{ + # and a compiler is available + if (length(obs_vars) > 1 & has_compiler()) { # Translate the R code for the derivatives to C code diffs.C <- paste(diffs, collapse = ";\n") diff --git a/check.log b/check.log index b7555c55..35ab651a 100644 --- a/check.log +++ b/check.log @@ -1,11 +1,11 @@ * using log directory ‘/home/jranke/git/mkin/mkin.Rcheck’ -* using R version 3.6.2 (2019-12-12) +* using R version 3.6.3 (2020-02-29) * using platform: x86_64-pc-linux-gnu (64-bit) * using session charset: UTF-8 * using options ‘--no-tests --as-cran’ * checking for file ‘mkin/DESCRIPTION’ ... OK * checking extension type ... Package -* this is package ‘mkin’ version ‘0.9.49.8’ +* this is package ‘mkin’ version ‘0.9.49.9’ * package encoding: UTF-8 * checking CRAN incoming feasibility ... Note_to_CRAN_maintainers Maintainer: ‘Johannes Ranke ’ @@ -56,7 +56,10 @@ Maintainer: ‘Johannes Ranke ’ * checking data for ASCII and uncompressed saves ... OK * checking installed files from ‘inst/doc’ ... OK * checking files in ‘vignettes’ ... OK -* checking examples ... OK +* checking examples ... NOTE +Examples with CPU or elapsed time > 5s + user system elapsed +mkinsub 2.015 4.083 0.703 * checking for unstated dependencies in ‘tests’ ... OK * checking tests ... SKIPPED * checking for unstated dependencies in vignettes ... OK @@ -66,5 +69,9 @@ Maintainer: ‘Johannes Ranke ’ * checking for detritus in the temp directory ... OK * DONE -Status: OK +Status: 1 NOTE +See + ‘/home/jranke/git/mkin/mkin.Rcheck/00check.log’ +for details. + diff --git a/test.log b/test.log index c51d06b8..2db18301 100644 --- a/test.log +++ b/test.log @@ -1,38 +1,53 @@ Loading mkin Testing mkin ✔ | OK F W S | Context -✔ | 5 | Calculation of Akaike weights ✔ | 2 | Export dataset for reading into CAKE -✔ | 10 | Confidence intervals and p-values [9.7 s] -✔ | 14 | Error model fitting [36.9 s] -✔ | 4 | Calculation of FOCUS chi2 error levels [2.2 s] -✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.3 s] -✔ | 6 | Test fitting the decline of metabolites from their maximum [0.7 s] -✔ | 1 | Fitting the logistic model [0.9 s] +✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.8 s] +✔ | 4 | Calculation of FOCUS chi2 error levels [2.3 s] +✔ | 4 | Fitting the SFORB model [1.7 s] +✔ | 5 | Calculation of Akaike weights +✔ | 10 | Confidence intervals and p-values [10.2 s] +✔ | 14 | Error model fitting [40.7 s] +✔ | 6 | Test fitting the decline of metabolites from their maximum [0.8 s] +✔ | 1 | Fitting the logistic model [1.0 s] ✔ | 1 | Test dataset class mkinds used in gmkin -✔ | 12 | Special cases of mkinfit calls [2.4 s] +✖ | 11 1 | Special cases of mkinfit calls [2.3 s] +──────────────────────────────────────────────────────────────────────────────── +test_mkinfit_errors.R:64: failure: We get reproducible output if quiet = FALSE +Results have changed from known value recorded in 'DFOP_FOCUS_C_messages.txt'. +2/165 mismatches +x[84]: "Sum of squared residuals at call 57: 4.364077" +y[84]: "Sum of squared residuals at call 57: 4.364078" + +x[105]: "85.00134 -0.7776046 -4.025878 1.248775 " +y[105]: "85.00135 -0.7776046 -4.025878 1.248775 " +──────────────────────────────────────────────────────────────────────────────── ✔ | 9 | mkinmod model generation and printing [0.2 s] ✔ | 3 | Model predictions with mkinpredict [0.3 s] -✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.1 s] -✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.3 s] +✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.3 s] +✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.2 s] ✔ | 3 | Summary -✔ | 11 | Plotting [0.6 s] +✖ | 10 1 | Plotting [0.6 s] +──────────────────────────────────────────────────────────────────────────────── +test_plots_summary_twa.R:118: failure: Plotting mkinfit and mmkin objects is reproducible +Figures don't match: plot-errmod-with-sfo-lin-a-obs.svg + +──────────────────────────────────────────────────────────────────────────────── ✔ | 4 | AIC calculation ✔ | 2 | Residuals extracted from mkinfit models -✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.3 s] -✔ | 4 | Fitting the SFORB model [1.7 s] +✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.6 s] ✔ | 1 | Summaries of old mkinfit objects -✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.2 s] -✔ | 7 1 | Hypothesis tests [32.3 s] +✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.4 s] +✔ | 7 1 | Hypothesis tests [33.6 s] ──────────────────────────────────────────────────────────────────────────────── test_tests.R:60: skip: We can do a likelihood ratio test using an update specification Reason: This errors out if called by testthat while it works in a normal R session ──────────────────────────────────────────────────────────────────────────────── ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 110.2 s +Duration: 117.3 s -OK: 138 -Failed: 0 +OK: 136 +Failed: 2 Warnings: 0 Skipped: 1 diff --git a/tests/figs/plotting/plot-errmod-with-sfo-lin-a-obs.svg b/tests/figs/plotting/plot-errmod-with-sfo-lin-a-obs.svg index 2b4930ba..7ba81d7b 100644 --- a/tests/figs/plotting/plot-errmod-with-sfo-lin-a-obs.svg +++ b/tests/figs/plotting/plot-errmod-with-sfo-lin-a-obs.svg @@ -156,7 +156,7 @@ 5 10 15 -20 +20 25 30 @@ -186,7 +186,7 @@ - + @@ -222,7 +222,7 @@ - + diff --git a/tests/testthat/DFOP_FOCUS_C_messages.txt b/tests/testthat/DFOP_FOCUS_C_messages.txt index 78438d06..c36f11db 100644 --- a/tests/testthat/DFOP_FOCUS_C_messages.txt +++ b/tests/testthat/DFOP_FOCUS_C_messages.txt @@ -81,7 +81,7 @@ Sum of squared residuals at call 55: 4.364078 85.01633 -0.7763163 -4.027611 1.248897 Sum of squared residuals at call 56: 4.364078 85.01633 -0.7763164 -4.027611 1.248897 -Sum of squared residuals at call 57: 4.364078 +Sum of squared residuals at call 57: 4.364077 85.01633 -0.7763163 -4.027611 1.248897 85.01633 -0.7763163 -4.027611 1.248897 85.00894 -0.7777917 -4.026307 1.24772 @@ -102,7 +102,7 @@ Sum of squared residuals at call 67: 4.362751 85.00518 -0.7773082 -4.026004 1.248453 85.00134 -0.7776046 -4.025878 1.248775 Sum of squared residuals at call 70: 4.362721 -85.00135 -0.7776046 -4.025878 1.248775 +85.00134 -0.7776046 -4.025878 1.248775 Sum of squared residuals at call 71: 4.362721 85.00134 -0.7776046 -4.025878 1.248775 Sum of squared residuals at call 72: 4.362721 diff --git a/tests/testthat/FOCUS_2006_D.csf b/tests/testthat/FOCUS_2006_D.csf index 358b50e3..a7f8edaf 100644 --- a/tests/testthat/FOCUS_2006_D.csf +++ b/tests/testthat/FOCUS_2006_D.csf @@ -5,7 +5,7 @@ Description: MeasurementUnits: % AR TimeUnits: days Comments: Created using mkin::CAKE_export -Date: 2019-11-13 +Date: 2020-03-31 Optimiser: IRLS [Data] -- cgit v1.2.3 From 64a476750ff57f4c612620bd58cc4ac42812e185 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Tue, 31 Mar 2020 09:52:19 +0200 Subject: Use inline documentation for R6 class mkinds --- R/mkinds.R | 53 ++++++++++++++++++--------------- docs/reference/mkinds.html | 64 +++++++++++++++++++++++++--------------- docs/reference/print.mkinds.html | 6 ++-- man/mkinds.Rd | 55 +++++++++++++++++++++++----------- man/print.mkinds.Rd | 2 +- test.log | 45 ++++++++++------------------ 6 files changed, 126 insertions(+), 99 deletions(-) (limited to 'R') diff --git a/R/mkinds.R b/R/mkinds.R index a66adb14..d6f296bf 100644 --- a/R/mkinds.R +++ b/R/mkinds.R @@ -1,43 +1,50 @@ #' A dataset class for mkin #' -#' A dataset class for mkin -#' -#' @name mkinds -#' @docType class -#' @format An \code{\link{R6Class}} generator object. -#' @section Fields: -#' -#' \describe{ \item{list("title")}{A full title for the dataset} -#' -#' \item{list("sampling")}{times The sampling times} -#' -#' \item{list("time_unit")}{The time unit} -#' -#' \item{list("observed")}{Names of the observed compounds} -#' -#' \item{list("unit")}{The unit of the observations} -#' -#' \item{list("replicates")}{The number of replicates} -#' -#' \item{list("data")}{A dataframe with at least the columns name, time and -#' value in order to be compatible with mkinfit} } +#' @description +#' At the moment this dataset class is hardly used in mkin. For example, +#' mkinfit does not take mkinds datasets as argument, but works with dataframes +#' such as the on contained in the data field of mkinds objects. Some datasets +#' provided by this package come as mkinds objects nevertheless. +#' #' @importFrom R6 R6Class -#' @keywords datasets +#' @seealso The S3 printing method \code{\link{print.mkinds}} #' @examples #' #' mds <- mkinds$new("FOCUS A", FOCUS_2006_A) +#' print(mds) #' #' @export mkinds <- R6Class("mkinds", public = list( + + #' @field title A full title for the dataset title = NULL, + + #' @field sampling_times The sampling times sampling_times = NULL, + + #' @field time_unit The time unit time_unit = NULL, + + #' @field observed Names of the observed variables observed = NULL, + + #' @field unit The unit of the observations unit = NULL, + + #' @field replicates The maximum number of replicates per sampling time replicates = NULL, + + #' @field data A data frame with at least the columns name, time + #' and value in order to be compatible with mkinfit data = NULL, + #' @description + #' Create a new mkinds object + #' @param title The dataset title + #' @param data The data + #' @param time_unit The time unit + #' @param unit The unit of the observations initialize = function(title = "", data, time_unit = NA, unit = NA) { self$title <- title @@ -56,8 +63,6 @@ mkinds <- R6Class("mkinds", #' Print mkinds objects #' -#' Print mkinds objects. -#' #' @param x An \code{\link{mkinds}} object. #' @param \dots Not used. #' @export diff --git a/docs/reference/mkinds.html b/docs/reference/mkinds.html index c1cdcf99..ef6fb35c 100644 --- a/docs/reference/mkinds.html +++ b/docs/reference/mkinds.html @@ -36,7 +36,10 @@ - + @@ -133,36 +136,36 @@
-

A dataset class for mkin

+

At the moment this dataset class is hardly used in mkin. For example, +mkinfit does not take mkinds datasets as argument, but works with dataframes +such as the on contained in the data field of mkinds objects. Some datasets +provided by this package come as mkinds objects nevertheless.

-

Format

+

See also

-

An R6Class generator object.

-

Fields

+

The S3 printing method print.mkinds

+

Public fields

- - - -
-
list("title")

A full title for the dataset

+

+
title

A full title for the dataset

-
list("sampling")

times The sampling times

+
sampling_times

The sampling times

-
list("time_unit")

The time unit

+
time_unit

The time unit

-
list("observed")

Names of the observed compounds

+
observed

Names of the observed variables

-
list("unit")

The unit of the observations

+
unit

The unit of the observations

-
list("replicates")

The number of replicates

+
replicates

The maximum number of replicates per sampling time

-
list("data")

A dataframe with at least the columns name, time and -value in order to be compatible with mkinfit

-
+
data

A data frame with at least the columns name, time +and value in order to be compatible with mkinfit

+

Methods

@@ -174,10 +177,20 @@ value in order to be compatible with mkinfit


Method new()

- -

Usage

+

Create a new mkinds object

Usage

mkinds$new(title = "", data, time_unit = NA, unit = NA)

+

Arguments

+

+
title

The dataset title

+ +
data

The data

+ +
time_unit

The time unit

+ +
unit

The unit of the observations

+ +


Method clone()

The objects of this class are cloneable with this method.

Usage

@@ -193,13 +206,18 @@ value in order to be compatible with mkinfit

Examples

-mds <- mkinds$new("FOCUS A", FOCUS_2006_A)
+mds <- mkinds$new("FOCUS A", FOCUS_2006_A) +print(mds)
#> <mkinds> with $title: FOCUS A +#> Observed compounds $observed: parent +#> Sampling times $sampling_times: 0, 3, 7, 14, 30, 62, 90, 118 +#> With a maximum of 1 replicates
+
@@ -133,7 +133,7 @@
-

Print mkinds objects.

+

Print mkinds objects

# S3 method for mkinds
diff --git a/man/mkinds.Rd b/man/mkinds.Rd
index 79eb0167..3bbb1c4b 100644
--- a/man/mkinds.Rd
+++ b/man/mkinds.Rd
@@ -1,38 +1,43 @@
 % Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/mkinds.R
-\docType{class}
 \name{mkinds}
 \alias{mkinds}
 \title{A dataset class for mkin}
-\format{An \code{\link{R6Class}} generator object.}
 \description{
-A dataset class for mkin
+At the moment this dataset class is hardly used in mkin. For example,
+mkinfit does not take mkinds datasets as argument, but works with dataframes
+such as the on contained in the data field of mkinds objects. Some datasets
+provided by this package come as mkinds objects nevertheless.
 }
-\section{Fields}{
+\examples{
 
+mds <- mkinds$new("FOCUS A", FOCUS_2006_A)
+print(mds)
 
-\describe{ \item{list("title")}{A full title for the dataset}
+}
+\seealso{
+The S3 printing method \code{\link{print.mkinds}}
+}
+\section{Public fields}{
+\if{html}{\out{
}} +\describe{ +\item{\code{title}}{A full title for the dataset} -\item{list("sampling")}{times The sampling times} +\item{\code{sampling_times}}{The sampling times} -\item{list("time_unit")}{The time unit} +\item{\code{time_unit}}{The time unit} -\item{list("observed")}{Names of the observed compounds} +\item{\code{observed}}{Names of the observed variables} -\item{list("unit")}{The unit of the observations} +\item{\code{unit}}{The unit of the observations} -\item{list("replicates")}{The number of replicates} +\item{\code{replicates}}{The maximum number of replicates per sampling time} -\item{list("data")}{A dataframe with at least the columns name, time and -value in order to be compatible with mkinfit} } +\item{\code{data}}{A data frame with at least the columns name, time +and value in order to be compatible with mkinfit} } - -\examples{ - -mds <- mkinds$new("FOCUS A", FOCUS_2006_A) - +\if{html}{\out{
}} } -\keyword{datasets} \section{Methods}{ \subsection{Public methods}{ \itemize{ @@ -43,10 +48,24 @@ mds <- mkinds$new("FOCUS A", FOCUS_2006_A) \if{html}{\out{
}} \if{html}{\out{}} \subsection{Method \code{new()}}{ +Create a new mkinds object \subsection{Usage}{ \if{html}{\out{
}}\preformatted{mkinds$new(title = "", data, time_unit = NA, unit = NA)}\if{html}{\out{
}} } +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{title}}{The dataset title} + +\item{\code{data}}{The data} + +\item{\code{time_unit}}{The time unit} + +\item{\code{unit}}{The unit of the observations} +} +\if{html}{\out{
}} +} } \if{html}{\out{
}} \if{html}{\out{}} diff --git a/man/print.mkinds.Rd b/man/print.mkinds.Rd index 54dc5a12..51ef3b76 100644 --- a/man/print.mkinds.Rd +++ b/man/print.mkinds.Rd @@ -12,5 +12,5 @@ \item{\dots}{Not used.} } \description{ -Print mkinds objects. +Print mkinds objects } diff --git a/test.log b/test.log index 2db18301..77130814 100644 --- a/test.log +++ b/test.log @@ -2,52 +2,37 @@ Loading mkin Testing mkin ✔ | OK F W S | Context ✔ | 2 | Export dataset for reading into CAKE -✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.8 s] +✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.7 s] ✔ | 4 | Calculation of FOCUS chi2 error levels [2.3 s] -✔ | 4 | Fitting the SFORB model [1.7 s] +✔ | 4 | Fitting the SFORB model [1.8 s] ✔ | 5 | Calculation of Akaike weights -✔ | 10 | Confidence intervals and p-values [10.2 s] -✔ | 14 | Error model fitting [40.7 s] +✔ | 10 | Confidence intervals and p-values [10.7 s] +✔ | 14 | Error model fitting [42.8 s] ✔ | 6 | Test fitting the decline of metabolites from their maximum [0.8 s] ✔ | 1 | Fitting the logistic model [1.0 s] ✔ | 1 | Test dataset class mkinds used in gmkin -✖ | 11 1 | Special cases of mkinfit calls [2.3 s] -──────────────────────────────────────────────────────────────────────────────── -test_mkinfit_errors.R:64: failure: We get reproducible output if quiet = FALSE -Results have changed from known value recorded in 'DFOP_FOCUS_C_messages.txt'. -2/165 mismatches -x[84]: "Sum of squared residuals at call 57: 4.364077" -y[84]: "Sum of squared residuals at call 57: 4.364078" - -x[105]: "85.00134 -0.7776046 -4.025878 1.248775 " -y[105]: "85.00135 -0.7776046 -4.025878 1.248775 " -──────────────────────────────────────────────────────────────────────────────── +✔ | 12 | Special cases of mkinfit calls [2.5 s] ✔ | 9 | mkinmod model generation and printing [0.2 s] -✔ | 3 | Model predictions with mkinpredict [0.3 s] -✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.3 s] -✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.2 s] +✔ | 3 | Model predictions with mkinpredict [0.4 s] +✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.4 s] +✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.3 s] ✔ | 3 | Summary -✖ | 10 1 | Plotting [0.6 s] -──────────────────────────────────────────────────────────────────────────────── -test_plots_summary_twa.R:118: failure: Plotting mkinfit and mmkin objects is reproducible -Figures don't match: plot-errmod-with-sfo-lin-a-obs.svg - -──────────────────────────────────────────────────────────────────────────────── +✔ | 11 | Plotting [0.6 s] ✔ | 4 | AIC calculation ✔ | 2 | Residuals extracted from mkinfit models -✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.6 s] +✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.7 s] ✔ | 1 | Summaries of old mkinfit objects -✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.4 s] -✔ | 7 1 | Hypothesis tests [33.6 s] +✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.7 s] +✔ | 7 1 | Hypothesis tests [35.7 s] ──────────────────────────────────────────────────────────────────────────────── test_tests.R:60: skip: We can do a likelihood ratio test using an update specification Reason: This errors out if called by testthat while it works in a normal R session ──────────────────────────────────────────────────────────────────────────────── ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 117.3 s +Duration: 123.0 s -OK: 136 -Failed: 2 +OK: 138 +Failed: 0 Warnings: 0 Skipped: 1 -- cgit v1.2.3 From 47ba9ea512b82fb8b31da8ec5558f3c0952d86d4 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 2 Apr 2020 10:58:34 +0200 Subject: Compiled models article, reduce distractions - Added a section with platform specific notes on getting compiled models to work to the compiled models article - Don't return empty SFORB parameter list from endpoints() if there is no SFORB model - Avoid warnings when using standardized = TRUE in plot.mmkin() --- NEWS.md | 10 +- R/endpoints.R | 20 +- R/plot.mmkin.R | 6 +- docs/articles/web_only/compiled_models.html | 171 ++++++-------- docs/news/index.html | 17 +- docs/reference/endpoints.html | 5 +- docs/reference/plot.mmkin.html | 7 + man/endpoints.Rd | 2 +- man/plot.mmkin.Rd | 5 + test.log | 30 +-- tests/testthat/FOCUS_2006_D.csf | 2 +- vignettes/web_only/FOCUS_Z.R | 115 ++++++++++ vignettes/web_only/FOCUS_Z.html | 332 ++++++++++++++++++---------- vignettes/web_only/compiled_models.R | 61 +++++ vignettes/web_only/compiled_models.Rmd | 53 ++++- vignettes/web_only/compiled_models.html | 189 +++++++++------- 16 files changed, 674 insertions(+), 351 deletions(-) create mode 100644 vignettes/web_only/FOCUS_Z.R create mode 100644 vignettes/web_only/compiled_models.R (limited to 'R') diff --git a/NEWS.md b/NEWS.md index 8fe50754..a2258e77 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,12 @@ -# mkin 0.9.49.9 (unreleased) +# mkin 0.9.49.10 (unreleased) + +- 'endpoints': Don't return the SFORB list component if it's empty. This reduces distraction and complies with the documentation + +- Article in compiled models: Add some platform specific code and suppress warnings about zero values being removed from the FOCUS D dataset + +- 'plot.mmkin': Add the argument 'standardized' to avoid warnings that occurred when it was passed as part of the additional arguments captured by the dots (...) + +# mkin 0.9.49.9 (2020-03-31) - 'mkinmod': Use pkgbuild::has_compiler instead of Sys.which('gcc'), as the latter will often fail even if Rtools are installed diff --git a/R/endpoints.R b/R/endpoints.R index 14beadea..f7ee483a 100644 --- a/R/endpoints.R +++ b/R/endpoints.R @@ -1,12 +1,12 @@ #' Function to calculate endpoints for further use from kinetic models fitted #' with mkinfit -#' +#' #' This function calculates DT50 and DT90 values as well as formation fractions #' from kinetic models fitted with mkinfit. If the SFORB model was specified #' for one of the parents or metabolites, the Eigenvalues are returned. These #' are equivalent to the rate constantes of the DFOP model, but with the #' advantage that the SFORB model can also be used for metabolites. -#' +#' #' @param fit An object of class \code{\link{mkinfit}}. #' @importFrom stats optimize #' @return A list with the components mentioned above. @@ -14,10 +14,10 @@ #' @author Johannes Ranke #' @keywords manip #' @examples -#' +#' #' fit <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE) -#' endpoints(fit) -#' +#' endpoints(fit) +#' #' @export endpoints <- function(fit) { # Calculate dissipation times DT50 and DT90 and formation @@ -29,8 +29,9 @@ endpoints <- function(fit) { parms.all <- c(fit$bparms.optim, fit$bparms.fixed) ep$ff <- vector() ep$SFORB <- vector() - ep$distimes <- data.frame(DT50 = rep(NA, length(obs_vars)), - DT90 = rep(NA, length(obs_vars)), + ep$distimes <- data.frame( + DT50 = rep(NA, length(obs_vars)), + DT90 = rep(NA, length(obs_vars)), row.names = obs_vars) for (obs_var in obs_vars) { type = names(fit$mkinmod$map[[obs_var]])[1] @@ -41,8 +42,8 @@ endpoints <- function(fit) { f_values = parms.all[f_names] f_to_sink = 1 - sum(f_values) names(f_to_sink) = ifelse(type == "SFORB", - paste(obs_var, "free", "sink", sep = "_"), - paste(obs_var, "sink", sep = "_")) + paste(obs_var, "free", "sink", sep = "_"), + paste(obs_var, "sink", sep = "_")) for (f_name in f_names) { ep$ff[[sub("f_", "", sub("_to_", "_", f_name))]] = f_values[[f_name]] } @@ -195,5 +196,6 @@ endpoints <- function(fit) { } ep$distimes[obs_var, c("DT50", "DT90")] = c(DT50, DT90) } + if (length(ep$SFORB) == 0) ep$SFORB <- NULL return(ep) } diff --git a/R/plot.mmkin.R b/R/plot.mmkin.R index 182e74ca..15ea86eb 100644 --- a/R/plot.mmkin.R +++ b/R/plot.mmkin.R @@ -15,6 +15,9 @@ #' @param resplot Should the residuals plotted against time, using #' \code{\link{mkinresplot}}, or as squared residuals against predicted #' values, with the error model, using \code{\link{mkinerrplot}}. +#' @param standardized Should the residuals be standardized? This option +#' is passed to \code{\link{mkinresplot}}, it only takes effect if +#' `resplot = "time"`. #' @param show_errmin Should the chi2 error level be shown on top of the plots #' to the left? #' @param errmin_var The variable for which the FOCUS chi2 error value should @@ -51,6 +54,7 @@ #' @export plot.mmkin <- function(x, main = "auto", legends = 1, resplot = c("time", "errmod"), + standardized = FALSE, show_errmin = TRUE, errmin_var = "All data", errmin_digits = 3, cex = 0.7, rel.height.middle = 0.9, @@ -136,7 +140,7 @@ plot.mmkin <- function(x, main = "auto", legends = 1, } if (resplot == "time") { - mkinresplot(fit, legend = FALSE, ...) + mkinresplot(fit, legend = FALSE, standardized = standardized, ...) } else { mkinerrplot(fit, legend = FALSE, ...) } diff --git a/docs/articles/web_only/compiled_models.html b/docs/articles/web_only/compiled_models.html index 363f0c38..6daabd5e 100644 --- a/docs/articles/web_only/compiled_models.html +++ b/docs/articles/web_only/compiled_models.html @@ -31,7 +31,7 @@ mkin - 0.9.49.6 + 0.9.49.9 @@ -90,7 +90,7 @@

Performance benefit by using compiled model definitions in mkin

Johannes Ranke

-

2019-11-01

+

2020-04-02

@@ -99,75 +99,49 @@ -
+

-Model that can also be solved with Eigenvalues

-

This evaluation is taken from the example section of mkinfit. When using an mkin version equal to or greater than 0.9-36 and a C compiler (gcc) is available, you will see a message that the model is being compiled from autogenerated C code when defining a model using mkinmod. The mkinmod() function checks for presence of the gcc compiler using

-
Sys.which("gcc")
-
##            gcc 
-## "/usr/bin/gcc"
+How to benefit from compiled models +

When using an mkin version equal to or greater than 0.9-36 and a C compiler is available, you will see a message that the model is being compiled from autogenerated C code when defining a model using mkinmod. Starting from version 0.9.49.9, the mkinmod() function checks for presence of a compiler using

+ +

In previous versions, it used Sys.which("gcc") for this check.

+

On Linux, you need to have the essential build tools like make and gcc or clang installed. On Debian based linux distributions, these will be pulled in by installing the build-essential package.

+

On MacOS, which I do not use personally, I have had reports that a compiler is available by default.

+

On Windows, you need to install Rtools and have the path to its bin directory in your PATH variable. You do not need to modify the PATH variable when installing Rtools. Instead, I would recommend to put the line

+
Sys.setenv(PATH = paste("C:/Rtools/bin", Sys.getenv("PATH"), sep=";"))
+

into your .Rprofile startup file. This is just a text file with some R code that is executed when your R session starts. It has to be named .Rprofile and has to be located in your home directory, which will generally be your Documents folder. You can check the location of the home directory used by R by issuing

+
Sys.getenv("HOME")
+
+
+

+Comparison with Eigenvalue based solutions

First, we build a simple degradation model for a parent compound with one metabolite.

-
library("mkin", quietly = TRUE)
-SFO_SFO <- mkinmod(
-  parent = mkinsub("SFO", "m1"),
-  m1 = mkinsub("SFO"))
+
library("mkin", quietly = TRUE)
+SFO_SFO <- mkinmod(
+  parent = mkinsub("SFO", "m1"),
+  m1 = mkinsub("SFO"))
## Successfully compiled differential equation model from auto-generated C code.
-

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the benchmark package.

- -
## Lade nötiges Paket: rbenchmark
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve",
-## use_compiled = FALSE, : Observations with value of zero were removed from
-## the data
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet =
-## TRUE): Observations with value of zero were removed from the data
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet
-## = TRUE): Observations with value of zero were removed from the data
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve",
-## use_compiled = FALSE, : Observations with value of zero were removed from
-## the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve",
-## use_compiled = FALSE, : Observations with value of zero were removed from
-## the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve",
-## use_compiled = FALSE, : Observations with value of zero were removed from
-## the data
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet =
-## TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet =
-## TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet =
-## TRUE): Observations with value of zero were removed from the data
-
## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet
-## = TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet
-## = TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet
-## = TRUE): Observations with value of zero were removed from the data
+

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the benchmark package. In the output of below code, the warnings about zero being removed from the FOCUS D dataset are suppressed.

+
##                    test replications elapsed relative user.self sys.self
-## 3     deSolve, compiled            3   3.143    1.000     3.142        0
-## 1 deSolve, not compiled            3  29.169    9.281    29.154        0
-## 2      Eigenvalue based            3   4.358    1.387     4.356        0
+## 3     deSolve, compiled            3   3.239    1.000     3.237        0
+## 1 deSolve, not compiled            3  29.758    9.187    29.739        0
+## 2      Eigenvalue based            3   4.558    1.407     4.554        0
 ##   user.child sys.child
 ## 3          0         0
 ## 1          0         0
@@ -178,52 +152,32 @@
 

Model that can not be solved with Eigenvalues

This evaluation is also taken from the example section of mkinfit.

-
if (require(rbenchmark)) {
-  FOMC_SFO <- mkinmod(
-    parent = mkinsub("FOMC", "m1"),
-    m1 = mkinsub( "SFO"))
-
-  b.2 <- benchmark(
-    "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D,
-                                      use_compiled = FALSE, quiet = TRUE),
-    "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE),
-    replications = 3)
-  print(b.2)
-  factor_FOMC_SFO <- round(b.2["1", "relative"])
-} else {
-  factor_FOMC_SFO <- NA
-  print("R package benchmark is not available")
-}
+
if (require(rbenchmark)) {
+  FOMC_SFO <- mkinmod(
+    parent = mkinsub("FOMC", "m1"),
+    m1 = mkinsub( "SFO"))
+
+  b.2 <- benchmark(
+    "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D,
+                                      use_compiled = FALSE, quiet = TRUE),
+    "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE),
+    replications = 3)
+  print(b.2)
+  factor_FOMC_SFO <- round(b.2["1", "relative"])
+} else {
+  factor_FOMC_SFO <- NA
+  print("R package benchmark is not available")
+}
## Successfully compiled differential equation model from auto-generated C code.
-
## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, use_compiled = FALSE, quiet =
-## TRUE): Observations with value of zero were removed from the data
-
## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE): Observations with
-## value of zero were removed from the data
-
## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, use_compiled = FALSE, quiet =
-## TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, use_compiled = FALSE, quiet =
-## TRUE): Observations with value of zero were removed from the data
-
-## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, use_compiled = FALSE, quiet =
-## TRUE): Observations with value of zero were removed from the data
-
## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE): Observations with
-## value of zero were removed from the data
-
-## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE): Observations with
-## value of zero were removed from the data
-
-## Warning in mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE): Observations with
-## value of zero were removed from the data
##                    test replications elapsed relative user.self sys.self
-## 2     deSolve, compiled            3   4.840    1.000     4.837        0
-## 1 deSolve, not compiled            3  54.338   11.227    54.309        0
+## 2     deSolve, compiled            3   4.984    1.000     4.981    0.000
+## 1 deSolve, not compiled            3  55.358   11.107    55.326    0.003
 ##   user.child sys.child
 ## 2          0         0
 ## 1          0         0

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

-

This vignette was built with mkin 0.9.49.6 on

-
## R version 3.6.1 (2019-07-05)
+

This vignette was built with mkin 0.9.49.9 on

+
## R version 3.6.3 (2020-02-29)
 ## Platform: x86_64-pc-linux-gnu (64-bit)
 ## Running under: Debian GNU/Linux 10 (buster)
## CPU model: AMD Ryzen 7 1700 Eight-Core Processor
@@ -236,7 +190,8 @@

Contents

diff --git a/docs/news/index.html b/docs/news/index.html index c2b5865f..fc9a0765 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -129,9 +129,19 @@
-
+

-mkin 0.9.49.9 (unreleased) Unreleased +mkin 0.9.49.10 (unreleased) Unreleased +

+
    +
  • ‘endpoints’: Don’t return the SFORB list component if it’s empty. This reduces distraction and complies with the documentation

  • +
  • Article in compiled models: Add some platform specific code and suppress warnings about zero values being removed from the FOCUS D dataset

  • +
  • ‘plot.mmkin’: Add the argument ‘standardized’ to avoid warnings that occurred when it was passed as part of the additional arguments captured by the dots (…)

  • +
+
+
+

+mkin 0.9.49.9 (2020-03-31) 2020-03-31

  • ‘mkinmod’: Use pkgbuild::has_compiler instead of Sys.which(‘gcc’), as the latter will often fail even if Rtools are installed

  • @@ -756,7 +766,8 @@

    Contents

    @@ -171,9 +171,6 @@ advantage that the SFORB model can also be used for metabolites.

    endpoints(fit)
#> $ff #> logical(0) #> -#> $SFORB -#> logical(0) -#> #> $distimes #> DT50 DT90 DT50back #> parent 1.785233 15.1479 4.559973 diff --git a/docs/reference/plot.mmkin.html b/docs/reference/plot.mmkin.html index 9d41b4c1..be60f228 100644 --- a/docs/reference/plot.mmkin.html +++ b/docs/reference/plot.mmkin.html @@ -149,6 +149,7 @@ the fit of at least one model to the same dataset is shown.

main = "auto", legends = 1, resplot = c("time", "errmod"), + standardized = FALSE, show_errmin = TRUE, errmin_var = "All data", errmin_digits = 3, @@ -179,6 +180,12 @@ column.

Should the residuals plotted against time, using mkinresplot, or as squared residuals against predicted values, with the error model, using mkinerrplot.

+ + + standardized +

Should the residuals be standardized? This option +is passed to mkinresplot, it only takes effect if +`resplot = "time"`.

show_errmin diff --git a/man/endpoints.Rd b/man/endpoints.Rd index 13182369..be180737 100644 --- a/man/endpoints.Rd +++ b/man/endpoints.Rd @@ -26,7 +26,7 @@ The function is used internally by \code{\link{summary.mkinfit}}. \examples{ fit <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE) - endpoints(fit) + endpoints(fit) } \author{ diff --git a/man/plot.mmkin.Rd b/man/plot.mmkin.Rd index f14e0362..982e8db6 100644 --- a/man/plot.mmkin.Rd +++ b/man/plot.mmkin.Rd @@ -10,6 +10,7 @@ of an mmkin object} main = "auto", legends = 1, resplot = c("time", "errmod"), + standardized = FALSE, show_errmin = TRUE, errmin_var = "All data", errmin_digits = 3, @@ -31,6 +32,10 @@ column.} \code{\link{mkinresplot}}, or as squared residuals against predicted values, with the error model, using \code{\link{mkinerrplot}}.} +\item{standardized}{Should the residuals be standardized? This option +is passed to \code{\link{mkinresplot}}, it only takes effect if +`resplot = "time"`.} + \item{show_errmin}{Should the chi2 error level be shown on top of the plots to the left?} diff --git a/test.log b/test.log index 77130814..84b13a10 100644 --- a/test.log +++ b/test.log @@ -2,35 +2,35 @@ Loading mkin Testing mkin ✔ | OK F W S | Context ✔ | 2 | Export dataset for reading into CAKE -✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.7 s] -✔ | 4 | Calculation of FOCUS chi2 error levels [2.3 s] -✔ | 4 | Fitting the SFORB model [1.8 s] +✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.5 s] +✔ | 4 | Calculation of FOCUS chi2 error levels [2.2 s] +✔ | 4 | Fitting the SFORB model [1.7 s] ✔ | 5 | Calculation of Akaike weights -✔ | 10 | Confidence intervals and p-values [10.7 s] -✔ | 14 | Error model fitting [42.8 s] -✔ | 6 | Test fitting the decline of metabolites from their maximum [0.8 s] -✔ | 1 | Fitting the logistic model [1.0 s] +✔ | 10 | Confidence intervals and p-values [9.6 s] +✔ | 14 | Error model fitting [38.4 s] +✔ | 6 | Test fitting the decline of metabolites from their maximum [0.7 s] +✔ | 1 | Fitting the logistic model [0.9 s] ✔ | 1 | Test dataset class mkinds used in gmkin -✔ | 12 | Special cases of mkinfit calls [2.5 s] +✔ | 12 | Special cases of mkinfit calls [2.2 s] ✔ | 9 | mkinmod model generation and printing [0.2 s] -✔ | 3 | Model predictions with mkinpredict [0.4 s] -✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.4 s] -✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.3 s] +✔ | 3 | Model predictions with mkinpredict [0.3 s] +✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.2 s] +✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.2 s] ✔ | 3 | Summary ✔ | 11 | Plotting [0.6 s] ✔ | 4 | AIC calculation ✔ | 2 | Residuals extracted from mkinfit models -✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.7 s] +✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.3 s] ✔ | 1 | Summaries of old mkinfit objects -✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.7 s] -✔ | 7 1 | Hypothesis tests [35.7 s] +✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.1 s] +✔ | 7 1 | Hypothesis tests [32.5 s] ──────────────────────────────────────────────────────────────────────────────── test_tests.R:60: skip: We can do a likelihood ratio test using an update specification Reason: This errors out if called by testthat while it works in a normal R session ──────────────────────────────────────────────────────────────────────────────── ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 123.0 s +Duration: 112.1 s OK: 138 Failed: 0 diff --git a/tests/testthat/FOCUS_2006_D.csf b/tests/testthat/FOCUS_2006_D.csf index a7f8edaf..fe5e481a 100644 --- a/tests/testthat/FOCUS_2006_D.csf +++ b/tests/testthat/FOCUS_2006_D.csf @@ -5,7 +5,7 @@ Description: MeasurementUnits: % AR TimeUnits: days Comments: Created using mkin::CAKE_export -Date: 2020-03-31 +Date: 2020-04-01 Optimiser: IRLS [Data] diff --git a/vignettes/web_only/FOCUS_Z.R b/vignettes/web_only/FOCUS_Z.R new file mode 100644 index 00000000..0c19794e --- /dev/null +++ b/vignettes/web_only/FOCUS_Z.R @@ -0,0 +1,115 @@ +## ---- include = FALSE--------------------------------------------------------- +require(knitr) +options(digits = 5) +opts_chunk$set(engine='R', tidy = FALSE) + +## ---- echo = TRUE, fig = TRUE, fig.width = 8, fig.height = 7------------------ +library(mkin, quietly = TRUE) +LOD = 0.5 +FOCUS_2006_Z = data.frame( + t = c(0, 0.04, 0.125, 0.29, 0.54, 1, 2, 3, 4, 7, 10, 14, 21, + 42, 61, 96, 124), + Z0 = c(100, 81.7, 70.4, 51.1, 41.2, 6.6, 4.6, 3.9, 4.6, 4.3, 6.8, + 2.9, 3.5, 5.3, 4.4, 1.2, 0.7), + Z1 = c(0, 18.3, 29.6, 46.3, 55.1, 65.7, 39.1, 36, 15.3, 5.6, 1.1, + 1.6, 0.6, 0.5 * LOD, NA, NA, NA), + Z2 = c(0, NA, 0.5 * LOD, 2.6, 3.8, 15.3, 37.2, 31.7, 35.6, 14.5, + 0.8, 2.1, 1.9, 0.5 * LOD, NA, NA, NA), + Z3 = c(0, NA, NA, NA, NA, 0.5 * LOD, 9.2, 13.1, 22.3, 28.4, 32.5, + 25.2, 17.2, 4.8, 4.5, 2.8, 4.4)) + +FOCUS_2006_Z_mkin <- mkin_wide_to_long(FOCUS_2006_Z) + +## ----FOCUS_2006_Z_fits_1, echo=TRUE, fig.height=6----------------------------- +Z.2a <- mkinmod(Z0 = mkinsub("SFO", "Z1"), + Z1 = mkinsub("SFO")) +m.Z.2a <- mkinfit(Z.2a, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.2a) +summary(m.Z.2a, data = FALSE)$bpar + +## ----FOCUS_2006_Z_fits_2, echo=TRUE, fig.height=6----------------------------- +Z.2a.ff <- mkinmod(Z0 = mkinsub("SFO", "Z1"), + Z1 = mkinsub("SFO"), + use_of_ff = "max") + +m.Z.2a.ff <- mkinfit(Z.2a.ff, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.2a.ff) +summary(m.Z.2a.ff, data = FALSE)$bpar + +## ----FOCUS_2006_Z_fits_3, echo=TRUE, fig.height=6----------------------------- +Z.3 <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE), + Z1 = mkinsub("SFO"), use_of_ff = "max") +m.Z.3 <- mkinfit(Z.3, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.3) +summary(m.Z.3, data = FALSE)$bpar + +## ----FOCUS_2006_Z_fits_5, echo=TRUE, fig.height=7----------------------------- +Z.5 <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO"), use_of_ff = "max") +m.Z.5 <- mkinfit(Z.5, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.5) + +## ----FOCUS_2006_Z_fits_6, echo=TRUE, fig.height=8----------------------------- +Z.FOCUS <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO", "Z3"), + Z3 = mkinsub("SFO"), + use_of_ff = "max") +m.Z.FOCUS <- mkinfit(Z.FOCUS, FOCUS_2006_Z_mkin, + parms.ini = m.Z.5$bparms.ode, + quiet = TRUE) +plot_sep(m.Z.FOCUS) +summary(m.Z.FOCUS, data = FALSE)$bpar +endpoints(m.Z.FOCUS) + +## ----FOCUS_2006_Z_fits_7, echo=TRUE, fig.height=8----------------------------- +Z.mkin.1 <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO", "Z3"), + Z3 = mkinsub("SFORB")) +m.Z.mkin.1 <- mkinfit(Z.mkin.1, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.mkin.1) +summary(m.Z.mkin.1, data = FALSE)$cov.unscaled + +## ----FOCUS_2006_Z_fits_9, echo=TRUE, fig.height=8----------------------------- +Z.mkin.3 <- mkinmod(Z0 = mkinsub("SFORB", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO")) +m.Z.mkin.3 <- mkinfit(Z.mkin.3, FOCUS_2006_Z_mkin, quiet = TRUE) +plot_sep(m.Z.mkin.3) + +## ----FOCUS_2006_Z_fits_10, echo=TRUE, fig.height=8---------------------------- +Z.mkin.4 <- mkinmod(Z0 = mkinsub("SFORB", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO", "Z3"), + Z3 = mkinsub("SFO")) +m.Z.mkin.4 <- mkinfit(Z.mkin.4, FOCUS_2006_Z_mkin, + parms.ini = m.Z.mkin.3$bparms.ode, + quiet = TRUE) +plot_sep(m.Z.mkin.4) + +## ----FOCUS_2006_Z_fits_11, echo=TRUE, fig.height=8---------------------------- +Z.mkin.5 <- mkinmod(Z0 = mkinsub("SFORB", "Z1", sink = FALSE), + Z1 = mkinsub("SFO", "Z2", sink = FALSE), + Z2 = mkinsub("SFO", "Z3"), + Z3 = mkinsub("SFORB")) +m.Z.mkin.5 <- mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin, + parms.ini = m.Z.mkin.4$bparms.ode[1:4], + quiet = TRUE) +plot_sep(m.Z.mkin.5) + +## ----FOCUS_2006_Z_fits_11a, echo=TRUE----------------------------------------- +m.Z.mkin.5a <- mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin, + parms.ini = c(m.Z.mkin.5$bparms.ode[1:7], + k_Z3_bound_free = 0), + fixed_parms = "k_Z3_bound_free", + quiet = TRUE) +plot_sep(m.Z.mkin.5a) + +## ----FOCUS_2006_Z_fits_11b, echo=TRUE----------------------------------------- +mkinparplot(m.Z.mkin.5a) + +## ----FOCUS_2006_Z_fits_11b_endpoints, echo=TRUE------------------------------- +endpoints(m.Z.mkin.5a) + diff --git a/vignettes/web_only/FOCUS_Z.html b/vignettes/web_only/FOCUS_Z.html index d7f0f88c..e8e6e2b4 100644 --- a/vignettes/web_only/FOCUS_Z.html +++ b/vignettes/web_only/FOCUS_Z.html @@ -1,17 +1,17 @@ - + - + - + Example evaluation of FOCUS dataset Z @@ -69,8 +69,6 @@ overflow: auto; margin-left: 2%; position: fixed; border: 1px solid #ccc; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 6px; } @@ -98,10 +96,15 @@ font-size: 12px; .tocify-subheader .tocify-subheader { text-indent: 30px; } - .tocify-subheader .tocify-subheader .tocify-subheader { text-indent: 40px; } +.tocify-subheader .tocify-subheader .tocify-subheader .tocify-subheader { +text-indent: 50px; +} +.tocify-subheader .tocify-subheader .tocify-subheader .tocify-subheader .tocify-subheader { +text-indent: 60px; +} .tocify .tocify-item > a, .tocify .nav-list .nav-header { margin: 0px; @@ -504,13 +507,13 @@ float: none; item.append($("", { - "text": self.text() + "html": self.html() })); } else { - item.text(self.text()); + item.html(self.html()); } @@ -1280,12 +1283,12 @@ window.initializeCodeFolding = function(show) { var currentIndex = 1; // select all R code blocks - var rCodeBlocks = $('pre.r, pre.python, pre.bash, pre.sql, pre.cpp, pre.stan'); + var rCodeBlocks = $('pre.r, pre.python, pre.bash, pre.sql, pre.cpp, pre.stan, pre.julia'); rCodeBlocks.each(function() { // create a collapsable div to wrap the code in var div = $('
'); - if (show) + if (show || $(this)[0].classList.contains('fold-show')) div.addClass('in'); var id = 'rcode-643E0F36' + currentIndex++; div.attr('id', id); @@ -1387,9 +1390,7 @@ h6 { - - -
- - - - +.tabset-dropdown > .nav-tabs { + display: inline-table; + max-height: 500px; + min-height: 44px; + overflow-y: auto; + background: white; + border: 1px solid #ddd; + border-radius: 4px; +} +.tabset-dropdown > .nav-tabs > li.active:before { + content: ""; + font-family: 'Glyphicons Halflings'; + display: inline-block; + padding: 10px; + border-right: 1px solid #ddd; +} +.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before { + content: ""; + border: none; +} +.tabset-dropdown > .nav-tabs.nav-tabs-open:before { + content: ""; + font-family: 'Glyphicons Halflings'; + display: inline-block; + padding: 10px; + border-right: 1px solid #ddd; +} - + + + + + + + +
+ +
@@ -1547,8 +1582,8 @@ div.tocify {

Example evaluation of FOCUS dataset Z

-

Johannes Ranke

-

2018-09-14

+

Johannes Ranke

+

2020-04-02

@@ -1579,44 +1614,53 @@ FOCUS_2006_Z_mkin <- mkin_wide_to_long(FOCUS_2006_Z)
Z.2a <- mkinmod(Z0 = mkinsub("SFO", "Z1"),
                 Z1 = mkinsub("SFO"))
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.2a <- mkinfit(Z.2a, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.2a)
-

+
m.Z.2a <- mkinfit(Z.2a, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.2a, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.2a)
+

summary(m.Z.2a, data = FALSE)$bpar
-
##             Estimate se_notrans    t value     Pr(>t) Lower Upper
-## Z0_0      9.7015e+01   3.553140 2.7304e+01 1.6793e-21    NA    NA
-## k_Z0_sink 1.2790e-11   0.226895 5.6368e-11 5.0000e-01    NA    NA
-## k_Z0_Z1   2.2360e+00   0.165073 1.3546e+01 7.3938e-14    NA    NA
-## k_Z1_sink 4.8212e-01   0.065854 7.3212e+00 3.5520e-08    NA    NA
+
##             Estimate se_notrans    t value     Pr(>t)    Lower    Upper
+## Z0_0      9.7015e+01   3.394528 2.8580e+01 6.4978e-21 91.66556 102.3642
+## k_Z0_sink 1.6067e-10   0.225471 7.1261e-10 5.0000e-01  0.00000      Inf
+## k_Z0_Z1   2.2360e+00   0.159156 1.4049e+01 1.1405e-13  1.95303   2.5600
+## k_Z1_sink 4.8212e-01   0.065492 7.3616e+00 5.1697e-08  0.40341   0.5762
+## sigma     4.8041e+00   0.637651 7.5341e+00 3.4463e-08  3.52677   6.0815

As obvious from the parameter summary (the component of the summary), the kinetic rate constant from parent compound Z to sink is very small and the t-test for this parameter suggests that it is not significantly different from zero. This suggests, in agreement with the analysis in the FOCUS kinetics report, to simplify the model by removing the pathway to sink.

A similar result can be obtained when formation fractions are used in the model formulation:

Z.2a.ff <- mkinmod(Z0 = mkinsub("SFO", "Z1"),
                    Z1 = mkinsub("SFO"),
                    use_of_ff = "max")
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.2a.ff <- mkinfit(Z.2a.ff, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.2a.ff)
-

+
m.Z.2a.ff <- mkinfit(Z.2a.ff, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.2a.ff, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.2a.ff)
+

summary(m.Z.2a.ff, data = FALSE)$bpar
-
##            Estimate se_notrans t value     Pr(>t) Lower Upper
-## Z0_0       97.01488   3.553145 27.3039 1.6793e-21    NA    NA
-## k_Z0        2.23601   0.216849 10.3114 3.6623e-11    NA    NA
-## k_Z1        0.48212   0.065854  7.3211 3.5520e-08    NA    NA
-## f_Z0_to_Z1  1.00000   0.101473  9.8548 9.7068e-11    NA    NA
+
##            Estimate se_notrans t value     Pr(>t)    Lower    Upper
+## Z0_0       97.01488   3.301084 29.3888 3.2971e-21 91.66556 102.3642
+## k_Z0        2.23601   0.207078 10.7979 3.3309e-11  1.95303   2.5600
+## k_Z1        0.48212   0.063265  7.6207 2.8155e-08  0.40341   0.5762
+## f_Z0_to_Z1  1.00000   0.094764 10.5525 5.3560e-11  0.00000   1.0000
+## sigma       4.80411   0.635638  7.5579 3.2592e-08  3.52677   6.0815

Here, the ilr transformed formation fraction fitted in the model takes a very large value, and the backtransformed formation fraction from parent Z to Z1 is practically unity. Here, the covariance matrix used for the calculation of confidence intervals is not returned as the model is overparameterised.

A simplified model is obtained by removing the pathway to the sink.

In the following, we use the parameterisation with formation fractions in order to be able to compare with the results in the FOCUS guidance, and as it makes it easier to use parameters obtained in a previous fit when adding a further metabolite.

Z.3 <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE),
                Z1 = mkinsub("SFO"), use_of_ff = "max")
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.3 <- mkinfit(Z.3, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.3)
-

+
m.Z.3 <- mkinfit(Z.3, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.3, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.3)
+

summary(m.Z.3, data = FALSE)$bpar
-
##      Estimate se_notrans t value     Pr(>t)    Lower   Upper
-## Z0_0 97.01488   2.681772  36.176 2.3636e-25 91.52152 102.508
-## k_Z0  2.23601   0.146861  15.225 2.2464e-15  1.95453   2.558
-## k_Z1  0.48212   0.042687  11.294 3.0686e-12  0.40216   0.578
+
##       Estimate se_notrans t value     Pr(>t)    Lower    Upper
+## Z0_0  97.01488   2.597342  37.352 2.0106e-24 91.67597 102.3538
+## k_Z0   2.23601   0.146904  15.221 9.1477e-15  1.95354   2.5593
+## k_Z1   0.48212   0.041727  11.554 4.8268e-12  0.40355   0.5760
+## sigma  4.80411   0.620208   7.746 1.6110e-08  3.52925   6.0790

As there is only one transformation product for Z0 and no pathway to sink, the formation fraction is internally fixed to unity.

@@ -1626,9 +1670,11 @@ plot_sep(m.Z.3) Z1 = mkinsub("SFO", "Z2", sink = FALSE), Z2 = mkinsub("SFO"), use_of_ff = "max")
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.5 <- mkinfit(Z.5, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.5)
-

+
m.Z.5 <- mkinfit(Z.5, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.5, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.5)
+

Finally, metabolite Z3 is added to the model. We use the optimised differential equation parameter values from the previous fit in order to accelerate the optimization.

Z.FOCUS <- mkinmod(Z0 = mkinsub("SFO", "Z1", sink = FALSE),
                    Z1 = mkinsub("SFO", "Z2", sink = FALSE),
@@ -1639,32 +1685,30 @@ plot_sep(m.Z.5)
m.Z.FOCUS <- mkinfit(Z.FOCUS, FOCUS_2006_Z_mkin,
                      parms.ini = m.Z.5$bparms.ode,
                      quiet = TRUE)
-
## Warning in mkinfit(Z.FOCUS, FOCUS_2006_Z_mkin, parms.ini = m.Z.5$bparms.ode, : Optimisation by method Port did not converge:
-## false convergence (8)
+
## Warning in mkinfit(Z.FOCUS, FOCUS_2006_Z_mkin, parms.ini = m.Z.5$bparms.ode, :
+## Observations with value of zero were removed from the data
plot_sep(m.Z.FOCUS)
-

+

summary(m.Z.FOCUS, data = FALSE)$bpar
##             Estimate se_notrans t value     Pr(>t)     Lower      Upper
-## Z0_0       96.837112   2.058861 47.0343 5.5877e-44 92.703779 100.970445
-## k_Z0        2.215368   0.118098 18.7587 7.6563e-25  1.990525   2.465609
-## k_Z1        0.478302   0.029289 16.3302 3.3408e-22  0.422977   0.540864
-## k_Z2        0.451617   0.044214 10.2144 3.1133e-14  0.371034   0.549702
-## k_Z3        0.058693   0.014296  4.1056 7.2924e-05  0.035994   0.095705
-## f_Z2_to_Z3  0.471516   0.057057  8.2639 2.8156e-11  0.360381   0.585548
+## Z0_0 96.838599 1.994271 48.5584 4.0281e-42 92.826628 100.850570 +## k_Z0 2.215405 0.118459 18.7019 1.0415e-23 1.989466 2.467003 +## k_Z1 0.478298 0.028257 16.9268 6.2399e-22 0.424701 0.538660 +## k_Z2 0.451618 0.042137 10.7177 1.6306e-14 0.374328 0.544866 +## k_Z3 0.058693 0.015246 3.8499 1.7803e-04 0.034805 0.098977 +## f_Z2_to_Z3 0.471509 0.058352 8.0804 9.6629e-11 0.357739 0.588318 +## sigma 3.984431 0.383402 10.3923 4.5575e-14 3.213126 4.755736
endpoints(m.Z.FOCUS)
## $ff
 ##   Z2_Z3 Z2_sink 
-## 0.47152 0.52848 
-## 
-## $SFORB
-## logical(0)
+## 0.47151 0.52849 
 ## 
 ## $distimes
 ##        DT50    DT90
 ## Z0  0.31288  1.0394
-## Z1  1.44918  4.8141
+## Z1  1.44919  4.8141
 ## Z2  1.53481  5.0985
-## Z3 11.80973 39.2311
+## Z3 11.80963 39.2308

This fit corresponds to the final result chosen in Appendix 7 of the FOCUS report. Confidence intervals returned by mkin are based on internally transformed parameters, however.

@@ -1676,9 +1720,11 @@ plot_sep(m.Z.5) Z2 = mkinsub("SFO", "Z3"), Z3 = mkinsub("SFORB"))
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.mkin.1 <- mkinfit(Z.mkin.1, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.mkin.1)
-

+
m.Z.mkin.1 <- mkinfit(Z.mkin.1, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.mkin.1, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.mkin.1)
+

summary(m.Z.mkin.1, data = FALSE)$cov.unscaled
## NULL

Therefore, a further stepwise model building is performed starting from the stage of parent and two metabolites, starting from the assumption that the model fit for the parent compound can be improved by using the SFORB model.

@@ -1686,9 +1732,11 @@ plot_sep(m.Z.mkin.1) Z1 = mkinsub("SFO", "Z2", sink = FALSE), Z2 = mkinsub("SFO"))
## Successfully compiled differential equation model from auto-generated C code.
-
m.Z.mkin.3 <- mkinfit(Z.mkin.3, FOCUS_2006_Z_mkin, quiet = TRUE)
-plot_sep(m.Z.mkin.3)
-

+
m.Z.mkin.3 <- mkinfit(Z.mkin.3, FOCUS_2006_Z_mkin, quiet = TRUE)
+
## Warning in mkinfit(Z.mkin.3, FOCUS_2006_Z_mkin, quiet = TRUE): Observations with
+## value of zero were removed from the data
+
plot_sep(m.Z.mkin.3)
+

This results in a much better representation of the behaviour of the parent compound Z0.

Finally, Z3 is added as well. These models appear overparameterised (no covariance matrix returned) if the sink for Z1 is left in the models.

Z.mkin.4 <- mkinmod(Z0 = mkinsub("SFORB", "Z1", sink = FALSE),
@@ -1698,9 +1746,11 @@ plot_sep(m.Z.mkin.3)
## Successfully compiled differential equation model from auto-generated C code.
m.Z.mkin.4 <- mkinfit(Z.mkin.4, FOCUS_2006_Z_mkin,
                       parms.ini = m.Z.mkin.3$bparms.ode,
-                      quiet = TRUE)
-plot_sep(m.Z.mkin.4)
-

+ quiet = TRUE) +
## Warning in mkinfit(Z.mkin.4, FOCUS_2006_Z_mkin, parms.ini = m.Z.mkin.
+## 3$bparms.ode, : Observations with value of zero were removed from the data
+
plot_sep(m.Z.mkin.4)
+

The error level of the fit, but especially of metabolite Z3, can be improved if the SFORB model is chosen for this metabolite, as this model is capable of representing the tailing of the metabolite decline phase.

Z.mkin.5 <- mkinmod(Z0 = mkinsub("SFORB", "Z1", sink = FALSE),
                     Z1 = mkinsub("SFO", "Z2", sink = FALSE),
@@ -1709,21 +1759,25 @@ plot_sep(m.Z.mkin.4)
## Successfully compiled differential equation model from auto-generated C code.
m.Z.mkin.5 <- mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin,
                       parms.ini = m.Z.mkin.4$bparms.ode[1:4],
-                      quiet = TRUE)
-plot_sep(m.Z.mkin.5)
-

+ quiet = TRUE) +
## Warning in mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin, parms.ini = m.Z.mkin.
+## 4$bparms.ode[1:4], : Observations with value of zero were removed from the data
+
plot_sep(m.Z.mkin.5)
+

The summary view of the backtransformed parameters shows that we get no confidence intervals due to overparameterisation. As the optimized is excessively small, it seems reasonable to fix it to zero.

m.Z.mkin.5a <- mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin,
                        parms.ini = c(m.Z.mkin.5$bparms.ode[1:7],
                                      k_Z3_bound_free = 0),
                        fixed_parms = "k_Z3_bound_free",
-                       quiet = TRUE)
-plot_sep(m.Z.mkin.5a)
-

+ quiet = TRUE) +
## Warning in mkinfit(Z.mkin.5, FOCUS_2006_Z_mkin, parms.ini = c(m.Z.mkin.
+## 5$bparms.ode[1:7], : Observations with value of zero were removed from the data
+
plot_sep(m.Z.mkin.5a)
+

As expected, the residual plots for Z0 and Z3 are more random than in the case of the all SFO model for which they were shown above. In conclusion, the model is proposed as the best-fit model for the dataset from Appendix 7 of the FOCUS report.

A graphical representation of the confidence intervals can finally be obtained.

mkinparplot(m.Z.mkin.5a)
-

+

The endpoints obtained with this model are

endpoints(m.Z.mkin.5a)
## $ff
@@ -1732,14 +1786,14 @@ plot_sep(m.Z.mkin.5a)
## ## $SFORB ## Z0_b1 Z0_b2 Z3_b1 Z3_b2 -## 2.4471382 0.0075127 0.0800075 0.0000000 +## 2.4471363 0.0075126 0.0800072 0.0000000 ## ## $distimes ## DT50 DT90 DT50_Z0_b1 DT50_Z0_b2 DT50_Z3_b1 DT50_Z3_b2 -## Z0 0.3043 1.1848 0.28325 92.264 NA NA +## Z0 0.3043 1.1848 0.28325 92.265 NA NA ## Z1 1.5148 5.0320 NA NA NA NA ## Z2 1.6414 5.4526 NA NA NA NA -## Z3 NA NA NA NA 8.6635 Inf +## Z3 NA NA NA NA 8.6636 Inf

It is clear the degradation rate of Z3 towards the end of the experiment is very low as DT50_Z3_b2 (the second Eigenvalue of the system of two differential equations representing the SFORB system for Z3, corresponding to the slower rate constant of the DFOP model) is reported to be infinity. However, this appears to be a feature of the data.

@@ -1772,6 +1826,54 @@ $(document).ready(function () { + + + + + + + + + - - - - + + + + + + + +
+ +
@@ -1613,25 +1582,36 @@ div.tocify {

Performance benefit by using compiled model definitions in mkin

-

Johannes Ranke

-

2019-04-04

+

Johannes Ranke

+

2020-04-02

-
-

Model that can also be solved with Eigenvalues

-

This evaluation is taken from the example section of mkinfit. When using an mkin version equal to or greater than 0.9-36 and a C compiler (gcc) is available, you will see a message that the model is being compiled from autogenerated C code when defining a model using mkinmod. The mkinmod() function checks for presence of the gcc compiler using

-
Sys.which("gcc")
-
##            gcc 
-## "/usr/bin/gcc"
+
+

How to benefit from compiled models

+

When using an mkin version equal to or greater than 0.9-36 and a C compiler is available, you will see a message that the model is being compiled from autogenerated C code when defining a model using mkinmod. Starting from version 0.9.49.9, the mkinmod() function checks for presence of a compiler using

+
pkgbuild::has_compiler()
+

In previous versions, it used Sys.which("gcc") for this check.

+
+

Platform specific notes

+

On Linux, you need to have the essential build tools like make and gcc or clang installed. On Debian based linux distributions, these will be pulled in by installing the build-essential package.

+

On MacOS, which I do not use personally, I have had reports that a compiler is available by default.

+

On Windows, you need to install Rtools and have the path to its bin directory in your PATH variable. You do not need to modify the PATH variable when installing Rtools. Instead, I would recommend to put the line

+
Sys.setenv(PATH = paste("C:/Rtools/bin", Sys.getenv("PATH"), sep=";"))
+

into your .Rprofile startup file. This is just a text file with some R code that is executed when your R session starts. It has to be named .Rprofile and has to be located in your home directory, which will generally be your Documents folder. You can check the location of the home directory used by R by issuing

+
Sys.getenv("HOME")
+
+
+
+

Comparison with Eigenvalue based solutions

First, we build a simple degradation model for a parent compound with one metabolite.

library("mkin", quietly = TRUE)
 SFO_SFO <- mkinmod(
   parent = mkinsub("SFO", "m1"),
   m1 = mkinsub("SFO"))
## Successfully compiled differential equation model from auto-generated C code.
-

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the benchmark package.

+

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the benchmark package. In the output of below code, the warnings about zero being removed from the FOCUS D dataset are suppressed.

if (require(rbenchmark)) {
   b.1 <- benchmark(
     "deSolve, not compiled" = mkinfit(SFO_SFO, FOCUS_2006_D,
@@ -1648,16 +1628,15 @@ SFO_SFO <- mkinmod(
   factor_SFO_SFO <- NA
   print("R package rbenchmark is not available")
 }
-
## Loading required package: rbenchmark
##                    test replications elapsed relative user.self sys.self
-## 3     deSolve, compiled            3   3.533    1.000     3.531        0
-## 1 deSolve, not compiled            3  46.050   13.034    46.030        0
-## 2      Eigenvalue based            3   5.068    1.434     5.066        0
+## 3     deSolve, compiled            3   3.148    1.000     3.146    0.000
+## 1 deSolve, not compiled            3  28.920    9.187    28.904    0.001
+## 2      Eigenvalue based            3   4.442    1.411     4.439    0.000
 ##   user.child sys.child
 ## 3          0         0
 ## 1          0         0
 ## 2          0         0
-

We see that using the compiled model is by a factor of around 13 faster than using the R version with the default ode solver, and it is even faster than the Eigenvalue based solution implemented in R which does not need iterative solution of the ODEs.

+

We see that using the compiled model is by a factor of around 9 faster than using the R version with the default ode solver, and it is even faster than the Eigenvalue based solution implemented in R which does not need iterative solution of the ODEs.

Model that can not be solved with Eigenvalues

@@ -1680,16 +1659,16 @@ SFO_SFO <- mkinmod( }
## Successfully compiled differential equation model from auto-generated C code.
##                    test replications elapsed relative user.self sys.self
-## 2     deSolve, compiled            3   4.934    1.000     4.931        0
-## 1 deSolve, not compiled            3  72.993   14.794    72.961        0
+## 2     deSolve, compiled            3   4.879    1.000     4.877        0
+## 1 deSolve, not compiled            3  53.551   10.976    53.525        0
 ##   user.child sys.child
 ## 2          0         0
 ## 1          0         0
-

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

-

This vignette was built with mkin 0.9.49.1 on

-
## R version 3.5.3 (2019-03-11)
+

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

+

This vignette was built with mkin 0.9.49.9 on

+
## R version 3.6.3 (2020-02-29)
 ## Platform: x86_64-pc-linux-gnu (64-bit)
-## Running under: Debian GNU/Linux 9 (stretch)
+## Running under: Debian GNU/Linux 10 (buster)
## CPU model: AMD Ryzen 7 1700 Eight-Core Processor
@@ -1713,6 +1692,54 @@ $(document).ready(function () { + + + + + + + + +