From e4e54169ab3961c354f580bc489d150299038367 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 9 Apr 2020 10:35:59 +0200 Subject: Improve test coverage and fix nlme test No idea why I had to do more assignments all of a sudden in test_nlme.R --- test.log | 32 ++-- .../figs/plotting/plot-errmod-with-focus-c-tc.svg | 128 +++++++++++++ .../plot-errmod-with-focus-d-obs-eigen.svg | 201 +++++++++++++++++++++ tests/testthat/FOCUS_2006_D.csf | 2 +- tests/testthat/test_nlme.R | 9 +- tests/testthat/test_plots_summary_twa.R | 10 + tests/testthat/test_residuals.R | 9 + 7 files changed, 370 insertions(+), 21 deletions(-) create mode 100644 tests/figs/plotting/plot-errmod-with-focus-c-tc.svg create mode 100644 tests/figs/plotting/plot-errmod-with-focus-d-obs-eigen.svg diff --git a/test.log b/test.log index af73d96a..b9d0cc74 100644 --- a/test.log +++ b/test.log @@ -2,34 +2,34 @@ Loading mkin Testing mkin ✔ | OK F W S | Context ✔ | 2 | Export dataset for reading into CAKE -✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.5 s] -✔ | 4 | Calculation of FOCUS chi2 error levels [2.2 s] -✔ | 4 | Fitting the SFORB model [1.7 s] +✔ | 13 | Results for FOCUS D established in expertise for UBA (Ranke 2014) [3.7 s] +✔ | 4 | Calculation of FOCUS chi2 error levels [2.3 s] +✔ | 4 | Fitting the SFORB model [1.8 s] ✔ | 5 | Calculation of Akaike weights -✔ | 10 | Confidence intervals and p-values [9.5 s] -✔ | 14 | Error model fitting [37.6 s] +✔ | 10 | Confidence intervals and p-values [10.1 s] +✔ | 14 | Error model fitting [41.3 s] ✔ | 6 | Test fitting the decline of metabolites from their maximum [0.8 s] ✔ | 1 | Fitting the logistic model [0.9 s] ✔ | 1 | Test dataset class mkinds used in gmkin -✔ | 12 | Special cases of mkinfit calls [2.3 s] +✔ | 12 | Special cases of mkinfit calls [2.5 s] ✔ | 9 | mkinmod model generation and printing [0.2 s] ✔ | 3 | Model predictions with mkinpredict [0.4 s] -✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.1 s] -✔ | 3 | Nonlinear mixed-effects models [2.1 s] -✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.2 s] +✔ | 16 | Evaluations according to 2015 NAFTA guidance [4.3 s] +✔ | 5 | Nonlinear mixed-effects models [11.6 s] +✔ | 4 | Calculation of maximum time weighted average concentrations (TWAs) [2.4 s] ✔ | 3 | Summary -✔ | 11 | Plotting [0.6 s] +✔ | 13 | Plotting [5.1 s] ✔ | 4 | AIC calculation -✔ | 2 | Residuals extracted from mkinfit models -✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.2 s] +✔ | 4 | Residuals extracted from mkinfit models +✔ | 2 | Complex test case from Schaefer et al. (2007) Piacenza paper [5.6 s] ✔ | 1 | Summaries of old mkinfit objects -✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.0 s] -✔ | 9 | Hypothesis tests [36.7 s] +✔ | 4 | Results for synthetic data established in expertise for UBA (Ranke 2014) [7.7 s] +✔ | 9 | Hypothesis tests [39.7 s] ══ Results ═════════════════════════════════════════════════════════════════════ -Duration: 117.1 s +Duration: 140.8 s -OK: 143 +OK: 149 Failed: 0 Warnings: 0 Skipped: 0 diff --git a/tests/figs/plotting/plot-errmod-with-focus-c-tc.svg b/tests/figs/plotting/plot-errmod-with-focus-c-tc.svg new file mode 100644 index 00000000..8adefc27 --- /dev/null +++ b/tests/figs/plotting/plot-errmod-with-focus-c-tc.svg @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + +parent + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 + + + + + + +0 +20 +40 +60 +80 + + + + + + +Predicted +Squared residual + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/figs/plotting/plot-errmod-with-focus-d-obs-eigen.svg b/tests/figs/plotting/plot-errmod-with-focus-d-obs-eigen.svg new file mode 100644 index 00000000..93e90dc5 --- /dev/null +++ b/tests/figs/plotting/plot-errmod-with-focus-d-obs-eigen.svg @@ -0,0 +1,201 @@ + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + +Time +Observed + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +parent +m1 + + + + + + + + + + + + + + + + + +0 +20 +40 +60 +80 +100 + + + + + + + + + +0 +20 +40 +60 +80 +100 +120 +140 + + + + + + +Predicted +Squared residual + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/FOCUS_2006_D.csf b/tests/testthat/FOCUS_2006_D.csf index 118f61f4..de5ee0a4 100644 --- a/tests/testthat/FOCUS_2006_D.csf +++ b/tests/testthat/FOCUS_2006_D.csf @@ -5,7 +5,7 @@ Description: MeasurementUnits: % AR TimeUnits: days Comments: Created using mkin::CAKE_export -Date: 2020-04-08 +Date: 2020-04-09 Optimiser: IRLS [Data] diff --git a/tests/testthat/test_nlme.R b/tests/testthat/test_nlme.R index 0d1fcd6d..efbc231a 100644 --- a/tests/testthat/test_nlme.R +++ b/tests/testthat/test_nlme.R @@ -31,6 +31,7 @@ test_that("nlme_function works correctly", { # The following assignment was introduced for nlme as evaluated by testthat # to find the function assign("nlme_f", nlme_f, pos = globalenv()) + assign("sampling_times", sampling_times, pos = globalenv()) m_nlme_raw <- nlme(value ~ SSasymp(time, 0, parent_0, log_k_parent_sink), data = grouped_data, @@ -68,7 +69,6 @@ test_that("nlme_function works correctly", { test_that("nlme_function works correctly in other cases", { dt50_in <- c(400, 800, 1200, 1600, 2000) - dt50_in_geomean <- geomean(dt50_in) k_in <- log(2) / dt50_in SFO <- mkinmod(parent = mkinsub("SFO")) pred_sfo <- function(k) { @@ -85,15 +85,16 @@ test_that("nlme_function works correctly in other cases", { names(ds_me_sfo_5) <- paste("Dataset", 1:15) dimnames(ds_me_sfo_5) <- list(Subset = 1:3, DT50 = dt50_in) - f_me_sfo_5 <- mmkin("SFO", ds_me_sfo_5) + f_me_sfo_5 <- mmkin("SFO", ds_me_sfo_5, quiet = TRUE) ds_me_sfo_5_grouped_mkin <- nlme_data(f_me_sfo_5) ds_me_sfo_5_mean_dp <- mean_degparms(f_me_sfo_5) me_sfo_function <- nlme_function(f_me_sfo_5) + assign("me_sfo_function", me_sfo_function, pos = globalenv()) f_nlme_sfo_5_all_mkin <- nlme(value ~ me_sfo_function(name, time, parent_0, log_k_parent_sink), - data = ds_me_sfo_5_grouped, + data = ds_me_sfo_5_grouped_mkin, fixed = parent_0 + log_k_parent_sink ~ 1, random = pdDiag(parent_0 + log_k_parent_sink ~ 1), start = ds_me_sfo_5_mean_dp) @@ -109,7 +110,7 @@ test_that("nlme_function works correctly in other cases", { # With less ideal starting values we get fits with lower AIC (not shown) f_nlme_sfo_5_all_mkin_nostart <- nlme(value ~ me_sfo_function(name, time, parent_0, log_k_parent_sink), - data = ds_me_sfo_5_grouped, + data = ds_me_sfo_5_grouped_mkin, fixed = parent_0 + log_k_parent_sink ~ 1, random = pdDiag(parent_0 + log_k_parent_sink ~ 1), start = c(parent_0 = 100, log_k_parent_sink = log(0.1))) diff --git a/tests/testthat/test_plots_summary_twa.R b/tests/testthat/test_plots_summary_twa.R index 77144665..c04192c1 100644 --- a/tests/testthat/test_plots_summary_twa.R +++ b/tests/testthat/test_plots_summary_twa.R @@ -99,6 +99,12 @@ test_that("Plotting mkinfit and mmkin objects is reproducible", { mkinerrplot_FOCUS_C_SFO <- function() mkinerrplot(fits[["SFO", "FOCUS_C"]]) mmkin_FOCUS_C <- function() plot(fits[, "FOCUS_C"]) mmkin_SFO <- function() plot(fits["SFO",]) + fit_D_obs_eigen <- suppressWarnings(mkinfit(SFO_SFO, FOCUS_2006_D, error_model = "obs", quiet = TRUE)) + fit_C_tc <- mkinfit("SFO", FOCUS_2006_C, error_model = "tc", quiet = TRUE) + + plot_errmod_fit_D_obs_eigen <- function() plot_err(fit_D_obs_eigen, sep_obs = FALSE) + plot_errmod_fit_C_tc <- function() plot_err(fit_C_tc) + plot_res_sfo_sfo <- function() plot_res(f_sfo_sfo_desolve) plot_err_sfo_sfo <- function() plot_err(f_sfo_sfo_desolve) plot_errmod_fit_obs_1 <- function() plot_err(fit_obs_1, sep_obs = FALSE) @@ -111,6 +117,10 @@ test_that("Plotting mkinfit and mmkin objects is reproducible", { vdiffr::expect_doppelganger("mkinerrplot for FOCUS C SFO", mkinerrplot_FOCUS_C_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) + vdiffr::expect_doppelganger("plot_errmod with FOCUS D obs eigen", plot_errmod_fit_D_obs_eigen) + vdiffr::expect_doppelganger("plot_errmod with FOCUS C tc", plot_errmod_fit_C_tc) + skip_on_travis() # Still not working on Travis, presumably because of deSolve producing + # different results when not working with a compiled model vdiffr::expect_doppelganger("plot_res for FOCUS D", plot_res_sfo_sfo) vdiffr::expect_doppelganger("plot_err for FOCUS D", plot_err_sfo_sfo) vdiffr::expect_doppelganger("plot_errmod with SFO_lin_a_tc", plot_errmod_fit_tc_1) diff --git a/tests/testthat/test_residuals.R b/tests/testthat/test_residuals.R index 35a143f6..e6e54dd5 100644 --- a/tests/testthat/test_residuals.R +++ b/tests/testthat/test_residuals.R @@ -7,4 +7,13 @@ test_that("Residuals are correctly returned", { expect_equivalent( residuals(f, standardized = TRUE)[1:3], c(-0.4171812, 1.4584875, -1.0472450), tolerance = 0.0001) + + expect_equivalent( + residuals(fit_obs_1, standardized = TRUE)[1:3], + c(-0.21771169, -0.33576060, 2.02836138)) + + expect_equivalent( + residuals(fit_tc_1, standardized = TRUE)[1:3], + c(-0.2474499, -0.3794267, 2.2749216), + tolerance = 1e-7) }) -- cgit v1.2.1