From c6079a807e2b400fe0c772603392aeacd887da2f Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Wed, 8 May 2019 20:57:48 +0200 Subject: Add functionality to plot the error model by plotting squared residuals against predicted values, and showing the variance function used in the fitted error model. Rebuild docs --- NEWS.md | 5 +- R/mkinerrplot.R | 84 ++++++++++ R/mkinresplot.R | 14 +- R/plot.mkinfit.R | 19 ++- R/plot.mmkin.R | 14 +- docs/articles/FOCUS_D.html | 8 +- docs/articles/FOCUS_L.html | 44 ++--- docs/articles/mkin.html | 2 +- docs/articles/twa.html | 2 +- docs/articles/web_only/FOCUS_Z.html | 2 +- docs/articles/web_only/NAFTA_examples.html | 2 +- docs/articles/web_only/benchmarks.html | 24 +-- docs/articles/web_only/compiled_models.html | 12 +- docs/news/index.html | 7 +- docs/reference/mkinerrplot-1.png | Bin 0 -> 35936 bytes docs/reference/mkinerrplot.html | 250 ++++++++++++++++++++++++++++ docs/reference/mkinfit.html | 28 ++-- docs/reference/mkinmod.html | 2 +- docs/reference/mkinpredict.html | 6 +- docs/reference/mmkin.html | 4 +- docs/reference/plot.mkinfit-1.png | Bin 45182 -> 45207 bytes docs/reference/plot.mkinfit-2.png | Bin 52916 -> 52884 bytes docs/reference/plot.mkinfit-3.png | Bin 43718 -> 51737 bytes docs/reference/plot.mkinfit-4.png | Bin 59800 -> 43712 bytes docs/reference/plot.mkinfit-5.png | Bin 0 -> 59934 bytes docs/reference/plot.mkinfit-6.png | Bin 0 -> 65811 bytes docs/reference/plot.mkinfit.html | 29 +++- docs/reference/plot.mmkin-2.png | Bin 34829 -> 34725 bytes docs/reference/plot.mmkin-4.png | Bin 0 -> 37126 bytes docs/reference/plot.mmkin.html | 19 ++- docs/reference/summary.mkinfit.html | 6 +- man/mkinerrplot.Rd | 75 +++++++++ man/plot.mkinfit.Rd | 21 ++- man/plot.mmkin.Rd | 17 +- test.log | 31 ++-- tests/testthat/FOCUS_2006_D.csf | 2 +- vignettes/mkin_benchmarks.rda | Bin 798 -> 797 bytes 37 files changed, 600 insertions(+), 129 deletions(-) create mode 100644 R/mkinerrplot.R create mode 100644 docs/reference/mkinerrplot-1.png create mode 100644 docs/reference/mkinerrplot.html create mode 100644 docs/reference/plot.mkinfit-5.png create mode 100644 docs/reference/plot.mkinfit-6.png create mode 100644 docs/reference/plot.mmkin-4.png create mode 100644 man/mkinerrplot.Rd diff --git a/NEWS.md b/NEWS.md index ad6e9a87..f250ceba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,11 @@ -# mkin 0.9.49.4 (2019-05-07) +# mkin 0.9.49.4 (2019-05-08) + - Direct minimization of the negative log-likelihood for non-constant error models (two-component and variance by variable). In the case the error model is constant variance, least squares is used as this is more stable - The argument 'reweight.method' to mkinfit and mmkin is now obsolete, use 'error_model' instead +- New function 'mkinerrplot'. This function is also used for residual plots in 'plot.mmkin' if the argument 'resplot = "errmod"' is given, and in 'plot.mkinfit' if 'show_errplot' is set to TRUE. + - Remove dependency on FME, only use nlminb for optimisation - Use the numDeriv package to calculate hessians diff --git a/R/mkinerrplot.R b/R/mkinerrplot.R new file mode 100644 index 00000000..a2adcefa --- /dev/null +++ b/R/mkinerrplot.R @@ -0,0 +1,84 @@ +# Copyright (C) 2008-2014,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 +if(getRversion() >= '2.15.1') utils::globalVariables(c("variable", "residual")) + +mkinerrplot <- function (object, + obs_vars = names(object$mkinmod$map), + xlim = c(0, 1.1 * max(object$data$predicted)), + xlab = "Predicted", ylab = "Squared residual", + maxy = "auto", legend= TRUE, lpos = "topright", + col_obs = "auto", pch_obs = "auto", + ...) +{ + obs_vars_all <- as.character(unique(object$data$variable)) + + if (length(obs_vars) > 0){ + obs_vars <- intersect(obs_vars_all, obs_vars) + } else obs_vars <- obs_vars_all + + residuals <- subset(object$data, variable %in% obs_vars, residual) + + if (maxy == "auto") maxy = max(residuals^2, na.rm = TRUE) + + # Set colors and symbols + if (col_obs[1] == "auto") { + col_obs <- 1:length(obs_vars) + } + + if (pch_obs[1] == "auto") { + pch_obs <- 1:length(obs_vars) + } + names(col_obs) <- names(pch_obs) <- obs_vars + + plot(0, type = "n", + xlab = xlab, ylab = ylab, + xlim = xlim, + ylim = c(0, 1.2 * maxy), ...) + + for(obs_var in obs_vars){ + residuals_plot <- subset(object$data, variable == obs_var, c("predicted", "residual")) + points(residuals_plot[["predicted"]], + residuals_plot[["residual"]]^2, + pch = pch_obs[obs_var], col = col_obs[obs_var]) + } + + if (object$err_mod == "const") { + abline(h = object$errparms^2, lty = 2, col = col_obs[obs_var]) + } + if (object$err_mod == "obs") { + for (obs_var in obs_vars) { + sigma_name = paste0("sigma_", obs_var) + abline(h = object$errparms[sigma_name]^2, lty = 2, + col = col_obs[obs_var]) + } + } + if (object$err_mod == "tc") { + sigma_plot <- function(predicted) { + sigma_twocomp(predicted, + sigma_low = object$errparms[1], + rsd_high = object$errparms[2])^2 + } + plot(sigma_plot, from = 0, to = max(object$data$predicted), + add = TRUE, lty = 2, col = col_obs[obs_var]) + } + + if (legend == TRUE) { + legend(lpos, inset = c(0.05, 0.05), legend = obs_vars, + col = col_obs[obs_vars], pch = pch_obs[obs_vars]) + } +} diff --git a/R/mkinresplot.R b/R/mkinresplot.R index 3650ef4b..739a80e9 100644 --- a/R/mkinresplot.R +++ b/R/mkinresplot.R @@ -23,7 +23,7 @@ mkinresplot <- function (object, xlab = "Time", ylab = "Residual", maxabs = "auto", legend= TRUE, lpos = "topright", ...) { - obs_vars_all <- as.character(unique(object$data$variable)) + obs_vars_all <- as.character(unique(object$data$variable)) if (length(obs_vars) > 0){ obs_vars <- intersect(obs_vars_all, obs_vars) @@ -33,18 +33,18 @@ mkinresplot <- function (object, if (maxabs == "auto") maxabs = max(abs(residuals), na.rm = TRUE) - col_obs <- pch_obs <- 1:length(obs_vars) - names(col_obs) <- names(pch_obs) <- obs_vars + col_obs <- pch_obs <- 1:length(obs_vars) + names(col_obs) <- names(pch_obs) <- obs_vars plot(0, type = "n", xlab = xlab, ylab = ylab, xlim = xlim, ylim = c(-1.2 * maxabs, 1.2 * maxabs), ...) - for(obs_var in obs_vars){ - residuals_plot <- subset(object$data, variable == obs_var, c("time", "residual")) - points(residuals_plot, pch = pch_obs[obs_var], col = col_obs[obs_var]) - } + for(obs_var in obs_vars){ + residuals_plot <- subset(object$data, variable == obs_var, c("time", "residual")) + points(residuals_plot, pch = pch_obs[obs_var], col = col_obs[obs_var]) + } abline(h = 0, lty = 2) diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R index ee836eb8..df9888e7 100644 --- a/R/plot.mkinfit.R +++ b/R/plot.mkinfit.R @@ -1,4 +1,4 @@ -# Copyright (C) 2010-2016 Johannes Ranke +# Copyright (C) 2010-2016,2019 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package mkin @@ -26,12 +26,16 @@ plot.mkinfit <- function(x, fit = x, pch_obs = col_obs, lty_obs = rep(1, length(obs_vars)), add = FALSE, legend = !add, - show_residuals = FALSE, maxabs = "auto", + show_residuals = FALSE, + show_errplot = FALSE, + maxabs = "auto", sep_obs = FALSE, rel.height.middle = 0.9, lpos = "topright", inset = c(0.05, 0.05), show_errmin = FALSE, errmin_digits = 3, ...) { if (add && show_residuals) stop("If adding to an existing plot we can not show residuals") + if (add && show_errplot) stop("If adding to an existing plot we can not show the error model plot") + if (show_residuals && show_errplot) stop("We can either show residuals over time or the error model plot, not both") if (add && sep_obs) stop("If adding to an existing plot we can not show observed variables separately") solution_type = fit$solution_type @@ -68,8 +72,7 @@ plot.mkinfit <- function(x, fit = x, # Create a plot layout only if not to be added to an existing plot # or only a single plot is requested (e.g. by plot.mmkin) do_layout = FALSE - if (show_residuals) do_layout = TRUE - if (sep_obs) do_layout = TRUE + if (show_residuals | sep_obs | show_errplot) do_layout = TRUE n_plot_rows = if (sep_obs) length(obs_vars) else 1 if (do_layout) { @@ -78,7 +81,7 @@ plot.mkinfit <- function(x, fit = x, # If the observed variables are shown separately, do row layout if (sep_obs) { - n_plot_cols = if (show_residuals) 2 else 1 + n_plot_cols = if (show_residuals | show_errplot) 2 else 1 n_plots = n_plot_rows * n_plot_cols # Set relative plot heights, so the first and the last plot are the norm @@ -201,6 +204,12 @@ plot.mkinfit <- function(x, fit = x, } abline(h = 0, lty = 2) } + + # Show error model plot if requested + if (show_errplot) { + mkinerrplot(fit, obs_vars = row_obs_vars, pch_obs = pch_obs[row_obs_vars], col_obs = col_obs[row_obs_vars], + legend = FALSE) + } } if (do_layout) par(oldpar, no.readonly = TRUE) } diff --git a/R/plot.mmkin.R b/R/plot.mmkin.R index ee7075d3..c9d98718 100644 --- a/R/plot.mmkin.R +++ b/R/plot.mmkin.R @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2016 Johannes Ranke +# Copyright (C) 2015-2016,2019 Johannes Ranke # Contact: jranke@uni-bremen.de # This file is part of the R package mkin @@ -16,11 +16,15 @@ # You should have received a copy of the GNU General Public License along with # this program. If not, see -plot.mmkin <- function(x, main = "auto", legends = 1, errmin_var = "All data", errmin_digits = 3, +plot.mmkin <- function(x, main = "auto", legends = 1, + resplot = c("time", "errmod"), + errmin_var = "All data", errmin_digits = 3, cex = 0.7, rel.height.middle = 0.9, ...) { n.m <- nrow(x) n.d <- ncol(x) + resplot <- match.arg(resplot) + # We can handle either a row (different models, same dataset) # or a column (same model, different datasets) if (n.m > 1 & n.d > 1) stop("Please select fits either for one model or for one dataset") @@ -90,7 +94,11 @@ plot.mmkin <- function(x, main = "auto", legends = 1, errmin_var = "All data", e } mtext(chi2_text, cex = cex, line = 0.4) - mkinresplot(fit, legend = FALSE, ...) + if (resplot == "time") { + mkinresplot(fit, legend = FALSE, ...) + } else { + mkinerrplot(fit, legend = FALSE, ...) + } mtext(paste(fit_name, "residuals"), cex = cex, line = 0.4) } diff --git a/docs/articles/FOCUS_D.html b/docs/articles/FOCUS_D.html index 644a35b5..ba694065 100644 --- a/docs/articles/FOCUS_D.html +++ b/docs/articles/FOCUS_D.html @@ -88,7 +88,7 @@

