From e6f9e9ca89e35e610d9895b979f1351a47451db0 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 29 May 2020 16:05:11 +0200 Subject: Improve handling of warnings, reorganize tests --- tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt | 4 + tests/testthat/summary_DFOP_FOCUS_D_eigen.txt | 4 + tests/testthat/test_AIC.R | 12 +++ tests/testthat/test_FOCUS_D_UBA_expertise.R | 2 +- tests/testthat/test_plot.R | 42 ++++++++ tests/testthat/test_plots_summary_twa.R | 128 ------------------------ tests/testthat/test_summary.R | 57 +++++++++++ tests/testthat/test_twa.R | 26 +++++ 8 files changed, 146 insertions(+), 129 deletions(-) create mode 100644 tests/testthat/test_AIC.R create mode 100644 tests/testthat/test_plot.R delete mode 100644 tests/testthat/test_plots_summary_twa.R create mode 100644 tests/testthat/test_summary.R create mode 100644 tests/testthat/test_twa.R (limited to 'tests/testthat') diff --git a/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt b/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt index 9245c40b..0b31a81f 100644 --- a/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt +++ b/tests/testthat/summary_DFOP_FOCUS_D_deSolve.txt @@ -33,6 +33,10 @@ Fixed parameter values: value type m1_0 0 state + +Warning(s): +Shapiro-Wilk test for standardized residuals: p = 0.0165 + Results: AIC BIC logLik diff --git a/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt b/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt index 141f57a1..8ced5c4d 100644 --- a/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt +++ b/tests/testthat/summary_DFOP_FOCUS_D_eigen.txt @@ -33,6 +33,10 @@ Fixed parameter values: value type m1_0 0 state + +Warning(s): +Shapiro-Wilk test for standardized residuals: p = 0.0165 + Results: AIC BIC logLik diff --git a/tests/testthat/test_AIC.R b/tests/testthat/test_AIC.R new file mode 100644 index 00000000..e9698f7c --- /dev/null +++ b/tests/testthat/test_AIC.R @@ -0,0 +1,12 @@ +context("AIC calculation") + +test_that("The AIC is reproducible", { + expect_equivalent(AIC(fits[["SFO", "FOCUS_C"]]), 59.3, scale = 1, tolerance = 0.1) + expect_equivalent(AIC(fits[, "FOCUS_C"]), + data.frame(df = c(3, 4, 5, 5), AIC = c(59.3, 44.7, 29.0, 39.2)), + scale = 1, tolerance = 0.1) + expect_error(AIC(fits["SFO", ]), "column object") + expect_equivalent(BIC(fits[, "FOCUS_C"]), + data.frame(df = c(3, 4, 5, 5), AIC = c(59.9, 45.5, 30.0, 40.2)), + scale = 1, tolerance = 0.1) +}) diff --git a/tests/testthat/test_FOCUS_D_UBA_expertise.R b/tests/testthat/test_FOCUS_D_UBA_expertise.R index 101c8e15..0a7e5219 100644 --- a/tests/testthat/test_FOCUS_D_UBA_expertise.R +++ b/tests/testthat/test_FOCUS_D_UBA_expertise.R @@ -5,7 +5,7 @@ context("Results for FOCUS D established in expertise for UBA (Ranke 2014)") test_that("Fits without formation fractions are correct for FOCUS D", { expect_warning( fit.noff <- mkinfit(SFO_SFO, FOCUS_D, quiet = TRUE), - "p-value.*Shapiro-Wilk") + "Shapiro-Wilk") expect_equal(round(as.numeric(endpoints(fit.noff)$distimes["parent", ]), 2), c(7.02, 23.33)) diff --git a/tests/testthat/test_plot.R b/tests/testthat/test_plot.R new file mode 100644 index 00000000..a33de07f --- /dev/null +++ b/tests/testthat/test_plot.R @@ -0,0 +1,42 @@ +context("Plotting") + +test_that("Plotting mkinfit and mmkin objects is reproducible", { + skip_on_cran() + plot_default_FOCUS_C_SFO <- function() plot(fits[["SFO", "FOCUS_C"]]) + plot_res_FOCUS_C_SFO <- function() plot(fits[["SFO", "FOCUS_C"]], show_residuals = TRUE) + plot_res_FOCUS_C_SFO_2 <- function() plot_res(fits[["SFO", "FOCUS_C"]]) + plot_sep_FOCUS_C_SFO <- function() plot_sep(fits[["SFO", "FOCUS_C"]]) + mkinparplot_FOCUS_C_SFO <- function() mkinparplot(fits[["SFO", "FOCUS_C"]]) + 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) + plot_errmod_fit_tc_1 <- function() plot_err(fit_tc_1, sep_obs = FALSE) + + skip_if(getRversion() > 4.0) + vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with defaults", plot_default_FOCUS_C_SFO) + vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with residuals like in gmkin", plot_res_FOCUS_C_SFO) + vdiffr::expect_doppelganger("plot_res for FOCUS C", plot_res_FOCUS_C_SFO_2) + vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with sep = TRUE", plot_sep_FOCUS_C_SFO) + vdiffr::expect_doppelganger("mkinparplot for FOCUS C SFO", mkinparplot_FOCUS_C_SFO) + 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 C tc", plot_errmod_fit_C_tc) + skip_on_travis() # Still not working on Travis, maybe because of deSolve producing + # different results when not working with a compiled model or eigenvalues + vdiffr::expect_doppelganger("plot_errmod with FOCUS D obs eigen", plot_errmod_fit_D_obs_eigen) + 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) + vdiffr::expect_doppelganger("plot_errmod with SFO_lin_a_obs", plot_errmod_fit_obs_1) +}) + diff --git a/tests/testthat/test_plots_summary_twa.R b/tests/testthat/test_plots_summary_twa.R deleted file mode 100644 index aedc9da3..00000000 --- a/tests/testthat/test_plots_summary_twa.R +++ /dev/null @@ -1,128 +0,0 @@ -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) - - ds <- "FOCUS_C" - 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("Summary") - -test_that("Summaries are reproducible", { - fit <- fits[["DFOP", "FOCUS_C"]] - test_summary <- summary(fit) - test_summary$fit_version <- "Dummy 0.0 for testing" - test_summary$fit_Rversion <- "Dummy R version for testing" - test_summary$date.fit <- "Dummy date for testing" - test_summary$date.summary <- "Dummy date for testing" - test_summary$calls <- "test 0" - test_summary$Corr <- signif(test_summary$Corr, 1) - test_summary$time <- c(elapsed = "test time 0") - # The correlation matrix is quite platform dependent - # It differs between i386 and amd64 on Windows - # and between Travis and my own Linux system - test_summary$Corr <- "Correlation matrix is platform dependent, not tested" - expect_known_output(print(test_summary), "summary_DFOP_FOCUS_C.txt") - - test_summary_2 <- summary(f_sfo_sfo_eigen) - test_summary_2$fit_version <- "Dummy 0.0 for testing" - test_summary_2$fit_Rversion <- "Dummy R version for testing" - test_summary_2$date.fit <- "Dummy date for testing" - test_summary_2$date.summary <- "Dummy date for testing" - test_summary_2$calls <- "test 0" - test_summary_2$time <- c(elapsed = "test time 0") - # The correlation matrix is quite platform dependent - # It differs between i386 and amd64 on Windows - # and between Travis and my own Linux system - # Even more so when using the Eigen method - test_summary_2$Corr <- "Correlation matrix is platform dependent, not tested" - # The residuals for this method are also platform sensitive - test_summary_2$data$residual <- "not tested" - expect_known_output(print(test_summary_2), "summary_DFOP_FOCUS_D_eigen.txt") - - test_summary_3 <- summary(f_sfo_sfo_desolve) - test_summary_3$fit_version <- "Dummy 0.0 for testing" - test_summary_3$fit_Rversion <- "Dummy R version for testing" - test_summary_3$date.fit <- "Dummy date for testing" - test_summary_3$date.summary <- "Dummy date for testing" - test_summary_3$calls <- "test 0" - test_summary_3$time <- c(elapsed = "test time 0") - # The correlation matrix is quite platform dependent - # It differs between i386 and amd64 on Windows - # and between Travis and my own Linux system - test_summary_3$Corr <- "Correlation matrix is platform dependent, not tested" - expect_known_output(print(test_summary_3), "summary_DFOP_FOCUS_D_deSolve.txt") -}) - -context("Plotting") - -test_that("Plotting mkinfit and mmkin objects is reproducible", { - skip_on_cran() - plot_default_FOCUS_C_SFO <- function() plot(fits[["SFO", "FOCUS_C"]]) - plot_res_FOCUS_C_SFO <- function() plot(fits[["SFO", "FOCUS_C"]], show_residuals = TRUE) - plot_res_FOCUS_C_SFO_2 <- function() plot_res(fits[["SFO", "FOCUS_C"]]) - plot_sep_FOCUS_C_SFO <- function() plot_sep(fits[["SFO", "FOCUS_C"]]) - mkinparplot_FOCUS_C_SFO <- function() mkinparplot(fits[["SFO", "FOCUS_C"]]) - 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) - plot_errmod_fit_tc_1 <- function() plot_err(fit_tc_1, sep_obs = FALSE) - - skip_if(getRversion() > 4.0) - vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with defaults", plot_default_FOCUS_C_SFO) - vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with residuals like in gmkin", plot_res_FOCUS_C_SFO) - vdiffr::expect_doppelganger("plot_res for FOCUS C", plot_res_FOCUS_C_SFO_2) - vdiffr::expect_doppelganger("mkinfit plot for FOCUS C with sep = TRUE", plot_sep_FOCUS_C_SFO) - vdiffr::expect_doppelganger("mkinparplot for FOCUS C SFO", mkinparplot_FOCUS_C_SFO) - 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 C tc", plot_errmod_fit_C_tc) - skip_on_travis() # Still not working on Travis, maybe because of deSolve producing - # different results when not working with a compiled model or eigenvalues - vdiffr::expect_doppelganger("plot_errmod with FOCUS D obs eigen", plot_errmod_fit_D_obs_eigen) - 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) - vdiffr::expect_doppelganger("plot_errmod with SFO_lin_a_obs", plot_errmod_fit_obs_1) -}) - -context("AIC calculation") - -test_that("The AIC is reproducible", { - expect_equivalent(AIC(fits[["SFO", "FOCUS_C"]]), 59.3, scale = 1, tolerance = 0.1) - expect_equivalent(AIC(fits[, "FOCUS_C"]), - data.frame(df = c(3, 4, 5, 5), AIC = c(59.3, 44.7, 29.0, 39.2)), - scale = 1, tolerance = 0.1) - expect_error(AIC(fits["SFO", ]), "column object") - expect_equivalent(BIC(fits[, "FOCUS_C"]), - data.frame(df = c(3, 4, 5, 5), AIC = c(59.9, 45.5, 30.0, 40.2)), - scale = 1, tolerance = 0.1) -}) diff --git a/tests/testthat/test_summary.R b/tests/testthat/test_summary.R new file mode 100644 index 00000000..5cf6ac6b --- /dev/null +++ b/tests/testthat/test_summary.R @@ -0,0 +1,57 @@ +context("Summary") + +test_that("Summaries are reproducible", { + fit <- fits[["DFOP", "FOCUS_C"]] + test_summary <- summary(fit) + test_summary$fit_version <- "Dummy 0.0 for testing" + test_summary$fit_Rversion <- "Dummy R version for testing" + test_summary$date.fit <- "Dummy date for testing" + test_summary$date.summary <- "Dummy date for testing" + test_summary$calls <- "test 0" + test_summary$Corr <- signif(test_summary$Corr, 1) + test_summary$time <- c(elapsed = "test time 0") + # The correlation matrix is quite platform dependent + # It differs between i386 and amd64 on Windows + # and between Travis and my own Linux system + test_summary$Corr <- "Correlation matrix is platform dependent, not tested" + expect_known_output(print(test_summary), "summary_DFOP_FOCUS_C.txt") + + test_summary_2 <- summary(f_sfo_sfo_eigen) + test_summary_2$fit_version <- "Dummy 0.0 for testing" + test_summary_2$fit_Rversion <- "Dummy R version for testing" + test_summary_2$date.fit <- "Dummy date for testing" + test_summary_2$date.summary <- "Dummy date for testing" + test_summary_2$calls <- "test 0" + test_summary_2$time <- c(elapsed = "test time 0") + # The correlation matrix is quite platform dependent + # It differs between i386 and amd64 on Windows + # and between Travis and my own Linux system + # Even more so when using the Eigen method + test_summary_2$Corr <- "Correlation matrix is platform dependent, not tested" + # The residuals for this method are also platform sensitive + test_summary_2$data$residual <- "not tested" + expect_known_output(print(test_summary_2), "summary_DFOP_FOCUS_D_eigen.txt") + + test_summary_3 <- summary(f_sfo_sfo_desolve) + test_summary_3$fit_version <- "Dummy 0.0 for testing" + test_summary_3$fit_Rversion <- "Dummy R version for testing" + test_summary_3$date.fit <- "Dummy date for testing" + test_summary_3$date.summary <- "Dummy date for testing" + test_summary_3$calls <- "test 0" + test_summary_3$time <- c(elapsed = "test time 0") + # The correlation matrix is quite platform dependent + # It differs between i386 and amd64 on Windows + # and between Travis and my own Linux system + test_summary_3$Corr <- "Correlation matrix is platform dependent, not tested" + expect_known_output(print(test_summary_3), "summary_DFOP_FOCUS_D_deSolve.txt") +}) + +test_that("A fit generated with mkin 0.9.48.1 can be summarised", { + # Generated with mkin 0.9.48.1 + # SFO_SFO <- mkinmod(parent = list(type = "SFO", to = "m1"), + # m1 = list(type = "SFO"), quiet = TRUE) + # fit_old <- mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE) + # save(fit_old, file = "~/git/mkin/inst/testdata/fit_old_FOCUS_D.rda", version = 2 ) + load(system.file("testdata/fit_old_FOCUS_D.rda", package = "mkin")) + expect_true(length(summary(fit_old)) > 0) +}) diff --git a/tests/testthat/test_twa.R b/tests/testthat/test_twa.R new file mode 100644 index 00000000..a8fc0bd9 --- /dev/null +++ b/tests/testthat/test_twa.R @@ -0,0 +1,26 @@ +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) + + ds <- "FOCUS_C" + 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) + } +}) + -- cgit v1.2.1