From 01c69fcff8c5a82b4c80faaeb44ff00827e792ca Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 14 May 2015 00:21:12 +0200 Subject: Start of migration from RUnit to testthat --- .gitignore | 1 + DESCRIPTION | 5 +- GNUmakefile | 4 +- inst/unitTests/Makefile | 15 --- inst/unitTests/runit.mkinerrmin.R | 62 ---------- inst/unitTests/runit.mkinfit.R | 227 ------------------------------------- inst/unitTests/runit.mkinpredict.R | 86 -------------- tests/doRUnit.R | 42 ------- tests/testthat.R | 4 + tests/testthat/runit.mkinerrmin.R | 62 ++++++++++ tests/testthat/runit.mkinfit.R | 227 +++++++++++++++++++++++++++++++++++++ tests/testthat/runit.mkinpredict.R | 86 ++++++++++++++ tests/testthat/test_parent_only.R | 48 ++++++++ 13 files changed, 433 insertions(+), 436 deletions(-) delete mode 100644 inst/unitTests/Makefile delete mode 100644 inst/unitTests/runit.mkinerrmin.R delete mode 100644 inst/unitTests/runit.mkinfit.R delete mode 100644 inst/unitTests/runit.mkinpredict.R delete mode 100644 tests/doRUnit.R create mode 100644 tests/testthat.R create mode 100644 tests/testthat/runit.mkinerrmin.R create mode 100644 tests/testthat/runit.mkinfit.R create mode 100644 tests/testthat/runit.mkinpredict.R create mode 100644 tests/testthat/test_parent_only.R diff --git a/.gitignore b/.gitignore index 39eac04e..81a0b7d7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ vignettes/*.toc vignettes/.build.timestamp vignettes/mkin.tex vignettes/FOCUS_Z.tex +vignettes/compiled_models_cache mkin.Rcheck diff --git a/DESCRIPTION b/DESCRIPTION index 03976d1e..0477a200 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Routines for Fitting Kinetic Models with One or More State Variables to Chemical Degradation Data Version: 0.9-35 -Date: 2015-02-20 +Date: 2015-05-13 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), @@ -17,7 +17,8 @@ Description: Calculation routines based on the FOCUS Kinetics Report (2006). for a particular purpose. Depends: minpack.lm, rootSolve Imports: FME, deSolve -Suggests: knitr, RUnit +Suggests: knitr, + testthat License: GPL LazyLoad: yes LazyData: yes diff --git a/GNUmakefile b/GNUmakefile index 126dede1..b731166b 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -18,13 +18,13 @@ SDDIR ?= $(RFSVN)/www/mkin_static pkgfiles = NEWS \ data/* \ DESCRIPTION \ - inst/unitTests* \ inst/staticdocs/README \ man/* \ NAMESPACE \ R/* \ README.md \ tests/* \ + tests/testthat* \ TODO \ vignettes/* @@ -69,7 +69,7 @@ clean: test: install-no-vignettes cd tests;\ - "$(RBIN)/Rscript" doRUnit.R + "$(RBIN)/Rscript" testthat.R vignettes/mkin.pdf: vignettes/mkin.Rnw "$(RBIN)/Rscript" -e "tools::buildVignette(file = 'vignettes/mkin.Rnw', dir = 'vignettes')" diff --git a/inst/unitTests/Makefile b/inst/unitTests/Makefile deleted file mode 100644 index 8d132253..00000000 --- a/inst/unitTests/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -TOP=../.. -PKG=${shell cd ${TOP};pwd} -SUITE=doRUnit.R -R=R - -all: inst test - -inst: # Install package - cd ${TOP}/..;\ - ${R} CMD INSTALL ${PKG} - -test: # Run unit tests - export RCMDCHECK=FALSE;\ - cd ${TOP}/tests;\ - ${R} --vanilla --slave < ${SUITE} diff --git a/inst/unitTests/runit.mkinerrmin.R b/inst/unitTests/runit.mkinerrmin.R deleted file mode 100644 index 56a33ff9..00000000 --- a/inst/unitTests/runit.mkinerrmin.R +++ /dev/null @@ -1,62 +0,0 @@ -# Test SFO_SFO model with FOCUS_2006_D against Schaefer 2007 paper, tolerance = 1% # {{{ -# and check chi2 error values against values obtained with mkin 0.33 -test.FOCUS_2006_D_SFO_SFO <- function() -{ - SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "min") - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - fit.1.e <- mkinfit(SFO_SFO.1, FOCUS_2006_D) - fit.1.d <- mkinfit(SFO_SFO.1, solution_type = "deSolve", FOCUS_2006_D) - fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D) - fit.2.d <- mkinfit(SFO_SFO.2, solution_type = "deSolve", FOCUS_2006_D) - - FOCUS_2006_D_results_schaefer07_means <- c( - parent_0 = 99.65, DT50_parent = 7.04, DT50_m1 = 131.34) - - r.1.e <- c(fit.1.e$bparms.optim[[1]], endpoints(fit.1.e)$distimes[[1]]) - r.1.d <- c(fit.1.d$bparms.optim[[1]], endpoints(fit.1.d)$distimes[[1]]) - r.2.e <- c(fit.2.e$bparms.optim[[1]], endpoints(fit.2.e)$distimes[[1]]) - r.2.d <- c(fit.2.d$bparms.optim[[1]], endpoints(fit.2.d)$distimes[[1]]) - - dev.1.e <- 100 * (r.1.e - FOCUS_2006_D_results_schaefer07_means)/r.1.e - checkIdentical(as.numeric(abs(dev.1.e)) < 1, rep(TRUE, 3)) - dev.1.d <- 100 * (r.1.d - FOCUS_2006_D_results_schaefer07_means)/r.1.d - checkIdentical(as.numeric(abs(dev.1.d)) < 1, rep(TRUE, 3)) - dev.2.e <- 100 * (r.2.e - FOCUS_2006_D_results_schaefer07_means)/r.2.e - checkIdentical(as.numeric(abs(dev.2.e)) < 1, rep(TRUE, 3)) - dev.2.d <- 100 * (r.2.d - FOCUS_2006_D_results_schaefer07_means)/r.2.d - checkIdentical(as.numeric(abs(dev.2.d)) < 1, rep(TRUE, 3)) - - round(mkinerrmin(fit.2.e), 4) - round(mkinerrmin(fit.2.d), 4) - - errmin.FOCUS_2006_D_rounded = data.frame( - err.min = c(0.0640, 0.0646, 0.0469), - n.optim = c(4, 2, 2), - df = c(15, 7, 8), - row.names = c("All data", "parent", "m1")) - checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), - errmin.FOCUS_2006_D_rounded) -} # }}} - -# Test SFO_SFO model with FOCUS_2006_E against values obtained with mkin 0.33 {{{ -test.FOCUS_2006_E_SFO_SFO <- function() -{ - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_E) - - round(mkinerrmin(fit.2.e), 4) - errmin.FOCUS_2006_E_rounded = data.frame( - err.min = c(0.1544, 0.1659, 0.1095), - n.optim = c(4, 2, 2), - df = c(13, 7, 6), - row.names = c("All data", "parent", "m1")) - checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), - errmin.FOCUS_2006_E_rounded) -} # }}} - - diff --git a/inst/unitTests/runit.mkinfit.R b/inst/unitTests/runit.mkinfit.R deleted file mode 100644 index 8eefb995..00000000 --- a/inst/unitTests/runit.mkinfit.R +++ /dev/null @@ -1,227 +0,0 @@ -# Copyright (C) 2010-2014 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 - -# Test SFO model to a relative tolerance of 1% # {{{ -test.FOCUS_2006_SFO <- function() -{ - SFO.1 <- mkinmod(parent = list(type = "SFO")) - SFO.2 <- mkinmod(parent = list(type = "SFO"), use_of_ff = "max") - - fit.A.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_A, quiet=TRUE) - fit.A.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_A, quiet=TRUE) - - median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "A", - c(M0, k, DT50, DT90)), "median")) - - fit.A.SFO.1.r <- as.numeric(c(fit.A.SFO.1$bparms.optim, endpoints(fit.A.SFO.1)$distimes)) - dev.A.SFO.1 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.1.r)/median.A.SFO), digits=1)) - checkIdentical(dev.A.SFO.1 < 1, rep(TRUE, length(dev.A.SFO.1))) - - fit.A.SFO.2.r <- as.numeric(c(fit.A.SFO.2$bparms.optim, endpoints(fit.A.SFO.2)$distimes)) - dev.A.SFO.2 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.2.r)/median.A.SFO), digits=1)) - checkIdentical(dev.A.SFO.2 < 1, rep(TRUE, length(dev.A.SFO.2))) - - fit.C.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_C, quiet=TRUE) - fit.C.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_C, quiet=TRUE) - - median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "C", - c(M0, k, DT50, DT90)), "median")) - - fit.C.SFO.1.r <- as.numeric(c(fit.C.SFO.1$bparms.optim, endpoints(fit.C.SFO.1)$distimes)) - dev.C.SFO.1 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.1.r)/median.C.SFO), digits=1)) - checkIdentical(dev.C.SFO.1 < 1, rep(TRUE, length(dev.C.SFO.1))) - - fit.C.SFO.2.r <- as.numeric(c(fit.C.SFO.2$bparms.optim, endpoints(fit.C.SFO.2)$distimes)) - dev.C.SFO.2 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.2.r)/median.C.SFO), digits=1)) - checkIdentical(dev.C.SFO.2 < 1, rep(TRUE, length(dev.C.SFO.2))) -} # }}} - -# Test FOMC model to a relative tolerance of 1% {{{ -# See kinfit vignette for a discussion of FOMC fits to FOCUS_2006_A -# In this case, only M0, DT50 and DT90 are checked -test.FOCUS_2006_FOMC <- function() -{ - FOMC <- mkinmod(parent = list(type = "FOMC")) - - # FOCUS_2006_A (compare kinfit vignette for discussion) - fit.A.FOMC <- mkinfit(FOMC, FOCUS_2006_A, quiet=TRUE) - - median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "A", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.A.FOMC.r <- as.numeric(c(fit.A.FOMC$bparms.optim, endpoints(fit.A.FOMC)$distimes[c("DT50", "DT90")])) - dev.A.FOMC <- abs(round(100 * ((median.A.FOMC - fit.A.FOMC.r)/median.A.FOMC), digits=1)) - dev.A.FOMC <- dev.A.FOMC[c(1, 4, 5)] - checkIdentical(dev.A.FOMC < 1, rep(TRUE, length(dev.A.FOMC))) - - # FOCUS_2006_B - fit.B.FOMC <- mkinfit(FOMC, FOCUS_2006_B, quiet=TRUE) - - median.B.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "B", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.B.FOMC.r <- as.numeric(c(fit.B.FOMC$bparms.optim, endpoints(fit.B.FOMC)$distimes[c("DT50", "DT90")])) - dev.B.FOMC <- abs(round(100 * ((median.B.FOMC - fit.B.FOMC.r)/median.B.FOMC), digits=1)) - dev.B.FOMC <- dev.B.FOMC[c(1, 4, 5)] - checkIdentical(dev.B.FOMC < 1, rep(TRUE, length(dev.B.FOMC))) - - # FOCUS_2006_C - fit.C.FOMC <- mkinfit(FOMC, FOCUS_2006_C, quiet=TRUE) - - median.C.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "C", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.C.FOMC.r <- as.numeric(c(fit.C.FOMC$bparms.optim, endpoints(fit.C.FOMC)$distimes[c("DT50", "DT90")])) - dev.C.FOMC <- abs(round(100 * ((median.C.FOMC - fit.C.FOMC.r)/median.C.FOMC), digits=1)) - dev.C.FOMC <- dev.C.FOMC[c(1, 4, 5)] - checkIdentical(dev.C.FOMC < 1, rep(TRUE, length(dev.C.FOMC))) -} # }}} - -# Test DFOP model, tolerance of 1% with the exception of f parameter for A {{{ -test.FOCUS_2006_DFOP <- function() -{ - DFOP <- mkinmod(parent = list(type = "DFOP")) - - # FOCUS_2006_A - # Results were too much dependent on algorithm, as this dataset - # is pretty much SFO. "Port" gave a lower deviance, but deviated from the - # median of FOCUS_2006 solutions - - # FOCUS_2006_B - fit.B.DFOP <- mkinfit(DFOP, FOCUS_2006_B, quiet=TRUE) - - median.B.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", - c(M0, k1, k2, f, DT50, DT90)), "median")) - - fit.B.DFOP.r <- as.numeric(c(fit.B.DFOP$bparms.optim, endpoints(fit.B.DFOP)$distimes[c("DT50", "DT90")])) - dev.B.DFOP <- abs(round(100 * ((median.B.DFOP - fit.B.DFOP.r)/median.B.DFOP), digits=1)) - # about 0.6% deviation for parameter f, the others are <= 0.1% - checkIdentical(dev.B.DFOP < 1, rep(TRUE, length(dev.B.DFOP))) -} # }}} - -# Test HS model to a relative tolerance of 1% excluding Mathematica values {{{ -# as they are unreliable -test.FOCUS_2006_HS <- function() -{ - HS <- mkinmod(parent = list(type = "HS")) - - # FOCUS_2006_A - fit.A.HS <- mkinfit(HS, FOCUS_2006_A, quiet=TRUE) - - median.A.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "A", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.A.HS.r <- as.numeric(c(fit.A.HS$bparms.optim, endpoints(fit.A.HS)$distimes[c("DT50", "DT90")])) - dev.A.HS <- abs(round(100 * ((median.A.HS - fit.A.HS.r)/median.A.HS), digits=1)) - # about 6.7% deviation for parameter f, the others are < 0.1% - checkIdentical(dev.A.HS < 1, rep(TRUE, length(dev.A.HS))) - - # FOCUS_2006_B - fit.B.HS <- mkinfit(HS, FOCUS_2006_B, quiet=TRUE) - - median.B.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "B", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.B.HS.r <- as.numeric(c(fit.B.HS$bparms.optim, endpoints(fit.B.HS)$distimes[c("DT50", "DT90")])) - dev.B.HS <- abs(round(100 * ((median.B.HS - fit.B.HS.r)/median.B.HS), digits=1)) - # < 10% deviation for M0, k1, DT50 and DT90, others are problematic - dev.B.HS <- dev.B.HS[c(1, 2, 5, 6)] - checkIdentical(dev.B.HS < 10, rep(TRUE, length(dev.B.HS))) - - # FOCUS_2006_C - fit.C.HS <- mkinfit(HS, FOCUS_2006_C, quiet=TRUE) - - median.C.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "C", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.C.HS.r <- as.numeric(c(fit.C.HS$bparms.optim, endpoints(fit.C.HS)$distimes[c("DT50", "DT90")])) - dev.C.HS <- abs(round(100 * ((median.C.HS - fit.C.HS.r)/median.C.HS), digits=1)) - # deviation <= 0.1% - checkIdentical(dev.C.HS < 1, rep(TRUE, length(dev.C.HS))) -} # }}} - -# Test SFORB model against DFOP solutions to a relative tolerance of 1% # {{{ -test.FOCUS_2006_SFORB <- function() -{ - SFORB <- mkinmod(parent = list(type = "SFORB")) - - # FOCUS_2006_A - # Again it does not make a lot of sense to use a SFO dataset for this - - # FOCUS_2006_B - fit.B.SFORB.1 <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE) - fit.B.SFORB.2 <- mkinfit(SFORB, FOCUS_2006_B, solution_type = "deSolve", quiet=TRUE) - - median.B.SFORB <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", - c(M0, k1, k2, DT50, DT90)), "median")) - - fit.B.SFORB.1.r <- as.numeric(c( - parent_0 = fit.B.SFORB.1$bparms.optim[[1]], - k1 = endpoints(fit.B.SFORB.1)$SFORB[[1]], - k2 = endpoints(fit.B.SFORB.1)$SFORB[[2]], - endpoints(fit.B.SFORB.1)$distimes[c("DT50", "DT90")])) - dev.B.SFORB.1 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.1.r)/median.B.SFORB), digits=1)) - checkIdentical(dev.B.SFORB.1 < 1, rep(TRUE, length(dev.B.SFORB.1))) - - fit.B.SFORB.2.r <- as.numeric(c( - parent_0 = fit.B.SFORB.2$bparms.optim[[1]], - k1 = endpoints(fit.B.SFORB.2)$SFORB[[1]], - k2 = endpoints(fit.B.SFORB.2)$SFORB[[2]], - endpoints(fit.B.SFORB.2)$distimes[c("DT50", "DT90")])) - dev.B.SFORB.2 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.2.r)/median.B.SFORB), digits=1)) - checkIdentical(dev.B.SFORB.2 < 1, rep(TRUE, length(dev.B.SFORB.2))) -} # }}} - -# Test eigenvalue based fit to Schaefer 2007 data against solution from conference paper {{{ -test.mkinfit.schaefer07_complex_example <- function() -{ - schaefer07_complex_model <- mkinmod( - parent = list(type = "SFO", to = c("A1", "B1", "C1"), sink = FALSE), - A1 = list(type = "SFO", to = "A2"), - B1 = list(type = "SFO"), - C1 = list(type = "SFO"), - A2 = list(type = "SFO"), use_of_ff = "max") - - # If we use the default algorithm 'Marq' we need to give a good starting - # estimate for k_A2 in order to find the solution published by Schaefer et al. - fit <- mkinfit(schaefer07_complex_model, method.modFit = "Port", - mkin_wide_to_long(schaefer07_complex_case, time = "time")) - s <- summary(fit) - r <- schaefer07_complex_results - attach(as.list(fit$bparms.optim)) - r$mkin <- c( - k_parent, - s$distimes["parent", "DT50"], - s$ff["parent_A1"], - k_A1, - s$distimes["A1", "DT50"], - s$ff["parent_B1"], - k_B1, - s$distimes["B1", "DT50"], - s$ff["parent_C1"], - k_C1, - s$distimes["C1", "DT50"], - s$ff["A1_A2"], - k_A2, - s$distimes["A2", "DT50"]) - r$means <- (r$KinGUI + r$ModelMaker)/2 - r$mkin.deviation <- abs(round(100 * ((r$mkin - r$means)/r$means), digits=1)) - checkIdentical(r$mkin.deviation < 10, rep(TRUE, 14)) -} # }}} - -# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/inst/unitTests/runit.mkinpredict.R b/inst/unitTests/runit.mkinpredict.R deleted file mode 100644 index 997857ce..00000000 --- a/inst/unitTests/runit.mkinpredict.R +++ /dev/null @@ -1,86 +0,0 @@ -# $Id: jranke $ - -# Copyright (C) 2012 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 - -# Check solution types for SFO {{{ -test.SFO_solution_types <- function() -{ - ot = seq(0, 100, by = 1) - SFO <- mkinmod(parent = list(type = "SFO")) - SFO.analytical <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "analytical"), time == 100), digits=5) - SFO.deSolve <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "deSolve"), time == 100), digits=5) - SFO.eigen <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "eigen"), time == 100), digits=5) - - checkEquals(SFO.analytical, SFO.deSolve) - checkEquals(SFO.analytical, SFO.eigen) -} # }}} - -# Check model specification and solution types for SFO_SFO {{{ -# Relative Tolerance is 0.01% -# Do not use time 0, as eigenvalue based solution does not give 0 at time 0 for metabolites -# and relative tolerance is thus not met -test.SFO_solution_types <- function() -{ - tol = 0.01 - SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "min") - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - ot = seq(0, 100, by = 1) - r.1.e <- subset(mkinpredict(SFO_SFO.1, - c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "eigen"), - time %in% c(1, 10, 50, 100)) - r.1.d <- subset(mkinpredict(SFO_SFO.1, - c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), - time %in% c(1, 10, 50, 100)) - - r.2.e <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "eigen"), - time %in% c(1, 10, 50, 100)) - r.2.d <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), - time %in% c(1, 10, 50, 100)) - - # Compare eigen and deSolve for minimum use of formation fractions - dev.1.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] - dev.1.e_d.percent = as.numeric(unlist((dev.1.e_d.percent))) - dev.1.e_d.percent = ifelse(is.na(dev.1.e_d.percent), 0, dev.1.e_d.percent) - checkIdentical(dev.1.e_d.percent < tol, rep(TRUE, length(dev.1.e_d.percent))) - - # Compare eigen and deSolve for maximum use of formation fractions - dev.2.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] - dev.2.e_d.percent = as.numeric(unlist((dev.2.e_d.percent))) - dev.2.e_d.percent = ifelse(is.na(dev.2.e_d.percent), 0, dev.2.e_d.percent) - checkIdentical(dev.2.e_d.percent < tol, rep(TRUE, length(dev.2.e_d.percent))) - - # Compare minimum and maximum use of formation fractions - dev.1_2.e.percent = 100 * (r.1.e[-1] - r.2.e[-1])/r.1.e[-1] - dev.1_2.e.percent = as.numeric(unlist((dev.1_2.e.percent))) - dev.1_2.e.percent = ifelse(is.na(dev.1_2.e.percent), 0, dev.1_2.e.percent) - checkIdentical(dev.1_2.e.percent < tol, rep(TRUE, length(dev.1_2.e.percent))) - -} # }}} - -# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/tests/doRUnit.R b/tests/doRUnit.R deleted file mode 100644 index 9faee940..00000000 --- a/tests/doRUnit.R +++ /dev/null @@ -1,42 +0,0 @@ -# Adapted from a version around 2.9 of the rcdk package by Rajarshi Guha -if(require("RUnit", quietly=TRUE)) { - - pkg <- "mkin" - path <- system.file(package=pkg, "unitTests") - - cat("\nRunning unit tests\n") - print(list(pkg=pkg, getwd=getwd(), pathToUnitTests=path)) - - library(package=pkg, character.only=TRUE) - - ## Define tests - testSuite <- defineTestSuite(name=paste(pkg, " Unit Tests"), - dirs=path) - ## Run - tests <- runTestSuite(testSuite) - - ## Default report name - pathReport <- file.path(path, "report") - - ## Report to stdout and text files - cat("------------------- UNIT TEST SUMMARY ---------------------\n\n") - printTextProtocol(tests, showDetails=FALSE) - printTextProtocol(tests, showDetails=FALSE, - fileName=paste(pathReport, "Summary.txt", sep="")) - printTextProtocol(tests, showDetails=TRUE, - fileName=paste(pathReport, ".txt", sep="")) - - ## Report to HTML file - printHTMLProtocol(tests, fileName=paste(pathReport, ".html", sep="")) - - ## Return stop() to cause R CMD check stop in case of - ## - failures i.e. FALSE to unit tests or - ## - errors i.e. R errors - tmp <- getErrors(tests) - if(tmp$nFail > 0 | tmp$nErr > 0) { - stop(paste("\n\nunit testing failed (#test failures: ", tmp$nFail, - ", #R errors: ", tmp$nErr, ")\n\n", sep="")) - } -} else { - warning("cannot run unit tests -- package RUnit is not available") -} diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 00000000..01c9b7cd --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(mkin) + +test_check("mkin") diff --git a/tests/testthat/runit.mkinerrmin.R b/tests/testthat/runit.mkinerrmin.R new file mode 100644 index 00000000..56a33ff9 --- /dev/null +++ b/tests/testthat/runit.mkinerrmin.R @@ -0,0 +1,62 @@ +# Test SFO_SFO model with FOCUS_2006_D against Schaefer 2007 paper, tolerance = 1% # {{{ +# and check chi2 error values against values obtained with mkin 0.33 +test.FOCUS_2006_D_SFO_SFO <- function() +{ + SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "min") + SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "max") + + fit.1.e <- mkinfit(SFO_SFO.1, FOCUS_2006_D) + fit.1.d <- mkinfit(SFO_SFO.1, solution_type = "deSolve", FOCUS_2006_D) + fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D) + fit.2.d <- mkinfit(SFO_SFO.2, solution_type = "deSolve", FOCUS_2006_D) + + FOCUS_2006_D_results_schaefer07_means <- c( + parent_0 = 99.65, DT50_parent = 7.04, DT50_m1 = 131.34) + + r.1.e <- c(fit.1.e$bparms.optim[[1]], endpoints(fit.1.e)$distimes[[1]]) + r.1.d <- c(fit.1.d$bparms.optim[[1]], endpoints(fit.1.d)$distimes[[1]]) + r.2.e <- c(fit.2.e$bparms.optim[[1]], endpoints(fit.2.e)$distimes[[1]]) + r.2.d <- c(fit.2.d$bparms.optim[[1]], endpoints(fit.2.d)$distimes[[1]]) + + dev.1.e <- 100 * (r.1.e - FOCUS_2006_D_results_schaefer07_means)/r.1.e + checkIdentical(as.numeric(abs(dev.1.e)) < 1, rep(TRUE, 3)) + dev.1.d <- 100 * (r.1.d - FOCUS_2006_D_results_schaefer07_means)/r.1.d + checkIdentical(as.numeric(abs(dev.1.d)) < 1, rep(TRUE, 3)) + dev.2.e <- 100 * (r.2.e - FOCUS_2006_D_results_schaefer07_means)/r.2.e + checkIdentical(as.numeric(abs(dev.2.e)) < 1, rep(TRUE, 3)) + dev.2.d <- 100 * (r.2.d - FOCUS_2006_D_results_schaefer07_means)/r.2.d + checkIdentical(as.numeric(abs(dev.2.d)) < 1, rep(TRUE, 3)) + + round(mkinerrmin(fit.2.e), 4) + round(mkinerrmin(fit.2.d), 4) + + errmin.FOCUS_2006_D_rounded = data.frame( + err.min = c(0.0640, 0.0646, 0.0469), + n.optim = c(4, 2, 2), + df = c(15, 7, 8), + row.names = c("All data", "parent", "m1")) + checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), + errmin.FOCUS_2006_D_rounded) +} # }}} + +# Test SFO_SFO model with FOCUS_2006_E against values obtained with mkin 0.33 {{{ +test.FOCUS_2006_E_SFO_SFO <- function() +{ + SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "max") + + fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_E) + + round(mkinerrmin(fit.2.e), 4) + errmin.FOCUS_2006_E_rounded = data.frame( + err.min = c(0.1544, 0.1659, 0.1095), + n.optim = c(4, 2, 2), + df = c(13, 7, 6), + row.names = c("All data", "parent", "m1")) + checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), + errmin.FOCUS_2006_E_rounded) +} # }}} + + diff --git a/tests/testthat/runit.mkinfit.R b/tests/testthat/runit.mkinfit.R new file mode 100644 index 00000000..8eefb995 --- /dev/null +++ b/tests/testthat/runit.mkinfit.R @@ -0,0 +1,227 @@ +# Copyright (C) 2010-2014 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 + +# Test SFO model to a relative tolerance of 1% # {{{ +test.FOCUS_2006_SFO <- function() +{ + SFO.1 <- mkinmod(parent = list(type = "SFO")) + SFO.2 <- mkinmod(parent = list(type = "SFO"), use_of_ff = "max") + + fit.A.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_A, quiet=TRUE) + fit.A.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_A, quiet=TRUE) + + median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "A", + c(M0, k, DT50, DT90)), "median")) + + fit.A.SFO.1.r <- as.numeric(c(fit.A.SFO.1$bparms.optim, endpoints(fit.A.SFO.1)$distimes)) + dev.A.SFO.1 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.1.r)/median.A.SFO), digits=1)) + checkIdentical(dev.A.SFO.1 < 1, rep(TRUE, length(dev.A.SFO.1))) + + fit.A.SFO.2.r <- as.numeric(c(fit.A.SFO.2$bparms.optim, endpoints(fit.A.SFO.2)$distimes)) + dev.A.SFO.2 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.2.r)/median.A.SFO), digits=1)) + checkIdentical(dev.A.SFO.2 < 1, rep(TRUE, length(dev.A.SFO.2))) + + fit.C.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_C, quiet=TRUE) + fit.C.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_C, quiet=TRUE) + + median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "C", + c(M0, k, DT50, DT90)), "median")) + + fit.C.SFO.1.r <- as.numeric(c(fit.C.SFO.1$bparms.optim, endpoints(fit.C.SFO.1)$distimes)) + dev.C.SFO.1 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.1.r)/median.C.SFO), digits=1)) + checkIdentical(dev.C.SFO.1 < 1, rep(TRUE, length(dev.C.SFO.1))) + + fit.C.SFO.2.r <- as.numeric(c(fit.C.SFO.2$bparms.optim, endpoints(fit.C.SFO.2)$distimes)) + dev.C.SFO.2 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.2.r)/median.C.SFO), digits=1)) + checkIdentical(dev.C.SFO.2 < 1, rep(TRUE, length(dev.C.SFO.2))) +} # }}} + +# Test FOMC model to a relative tolerance of 1% {{{ +# See kinfit vignette for a discussion of FOMC fits to FOCUS_2006_A +# In this case, only M0, DT50 and DT90 are checked +test.FOCUS_2006_FOMC <- function() +{ + FOMC <- mkinmod(parent = list(type = "FOMC")) + + # FOCUS_2006_A (compare kinfit vignette for discussion) + fit.A.FOMC <- mkinfit(FOMC, FOCUS_2006_A, quiet=TRUE) + + median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "A", + c(M0, alpha, beta, DT50, DT90)), "median")) + + fit.A.FOMC.r <- as.numeric(c(fit.A.FOMC$bparms.optim, endpoints(fit.A.FOMC)$distimes[c("DT50", "DT90")])) + dev.A.FOMC <- abs(round(100 * ((median.A.FOMC - fit.A.FOMC.r)/median.A.FOMC), digits=1)) + dev.A.FOMC <- dev.A.FOMC[c(1, 4, 5)] + checkIdentical(dev.A.FOMC < 1, rep(TRUE, length(dev.A.FOMC))) + + # FOCUS_2006_B + fit.B.FOMC <- mkinfit(FOMC, FOCUS_2006_B, quiet=TRUE) + + median.B.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "B", + c(M0, alpha, beta, DT50, DT90)), "median")) + + fit.B.FOMC.r <- as.numeric(c(fit.B.FOMC$bparms.optim, endpoints(fit.B.FOMC)$distimes[c("DT50", "DT90")])) + dev.B.FOMC <- abs(round(100 * ((median.B.FOMC - fit.B.FOMC.r)/median.B.FOMC), digits=1)) + dev.B.FOMC <- dev.B.FOMC[c(1, 4, 5)] + checkIdentical(dev.B.FOMC < 1, rep(TRUE, length(dev.B.FOMC))) + + # FOCUS_2006_C + fit.C.FOMC <- mkinfit(FOMC, FOCUS_2006_C, quiet=TRUE) + + median.C.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "C", + c(M0, alpha, beta, DT50, DT90)), "median")) + + fit.C.FOMC.r <- as.numeric(c(fit.C.FOMC$bparms.optim, endpoints(fit.C.FOMC)$distimes[c("DT50", "DT90")])) + dev.C.FOMC <- abs(round(100 * ((median.C.FOMC - fit.C.FOMC.r)/median.C.FOMC), digits=1)) + dev.C.FOMC <- dev.C.FOMC[c(1, 4, 5)] + checkIdentical(dev.C.FOMC < 1, rep(TRUE, length(dev.C.FOMC))) +} # }}} + +# Test DFOP model, tolerance of 1% with the exception of f parameter for A {{{ +test.FOCUS_2006_DFOP <- function() +{ + DFOP <- mkinmod(parent = list(type = "DFOP")) + + # FOCUS_2006_A + # Results were too much dependent on algorithm, as this dataset + # is pretty much SFO. "Port" gave a lower deviance, but deviated from the + # median of FOCUS_2006 solutions + + # FOCUS_2006_B + fit.B.DFOP <- mkinfit(DFOP, FOCUS_2006_B, quiet=TRUE) + + median.B.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", + c(M0, k1, k2, f, DT50, DT90)), "median")) + + fit.B.DFOP.r <- as.numeric(c(fit.B.DFOP$bparms.optim, endpoints(fit.B.DFOP)$distimes[c("DT50", "DT90")])) + dev.B.DFOP <- abs(round(100 * ((median.B.DFOP - fit.B.DFOP.r)/median.B.DFOP), digits=1)) + # about 0.6% deviation for parameter f, the others are <= 0.1% + checkIdentical(dev.B.DFOP < 1, rep(TRUE, length(dev.B.DFOP))) +} # }}} + +# Test HS model to a relative tolerance of 1% excluding Mathematica values {{{ +# as they are unreliable +test.FOCUS_2006_HS <- function() +{ + HS <- mkinmod(parent = list(type = "HS")) + + # FOCUS_2006_A + fit.A.HS <- mkinfit(HS, FOCUS_2006_A, quiet=TRUE) + + median.A.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "A", + c(M0, k1, k2, tb, DT50, DT90)), "median")) + + fit.A.HS.r <- as.numeric(c(fit.A.HS$bparms.optim, endpoints(fit.A.HS)$distimes[c("DT50", "DT90")])) + dev.A.HS <- abs(round(100 * ((median.A.HS - fit.A.HS.r)/median.A.HS), digits=1)) + # about 6.7% deviation for parameter f, the others are < 0.1% + checkIdentical(dev.A.HS < 1, rep(TRUE, length(dev.A.HS))) + + # FOCUS_2006_B + fit.B.HS <- mkinfit(HS, FOCUS_2006_B, quiet=TRUE) + + median.B.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "B", + c(M0, k1, k2, tb, DT50, DT90)), "median")) + + fit.B.HS.r <- as.numeric(c(fit.B.HS$bparms.optim, endpoints(fit.B.HS)$distimes[c("DT50", "DT90")])) + dev.B.HS <- abs(round(100 * ((median.B.HS - fit.B.HS.r)/median.B.HS), digits=1)) + # < 10% deviation for M0, k1, DT50 and DT90, others are problematic + dev.B.HS <- dev.B.HS[c(1, 2, 5, 6)] + checkIdentical(dev.B.HS < 10, rep(TRUE, length(dev.B.HS))) + + # FOCUS_2006_C + fit.C.HS <- mkinfit(HS, FOCUS_2006_C, quiet=TRUE) + + median.C.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "C", + c(M0, k1, k2, tb, DT50, DT90)), "median")) + + fit.C.HS.r <- as.numeric(c(fit.C.HS$bparms.optim, endpoints(fit.C.HS)$distimes[c("DT50", "DT90")])) + dev.C.HS <- abs(round(100 * ((median.C.HS - fit.C.HS.r)/median.C.HS), digits=1)) + # deviation <= 0.1% + checkIdentical(dev.C.HS < 1, rep(TRUE, length(dev.C.HS))) +} # }}} + +# Test SFORB model against DFOP solutions to a relative tolerance of 1% # {{{ +test.FOCUS_2006_SFORB <- function() +{ + SFORB <- mkinmod(parent = list(type = "SFORB")) + + # FOCUS_2006_A + # Again it does not make a lot of sense to use a SFO dataset for this + + # FOCUS_2006_B + fit.B.SFORB.1 <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE) + fit.B.SFORB.2 <- mkinfit(SFORB, FOCUS_2006_B, solution_type = "deSolve", quiet=TRUE) + + median.B.SFORB <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", + c(M0, k1, k2, DT50, DT90)), "median")) + + fit.B.SFORB.1.r <- as.numeric(c( + parent_0 = fit.B.SFORB.1$bparms.optim[[1]], + k1 = endpoints(fit.B.SFORB.1)$SFORB[[1]], + k2 = endpoints(fit.B.SFORB.1)$SFORB[[2]], + endpoints(fit.B.SFORB.1)$distimes[c("DT50", "DT90")])) + dev.B.SFORB.1 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.1.r)/median.B.SFORB), digits=1)) + checkIdentical(dev.B.SFORB.1 < 1, rep(TRUE, length(dev.B.SFORB.1))) + + fit.B.SFORB.2.r <- as.numeric(c( + parent_0 = fit.B.SFORB.2$bparms.optim[[1]], + k1 = endpoints(fit.B.SFORB.2)$SFORB[[1]], + k2 = endpoints(fit.B.SFORB.2)$SFORB[[2]], + endpoints(fit.B.SFORB.2)$distimes[c("DT50", "DT90")])) + dev.B.SFORB.2 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.2.r)/median.B.SFORB), digits=1)) + checkIdentical(dev.B.SFORB.2 < 1, rep(TRUE, length(dev.B.SFORB.2))) +} # }}} + +# Test eigenvalue based fit to Schaefer 2007 data against solution from conference paper {{{ +test.mkinfit.schaefer07_complex_example <- function() +{ + schaefer07_complex_model <- mkinmod( + parent = list(type = "SFO", to = c("A1", "B1", "C1"), sink = FALSE), + A1 = list(type = "SFO", to = "A2"), + B1 = list(type = "SFO"), + C1 = list(type = "SFO"), + A2 = list(type = "SFO"), use_of_ff = "max") + + # If we use the default algorithm 'Marq' we need to give a good starting + # estimate for k_A2 in order to find the solution published by Schaefer et al. + fit <- mkinfit(schaefer07_complex_model, method.modFit = "Port", + mkin_wide_to_long(schaefer07_complex_case, time = "time")) + s <- summary(fit) + r <- schaefer07_complex_results + attach(as.list(fit$bparms.optim)) + r$mkin <- c( + k_parent, + s$distimes["parent", "DT50"], + s$ff["parent_A1"], + k_A1, + s$distimes["A1", "DT50"], + s$ff["parent_B1"], + k_B1, + s$distimes["B1", "DT50"], + s$ff["parent_C1"], + k_C1, + s$distimes["C1", "DT50"], + s$ff["A1_A2"], + k_A2, + s$distimes["A2", "DT50"]) + r$means <- (r$KinGUI + r$ModelMaker)/2 + r$mkin.deviation <- abs(round(100 * ((r$mkin - r$means)/r$means), digits=1)) + checkIdentical(r$mkin.deviation < 10, rep(TRUE, 14)) +} # }}} + +# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/tests/testthat/runit.mkinpredict.R b/tests/testthat/runit.mkinpredict.R new file mode 100644 index 00000000..997857ce --- /dev/null +++ b/tests/testthat/runit.mkinpredict.R @@ -0,0 +1,86 @@ +# $Id: jranke $ + +# Copyright (C) 2012 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 + +# Check solution types for SFO {{{ +test.SFO_solution_types <- function() +{ + ot = seq(0, 100, by = 1) + SFO <- mkinmod(parent = list(type = "SFO")) + SFO.analytical <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), + c(parent = 100), ot, solution_type = "analytical"), time == 100), digits=5) + SFO.deSolve <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), + c(parent = 100), ot, solution_type = "deSolve"), time == 100), digits=5) + SFO.eigen <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), + c(parent = 100), ot, solution_type = "eigen"), time == 100), digits=5) + + checkEquals(SFO.analytical, SFO.deSolve) + checkEquals(SFO.analytical, SFO.eigen) +} # }}} + +# Check model specification and solution types for SFO_SFO {{{ +# Relative Tolerance is 0.01% +# Do not use time 0, as eigenvalue based solution does not give 0 at time 0 for metabolites +# and relative tolerance is thus not met +test.SFO_solution_types <- function() +{ + tol = 0.01 + SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "min") + SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "max") + + ot = seq(0, 100, by = 1) + r.1.e <- subset(mkinpredict(SFO_SFO.1, + c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "eigen"), + time %in% c(1, 10, 50, 100)) + r.1.d <- subset(mkinpredict(SFO_SFO.1, + c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), + time %in% c(1, 10, 50, 100)) + + r.2.e <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "eigen"), + time %in% c(1, 10, 50, 100)) + r.2.d <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), + time %in% c(1, 10, 50, 100)) + + # Compare eigen and deSolve for minimum use of formation fractions + dev.1.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] + dev.1.e_d.percent = as.numeric(unlist((dev.1.e_d.percent))) + dev.1.e_d.percent = ifelse(is.na(dev.1.e_d.percent), 0, dev.1.e_d.percent) + checkIdentical(dev.1.e_d.percent < tol, rep(TRUE, length(dev.1.e_d.percent))) + + # Compare eigen and deSolve for maximum use of formation fractions + dev.2.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] + dev.2.e_d.percent = as.numeric(unlist((dev.2.e_d.percent))) + dev.2.e_d.percent = ifelse(is.na(dev.2.e_d.percent), 0, dev.2.e_d.percent) + checkIdentical(dev.2.e_d.percent < tol, rep(TRUE, length(dev.2.e_d.percent))) + + # Compare minimum and maximum use of formation fractions + dev.1_2.e.percent = 100 * (r.1.e[-1] - r.2.e[-1])/r.1.e[-1] + dev.1_2.e.percent = as.numeric(unlist((dev.1_2.e.percent))) + dev.1_2.e.percent = ifelse(is.na(dev.1_2.e.percent), 0, dev.1_2.e.percent) + checkIdentical(dev.1_2.e.percent < tol, rep(TRUE, length(dev.1_2.e.percent))) + +} # }}} + +# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/tests/testthat/test_parent_only.R b/tests/testthat/test_parent_only.R new file mode 100644 index 00000000..16fc5131 --- /dev/null +++ b/tests/testthat/test_parent_only.R @@ -0,0 +1,48 @@ +context("Fitting of parent only models") + +calc_dev.percent <- function(fitlist, reference) { + for (i in 1:length(fitlist)) { + fit <- fitlist[[i]] + results <- c(fit$bparms.optim, + endpoints(fit)$distimes$DT50, + endpoints(fit)$distimes$DT90) + dev.percent[[i]] <- abs(100 * ((reference - results)/reference)) + } + return(dev.percent) +} + +SFO <- mkinmod(parent = list(type = "SFO")) +FOMC <- mkinmod(parent = list(type = "FOMC")) + +test_that("SFO fit for FOCUS A deviates less than 0.1% from median of values from FOCUS report", { + fits.A.SFO <- list() + fits.A.SFO[[1]] <- mkinfit("SFO", FOCUS_2006_A, quiet=TRUE) + fits.A.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE) + fits.A.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE, solution_type = "eigen") + fits.A.SFO[[4]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE, solution_type = "deSolve") + + median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, + dataset == "A", + c(M0, k, DT50, DT90)), "median")) + + dev.percent <- calc_dev.percent(fits.A.SFO, median.A.SFO) + expect_equivalent(dev.percent[[1]] < 0.1, rep(TRUE, 4)) + expect_equivalent(dev.percent[[2]] < 0.1, rep(TRUE, 4)) + expect_equivalent(dev.percent[[3]] < 0.1, rep(TRUE, 4)) + expect_equivalent(dev.percent[[4]] < 0.1, rep(TRUE, 4)) +}) + +test_that("SFO fit for FOCUS C deviates less than 0.1% from median of values from FOCUS report", { + fits.C.SFO <- list() + fits.C.SFO[[1]] <- mkinfit("SFO", FOCUS_2006_C, quiet=TRUE) + fits.C.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_C, quiet=TRUE) + fits.C.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_C, quiet=TRUE, solution_type = "deSolve") + + median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, + dataset == "C", + c(M0, k, DT50, DT90)), "median")) + dev.percent <- calc_dev.percent(fits.C.SFO, median.C.SFO) + expect_equivalent(dev.percent[[1]] < 0.1, rep(TRUE, 4)) + expect_equivalent(dev.percent[[2]] < 0.1, rep(TRUE, 4)) + expect_equivalent(dev.percent[[3]] < 0.1, rep(TRUE, 4)) +}) -- cgit v1.2.1 From 656466946f093617ababebe5ec3b36809234112a Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 08:49:40 +0200 Subject: Finished migration from RUnit to testthat --- DESCRIPTION | 2 +- GNUmakefile | 1 + NEWS.md | 5 + tests/testthat/runit.mkinerrmin.R | 62 ------- tests/testthat/runit.mkinfit.R | 227 -------------------------- tests/testthat/test_FOCUS_D_UBA_expertise.R | 58 +++++++ tests/testthat/test_FOCUS_chi2_error_level.R | 53 ++++++ tests/testthat/test_parent_only.R | 210 +++++++++++++++++++++--- tests/testthat/test_schaefer07_complex_case.R | 68 ++++++++ 9 files changed, 370 insertions(+), 316 deletions(-) delete mode 100644 tests/testthat/runit.mkinerrmin.R delete mode 100644 tests/testthat/runit.mkinfit.R create mode 100644 tests/testthat/test_FOCUS_D_UBA_expertise.R create mode 100644 tests/testthat/test_FOCUS_chi2_error_level.R create mode 100644 tests/testthat/test_schaefer07_complex_case.R diff --git a/DESCRIPTION b/DESCRIPTION index 0477a200..5812868b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Routines for Fitting Kinetic Models with One or More State Variables to Chemical Degradation Data Version: 0.9-35 -Date: 2015-05-13 +Date: 2015-05-14 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), diff --git a/GNUmakefile b/GNUmakefile index b731166b..783ceb14 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -104,5 +104,6 @@ winbuilder: build submit: @echo "\nHow about make test, make check, make winbuilder and make r-forge?" @echo "\nIs the DESCRIPTION file up to date?" + @echo "\nIs the NEWS.md file up to date?" @echo "\nAre you sure you want to release to CRAN?" @echo "\nThen use the form at http://cran.r-project.org/submit.html" diff --git a/NEWS.md b/NEWS.md index 1caaa148..cc9f28f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # CHANGES in mkin VERSION 0.9-35 +## MAJOR CHANGES + +- Switch from RUnit to testthat for testing + ## BUG FIXES - `mkinparplot()`: Avoid warnings that occurred when not all confidence intervals were available in the summary of the fit @@ -10,6 +14,7 @@ ## NEW FEATURES - `mkinfit()`: Report successful termination when quiet = FALSE. This is helpful for more difficult problems fitted with reweight.method = obs, as no progress is often indicated during the reweighting. +- A first test using results established in the expertise written for the German Federal Environmental Agency (UBA) was added. # CHANGES in mkin VERSION 0.9-34 diff --git a/tests/testthat/runit.mkinerrmin.R b/tests/testthat/runit.mkinerrmin.R deleted file mode 100644 index 56a33ff9..00000000 --- a/tests/testthat/runit.mkinerrmin.R +++ /dev/null @@ -1,62 +0,0 @@ -# Test SFO_SFO model with FOCUS_2006_D against Schaefer 2007 paper, tolerance = 1% # {{{ -# and check chi2 error values against values obtained with mkin 0.33 -test.FOCUS_2006_D_SFO_SFO <- function() -{ - SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "min") - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - fit.1.e <- mkinfit(SFO_SFO.1, FOCUS_2006_D) - fit.1.d <- mkinfit(SFO_SFO.1, solution_type = "deSolve", FOCUS_2006_D) - fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_D) - fit.2.d <- mkinfit(SFO_SFO.2, solution_type = "deSolve", FOCUS_2006_D) - - FOCUS_2006_D_results_schaefer07_means <- c( - parent_0 = 99.65, DT50_parent = 7.04, DT50_m1 = 131.34) - - r.1.e <- c(fit.1.e$bparms.optim[[1]], endpoints(fit.1.e)$distimes[[1]]) - r.1.d <- c(fit.1.d$bparms.optim[[1]], endpoints(fit.1.d)$distimes[[1]]) - r.2.e <- c(fit.2.e$bparms.optim[[1]], endpoints(fit.2.e)$distimes[[1]]) - r.2.d <- c(fit.2.d$bparms.optim[[1]], endpoints(fit.2.d)$distimes[[1]]) - - dev.1.e <- 100 * (r.1.e - FOCUS_2006_D_results_schaefer07_means)/r.1.e - checkIdentical(as.numeric(abs(dev.1.e)) < 1, rep(TRUE, 3)) - dev.1.d <- 100 * (r.1.d - FOCUS_2006_D_results_schaefer07_means)/r.1.d - checkIdentical(as.numeric(abs(dev.1.d)) < 1, rep(TRUE, 3)) - dev.2.e <- 100 * (r.2.e - FOCUS_2006_D_results_schaefer07_means)/r.2.e - checkIdentical(as.numeric(abs(dev.2.e)) < 1, rep(TRUE, 3)) - dev.2.d <- 100 * (r.2.d - FOCUS_2006_D_results_schaefer07_means)/r.2.d - checkIdentical(as.numeric(abs(dev.2.d)) < 1, rep(TRUE, 3)) - - round(mkinerrmin(fit.2.e), 4) - round(mkinerrmin(fit.2.d), 4) - - errmin.FOCUS_2006_D_rounded = data.frame( - err.min = c(0.0640, 0.0646, 0.0469), - n.optim = c(4, 2, 2), - df = c(15, 7, 8), - row.names = c("All data", "parent", "m1")) - checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), - errmin.FOCUS_2006_D_rounded) -} # }}} - -# Test SFO_SFO model with FOCUS_2006_E against values obtained with mkin 0.33 {{{ -test.FOCUS_2006_E_SFO_SFO <- function() -{ - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - fit.2.e <- mkinfit(SFO_SFO.2, FOCUS_2006_E) - - round(mkinerrmin(fit.2.e), 4) - errmin.FOCUS_2006_E_rounded = data.frame( - err.min = c(0.1544, 0.1659, 0.1095), - n.optim = c(4, 2, 2), - df = c(13, 7, 6), - row.names = c("All data", "parent", "m1")) - checkEqualsNumeric(round(mkinerrmin(fit.2.e), 4), - errmin.FOCUS_2006_E_rounded) -} # }}} - - diff --git a/tests/testthat/runit.mkinfit.R b/tests/testthat/runit.mkinfit.R deleted file mode 100644 index 8eefb995..00000000 --- a/tests/testthat/runit.mkinfit.R +++ /dev/null @@ -1,227 +0,0 @@ -# Copyright (C) 2010-2014 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 - -# Test SFO model to a relative tolerance of 1% # {{{ -test.FOCUS_2006_SFO <- function() -{ - SFO.1 <- mkinmod(parent = list(type = "SFO")) - SFO.2 <- mkinmod(parent = list(type = "SFO"), use_of_ff = "max") - - fit.A.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_A, quiet=TRUE) - fit.A.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_A, quiet=TRUE) - - median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "A", - c(M0, k, DT50, DT90)), "median")) - - fit.A.SFO.1.r <- as.numeric(c(fit.A.SFO.1$bparms.optim, endpoints(fit.A.SFO.1)$distimes)) - dev.A.SFO.1 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.1.r)/median.A.SFO), digits=1)) - checkIdentical(dev.A.SFO.1 < 1, rep(TRUE, length(dev.A.SFO.1))) - - fit.A.SFO.2.r <- as.numeric(c(fit.A.SFO.2$bparms.optim, endpoints(fit.A.SFO.2)$distimes)) - dev.A.SFO.2 <- abs(round(100 * ((median.A.SFO - fit.A.SFO.2.r)/median.A.SFO), digits=1)) - checkIdentical(dev.A.SFO.2 < 1, rep(TRUE, length(dev.A.SFO.2))) - - fit.C.SFO.1 <- mkinfit(SFO.1, FOCUS_2006_C, quiet=TRUE) - fit.C.SFO.2 <- mkinfit(SFO.2, FOCUS_2006_C, quiet=TRUE) - - median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "C", - c(M0, k, DT50, DT90)), "median")) - - fit.C.SFO.1.r <- as.numeric(c(fit.C.SFO.1$bparms.optim, endpoints(fit.C.SFO.1)$distimes)) - dev.C.SFO.1 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.1.r)/median.C.SFO), digits=1)) - checkIdentical(dev.C.SFO.1 < 1, rep(TRUE, length(dev.C.SFO.1))) - - fit.C.SFO.2.r <- as.numeric(c(fit.C.SFO.2$bparms.optim, endpoints(fit.C.SFO.2)$distimes)) - dev.C.SFO.2 <- abs(round(100 * ((median.C.SFO - fit.C.SFO.2.r)/median.C.SFO), digits=1)) - checkIdentical(dev.C.SFO.2 < 1, rep(TRUE, length(dev.C.SFO.2))) -} # }}} - -# Test FOMC model to a relative tolerance of 1% {{{ -# See kinfit vignette for a discussion of FOMC fits to FOCUS_2006_A -# In this case, only M0, DT50 and DT90 are checked -test.FOCUS_2006_FOMC <- function() -{ - FOMC <- mkinmod(parent = list(type = "FOMC")) - - # FOCUS_2006_A (compare kinfit vignette for discussion) - fit.A.FOMC <- mkinfit(FOMC, FOCUS_2006_A, quiet=TRUE) - - median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "A", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.A.FOMC.r <- as.numeric(c(fit.A.FOMC$bparms.optim, endpoints(fit.A.FOMC)$distimes[c("DT50", "DT90")])) - dev.A.FOMC <- abs(round(100 * ((median.A.FOMC - fit.A.FOMC.r)/median.A.FOMC), digits=1)) - dev.A.FOMC <- dev.A.FOMC[c(1, 4, 5)] - checkIdentical(dev.A.FOMC < 1, rep(TRUE, length(dev.A.FOMC))) - - # FOCUS_2006_B - fit.B.FOMC <- mkinfit(FOMC, FOCUS_2006_B, quiet=TRUE) - - median.B.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "B", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.B.FOMC.r <- as.numeric(c(fit.B.FOMC$bparms.optim, endpoints(fit.B.FOMC)$distimes[c("DT50", "DT90")])) - dev.B.FOMC <- abs(round(100 * ((median.B.FOMC - fit.B.FOMC.r)/median.B.FOMC), digits=1)) - dev.B.FOMC <- dev.B.FOMC[c(1, 4, 5)] - checkIdentical(dev.B.FOMC < 1, rep(TRUE, length(dev.B.FOMC))) - - # FOCUS_2006_C - fit.C.FOMC <- mkinfit(FOMC, FOCUS_2006_C, quiet=TRUE) - - median.C.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "C", - c(M0, alpha, beta, DT50, DT90)), "median")) - - fit.C.FOMC.r <- as.numeric(c(fit.C.FOMC$bparms.optim, endpoints(fit.C.FOMC)$distimes[c("DT50", "DT90")])) - dev.C.FOMC <- abs(round(100 * ((median.C.FOMC - fit.C.FOMC.r)/median.C.FOMC), digits=1)) - dev.C.FOMC <- dev.C.FOMC[c(1, 4, 5)] - checkIdentical(dev.C.FOMC < 1, rep(TRUE, length(dev.C.FOMC))) -} # }}} - -# Test DFOP model, tolerance of 1% with the exception of f parameter for A {{{ -test.FOCUS_2006_DFOP <- function() -{ - DFOP <- mkinmod(parent = list(type = "DFOP")) - - # FOCUS_2006_A - # Results were too much dependent on algorithm, as this dataset - # is pretty much SFO. "Port" gave a lower deviance, but deviated from the - # median of FOCUS_2006 solutions - - # FOCUS_2006_B - fit.B.DFOP <- mkinfit(DFOP, FOCUS_2006_B, quiet=TRUE) - - median.B.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", - c(M0, k1, k2, f, DT50, DT90)), "median")) - - fit.B.DFOP.r <- as.numeric(c(fit.B.DFOP$bparms.optim, endpoints(fit.B.DFOP)$distimes[c("DT50", "DT90")])) - dev.B.DFOP <- abs(round(100 * ((median.B.DFOP - fit.B.DFOP.r)/median.B.DFOP), digits=1)) - # about 0.6% deviation for parameter f, the others are <= 0.1% - checkIdentical(dev.B.DFOP < 1, rep(TRUE, length(dev.B.DFOP))) -} # }}} - -# Test HS model to a relative tolerance of 1% excluding Mathematica values {{{ -# as they are unreliable -test.FOCUS_2006_HS <- function() -{ - HS <- mkinmod(parent = list(type = "HS")) - - # FOCUS_2006_A - fit.A.HS <- mkinfit(HS, FOCUS_2006_A, quiet=TRUE) - - median.A.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "A", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.A.HS.r <- as.numeric(c(fit.A.HS$bparms.optim, endpoints(fit.A.HS)$distimes[c("DT50", "DT90")])) - dev.A.HS <- abs(round(100 * ((median.A.HS - fit.A.HS.r)/median.A.HS), digits=1)) - # about 6.7% deviation for parameter f, the others are < 0.1% - checkIdentical(dev.A.HS < 1, rep(TRUE, length(dev.A.HS))) - - # FOCUS_2006_B - fit.B.HS <- mkinfit(HS, FOCUS_2006_B, quiet=TRUE) - - median.B.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "B", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.B.HS.r <- as.numeric(c(fit.B.HS$bparms.optim, endpoints(fit.B.HS)$distimes[c("DT50", "DT90")])) - dev.B.HS <- abs(round(100 * ((median.B.HS - fit.B.HS.r)/median.B.HS), digits=1)) - # < 10% deviation for M0, k1, DT50 and DT90, others are problematic - dev.B.HS <- dev.B.HS[c(1, 2, 5, 6)] - checkIdentical(dev.B.HS < 10, rep(TRUE, length(dev.B.HS))) - - # FOCUS_2006_C - fit.C.HS <- mkinfit(HS, FOCUS_2006_C, quiet=TRUE) - - median.C.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, dataset == "C", - c(M0, k1, k2, tb, DT50, DT90)), "median")) - - fit.C.HS.r <- as.numeric(c(fit.C.HS$bparms.optim, endpoints(fit.C.HS)$distimes[c("DT50", "DT90")])) - dev.C.HS <- abs(round(100 * ((median.C.HS - fit.C.HS.r)/median.C.HS), digits=1)) - # deviation <= 0.1% - checkIdentical(dev.C.HS < 1, rep(TRUE, length(dev.C.HS))) -} # }}} - -# Test SFORB model against DFOP solutions to a relative tolerance of 1% # {{{ -test.FOCUS_2006_SFORB <- function() -{ - SFORB <- mkinmod(parent = list(type = "SFORB")) - - # FOCUS_2006_A - # Again it does not make a lot of sense to use a SFO dataset for this - - # FOCUS_2006_B - fit.B.SFORB.1 <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE) - fit.B.SFORB.2 <- mkinfit(SFORB, FOCUS_2006_B, solution_type = "deSolve", quiet=TRUE) - - median.B.SFORB <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, dataset == "B", - c(M0, k1, k2, DT50, DT90)), "median")) - - fit.B.SFORB.1.r <- as.numeric(c( - parent_0 = fit.B.SFORB.1$bparms.optim[[1]], - k1 = endpoints(fit.B.SFORB.1)$SFORB[[1]], - k2 = endpoints(fit.B.SFORB.1)$SFORB[[2]], - endpoints(fit.B.SFORB.1)$distimes[c("DT50", "DT90")])) - dev.B.SFORB.1 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.1.r)/median.B.SFORB), digits=1)) - checkIdentical(dev.B.SFORB.1 < 1, rep(TRUE, length(dev.B.SFORB.1))) - - fit.B.SFORB.2.r <- as.numeric(c( - parent_0 = fit.B.SFORB.2$bparms.optim[[1]], - k1 = endpoints(fit.B.SFORB.2)$SFORB[[1]], - k2 = endpoints(fit.B.SFORB.2)$SFORB[[2]], - endpoints(fit.B.SFORB.2)$distimes[c("DT50", "DT90")])) - dev.B.SFORB.2 <- abs(round(100 * ((median.B.SFORB - fit.B.SFORB.2.r)/median.B.SFORB), digits=1)) - checkIdentical(dev.B.SFORB.2 < 1, rep(TRUE, length(dev.B.SFORB.2))) -} # }}} - -# Test eigenvalue based fit to Schaefer 2007 data against solution from conference paper {{{ -test.mkinfit.schaefer07_complex_example <- function() -{ - schaefer07_complex_model <- mkinmod( - parent = list(type = "SFO", to = c("A1", "B1", "C1"), sink = FALSE), - A1 = list(type = "SFO", to = "A2"), - B1 = list(type = "SFO"), - C1 = list(type = "SFO"), - A2 = list(type = "SFO"), use_of_ff = "max") - - # If we use the default algorithm 'Marq' we need to give a good starting - # estimate for k_A2 in order to find the solution published by Schaefer et al. - fit <- mkinfit(schaefer07_complex_model, method.modFit = "Port", - mkin_wide_to_long(schaefer07_complex_case, time = "time")) - s <- summary(fit) - r <- schaefer07_complex_results - attach(as.list(fit$bparms.optim)) - r$mkin <- c( - k_parent, - s$distimes["parent", "DT50"], - s$ff["parent_A1"], - k_A1, - s$distimes["A1", "DT50"], - s$ff["parent_B1"], - k_B1, - s$distimes["B1", "DT50"], - s$ff["parent_C1"], - k_C1, - s$distimes["C1", "DT50"], - s$ff["A1_A2"], - k_A2, - s$distimes["A2", "DT50"]) - r$means <- (r$KinGUI + r$ModelMaker)/2 - r$mkin.deviation <- abs(round(100 * ((r$mkin - r$means)/r$means), digits=1)) - checkIdentical(r$mkin.deviation < 10, rep(TRUE, 14)) -} # }}} - -# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/tests/testthat/test_FOCUS_D_UBA_expertise.R b/tests/testthat/test_FOCUS_D_UBA_expertise.R new file mode 100644 index 00000000..f9322714 --- /dev/null +++ b/tests/testthat/test_FOCUS_D_UBA_expertise.R @@ -0,0 +1,58 @@ +# Copyright (C) 2015 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +context("Results for FOCUS D established in expertise for UBA (Ranke 2014)") + +SFO_SFO <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO")) +SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), + use_of_ff = "max") + +fit.default <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE) +fit.ff <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE) + +# Results are from p. 40 + +test_that("Fitted parameters are correct for FOCUS D", { + expect_equivalent(round(fit.ff$bparms.optim, c(2, 4, 4, 4)), + c(99.60, 0.0987, 0.0053, 0.5145)) +}) + +test_that("Fitted parameters are correct for FOCUS D", { + expect_equivalent(round(100 * mkinerrmin(fit.ff)$err.min, 2), + c(6.40, 6.46, 4.69)) +}) + +test_that("DT50/90 are correct for FOCUS D when using formation fractions", { + expect_equal(round(as.numeric(endpoints(fit.ff)$distimes["parent", ]), 2), + c(7.02, 23.33)) + expect_equal(round(as.numeric(endpoints(fit.ff)$distimes["m1", ]), 1), + c(131.8, 437.7)) +}) + +test_that("DT50/90 are correct for FOCUS D when not using formation fractions", { + expect_equal(round(as.numeric(endpoints(fit.default)$distimes["parent", ]), 2), + c(7.02, 23.33)) + expect_equal(round(as.numeric(endpoints(fit.default)$distimes["m1", ]), 1), + c(131.8, 437.7)) +}) + +# References: +# Ranke (2014) Prüfung und Validierung von Modellierungssoftware als Alternative +# zu ModelMaker 4.0, Umweltbundesamt Projektnummer 27452 diff --git a/tests/testthat/test_FOCUS_chi2_error_level.R b/tests/testthat/test_FOCUS_chi2_error_level.R new file mode 100644 index 00000000..65e1ada7 --- /dev/null +++ b/tests/testthat/test_FOCUS_chi2_error_level.R @@ -0,0 +1,53 @@ +# Copyright (C) 2014-15 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 + +# These tests were migrated from inst/unitTests/runit.mkinerrmin.R + +context("Calculation of FOCUS chi2 error levels") + +SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), + use_of_ff = "max") + +test_that("Chi2 error levels for FOCUS D are as in mkin 0.9-33", { + + fit <- mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE) + + errmin.FOCUS_2006_D_rounded = data.frame( + err.min = c(0.0640, 0.0646, 0.0469), + n.optim = c(4, 2, 2), + df = c(15, 7, 8), + row.names = c("All data", "parent", "m1")) + + expect_equal(round(mkinerrmin(fit), 4), + errmin.FOCUS_2006_D_rounded) +}) + +test_that("Chi2 error levels for FOCUS E are as in mkin 0.9-33", { + + fit <- mkinfit(SFO_SFO.ff, FOCUS_2006_E, quiet = TRUE) + + errmin.FOCUS_2006_E_rounded = data.frame( + err.min = c(0.1544, 0.1659, 0.1095), + n.optim = c(4, 2, 2), + df = c(13, 7, 6), + row.names = c("All data", "parent", "m1")) + + expect_equal(round(mkinerrmin(fit), 4), + errmin.FOCUS_2006_E_rounded) +}) diff --git a/tests/testthat/test_parent_only.R b/tests/testthat/test_parent_only.R index 16fc5131..2ed38ffa 100644 --- a/tests/testthat/test_parent_only.R +++ b/tests/testthat/test_parent_only.R @@ -1,11 +1,35 @@ +# Copyright (C) 2015 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + context("Fitting of parent only models") -calc_dev.percent <- function(fitlist, reference) { +calc_dev.percent <- function(fitlist, reference, endpoints = TRUE, round_results = NULL) { + dev.percent <- list() for (i in 1:length(fitlist)) { fit <- fitlist[[i]] - results <- c(fit$bparms.optim, - endpoints(fit)$distimes$DT50, - endpoints(fit)$distimes$DT90) + if (endpoints) { + results <- c(fit$bparms.optim, + endpoints(fit)$distimes$DT50, + endpoints(fit)$distimes$DT90) + } else { + results <- fit$bparms.optim + } + if (!missing(round_results)) results <- round(results, round_results) dev.percent[[i]] <- abs(100 * ((reference - results)/reference)) } return(dev.percent) @@ -13,36 +37,170 @@ calc_dev.percent <- function(fitlist, reference) { SFO <- mkinmod(parent = list(type = "SFO")) FOMC <- mkinmod(parent = list(type = "FOMC")) +DFOP <- mkinmod(parent = list(type = "DFOP")) +HS <- mkinmod(parent = list(type = "HS")) +SFORB <- mkinmod(parent = list(type = "SFORB")) -test_that("SFO fit for FOCUS A deviates less than 0.1% from median of values from FOCUS report", { - fits.A.SFO <- list() - fits.A.SFO[[1]] <- mkinfit("SFO", FOCUS_2006_A, quiet=TRUE) - fits.A.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE) - fits.A.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE, solution_type = "eigen") - fits.A.SFO[[4]] <- mkinfit(SFO, FOCUS_2006_A, quiet=TRUE, solution_type = "deSolve") +test_that("Fits for FOCUS A deviate less than 0.1% from median of values from FOCUS report", { + fit.A.SFO <- list(mkinfit("SFO", FOCUS_2006_A, quiet = TRUE)) median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, dataset == "A", c(M0, k, DT50, DT90)), "median")) - dev.percent <- calc_dev.percent(fits.A.SFO, median.A.SFO) - expect_equivalent(dev.percent[[1]] < 0.1, rep(TRUE, 4)) - expect_equivalent(dev.percent[[2]] < 0.1, rep(TRUE, 4)) - expect_equivalent(dev.percent[[3]] < 0.1, rep(TRUE, 4)) - expect_equivalent(dev.percent[[4]] < 0.1, rep(TRUE, 4)) + dev.percent.A.SFO <- calc_dev.percent(fit.A.SFO, median.A.SFO) + expect_equivalent(dev.percent.A.SFO[[1]] < 0.1, rep(TRUE, 4)) + + fit.A.FOMC <- list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE)) + + median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, + dataset == "A", + c(M0, alpha, beta, DT50, DT90)), "median")) + + dev.percent.A.FOMC <- calc_dev.percent(fit.A.FOMC, median.A.FOMC) + #expect_equivalent(dev.percent.A.FOMC[[1]] < 0.1, rep(TRUE, 5)) # alpha and beta ill-determined + expect_equivalent(dev.percent.A.FOMC[[1]][c(1, 4, 5)] < 0.1, rep(TRUE, 3)) + + fit.A.DFOP <- list(mkinfit("DFOP", FOCUS_2006_A, quiet = TRUE)) + + median.A.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, + dataset == "A", + c(M0, k1, k2, f, DT50, DT90)), "median")) + + dev.percent.A.DFOP <- calc_dev.percent(fit.A.DFOP, median.A.DFOP) + #expect_equivalent(dev.percent.A.DFOP[[1]] < 0.1, rep(TRUE, 6)) # g/f is ill-determined + expect_equivalent(dev.percent.A.DFOP[[1]][c(1, 2, 3, 5, 6)] < 0.1, rep(TRUE, 5)) + + fit.A.HS <- list(mkinfit("HS", FOCUS_2006_A, quiet = TRUE)) + + median.A.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, + dataset == "A", + c(M0, k1, k2, tb, DT50, DT90)), "median")) + + dev.percent.A.HS <- calc_dev.percent(fit.A.HS, median.A.HS) + expect_equivalent(dev.percent.A.HS[[1]] < 0.1, rep(TRUE, 6)) }) -test_that("SFO fit for FOCUS C deviates less than 0.1% from median of values from FOCUS report", { - fits.C.SFO <- list() - fits.C.SFO[[1]] <- mkinfit("SFO", FOCUS_2006_C, quiet=TRUE) - fits.C.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_C, quiet=TRUE) - fits.C.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_C, quiet=TRUE, solution_type = "deSolve") +test_that("Fits for FOCUS B deviate less than 0.1% from median of values from FOCUS report", { + fit.B.SFO <- list(mkinfit("SFO", FOCUS_2006_B, quiet = TRUE)) + + median.B.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, + dataset == "B", + c(M0, k, DT50, DT90)), "median")) + + dev.percent.B.SFO <- calc_dev.percent(fit.B.SFO, median.B.SFO) + expect_equivalent(dev.percent.B.SFO[[1]] < 0.1, rep(TRUE, 4)) + + fit.B.FOMC <- list(mkinfit("FOMC", FOCUS_2006_B, quiet = TRUE)) + + median.B.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, + dataset == "B", + c(M0, alpha, beta, DT50, DT90)), "median")) + + dev.percent.B.FOMC <- calc_dev.percent(fit.B.FOMC, median.B.FOMC) + expect_equivalent(dev.percent.B.FOMC[[1]] < 0.1, rep(TRUE, 5)) + + fit.B.DFOP <- list(mkinfit("DFOP", FOCUS_2006_B, quiet = TRUE)) + + median.B.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B, + dataset == "B", + c(M0, k1, k2, f, DT50, DT90)), "median")) + + dev.percent.B.DFOP <- calc_dev.percent(fit.B.DFOP, median.B.DFOP) + #expect_equivalent(dev.percent.B.DFOP[[1]] < 0.1, rep(TRUE, 6)) # g/f is ill-determined + expect_equivalent(dev.percent.B.DFOP[[1]][c(1, 2, 3, 5, 6)] < 0.1, rep(TRUE, 5)) + + fit.B.HS <- list(mkinfit("HS", FOCUS_2006_B, quiet = TRUE)) + + median.B.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, + dataset == "B", + c(M0, k1, k2, tb, DT50, DT90)), + "median", na.rm = TRUE)) + + dev.percent.B.HS <- calc_dev.percent(fit.B.HS, median.B.HS) + expect_equivalent(dev.percent.B.HS[[1]] < 0.1, rep(TRUE, 6)) + + fit.B.SFORB <- list(mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE)) + dev.percent.B.SFORB <- calc_dev.percent(fit.B.SFORB, median.B.DFOP) + expect_equivalent(dev.percent.B.SFORB[[1]][c(1, 5, 6)] < 0.1, rep(TRUE, 3)) +}) + +test_that("Fits for FOCUS C deviate less than 0.1% from median of values from FOCUS report", { + fit.C.SFO <- list(mkinfit("SFO", FOCUS_2006_C, quiet = TRUE)) median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F, - dataset == "C", - c(M0, k, DT50, DT90)), "median")) - dev.percent <- calc_dev.percent(fits.C.SFO, median.C.SFO) - expect_equivalent(dev.percent[[1]] < 0.1, rep(TRUE, 4)) - expect_equivalent(dev.percent[[2]] < 0.1, rep(TRUE, 4)) - expect_equivalent(dev.percent[[3]] < 0.1, rep(TRUE, 4)) + dataset == "C", + c(M0, k, DT50, DT90)), "median")) + + dev.percent.C.SFO <- calc_dev.percent(fit.C.SFO, median.C.SFO) + expect_equivalent(dev.percent.C.SFO[[1]] < 0.1, rep(TRUE, 4)) + + fit.C.FOMC <- list(mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE)) + + median.C.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, + dataset == "C", + c(M0, alpha, beta, DT50, DT90)), "median")) + + dev.percent.C.FOMC <- calc_dev.percent(fit.C.FOMC, median.C.FOMC, + round_results = 2) # Not enough precision in FOCUS results + expect_equivalent(dev.percent.C.FOMC[[1]] < 0.1, rep(TRUE, 5)) + + fit.C.HS <- list(mkinfit("HS", FOCUS_2006_C, quiet = TRUE)) + + median.C.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F, + dataset == "C", + c(M0, k1, k2, tb, DT50, DT90)), "median")) + + dev.percent.C.HS <- calc_dev.percent(fit.C.HS, median.C.HS, round_results = c(2, 4, 6, 2, 2)) + # Not enouth precision in k2 available + expect_equivalent(dev.percent.C.HS[[1]] < c(0.1, 0.1, 0.3, 0.1, 0.1, 0.1), rep(TRUE, 6)) +}) + +test_that("SFO fits give approximately (0.001%) equal results with different solution methods", { + fit.A.SFO.default <- mkinfit("SFO", FOCUS_2006_A, quiet = TRUE)$bparms.optim + + fits.A.SFO <- list() + fits.A.SFO[[1]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE) + fits.A.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE, solution_type = "eigen") + fits.A.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE, solution_type = "deSolve") + + dev.percent <- calc_dev.percent(fits.A.SFO, fit.A.SFO.default, endpoints = FALSE) + expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 2)) + expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 2)) + expect_equivalent(dev.percent[[3]] < 0.001, rep(TRUE, 2)) +}) + +test_that("FOMC fits give approximately (0.001%) equal results with different solution methods", { + fit.C.FOMC.default <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE)$bparms.optim + + fits.C.FOMC <- list() + fits.C.FOMC[[1]] <- mkinfit(FOMC, FOCUS_2006_C, quiet = TRUE) + fits.C.FOMC[[2]] <- mkinfit(FOMC, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve") + + dev.percent <- calc_dev.percent(fits.C.FOMC, fit.C.FOMC.default, endpoints = FALSE) + expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 3)) + expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 3)) +}) + +test_that("DFOP fits give approximately (0.001%) equal results with different solution methods", { + fit.C.DFOP.default <- mkinfit("DFOP", FOCUS_2006_C, quiet = TRUE)$bparms.optim + + fits.C.DFOP <- list() + fits.C.DFOP[[1]] <- mkinfit(DFOP, FOCUS_2006_C, quiet = TRUE) + fits.C.DFOP[[2]] <- mkinfit(DFOP, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve") + + dev.percent <- calc_dev.percent(fits.C.DFOP, fit.C.DFOP.default, endpoints = FALSE) + expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 4)) + expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 4)) +}) + +test_that("SFORB fits give approximately (0.001%) equal results with different solution methods", { + fit.B.SFORB.default <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE)$bparms.optim + + fits.B.SFORB <- list() + fits.B.SFORB[[1]] <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE, solution_type = "eigen") + fits.B.SFORB[[2]] <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE, solution_type = "deSolve") + dev.percent <- calc_dev.percent(fits.B.SFORB, fit.B.SFORB.default, endpoints = FALSE) + expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 4)) + expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 4)) }) diff --git a/tests/testthat/test_schaefer07_complex_case.R b/tests/testthat/test_schaefer07_complex_case.R new file mode 100644 index 00000000..1ef5106e --- /dev/null +++ b/tests/testthat/test_schaefer07_complex_case.R @@ -0,0 +1,68 @@ +# Copyright (C) 2014-2015 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 + +# This test was migrated from a RUnit test inst/unitTests/runit.mkinfit.R + +context("Complex test case from Schaefer et al. (2007) Piacenza paper") + +schaefer07_complex_model <- mkinmod( + parent = list(type = "SFO", to = c("A1", "B1", "C1"), sink = FALSE), + A1 = list(type = "SFO", to = "A2"), + B1 = list(type = "SFO"), + C1 = list(type = "SFO"), + A2 = list(type = "SFO"), use_of_ff = "max") + +schaefer07_long <- mkin_wide_to_long(schaefer07_complex_case, time = "time") + +fit.default <- mkinfit(schaefer07_complex_model, schaefer07_long, quiet = TRUE) + +test_that("Complex test case from Schaefer (2007) can be reproduced (10% tolerance)", { + + s <- summary(fit.default) + r <- schaefer07_complex_results + + with(as.list(fit.default$bparms.optim), { + r$mkin <<- c( + k_parent, + s$distimes["parent", "DT50"], + s$ff["parent_A1"], + k_A1, + s$distimes["A1", "DT50"], + s$ff["parent_B1"], + k_B1, + s$distimes["B1", "DT50"], + s$ff["parent_C1"], + k_C1, + s$distimes["C1", "DT50"], + s$ff["A1_A2"], + k_A2, + s$distimes["A2", "DT50"]) + } + ) + r$means <- (r$KinGUI + r$ModelMaker)/2 + r$mkin.deviation <- abs(round(100 * ((r$mkin - r$means)/r$means), digits=1)) + expect_equal(r$mkin.deviation < 10, rep(TRUE, 14)) +}) + +test_that("We avoid the local minumum with default settings", { + # If we use optimisation algorithm 'Marq' we get a local minimum with a + # sum of squared residuals of 273.3707 + # When using 'Marq', we need to give a good starting estimate e.g. for k_A2 in + # order to get the optimum with sum of squared residuals 240.5686 + expect_equal(round(fit.default$ssr, 4), 240.5686) +}) -- cgit v1.2.1 From 25a9e7b1014d0b9bc956b45a5da61496a7b37ccf Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 10:39:55 +0200 Subject: Skip testing FOMC with FOCUS A Windows Testing the package on winbuilder shows that the Port algorithm does not converge on Windows with this combination, which is not a problem as the FOMC model is not appropriate for the dataset anyways. --- tests/testthat/test_parent_only.R | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test_parent_only.R b/tests/testthat/test_parent_only.R index 2ed38ffa..b022fe4e 100644 --- a/tests/testthat/test_parent_only.R +++ b/tests/testthat/test_parent_only.R @@ -51,15 +51,21 @@ test_that("Fits for FOCUS A deviate less than 0.1% from median of values from FO dev.percent.A.SFO <- calc_dev.percent(fit.A.SFO, median.A.SFO) expect_equivalent(dev.percent.A.SFO[[1]] < 0.1, rep(TRUE, 4)) - fit.A.FOMC <- list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE)) - - median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, - dataset == "A", - c(M0, alpha, beta, DT50, DT90)), "median")) - - dev.percent.A.FOMC <- calc_dev.percent(fit.A.FOMC, median.A.FOMC) - #expect_equivalent(dev.percent.A.FOMC[[1]] < 0.1, rep(TRUE, 5)) # alpha and beta ill-determined - expect_equivalent(dev.percent.A.FOMC[[1]][c(1, 4, 5)] < 0.1, rep(TRUE, 3)) + # Fitting FOCUS A with FOMC is possible, but the correlation between + # alpha and beta obtained on Linux is 1.0000, and the test failed on Windows, + # as the Port algorithm did not converge (winbuilder, 2015-05-15) + if (.Platform$OS.type != "windows") { + fit.A.FOMC <- list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE)) + + + median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, + dataset == "A", + c(M0, alpha, beta, DT50, DT90)), "median")) + + dev.percent.A.FOMC <- calc_dev.percent(fit.A.FOMC, median.A.FOMC) + # alpha and are beta ill-determined, do not compare those + expect_equivalent(dev.percent.A.FOMC[[1]][c(1, 4, 5)] < 0.1, rep(TRUE, 3)) + } fit.A.DFOP <- list(mkinfit("DFOP", FOCUS_2006_A, quiet = TRUE)) -- cgit v1.2.1 From 9bb55ca2a59c8ce7d3b1069eec7a8aaf57e61df0 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 10:41:59 +0200 Subject: Correct the date --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5812868b..9090f7a5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Routines for Fitting Kinetic Models with One or More State Variables to Chemical Degradation Data Version: 0.9-35 -Date: 2015-05-14 +Date: 2015-05-15 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), -- cgit v1.2.1 From fbf43bbfd6e7ed265fea1cfd0e6b0004dbb6cde2 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 11:02:19 +0200 Subject: Also migrate test for mkinpredict --- tests/testthat/runit.mkinpredict.R | 86 ------------------------------- tests/testthat/test_mkinpredict_SFO_SFO.R | 70 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 86 deletions(-) delete mode 100644 tests/testthat/runit.mkinpredict.R create mode 100644 tests/testthat/test_mkinpredict_SFO_SFO.R diff --git a/tests/testthat/runit.mkinpredict.R b/tests/testthat/runit.mkinpredict.R deleted file mode 100644 index 997857ce..00000000 --- a/tests/testthat/runit.mkinpredict.R +++ /dev/null @@ -1,86 +0,0 @@ -# $Id: jranke $ - -# Copyright (C) 2012 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 - -# Check solution types for SFO {{{ -test.SFO_solution_types <- function() -{ - ot = seq(0, 100, by = 1) - SFO <- mkinmod(parent = list(type = "SFO")) - SFO.analytical <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "analytical"), time == 100), digits=5) - SFO.deSolve <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "deSolve"), time == 100), digits=5) - SFO.eigen <- round(subset(mkinpredict(SFO, c(k_parent_sink = 0.1), - c(parent = 100), ot, solution_type = "eigen"), time == 100), digits=5) - - checkEquals(SFO.analytical, SFO.deSolve) - checkEquals(SFO.analytical, SFO.eigen) -} # }}} - -# Check model specification and solution types for SFO_SFO {{{ -# Relative Tolerance is 0.01% -# Do not use time 0, as eigenvalue based solution does not give 0 at time 0 for metabolites -# and relative tolerance is thus not met -test.SFO_solution_types <- function() -{ - tol = 0.01 - SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "min") - SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), use_of_ff = "max") - - ot = seq(0, 100, by = 1) - r.1.e <- subset(mkinpredict(SFO_SFO.1, - c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "eigen"), - time %in% c(1, 10, 50, 100)) - r.1.d <- subset(mkinpredict(SFO_SFO.1, - c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), - time %in% c(1, 10, 50, 100)) - - r.2.e <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "eigen"), - time %in% c(1, 10, 50, 100)) - r.2.d <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), - c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), - time %in% c(1, 10, 50, 100)) - - # Compare eigen and deSolve for minimum use of formation fractions - dev.1.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] - dev.1.e_d.percent = as.numeric(unlist((dev.1.e_d.percent))) - dev.1.e_d.percent = ifelse(is.na(dev.1.e_d.percent), 0, dev.1.e_d.percent) - checkIdentical(dev.1.e_d.percent < tol, rep(TRUE, length(dev.1.e_d.percent))) - - # Compare eigen and deSolve for maximum use of formation fractions - dev.2.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] - dev.2.e_d.percent = as.numeric(unlist((dev.2.e_d.percent))) - dev.2.e_d.percent = ifelse(is.na(dev.2.e_d.percent), 0, dev.2.e_d.percent) - checkIdentical(dev.2.e_d.percent < tol, rep(TRUE, length(dev.2.e_d.percent))) - - # Compare minimum and maximum use of formation fractions - dev.1_2.e.percent = 100 * (r.1.e[-1] - r.2.e[-1])/r.1.e[-1] - dev.1_2.e.percent = as.numeric(unlist((dev.1_2.e.percent))) - dev.1_2.e.percent = ifelse(is.na(dev.1_2.e.percent), 0, dev.1_2.e.percent) - checkIdentical(dev.1_2.e.percent < tol, rep(TRUE, length(dev.1_2.e.percent))) - -} # }}} - -# vim: set foldmethod=marker ts=2 sw=2 expandtab: diff --git a/tests/testthat/test_mkinpredict_SFO_SFO.R b/tests/testthat/test_mkinpredict_SFO_SFO.R new file mode 100644 index 00000000..1238bb28 --- /dev/null +++ b/tests/testthat/test_mkinpredict_SFO_SFO.R @@ -0,0 +1,70 @@ +# $Id: jranke $ + +# Copyright (C) 2012-2015 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 + +# This was migrated from an RUnit test in inst/unitTests/runit.mkinpredict.R + +context("Model predictions with mkinpredict") +test_that("Variants of model predictions for SFO_SFO model give equivalent results", { + # Check model specification and solution types for SFO_SFO + # Relative Tolerance is 0.01% + # Do not use time 0, as eigenvalue based solution does not give 0 at time 0 for metabolites + # and relative tolerance is thus not met + tol = 0.01 + SFO_SFO.1 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "min") + SFO_SFO.2 <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), use_of_ff = "max") + + ot = seq(0, 100, by = 1) + r.1.e <- subset(mkinpredict(SFO_SFO.1, + c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "eigen"), + time %in% c(1, 10, 50, 100)) + r.1.d <- subset(mkinpredict(SFO_SFO.1, + c(k_parent_m1 = 0.1, k_parent_sink = 0.1, k_m1_sink = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), + time %in% c(1, 10, 50, 100)) + + r.2.e <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "eigen"), + time %in% c(1, 10, 50, 100)) + r.2.d <- subset(mkinpredict(SFO_SFO.2, c(k_parent = 0.2, f_parent_to_m1 = 0.5, k_m1 = 0.1), + c(parent = 100, m1 = 0), ot, solution_type = "deSolve"), + time %in% c(1, 10, 50, 100)) + + # Compare eigen and deSolve for minimum use of formation fractions + dev.1.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] + dev.1.e_d.percent = as.numeric(unlist((dev.1.e_d.percent))) + dev.1.e_d.percent = ifelse(is.na(dev.1.e_d.percent), 0, dev.1.e_d.percent) + expect_equivalent(dev.1.e_d.percent < tol, rep(TRUE, length(dev.1.e_d.percent))) + + # Compare eigen and deSolve for maximum use of formation fractions + dev.2.e_d.percent = 100 * (r.1.e[-1] - r.1.d[-1])/r.1.e[-1] + dev.2.e_d.percent = as.numeric(unlist((dev.2.e_d.percent))) + dev.2.e_d.percent = ifelse(is.na(dev.2.e_d.percent), 0, dev.2.e_d.percent) + expect_equivalent(dev.2.e_d.percent < tol, rep(TRUE, length(dev.2.e_d.percent))) + + # Compare minimum and maximum use of formation fractions + dev.1_2.e.percent = 100 * (r.1.e[-1] - r.2.e[-1])/r.1.e[-1] + dev.1_2.e.percent = as.numeric(unlist((dev.1_2.e.percent))) + dev.1_2.e.percent = ifelse(is.na(dev.1_2.e.percent), 0, dev.1_2.e.percent) + expect_equivalent(dev.1_2.e.percent < tol, rep(TRUE, length(dev.1_2.e.percent))) + +}) -- cgit v1.2.1 From 4fa48c8ef79d75c008fe5cbd0f57b3d96db0b888 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 11:03:43 +0200 Subject: Make FOMC test for FOCUS A robust towards non-convergence --- tests/testthat/test_parent_only.R | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test_parent_only.R b/tests/testthat/test_parent_only.R index b022fe4e..c919cb9c 100644 --- a/tests/testthat/test_parent_only.R +++ b/tests/testthat/test_parent_only.R @@ -52,11 +52,10 @@ test_that("Fits for FOCUS A deviate less than 0.1% from median of values from FO expect_equivalent(dev.percent.A.SFO[[1]] < 0.1, rep(TRUE, 4)) # Fitting FOCUS A with FOMC is possible, but the correlation between - # alpha and beta obtained on Linux is 1.0000, and the test failed on Windows, - # as the Port algorithm did not converge (winbuilder, 2015-05-15) - if (.Platform$OS.type != "windows") { - fit.A.FOMC <- list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE)) - + # alpha and beta, when obtained, is 1.0000, and the fit sometimes failed on + # Windows, as the Port algorithm did not converge (winbuilder, 2015-05-15) + fit.A.FOMC <- try(list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE))) + if (!inherits(fit.A.FOMC, "try-error")) { median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F, dataset == "A", -- cgit v1.2.1 From fcc28ed8bafd5c3b989033e8f1afefaae0c3fe41 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 11:13:19 +0200 Subject: Better description of the release procedure --- GNUmakefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 783ceb14..979caca9 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -102,8 +102,8 @@ winbuilder: build curl -T $(TGZ) ftp://anonymous@win-builder.r-project.org/R-devel/ submit: - @echo "\nHow about make test, make check, make winbuilder and make r-forge?" + @echo "\nHow about make test, make check, make winbuilder" @echo "\nIs the DESCRIPTION file up to date?" @echo "\nIs the NEWS.md file up to date?" @echo "\nAre you sure you want to release to CRAN?" - @echo "\nThen use the form at http://cran.r-project.org/submit.html" + @echo "\nThen make r-forge, commit leftover changes if any, tag the release and use the form at http://cran.r-project.org/submit.html" -- cgit v1.2.1 From ded6e62ad7d1a4aa565a06f33964db0af8c7d78b Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 11:45:12 +0200 Subject: Relax test tolerance for SFORB, FOCUS B and deSolve While this was OK on my local windows machine, the test failed on winbuilder --- tests/testthat/test_parent_only.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test_parent_only.R b/tests/testthat/test_parent_only.R index c919cb9c..5dcf297c 100644 --- a/tests/testthat/test_parent_only.R +++ b/tests/testthat/test_parent_only.R @@ -199,7 +199,7 @@ test_that("DFOP fits give approximately (0.001%) equal results with different so expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 4)) }) -test_that("SFORB fits give approximately (0.001%) equal results with different solution methods", { +test_that("SFORB fits give approximately (0.002%) equal results with different solution methods", { fit.B.SFORB.default <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE)$bparms.optim fits.B.SFORB <- list() @@ -207,5 +207,5 @@ test_that("SFORB fits give approximately (0.001%) equal results with different s fits.B.SFORB[[2]] <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE, solution_type = "deSolve") dev.percent <- calc_dev.percent(fits.B.SFORB, fit.B.SFORB.default, endpoints = FALSE) expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 4)) - expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 4)) + expect_equivalent(dev.percent[[2]] < 0.002, rep(TRUE, 4)) }) -- cgit v1.2.1 From c4960b6d8f0fe84e9a05966d4208a89d780a887c Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 11:52:54 +0200 Subject: Quote FME package name in DESCRIPTION --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9090f7a5..a811e510 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), Description: Calculation routines based on the FOCUS Kinetics Report (2006). Includes a function for conveniently defining differential equation models, model solution based on eigenvalues if possible or using numerical solvers - and a choice of the optimisation methods made available by the FME package. + and a choice of the optimisation methods made available by the 'FME' package. Please note that no warranty is implied for correctness of results or fitness for a particular purpose. Depends: minpack.lm, rootSolve -- cgit v1.2.1 From 9a5d2378845e584a2a2c6c09f7ef99a82d09273b Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 13:04:36 +0200 Subject: Add synthetic data generated for UBA expertise, tests The new tests are based on the results documented in the expertise I wrote for the UBA (currently unpublished). --- NEWS.md | 2 + data/synthetic_data_for_UBA_2014.RData | Bin 0 -> 2243 bytes man/synthetic_data_for_UBA.Rd | 65 +++++++++++++++++++++ tests/testthat/test_FOCUS_D_UBA_expertise.R | 2 +- tests/testthat/test_synthetic_data_for_UBA_2014.R | 67 ++++++++++++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 data/synthetic_data_for_UBA_2014.RData create mode 100644 man/synthetic_data_for_UBA.Rd create mode 100644 tests/testthat/test_synthetic_data_for_UBA_2014.R diff --git a/NEWS.md b/NEWS.md index cc9f28f9..c0874a6e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,8 @@ - `mkinfit()`: Report successful termination when quiet = FALSE. This is helpful for more difficult problems fitted with reweight.method = obs, as no progress is often indicated during the reweighting. - A first test using results established in the expertise written for the German Federal Environmental Agency (UBA) was added. +- Add synthetic datasets generated for expertise written for the German Federal Environmental Agency UBA +- Add tests based on these datasets # CHANGES in mkin VERSION 0.9-34 diff --git a/data/synthetic_data_for_UBA_2014.RData b/data/synthetic_data_for_UBA_2014.RData new file mode 100644 index 00000000..12132c97 Binary files /dev/null and b/data/synthetic_data_for_UBA_2014.RData differ diff --git a/man/synthetic_data_for_UBA.Rd b/man/synthetic_data_for_UBA.Rd new file mode 100644 index 00000000..5d924afd --- /dev/null +++ b/man/synthetic_data_for_UBA.Rd @@ -0,0 +1,65 @@ +\name{synthetic_data_for_UBA_2014} +\alias{synthetic_data_for_UBA_2014} +\docType{data} +\title{ + Synthetic datasets for one parent compound with two metabolites +} +\description{ + The 12 datasets were generated using four different models and three different + variance components. The four models are either the SFO or the DFOP model with either + two sequential or two parallel metabolites. + + Variance component 'a' is based on a normal distribution with standard deviation of 3, + Variance component 'b' is also based on a normal distribution, but with a standard deviation of 7. + Variance component 'c' is based on the error model from Rocke and Lorenzato (1995), with the + minimum standard deviation (for small y values) of 0.5, and a proportionality constant of 0.07 + for the increase of the standard deviation with y. + + Initial concentrations for metabolites and all values where adding the variance component resulted + in a value below the assumed limit of detection of 0.1 were set to \code{NA}. + + As an example, the first dataset has the title \code{SFO_lin_a} and is based on the SFO model + with two sequential metabolites (linear pathway), with added variance component 'a'. + + Compare also the code in the example section to see the degradation models. +} +\usage{synthetic_data_for_UBA_2014} +\format{ + A list containing datasets in the form internally used by the 'gmkin' package. + The list has twelfe components. Each of the components is one dataset that has, + among others, the following components + \describe{ + \item{\code{title}}{The name of the dataset, e.g. \code{SFO_lin_a}} + \item{\code{data}}{A data frame with the data in the form expected by \code{\link{mkinfit}}} + } +} +\source{ + Ranke (2014) Prüfung und Validierung von Modellierungssoftware als Alternative + zu ModelMaker 4.0, Umweltbundesamt Projektnummer 27452 + + Rocke, David M. und Lorenzato, Stefan (1995) A two-component model for + measurement error in analytical chemistry. Technometrics 37(2), 176-184. +} +\examples{ +m_synth_SFO_lin <- mkinmod(parent = list(type = "SFO", to = "M1"), + M1 = list(type = "SFO", to = "M2"), + M2 = list(type = "SFO"), use_of_ff = "max") + + +m_synth_SFO_par <- mkinmod(parent = list(type = "SFO", to = c("M1", "M2"), + sink = FALSE), + M1 = list(type = "SFO"), + M2 = list(type = "SFO"), use_of_ff = "max") + +m_synth_DFOP_lin <- mkinmod(parent = list(type = "DFOP", to = "M1"), + M1 = list(type = "SFO", to = "M2"), + M2 = list(type = "SFO"), use_of_ff = "max") + +m_synth_DFOP_par <- mkinmod(parent = list(type = "DFOP", to = c("M1", "M2"), + sink = FALSE), + M1 = list(type = "SFO"), + M2 = list(type = "SFO"), use_of_ff = "max") + +mkinfit(m_synth_SFO_lin, synthetic_data_for_UBA_2014[[1]]$data) +} +\keyword{datasets} diff --git a/tests/testthat/test_FOCUS_D_UBA_expertise.R b/tests/testthat/test_FOCUS_D_UBA_expertise.R index f9322714..ce1e0a5a 100644 --- a/tests/testthat/test_FOCUS_D_UBA_expertise.R +++ b/tests/testthat/test_FOCUS_D_UBA_expertise.R @@ -34,7 +34,7 @@ test_that("Fitted parameters are correct for FOCUS D", { c(99.60, 0.0987, 0.0053, 0.5145)) }) -test_that("Fitted parameters are correct for FOCUS D", { +test_that("FOCUS chi2 error levels are correct for FOCUS D", { expect_equivalent(round(100 * mkinerrmin(fit.ff)$err.min, 2), c(6.40, 6.46, 4.69)) }) diff --git a/tests/testthat/test_synthetic_data_for_UBA_2014.R b/tests/testthat/test_synthetic_data_for_UBA_2014.R new file mode 100644 index 00000000..2bf01075 --- /dev/null +++ b/tests/testthat/test_synthetic_data_for_UBA_2014.R @@ -0,0 +1,67 @@ +# Copyright (C) 2015 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +context("Results for synthetic data established in expertise for UBA (Ranke 2014)") + + +m_synth_SFO_lin <- mkinmod(parent = list(type = "SFO", to = "M1"), + M1 = list(type = "SFO", to = "M2"), + M2 = list(type = "SFO"), use_of_ff = "max") + + +m_synth_DFOP_par <- mkinmod(parent = list(type = "DFOP", to = c("M1", "M2")), + M1 = list(type = "SFO"), + M2 = list(type = "SFO"), use_of_ff = "max") + +fit_SFO_lin_a <- mkinfit(m_synth_SFO_lin, + synthetic_data_for_UBA_2014[[1]]$data, + quiet = TRUE) +fit_DFOP_par_c <- mkinfit(m_synth_DFOP_par, + synthetic_data_for_UBA_2014[[12]]$data, + quiet = TRUE) + +# Results for SFO_lin_a from p. 48 + +test_that("Fitted parameters are correct for SFO_lin_a", { + parms <- round(fit_SFO_lin_a$bparms.optim, c(1, 4, 4, 4, 4, 4)) + expect_equivalent(parms, c(102.1, 0.7393, 0.2992, 0.0202, 0.7687, 0.7229)) +}) + +test_that("FOCUS chi2 error levels are correct for SFO_lin_a", { + errmin <- round(100 * mkinerrmin(fit_SFO_lin_a)$err.min, 2) + expect_equivalent(errmin, c(8.45, 8.66, 10.58, 3.59)) +}) + +# Results for DFOP_par_c from p. 54 + +test_that("Fitted parameters are correct for DFOP_par_c", { + parms <- round(fit_DFOP_par_c$bparms.optim, c(1, 4, 4, 4, 4, 4, 4, 4)) + expect_equal(parms, c(parent_0 = 103.0, + k_M1 = 0.0389, k_M2 = 0.0095, + f_parent_to_M1 = 0.5565, f_parent_to_M2 = 0.3784, + k1 = 0.3263, k2 = 0.0202, g = 0.7130)) +}) + +test_that("FOCUS chi2 error levels are correct for DFOP_par_c", { + errmin <- round(100 * mkinerrmin(fit_DFOP_par_c)$err.min, 2) + expect_equivalent(errmin, c(4.03, 3.05, 5.07, 3.17)) +}) + +# References: +# Ranke (2014) Prüfung und Validierung von Modellierungssoftware als Alternative +# zu ModelMaker 4.0, Umweltbundesamt Projektnummer 27452 -- cgit v1.2.1 From 89cd81cf47c9f05831e854d5185b5cc599745db4 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 15 May 2015 13:31:28 +0200 Subject: Update of vignettes during staticdoc documentation --- vignettes/FOCUS_L.html | 122 ++++++++++++++++++++++++------------------------- vignettes/FOCUS_Z.pdf | Bin 214499 -> 215014 bytes vignettes/mkin.pdf | Bin 160335 -> 160260 bytes 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/vignettes/FOCUS_L.html b/vignettes/FOCUS_L.html index b5228532..a06f360e 100644 --- a/vignettes/FOCUS_L.html +++ b/vignettes/FOCUS_L.html @@ -236,17 +236,17 @@ given in the FOCUS report.

summary(m.L1.SFO) -
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:49 2014 
-## Date of summary: Thu Dec 18 09:52:49 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:55 2015 
+## Date of summary: Fri May 15 13:29:55 2015 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 37 model solutions performed in 0.214 s
+## Fitted with method Port using 37 model solutions performed in 0.082 s
 ## 
 ## Weighting: none
 ## 
@@ -323,13 +323,13 @@ summary(m.L1.SFO)
 
plot(m.L1.SFO)
 
-

plot of chunk unnamed-chunk-4 +

plot of chunk unnamed-chunk-4 The residual plot can be easily obtained by

mkinresplot(m.L1.SFO, ylab = "Observed", xlab = "Time")
 
-

plot of chunk unnamed-chunk-5

+

plot of chunk unnamed-chunk-5

For comparison, the FOMC model is fitted as well, and the chi2 error level is checked.

@@ -344,10 +344,10 @@ is checked.

summary(m.L1.FOMC, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:50 2014 
-## Date of summary: Thu Dec 18 09:52:50 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:55 2015 
+## Date of summary: Fri May 15 13:29:55 2015 
 ## 
 ## 
 ## Warning: Optimisation by method Port did not converge.
@@ -359,7 +359,7 @@ is checked.

## ## Model predictions using solution type analytical ## -## Fitted with method Port using 188 model solutions performed in 1.085 s +## Fitted with method Port using 188 model solutions performed in 0.412 s ## ## Weighting: none ## @@ -443,17 +443,17 @@ FOCUS_2006_L2_mkin <- mkin_wide_to_long(FOCUS_2006_L2) summary(m.L2.SFO)
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:51 2014 
-## Date of summary: Thu Dec 18 09:52:51 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:56 2015 
+## Date of summary: Fri May 15 13:29:56 2015 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 41 model solutions performed in 0.238 s
+## Fitted with method Port using 41 model solutions performed in 0.091 s
 ## 
 ## Weighting: none
 ## 
@@ -527,7 +527,7 @@ plot(m.L2.SFO)
 mkinresplot(m.L2.SFO)
 
-

plot of chunk unnamed-chunk-9

+

plot of chunk unnamed-chunk-9

In the FOCUS kinetics report, it is stated that there is no apparent systematic error observed from the residual plot up to the measured DT90 (approximately at @@ -548,22 +548,22 @@ plot(m.L2.FOMC) mkinresplot(m.L2.FOMC)

-

plot of chunk unnamed-chunk-10

+

plot of chunk unnamed-chunk-10

summary(m.L2.FOMC, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:51 2014 
-## Date of summary: Thu Dec 18 09:52:51 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:56 2015 
+## Date of summary: Fri May 15 13:29:56 2015 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 81 model solutions performed in 0.464 s
+## Fitted with method Port using 81 model solutions performed in 0.192 s
 ## 
 ## Weighting: none
 ## 
@@ -622,7 +622,7 @@ experimental error has to be assumed in order to explain the data.

plot(m.L2.DFOP)
-

plot of chunk unnamed-chunk-11

+

plot of chunk unnamed-chunk-11

Here, the default starting parameters for the DFOP model obviously do not lead to a reasonable solution. Therefore the fit is repeated with different starting @@ -634,15 +634,15 @@ parameters.

plot(m.L2.DFOP)
-

plot of chunk unnamed-chunk-12

+

plot of chunk unnamed-chunk-12

summary(m.L2.DFOP, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:56 2014 
-## Date of summary: Thu Dec 18 09:52:56 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:58 2015 
+## Date of summary: Fri May 15 13:29:58 2015 
 ## 
 ## Equations:
 ## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
@@ -651,7 +651,7 @@ plot(m.L2.DFOP)
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 336 model solutions performed in 1.986 s
+## Fitted with method Port using 336 model solutions performed in 0.775 s
 ## 
 ## Weighting: none
 ## 
@@ -723,22 +723,22 @@ FOCUS_2006_L3_mkin <- mkin_wide_to_long(FOCUS_2006_L3)
 plot(m.L3.SFO)
 
-

plot of chunk unnamed-chunk-14

+

plot of chunk unnamed-chunk-14

summary(m.L3.SFO)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:56 2014 
-## Date of summary: Thu Dec 18 09:52:56 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:58 2015 
+## Date of summary: Fri May 15 13:29:58 2015 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 43 model solutions performed in 0.246 s
+## Fitted with method Port using 43 model solutions performed in 0.099 s
 ## 
 ## Weighting: none
 ## 
@@ -809,22 +809,22 @@ does not fit very well. 

plot(m.L3.FOMC)
-

plot of chunk unnamed-chunk-15

+

plot of chunk unnamed-chunk-15

summary(m.L3.FOMC, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:57 2014 
-## Date of summary: Thu Dec 18 09:52:57 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:58 2015 
+## Date of summary: Fri May 15 13:29:58 2015 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 83 model solutions performed in 0.475 s
+## Fitted with method Port using 83 model solutions performed in 0.186 s
 ## 
 ## Weighting: none
 ## 
@@ -882,15 +882,15 @@ considerably:

plot(m.L3.DFOP)
-

plot of chunk unnamed-chunk-16

+

plot of chunk unnamed-chunk-16

summary(m.L3.DFOP, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:58 2014 
-## Date of summary: Thu Dec 18 09:52:58 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:59 2015 
+## Date of summary: Fri May 15 13:29:59 2015 
 ## 
 ## Equations:
 ## d_parent = - ((k1 * g * exp(-k1 * time) + k2 * (1 - g) * exp(-k2 *
@@ -899,7 +899,7 @@ plot(m.L3.DFOP)
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 137 model solutions performed in 0.813 s
+## Fitted with method Port using 137 model solutions performed in 0.314 s
 ## 
 ## Weighting: none
 ## 
@@ -980,22 +980,22 @@ FOCUS_2006_L4_mkin <- mkin_wide_to_long(FOCUS_2006_L4)
 plot(m.L4.SFO)
 
-

plot of chunk unnamed-chunk-18

+

plot of chunk unnamed-chunk-18

summary(m.L4.SFO, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:58 2014 
-## Date of summary: Thu Dec 18 09:52:58 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:59 2015 
+## Date of summary: Fri May 15 13:29:59 2015 
 ## 
 ## Equations:
 ## d_parent = - k_parent_sink * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 46 model solutions performed in 0.264 s
+## Fitted with method Port using 46 model solutions performed in 0.109 s
 ## 
 ## Weighting: none
 ## 
@@ -1055,22 +1055,22 @@ fits very well. 

plot(m.L4.FOMC)
-

plot of chunk unnamed-chunk-19

+

plot of chunk unnamed-chunk-19

summary(m.L4.FOMC, data = FALSE)
 
-
## mkin version:    0.9.34 
-## R version:       3.1.2 
-## Date of fit:     Thu Dec 18 09:52:59 2014 
-## Date of summary: Thu Dec 18 09:52:59 2014 
+
## mkin version:    0.9.35 
+## R version:       3.2.0 
+## Date of fit:     Fri May 15 13:29:59 2015 
+## Date of summary: Fri May 15 13:29:59 2015 
 ## 
 ## Equations:
 ## d_parent = - (alpha/beta) * ((time/beta) + 1)^-1 * parent
 ## 
 ## Model predictions using solution type analytical 
 ## 
-## Fitted with method Port using 66 model solutions performed in 0.375 s
+## Fitted with method Port using 66 model solutions performed in 0.146 s
 ## 
 ## Weighting: none
 ## 
diff --git a/vignettes/FOCUS_Z.pdf b/vignettes/FOCUS_Z.pdf
index f8fd354f..3174a23a 100644
Binary files a/vignettes/FOCUS_Z.pdf and b/vignettes/FOCUS_Z.pdf differ
diff --git a/vignettes/mkin.pdf b/vignettes/mkin.pdf
index f83f8735..8f1e0884 100644
Binary files a/vignettes/mkin.pdf and b/vignettes/mkin.pdf differ
-- 
cgit v1.2.1