Example evaluation of FOCUS Example Dataset D

Johannes Ranke

-

2019-05-07

+

2019-05-08

@@ -168,8 +168,8 @@
summary(fit)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:46 2019 
-## Date of summary: Tue May  7 08:37:46 2019 
+## Date of fit:     Wed May  8 20:52:27 2019 
+## Date of summary: Wed May  8 20:52:27 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - k_parent_sink * parent - k_parent_m1 * parent
@@ -177,7 +177,7 @@
 ## 
 ## Model predictions using solution type deSolve 
 ## 
-## Fitted using 389 model solutions performed in 1.018 s
+## Fitted using 389 model solutions performed in 0.998 s
 ## 
 ## Error model:
 ## Constant variance 
diff --git a/docs/articles/FOCUS_L.html b/docs/articles/FOCUS_L.html
index 2cffb323..6771bf63 100644
--- a/docs/articles/FOCUS_L.html
+++ b/docs/articles/FOCUS_L.html
@@ -88,7 +88,7 @@
       

Example evaluation of FOCUS Laboratory Data L1 to L3

Johannes Ranke

-

2019-05-07

+

2019-05-08

@@ -114,15 +114,15 @@ summary(m.L1.SFO)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:48 2019 
-## Date of summary: Tue May  7 08:37:48 2019 
+## Date of fit:     Wed May  8 20:52:29 2019 
+## Date of summary: Wed May  8 20:52:29 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 133 model solutions performed in 0.284 s
+## Fitted using 133 model solutions performed in 0.276 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -215,8 +215,8 @@
 ## finite result is doubtful
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:50 2019 
-## Date of summary: Tue May  7 08:37:50 2019 
+## Date of fit:     Wed May  8 20:52:31 2019 
+## Date of summary: Wed May  8 20:52:31 2019 
 ## 
 ## 
 ## Warning: Optimisation did not converge:
