From be809ea23b256f627216958fcd90489ee0844ab8 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 25 Feb 2019 14:50:13 +0100 Subject: Add plotting tests, add nafta to pkgdown --- tests/figs/deps.txt | 3 + tests/figs/plotting/mmkin-plot-for-focus-c.svg | 569 +++++++++++++++++++++ .../plotting/mmkin-plot-for-sfo-focus-c-and-d.svg | 300 +++++++++++ tests/testthat/test_plots_twa.R | 61 +++ tests/testthat/test_twa.R | 49 -- 5 files changed, 933 insertions(+), 49 deletions(-) create mode 100644 tests/figs/deps.txt create mode 100644 tests/figs/plotting/mmkin-plot-for-focus-c.svg create mode 100644 tests/figs/plotting/mmkin-plot-for-sfo-focus-c-and-d.svg create mode 100644 tests/testthat/test_plots_twa.R delete mode 100644 tests/testthat/test_twa.R (limited to 'tests') diff --git a/tests/figs/deps.txt b/tests/figs/deps.txt new file mode 100644 index 00000000..0f6c3754 --- /dev/null +++ b/tests/figs/deps.txt @@ -0,0 +1,3 @@ +- vdiffr-svg-engine: 1.0 +- vdiffr: 0.3.0.9000 +- freetypeharfbuzz: 0.2.5 diff --git a/tests/figs/plotting/mmkin-plot-for-focus-c.svg b/tests/figs/plotting/mmkin-plot-for-focus-c.svg new file mode 100644 index 00000000..1c697328 --- /dev/null +++ b/tests/figs/plotting/mmkin-plot-for-focus-c.svg @@ -0,0 +1,569 @@ + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + +parent + + + + + +FOCUS_C +SFO + +χ +2 + +error level += +15.8% + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +-10 +-5 +0 +5 +10 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + +SFO residuals + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + + + +FOCUS_C +FOMC + +χ +2 + +error level += +6.66% + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + + + +-3 +-1 +1 +2 +3 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + +FOMC residuals + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + + + +FOCUS_C +DFOP + +χ +2 + +error level += +2.66% + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + + + +-1.5 +-0.5 +0.5 +1.5 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + +DFOP residuals + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + + + +FOCUS_C +HS + +χ +2 + +error level += +4.7% + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +-2 +-1 +0 +1 +2 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + +HS residuals + + + + + + diff --git a/tests/figs/plotting/mmkin-plot-for-sfo-focus-c-and-d.svg b/tests/figs/plotting/mmkin-plot-for-sfo-focus-c-and-d.svg new file mode 100644 index 00000000..72496e86 --- /dev/null +++ b/tests/figs/plotting/mmkin-plot-for-sfo-focus-c-and-d.svg @@ -0,0 +1,300 @@ + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + +parent + + + + + +SFO +FOCUS_C + +χ +2 + +error level += +15.8% + + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +-10 +-5 +0 +5 +10 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + +FOCUS_C residuals + + + + + + + + + + + + + + + +0 +20 +40 +60 + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +SFO +FOCUS_D + +χ +2 + +error level += +6.45% + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 + + + + + + +-10 +-5 +0 +5 +10 + + + + + + +Time +Residual + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +FOCUS_D residuals + + + + + + diff --git a/tests/testthat/test_plots_twa.R b/tests/testthat/test_plots_twa.R new file mode 100644 index 00000000..fe0ba325 --- /dev/null +++ b/tests/testthat/test_plots_twa.R @@ -0,0 +1,61 @@ +# Copyright (C) 2016-2019 Johannes Ranke +# Contact: jranke@uni-bremen.de + +# This file is part of the R package mkin + +# mkin is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. + +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. + +# You should have received a copy of the GNU General Public License along with +# this program. If not, see + +models <- c("SFO", "FOMC", "DFOP", "HS") +fits <- mmkin(models, + list(FOCUS_C = FOCUS_2006_C, FOCUS_D = FOCUS_2006_D), + quiet = TRUE, cores = if (Sys.getenv("TRAVIS") == "") 15 else 1) + +context("Calculation of maximum time weighted average concentrations (TWAs)") + +test_that("Time weighted average concentrations are correct", { + skip_on_cran() + + outtimes_10 <- seq(0, 10, length.out = 10000) + + for (ds in c("FOCUS_C", "FOCUS_D")) { + for (model in models) { + fit <- fits[[model, ds]] + bpar <- summary(fit)$bpar[, "Estimate"] + pred_10 <- mkinpredict(fit$mkinmod, + odeparms = bpar[2:length(bpar)], + odeini = c(parent = bpar[[1]]), + outtimes = outtimes_10) + twa_num <- mean(pred_10$parent) + names(twa_num) <- 10 + twa_ana <- max_twa_parent(fit, 10) + + # Test for absolute difference (scale = 1) + # The tolerance can be reduced if the length of outtimes is increased, + # but this needs more computing time so we stay with lenght.out = 10k + expect_equal(twa_num, twa_ana, tolerance = 0.003, scale = 1) + } + } +}) + +context("Plotting") + +test_that("Plotting mmkin objects is reproducible", { + skip_on_cran() + mmkin_FOCUS_C <- function() plot(fits[, "FOCUS_C"]) + mmkin_SFO <- function() plot(fits["SFO",]) + + vdiffr::expect_doppelganger("mmkin plot for FOCUS C", mmkin_FOCUS_C) + vdiffr::expect_doppelganger("mmkin plot for SFO (FOCUS C and D)", mmkin_SFO) +}) + diff --git a/tests/testthat/test_twa.R b/tests/testthat/test_twa.R deleted file mode 100644 index 42b74a7f..00000000 --- a/tests/testthat/test_twa.R +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright (C) 2016,2017,2018 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("Calculation of maximum time weighted average concentrations (TWAs)") - - -test_that("Time weighted average concentrations are correct", { - skip_on_cran() - twa_models <- c("SFO", "FOMC", "DFOP", "HS") - fits <- mmkin(twa_models, - list(FOCUS_C = FOCUS_2006_C, FOCUS_D = FOCUS_2006_D), - quiet = TRUE, cores = if (Sys.getenv("TRAVIS") == "") 15 else 1) - - outtimes_10 <- seq(0, 10, length.out = 10000) - - for (ds in c("FOCUS_C", "FOCUS_D")) { - for (model in twa_models) { - fit <- fits[[model, ds]] - bpar <- summary(fit)$bpar[, "Estimate"] - pred_10 <- mkinpredict(fit$mkinmod, - odeparms = bpar[2:length(bpar)], - odeini = c(parent = bpar[[1]]), - outtimes = outtimes_10) - twa_num <- mean(pred_10$parent) - names(twa_num) <- 10 - twa_ana <- max_twa_parent(fit, 10) - - # Test for absolute difference (scale = 1) - # The tolerance can be reduced if the length of outtimes is increased, - # but this needs more computing time so we stay with lenght.out = 10k - expect_equal(twa_num, twa_ana, tolerance = 0.003, scale = 1) - } - } -}) -- cgit v1.2.1