@@ -228,7 +228,7 @@
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 899 model solutions performed in 1.913 s
+## Fitted using 899 model solutions performed in 1.868 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -319,15 +319,15 @@
 
summary(m.L2.FOMC, data = FALSE)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:51 2019 
-## Date of summary: Tue May  7 08:37:51 2019 
+## Date of fit:     Wed May  8 20:52:32 2019 
+## Date of summary: Wed May  8 20:52:32 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 239 model solutions performed in 0.494 s
+## Fitted using 239 model solutions performed in 0.483 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -394,8 +394,8 @@
 
summary(m.L2.DFOP, data = FALSE)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:53 2019 
-## Date of summary: Tue May  7 08:37:53 2019 
+## Date of fit:     Wed May  8 20:52:33 2019 
+## Date of summary: Wed May  8 20:52:33 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) *
@@ -404,7 +404,7 @@
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 572 model solutions performed in 1.244 s
+## Fitted using 572 model solutions performed in 1.185 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -493,8 +493,8 @@
 
summary(mm.L3[["DFOP", 1]])
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:55 2019 
-## Date of summary: Tue May  7 08:37:55 2019 
+## Date of fit:     Wed May  8 20:52:35 2019 
+## Date of summary: Wed May  8 20:52:35 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) *
@@ -503,7 +503,7 @@
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 373 model solutions performed in 0.791 s
+## Fitted using 373 model solutions performed in 0.768 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -598,15 +598,15 @@
 
summary(mm.L4[["SFO", 1]], data = FALSE)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:56 2019 
-## Date of summary: Tue May  7 08:37:56 2019 
+## Date of fit:     Wed May  8 20:52:36 2019 
+## Date of summary: Wed May  8 20:52:37 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 142 model solutions performed in 0.29 s
+## Fitted using 142 model solutions performed in 0.289 s
 ## 
 ## Error model:
 ## Constant variance 
@@ -662,15 +662,15 @@
 
summary(mm.L4[["FOMC", 1]], data = FALSE)
## mkin version used for fitting:    0.9.49.4 
 ## R version used for fitting:       3.6.0 
-## Date of fit:     Tue May  7 08:37:56 2019 
-## Date of summary: Tue May  7 08:37:56 2019 
+## Date of fit:     Wed May  8 20:52:36 2019 
+## Date of summary: Wed May  8 20:52:37 2019 
 ## 
 ## Equations:
 ## d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted using 224 model solutions performed in 0.453 s
+## Fitted using 224 model solutions performed in 0.45 s
 ## 
 ## Error model:
 ## Constant variance 
diff --git a/docs/articles/mkin.html b/docs/articles/mkin.html
index 271da77e..c274696c 100644
--- a/docs/articles/mkin.html
+++ b/docs/articles/mkin.html
@@ -88,7 +88,7 @@
       

Introduction to mkin

Johannes Ranke

-

2019-05-07

+

2019-05-08

diff --git a/docs/articles/twa.html b/docs/articles/twa.html index 9f08036a..9f660fab 100644 --- a/docs/articles/twa.html +++ b/docs/articles/twa.html @@ -88,7 +88,7 @@

Calculation of time weighted average concentrations with mkin

Johannes Ranke

-

2019-05-07

+

2019-05-08

diff --git a/docs/articles/web_only/FOCUS_Z.html b/docs/articles/web_only/FOCUS_Z.html index 3cda0261..9184fad5 100644 --- a/docs/articles/web_only/FOCUS_Z.html +++ b/docs/articles/web_only/FOCUS_Z.html @@ -88,7 +88,7 @@

Example evaluation of FOCUS dataset Z

Johannes Ranke

-

2019-05-07

+

2019-05-08

diff --git a/docs/articles/web_only/NAFTA_examples.html b/docs/articles/web_only/NAFTA_examples.html index 237b506e..4d9e9b2d 100644 --- a/docs/articles/web_only/NAFTA_examples.html +++ b/docs/articles/web_only/NAFTA_examples.html @@ -88,7 +88,7 @@

Evaluation of example datasets from Attachment 1 to the US EPA SOP for the NAFTA guidance

Johannes Ranke

-

2019-05-07

+

2019-05-08

diff --git a/docs/articles/web_only/benchmarks.html b/docs/articles/web_only/benchmarks.html index a250f005..a53f1927 100644 --- a/docs/articles/web_only/benchmarks.html +++ b/docs/articles/web_only/benchmarks.html @@ -88,7 +88,7 @@

Benchmark timings for mkin on various systems

Johannes Ranke

-

2019-05-07

+

2019-05-08

@@ -198,67 +198,67 @@ ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 8.184 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 7.064 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 7.296 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 5.982 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 5.792 ## t2 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 11.019 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 22.889 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 12.558 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 21.239 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 17.777 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 17.398 ## t3 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 3.764 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 4.649 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 4.786 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 4.510 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.545 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.427 ## t4 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 14.347 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 13.789 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 8.461 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 13.805 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 16.684 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 16.104 ## t5 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 9.495 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 6.395 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 5.675 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 7.386 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 7.868 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 7.527 ## t6 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 2.623 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 2.542 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 2.723 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 2.643 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 2.66 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 2.541 ## t7 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 4.587 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 4.128 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 4.478 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 4.374 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.496 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.308 ## t8 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 7.525 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 4.632 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 4.862 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 7.02 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.919 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 4.775 ## t9 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 16.621 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 8.171 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 7.618 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 11.124 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 11.683 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 11.116 ## t10 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 8.576 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 3.676 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 3.579 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 5.388 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 5.344 +## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 5.063 ## t11 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.48.1 31.267 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.1 5.636 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.2 5.574 ## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.3 7.365 -## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 7.752
+## Linux, AMD Ryzen 7 1700 Eight-Core Processor, mkin version 0.9.49.4 7.784
save(mkin_benchmarks, file = "~/git/mkin/vignettes/mkin_benchmarks.rda")
diff --git a/docs/articles/web_only/compiled_models.html b/docs/articles/web_only/compiled_models.html index 54d3148f..f296c8a4 100644 --- a/docs/articles/web_only/compiled_models.html +++ b/docs/articles/web_only/compiled_models.html @@ -88,7 +88,7 @@

Performance benefit by using compiled model definitions in mkin

Johannes Ranke

-

2019-05-07

+

2019-05-08

@@ -163,9 +163,9 @@ ## Warning in mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet ## = TRUE): Observations with value of zero were removed from the data
##                    test replications elapsed relative user.self sys.self
-## 3     deSolve, compiled            3   3.067    1.000     3.065        0
-## 1 deSolve, not compiled            3  28.135    9.173    28.122        0
-## 2      Eigenvalue based            3   4.306    1.404     4.303        0
+## 3     deSolve, compiled            3   3.131    1.000     3.129        0
+## 1 deSolve, not compiled            3  28.306    9.041    28.290        0
+## 2      Eigenvalue based            3   4.361    1.393     4.358        0
 ##   user.child sys.child
 ## 3          0         0
 ## 1          0         0
@@ -214,8 +214,8 @@
 ## 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.827    1.000     4.825        0
-## 1 deSolve, not compiled            3  52.646   10.907    52.622        0
+## 2     deSolve, compiled            3   5.023    1.000     5.021        0
+## 1 deSolve, not compiled            3  53.267   10.605    53.235        0
 ##   user.child sys.child
 ## 2          0         0
 ## 1          0         0
diff --git a/docs/news/index.html b/docs/news/index.html index d165b6f9..11414ed3 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -122,13 +122,14 @@ -
+

-mkin 0.9.49.4 (2019-05-07) Unreleased +mkin 0.9.49.4 (2019-05-08) Unreleased

  • Direct minimization of the negative log-likelihood for non-constant error models (two-component and variance by variable). In the case the error model is constant variance, least squares is used as this is more stable

  • The argument ‘reweight.method’ to mkinfit and mmkin is now obsolete, use ‘error_model’ instead

  • +
  • New function ‘mkinerrplot’. This function is also used for residual plots in ‘plot.mmkin’ if the argument ‘resplot = “errmod”’ is given, and in ‘plot.mkinfit’ if ‘show_errplot’ is set to TRUE.

  • Remove dependency on FME, only use nlminb for optimisation

  • Use the numDeriv package to calculate hessians

  • Add a benchmark vignette to document the impact on performance. For very simple fits, the new code is a bit slower, presumably because of the time it takes to calculate the hessian matrices with and without parameter transformation

  • @@ -695,7 +696,7 @@

    Contents

    #> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:16 2019 -#> Date of summary: Tue May 7 08:36:16 2019 +#> Date of fit: Wed May 8 20:50:50 2019 +#> Date of summary: Wed May 8 20:50:50 2019 #> #> Equations: #> d_parent/dt = - (alpha/beta) * 1/((time/beta) + 1) * parent #> #> Model predictions using solution type analytical #> -#> Fitted using 222 model solutions performed in 0.89 s +#> Fitted using 222 model solutions performed in 0.456 s #> #> Error model: #> Constant variance @@ -443,7 +443,7 @@ Per default, parameters in the kinetic models are internally transformed in 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)))
    #> Warning: Observations with value of zero were removed from the data
    #> User System verstrichen -#> 2.251 0.000 2.253
    coef(fit)
    #> NULL
    #> $ff +#> 1.488 0.000 1.488
    coef(fit)
    #> NULL
    #> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 #> @@ -515,7 +515,7 @@ Per default, parameters in the kinetic models are internally transformed in #> Sum of squared residuals at call 126: 371.2134 #> Sum of squared residuals at call 135: 371.2134 #> Negative log-likelihood at call 145: 97.22429
    #> Optimisation successfully terminated.
    #> User System verstrichen -#> 1.151 0.000 1.152
    coef(fit.deSolve)
    #> NULL
    endpoints(fit.deSolve)
    #> $ff +#> 1.086 0.000 1.087
    coef(fit.deSolve)
    #> NULL
    endpoints(fit.deSolve)
    #> $ff #> parent_sink parent_m1 m1_sink #> 0.485524 0.514476 1.000000 #> @@ -547,8 +547,8 @@ Per default, parameters in the kinetic models are internally transformed in SFO_SFO.ff <- mkinmod(parent = mkinsub("SFO", "m1"), 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)
    #> Warning: Observations with value of zero were removed from the data
    summary(f.noweight)
    #> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:33 2019 -#> Date of summary: Tue May 7 08:36:33 2019 +#> Date of fit: Wed May 8 20:51:06 2019 +#> Date of summary: Wed May 8 20:51:06 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -556,7 +556,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 421 model solutions performed in 1.1 s +#> Fitted using 421 model solutions performed in 1.082 s #> #> Error model: #> Constant variance @@ -665,8 +665,8 @@ Per default, parameters in the kinetic models are internally transformed in #> 120 m1 25.15 28.78984 -3.640e+00 #> 120 m1 33.31 28.78984 4.520e+00
    f.obs <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, error_model = "obs", quiet = TRUE)
    #> Warning: Observations with value of zero were removed from the data
    summary(f.obs)
    #> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:35 2019 -#> Date of summary: Tue May 7 08:36:35 2019 +#> Date of fit: Wed May 8 20:51:08 2019 +#> Date of summary: Wed May 8 20:51:08 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -674,7 +674,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 758 model solutions performed in 1.991 s +#> Fitted using 758 model solutions performed in 1.971 s #> #> Error model: #> Variance unique to each observed variable @@ -795,8 +795,8 @@ Per default, parameters in the kinetic models are internally transformed in #> 120 m1 25.15 28.80430 -3.654e+00 #> 120 m1 33.31 28.80430 4.506e+00
    f.tc <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, error_model = "tc", quiet = TRUE)
    #> Warning: Observations with value of zero were removed from the data
    summary(f.tc)
    #> mkin version used for fitting: 0.9.49.4 #> R version used for fitting: 3.6.0 -#> Date of fit: Tue May 7 08:36:39 2019 -#> Date of summary: Tue May 7 08:36:39 2019 +#> Date of fit: Wed May 8 20:51:11 2019 +#> Date of summary: Wed May 8 20:51:11 2019 #> #> Equations: #> d_parent/dt = - k_parent * parent @@ -804,7 +804,7 @@ Per default, parameters in the kinetic models are internally transformed in #> #> Model predictions using solution type deSolve #> -#> Fitted using 821 model solutions performed in 3.304 s +#> Fitted using 821 model solutions performed in 3.29 s #> #> Error model: #> Two-component variance function diff --git a/docs/reference/mkinmod.html b/docs/reference/mkinmod.html index d0b2a0eb..51be5465 100644 --- a/docs/reference/mkinmod.html +++ b/docs/reference/mkinmod.html @@ -234,7 +234,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 fileb5a1e31297a.c 2> fileb5a1e31297a.c.err.txt +#> /usr/lib/R/bin/R CMD SHLIB file4bbd307f8763.c 2> file4bbd307f8763.c.err.txt #> Program source: #> 1: #include <R.h> #> 2: diff --git a/docs/reference/mkinpredict.html b/docs/reference/mkinpredict.html index 522cbbfe..8e2e307b 100644 --- a/docs/reference/mkinpredict.html +++ b/docs/reference/mkinpredict.html @@ -328,17 +328,17 @@ 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.003 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 #> 201 20 4.978707 27.46227
    #> User System verstrichen -#> 0.001 0.000 0.002
    system.time( +#> 0.002 0.000 0.001
    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", use_compiled = FALSE)[201,]))
    #> time parent m1 #> 201 20 4.978707 27.46227
    #> User System verstrichen -#> 0.021 0.000 0.022
    +#> 0.021 0.000 0.021
    # Predict from a fitted model f <- mkinfit(SFO_SFO, FOCUS_2006_C)
    #> Ordinary least squares optimisation
    #> Sum of squared residuals at call 1: 552.5739 #> Sum of squared residuals at call 3: 552.5739 diff --git a/docs/reference/mmkin.html b/docs/reference/mmkin.html index d0cd748c..3e297eb3 100644 --- a/docs/reference/mmkin.html +++ b/docs/reference/mmkin.html @@ -194,8 +194,8 @@ time_1 <- system.time(fits.4 <- mmkin(models, datasets, cores = 1, quiet = TRUE)) time_default
    #> User System verstrichen -#> 0.047 0.033 5.463
    time_1
    #> User System verstrichen -#> 19.909 0.004 19.967
    +#> 0.048 0.024 5.085
    time_1
    #> User System verstrichen +#> 19.074 0.004 19.089
    endpoints(fits.0[["SFO_lin", 2]])
    #> $ff #> parent_M1 parent_sink M1_M2 M1_sink #> 0.7340481 0.2659519 0.7505684 0.2494316 diff --git a/docs/reference/plot.mkinfit-1.png b/docs/reference/plot.mkinfit-1.png index e7b3bac7..f70e9e31 100644 Binary files a/docs/reference/plot.mkinfit-1.png and b/docs/reference/plot.mkinfit-1.png differ diff --git a/docs/reference/plot.mkinfit-2.png b/docs/reference/plot.mkinfit-2.png index 15bdba55..6facce0f 100644 Binary files a/docs/reference/plot.mkinfit-2.png and b/docs/reference/plot.mkinfit-2.png differ diff --git a/docs/reference/plot.mkinfit-3.png b/docs/reference/plot.mkinfit-3.png index 52de09bd..b53b3134 100644 Binary files a/docs/reference/plot.mkinfit-3.png and b/docs/reference/plot.mkinfit-3.png differ diff --git a/docs/reference/plot.mkinfit-4.png b/docs/reference/plot.mkinfit-4.png index a832ede2..70a936ee 100644 Binary files a/docs/reference/plot.mkinfit-4.png and b/docs/reference/plot.mkinfit-4.png differ diff --git a/docs/reference/plot.mkinfit-5.png b/docs/reference/plot.mkinfit-5.png new file mode 100644 index 00000000..2e208996 Binary files /dev/null and b/docs/reference/plot.mkinfit-5.png differ diff --git a/docs/reference/plot.mkinfit-6.png b/docs/reference/plot.mkinfit-6.png new file mode 100644 index 00000000..1da876b4 Binary files /dev/null and b/docs/reference/plot.mkinfit-6.png differ diff --git a/docs/reference/plot.mkinfit.html b/docs/reference/plot.mkinfit.html index 525200ff..7bfc17f6 100644 --- a/docs/reference/plot.mkinfit.html +++ b/docs/reference/plot.mkinfit.html @@ -36,7 +36,7 @@ from a previous successful call to mkinfit and plots the observed data together with the solution of the fitted model. If the current plot device is a tikz device, - then latex is being used for the formatting of the chi2 error level, + then latex is being used for the formatting of the chi2 error level, if show_errmin = TRUE." /> @@ -137,7 +137,7 @@ If the current plot device is a tikz device, from a previous successful call to mkinfit and plots the observed data together with the solution of the fitted model.

    If the current plot device is a tikz device, - then latex is being used for the formatting of the chi2 error level, + then latex is being used for the formatting of the chi2 error level, if show_errmin = TRUE.

    @@ -151,7 +151,9 @@ plot(x, fit = x, col_obs = 1:length(obs_vars), pch_obs = col_obs, lty_obs = rep(1, length(obs_vars)), add = FALSE, legend = !add, - show_residuals = FALSE, maxabs = "auto", + show_residuals = FALSE, + show_errplot = FALSE, + maxabs = "auto", sep_obs = FALSE, rel.height.middle = 0.9, lpos = "topright", inset = c(0.05, 0.05), show_errmin = FALSE, errmin_digits = 3, …) @@ -213,10 +215,17 @@ plot_sep(fit, sep_obs = TRUE, show_residuals = TRUE, show_errmin = TRUE, … show_residuals

    Should residuals be shown? If only one plot of the fits is shown, the - residual plot is in the lower third of the plot? Otherwise, i.e. if + residual plot is in the lower third of the plot. Otherwise, i.e. if "sep_obs" is given, the residual plots will be located to the right of the plots of the fitted curves.

    + + show_errplot +

    Should squared residuals and the error model be shown? If only one plot of + the fits is shown, this plot is in the lower third of the plot. + Otherwise, i.e. if "sep_obs" is given, the residual plots will be located + to the right of the plots of the fitted curves.

    + maxabs

    Maximum absolute value of the residuals. This is used for the scaling of @@ -263,14 +272,18 @@ plot_sep(fit, sep_obs = TRUE, show_residuals = TRUE, show_errmin = TRUE, …

    # One parent compound, one metabolite, both single first order, path from # parent to sink included SFO_SFO <- mkinmod(parent = mkinsub("SFO", "m1", full = "Parent"), - m1 = mkinsub("SFO", full = "Metabolite M1" ))
    #> Successfully compiled differential equation model from auto-generated C code.
    fit <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE)
    #> Warning: Observations with value of zero were removed from the data
    plot(fit)
    plot(fit, show_residuals = TRUE)
    + m1 = mkinsub("SFO", full = "Metabolite M1" ))
    #> Successfully compiled differential equation model from auto-generated C code.
    fit <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE, error_model = "tc")
    #> Warning: Observations with value of zero were removed from the data
    plot(fit)
    plot(fit, show_residuals = TRUE)
    plot(fit, show_errplot = TRUE)
    # Show the observed variables separately -plot(fit, sep_obs = TRUE, lpos = c("topright", "bottomright"))
    +plot(fit, sep_obs = TRUE, lpos = c("topright", "bottomright"))
    # Show the observed variables separately, with residuals plot(fit, sep_obs = TRUE, show_residuals = TRUE, lpos = c("topright", "bottomright"), - show_errmin = TRUE)
    + show_errmin = TRUE)
    # The same can be obtained with less typing, using the convenience function plot_sep -plot_sep(fit, lpos = c("topright", "bottomright"))
    +plot_sep(fit, lpos = c("topright", "bottomright")) + +# Show the observed variables separately, with the error model +plot(fit, sep_obs = TRUE, show_errplot = TRUE, lpos = c("topright", "bottomright"), + show_errmin = TRUE)
# S3 method for mmkin
-plot(x, main = "auto", legends = 1, errmin_var = "All data", errmin_digits = 3,
-              cex = 0.7, rel.height.middle = 0.9, ...)
+plot(x, main = "auto", legends = 1, + resplot = c("time", "errmod"), errmin_var = "All data", errmin_digits = 3, + cex = 0.7, rel.height.middle = 0.9, ...)

Arguments

@@ -159,6 +160,12 @@ If the current plot device is a tikz device, + + + + @@ -187,15 +194,17 @@ If the current plot device is a tikz device,

Examples

-
# Only use one core not to offend CRAN checks +
# Only use one core not to offend CRAN checks fits <- mmkin(c("FOMC", "HS"), list("FOCUS B" = FOCUS_2006_B, "FOCUS C" = FOCUS_2006_C), # named list for titles - cores = 1, quiet = TRUE) + cores = 1, quiet = TRUE, error_model = "tc") plot(fits[, "FOCUS C"])
plot(fits["FOMC", ])
# We can also plot a single fit, if we like the way plot.mmkin works, but then the plot # height should be smaller than the plot width (this is not possible for the html pages # generated by pkgdown, as far as I know). - plot(fits["FOMC", "FOCUS C"]) # same as plot(fits[1, 2])
+ plot(fits["FOMC", "FOCUS C"]) # same as plot(fits[1, 2])
+ # Show the error models + plot(fits["FOMC", ], resplot = "errmod")
legends

An index for the fits for which legends should be shown.

resplot

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

errmin_var

The variable for which the FOCUS chi2 error value should be shown.