diff options
Diffstat (limited to 'tests/testthat')
-rw-r--r-- | tests/testthat/setup_script.R | 75 | ||||
-rw-r--r-- | tests/testthat/slow/test_roundtrip_error_parameters.R | 45 | ||||
-rw-r--r-- | tests/testthat/summary_DFOP_FOCUS_D.txt | 125 | ||||
-rw-r--r-- | tests/testthat/test_CAKE_export.R | 8 | ||||
-rw-r--r-- | tests/testthat/test_FOCUS_D_UBA_expertise.R | 6 | ||||
-rw-r--r-- | tests/testthat/test_SFORB.R | 32 | ||||
-rw-r--r-- | tests/testthat/test_confidence.R | 15 | ||||
-rw-r--r-- | tests/testthat/test_error_models.R | 31 | ||||
-rw-r--r-- | tests/testthat/test_plots_summary_twa.R | 27 |
9 files changed, 292 insertions, 72 deletions
diff --git a/tests/testthat/setup_script.R b/tests/testthat/setup_script.R new file mode 100644 index 00000000..c1d46dcb --- /dev/null +++ b/tests/testthat/setup_script.R @@ -0,0 +1,75 @@ +# 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 <http://www.gnu.org/licenses/> + +require(mkin) +require(testthat) + +# Per default (on my box where I set NOT_CRAN) use all cores minus one +if (identical(Sys.getenv("NOT_CRAN"), "true")) { + n_cores <- parallel::detectCores() - 1 +} else { + n_cores <- 1 +} + +# We are only allowed one core on travis, but they also set NOT_CRAN=true +if (Sys.getenv("TRAVIS") != "") n_cores = 1 + +# On Windows we would need to make a cluster first +if (Sys.info()["sysname"] == "Windows") n_cores = 1 + +# mmkin object of parent fits for tests +models <- c("SFO", "FOMC", "DFOP", "HS") +fits <- mmkin(models, + list(FOCUS_C = FOCUS_2006_C, FOCUS_D = FOCUS_2006_D), + quiet = TRUE, cores = n_cores) + +# One metabolite +SFO_SFO <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), quiet = TRUE) +SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"), + m1 = list(type = "SFO"), + use_of_ff = "max", quiet = TRUE) + +f_sfo_sfo <- mkinfit(SFO_SFO, + subset(FOCUS_2006_D, value != 0), + quiet = TRUE) + +f_sfo_sfo.ff <- mkinfit(SFO_SFO.ff, + subset(FOCUS_2006_D, value != 0), + quiet = TRUE) + +# Two metabolites +SFO_lin_a <- synthetic_data_for_UBA_2014[[1]]$data + +DFOP_par_c <- synthetic_data_for_UBA_2014[[12]]$data + +m_synth_SFO_lin <- mkinmod( + parent = mkinsub("SFO", "M1"), + M1 = mkinsub("SFO", "M2"), + M2 = mkinsub("SFO"), + use_of_ff = "max", quiet = TRUE) + +m_synth_DFOP_par <- mkinmod(parent = mkinsub("DFOP", c("M1", "M2")), + M1 = mkinsub("SFO"), + M2 = mkinsub("SFO"), + use_of_ff = "max", quiet = TRUE) + +f_SFO_lin_mkin_OLS <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE) +f_SFO_lin_mkin_ML <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE, + error_model = "const", error_model_algorithm = "direct") + diff --git a/tests/testthat/slow/test_roundtrip_error_parameters.R b/tests/testthat/slow/test_roundtrip_error_parameters.R index 97510563..7fa6c4e1 100644 --- a/tests/testthat/slow/test_roundtrip_error_parameters.R +++ b/tests/testthat/slow/test_roundtrip_error_parameters.R @@ -1,3 +1,36 @@ +# Copyright (C) 2018,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 <http://www.gnu.org/licenses/> + +context("Roundtripping error model parameters") + +# Per default (on my box where I set NOT_CRAN) use all cores minus one +if (identical(Sys.getenv("NOT_CRAN"), "true")) { + n_cores <- parallel::detectCores() - 1 +} else { + n_cores <- 1 +} + +# We are only allowed one core on travis, but they also set NOT_CRAN=true +if (Sys.getenv("TRAVIS") != "") n_cores = 1 + +# On Windows we would need to make a cluster first +if (Sys.info()["sysname"] == "Windows") n_cores = 1 + test_that("Reweighting method 'tc' produces reasonable variance estimates", { # Check if we can approximately obtain the parameters and the error model @@ -19,18 +52,6 @@ test_that("Reweighting method 'tc' produces reasonable variance estimates", { sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07), n = 1, reps = 100, digits = 5, LOD = -Inf, seed = 123456) - # Per default (on my box where I set NOT_CRAN) use all cores minus one - if (identical(Sys.getenv("NOT_CRAN"), "true")) { - n_cores <- parallel::detectCores() - 1 - } else { - n_cores <- 1 - } - - # We are only allowed one core on travis, but they also set NOT_CRAN=true - if (Sys.getenv("TRAVIS") != "") n_cores = 1 - - # On Windows we would need to make a cluster first - if (Sys.info()["sysname"] == "Windows") n_cores = 1 # Unweighted fits f_2_10 <- mmkin("DFOP", d_2_10, error_model = "const", quiet = TRUE, diff --git a/tests/testthat/summary_DFOP_FOCUS_D.txt b/tests/testthat/summary_DFOP_FOCUS_D.txt new file mode 100644 index 00000000..8abc15e7 --- /dev/null +++ b/tests/testthat/summary_DFOP_FOCUS_D.txt @@ -0,0 +1,125 @@ +mkin version used for fitting: Dummy 0.0 for testing +R version used for fitting: Dummy R version for testing +Date of fit: Dummy date for testing +Date of summary: Dummy date for testing + +Equations: +d_parent/dt = - k_parent_sink * parent - k_parent_m1 * parent +d_m1/dt = + k_parent_m1 * parent - k_m1_sink * m1 + +Model predictions using solution type deSolve + +Fitted using test 0 model solutions performed in test time 0 s + +Error model: Constant variance + +Error model algorithm: OLS + +Starting values for parameters to be optimised: + value type +parent_0 100.7500 state +k_parent_sink 0.1000 deparm +k_parent_m1 0.1001 deparm +k_m1_sink 0.1002 deparm + +Starting values for the transformed parameters actually optimised: + value lower upper +parent_0 100.750000 -Inf Inf +log_k_parent_sink -2.302585 -Inf Inf +log_k_parent_m1 -2.301586 -Inf Inf +log_k_m1_sink -2.300587 -Inf Inf + +Fixed parameter values: + value type +m1_0 0 state + +Optimised, transformed parameters with symmetric confidence intervals: + Estimate Std. Error Lower Upper +parent_0 99.600 1.57000 96.400 102.800 +log_k_parent_sink -3.038 0.07626 -3.193 -2.883 +log_k_parent_m1 -2.980 0.04033 -3.062 -2.898 +log_k_m1_sink -5.248 0.13320 -5.518 -4.977 +sigma 3.126 0.35850 2.396 3.855 + +Parameter correlation: + parent_0 log_k_parent_sink log_k_parent_m1 log_k_m1_sink +parent_0 1e+00 6e-01 -6e-02 -2e-01 +log_k_parent_sink 6e-01 1e+00 -9e-02 -6e-01 +log_k_parent_m1 -6e-02 -9e-02 1e+00 5e-01 +log_k_m1_sink -2e-01 -6e-01 5e-01 1e+00 +sigma 1e-09 -9e-10 2e-08 -1e-10 + sigma +parent_0 1e-09 +log_k_parent_sink -9e-10 +log_k_parent_m1 2e-08 +log_k_m1_sink -1e-10 +sigma 1e+00 + +Backtransformed parameters: +Confidence intervals for internally transformed parameters are asymmetric. +t-test (unrealistically) based on the assumption of normal distribution +for estimators of untransformed parameters. + Estimate t value Pr(>t) Lower Upper +parent_0 99.600000 63.430 2.298e-36 96.400000 1.028e+02 +k_parent_sink 0.047920 13.110 6.126e-15 0.041030 5.596e-02 +k_parent_m1 0.050780 24.800 3.269e-23 0.046780 5.512e-02 +k_m1_sink 0.005261 7.510 6.165e-09 0.004012 6.898e-03 +sigma 3.126000 8.718 2.235e-10 2.396000 3.855e+00 + +FOCUS Chi2 error levels in percent: + err.min n.optim df +All data 6.398 4 15 +parent 6.827 3 6 +m1 4.490 1 9 + +Resulting formation fractions: + ff +parent_sink 0.4855 +parent_m1 0.5145 +m1_sink 1.0000 + +Estimated disappearance times: + DT50 DT90 +parent 7.023 23.33 +m1 131.761 437.70 + +Data: + time variable observed predicted residual + 0 parent 99.46 99.59848 -1.385e-01 + 0 parent 102.04 99.59848 2.442e+00 + 1 parent 93.50 90.23787 3.262e+00 + 1 parent 92.50 90.23787 2.262e+00 + 3 parent 63.23 74.07320 -1.084e+01 + 3 parent 68.99 74.07320 -5.083e+00 + 7 parent 52.32 49.91207 2.408e+00 + 7 parent 55.13 49.91207 5.218e+00 + 14 parent 27.27 25.01257 2.257e+00 + 14 parent 26.64 25.01257 1.627e+00 + 21 parent 11.50 12.53462 -1.035e+00 + 21 parent 11.64 12.53462 -8.946e-01 + 35 parent 2.85 3.14787 -2.979e-01 + 35 parent 2.91 3.14787 -2.379e-01 + 50 parent 0.69 0.71624 -2.624e-02 + 50 parent 0.63 0.71624 -8.624e-02 + 75 parent 0.05 0.06074 -1.074e-02 + 75 parent 0.06 0.06074 -7.382e-04 + 1 m1 4.84 4.80296 3.704e-02 + 1 m1 5.64 4.80296 8.370e-01 + 3 m1 12.91 13.02400 -1.140e-01 + 3 m1 12.96 13.02400 -6.400e-02 + 7 m1 22.97 25.04476 -2.075e+00 + 7 m1 24.47 25.04476 -5.748e-01 + 14 m1 41.69 36.69002 5.000e+00 + 14 m1 33.21 36.69002 -3.480e+00 + 21 m1 44.37 41.65310 2.717e+00 + 21 m1 46.44 41.65310 4.787e+00 + 35 m1 41.22 43.31312 -2.093e+00 + 35 m1 37.95 43.31312 -5.363e+00 + 50 m1 41.19 41.21831 -2.831e-02 + 50 m1 40.01 41.21831 -1.208e+00 + 75 m1 40.09 36.44703 3.643e+00 + 75 m1 33.85 36.44703 -2.597e+00 + 100 m1 31.04 31.98163 -9.416e-01 + 100 m1 33.13 31.98163 1.148e+00 + 120 m1 25.15 28.78984 -3.640e+00 + 120 m1 33.31 28.78984 4.520e+00 diff --git a/tests/testthat/test_CAKE_export.R b/tests/testthat/test_CAKE_export.R index 23b424fc..a95a27ee 100644 --- a/tests/testthat/test_CAKE_export.R +++ b/tests/testthat/test_CAKE_export.R @@ -19,8 +19,9 @@ context("Export dataset for reading into CAKE") test_that("Exporting is reproducible", { - CAKE_export(list("FOCUS C" = FOCUS_2006_C, - "FOCUS D" = FOCUS_2006_D), + CAKE_export( + ds = list("FOCUS C" = FOCUS_2006_C, + "FOCUS D" = FOCUS_2006_D), map = c(parent = "Parent", m1 = "M1"), links = c(parent = "m1"), filename = "FOCUS_2006_D.csf", overwrite = TRUE, @@ -28,4 +29,7 @@ test_that("Exporting is reproducible", { csf <- readLines(con = "FOCUS_2006_D.csf") csf[8] <- "Date: Dummy date 0000-00-00" expect_known_value(csf, file = "FOCUS_2006_D.rds") + expect_error(CAKE_export(ds = list("FOCUS C" = FOCUS_2006_C), + filename = "FOCUS_2006_D.csf", overwrite = FALSE), + "already exists") }) diff --git a/tests/testthat/test_FOCUS_D_UBA_expertise.R b/tests/testthat/test_FOCUS_D_UBA_expertise.R index a282f5e7..614305b1 100644 --- a/tests/testthat/test_FOCUS_D_UBA_expertise.R +++ b/tests/testthat/test_FOCUS_D_UBA_expertise.R @@ -19,12 +19,6 @@ context("Results for FOCUS D established in expertise for UBA (Ranke 2014)") # Results are from p. 40 -SFO_SFO <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), quiet = TRUE) -SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"), - m1 = list(type = "SFO"), - use_of_ff = "max", quiet = TRUE) - test_that("Fits without formation fractions are correct for FOCUS D", { fit.default <- expect_warning(mkinfit(SFO_SFO, FOCUS_2006_D, quiet = TRUE), "value of zero") diff --git a/tests/testthat/test_SFORB.R b/tests/testthat/test_SFORB.R new file mode 100644 index 00000000..49b3beed --- /dev/null +++ b/tests/testthat/test_SFORB.R @@ -0,0 +1,32 @@ +# Copyright (C) 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 <http://www.gnu.org/licenses/> + +context("Fitting the SFORB model") + +logistic <- mkinmod(parent = mkinsub("logistic")) + +test_that("Fitting the SFORB model is equivalent to fitting DFOP", { + f_sforb <- mkinfit("SFORB", FOCUS_2006_C, quiet = TRUE) + f_dfop <- mkinfit("DFOP", FOCUS_2006_C, quiet = TRUE) + expect_equivalent(endpoints(f_sforb)$distimes, endpoints(f_dfop)$distimes, + tolerance = 1e-6) + s_sforb <- capture_output(print(summary(f_sforb))) + expect_match(s_sforb, "Estimated Eigenvalues of SFORB model\\(s\\):") + expect_match(s_sforb, "parent_b1 parent_b2") + expect_match(s_sforb, "0.45956 *0.01785") +}) diff --git a/tests/testthat/test_confidence.R b/tests/testthat/test_confidence.R index e5cc1954..0423302b 100644 --- a/tests/testthat/test_confidence.R +++ b/tests/testthat/test_confidence.R @@ -18,20 +18,8 @@ context("Confidence intervals and p-values") -m_synth_SFO_lin <- mkinmod( - parent = mkinsub("SFO", "M1"), - M1 = mkinsub("SFO", "M2"), - M2 = mkinsub("SFO"), - use_of_ff = "max", quiet = TRUE) - -SFO_lin_a <- synthetic_data_for_UBA_2014[[1]]$data - test_that("Confidence intervals are stable", { - f_1_mkin_OLS <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE) - f_1_mkin_ML <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE, - error_model = "const", error_model_algorithm = "direct") - - bpar_1 <- summary(f_1_mkin_ML)$bpar[, c("Estimate", "Lower", "Upper")] + bpar_1 <- summary(f_SFO_lin_mkin_ML)$bpar[, c("Estimate", "Lower", "Upper")] # The reference used here is mkin 0.9.48.1 bpar_1_mkin_0.9 <- read.table(text = "parent_0 102.0000 98.6000 106.0000 @@ -47,5 +35,6 @@ col.names = c("parameter", "estimate", "lower", "upper")) # Relative difference of lower bound of the confidence interval is < 0.02 expect_equivalent(bpar_1[1:6, "Lower"], bpar_1_mkin_0.9$lower, scale = bpar_1_mkin_0.9$lower, tolerance = 0.02) + expect_equivalent(f_SFO_lin_mkin_OLS$bpar, f_SFO_lin_mkin_ML$bpar) }) diff --git a/tests/testthat/test_error_models.R b/tests/testthat/test_error_models.R index f4015e00..ba40bae6 100644 --- a/tests/testthat/test_error_models.R +++ b/tests/testthat/test_error_models.R @@ -18,37 +18,6 @@ context("Error model fitting") -m_synth_SFO_lin <- mkinmod(parent = mkinsub("SFO", "M1"), - M1 = mkinsub("SFO", "M2"), - M2 = mkinsub("SFO"), - use_of_ff = "max", quiet = TRUE) - -m_synth_DFOP_par <- mkinmod(parent = mkinsub("DFOP", c("M1", "M2")), - M1 = mkinsub("SFO"), - M2 = mkinsub("SFO"), - use_of_ff = "max", quiet = TRUE) - -SFO_lin_a <- synthetic_data_for_UBA_2014[[1]]$data - -DFOP_par_c <- synthetic_data_for_UBA_2014[[12]]$data - -test_that("Error model 'const' works", { - skip_on_cran() - fit_const_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "const", quiet = TRUE) - bpar_1 <- fit_const_1$bparms.optim - # The reference used here is mkin 0.9.48.1 - bpar_1_mkin_0.9 <- read.table(text = -"parent_0 102.0000 -k_parent 0.7390 -k_M1 0.2990 -k_M2 0.0202 -f_parent_to_M1 0.7690 -f_M1_to_M2 0.7230", -col.names = c("parameter", "estimate")) - - expect_equivalent(signif(bpar_1, 3), bpar_1_mkin_0.9$estimate) -}) - test_that("Error model 'obs' works", { skip_on_cran() fit_obs_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "obs", quiet = TRUE) diff --git a/tests/testthat/test_plots_summary_twa.R b/tests/testthat/test_plots_summary_twa.R index 43036f1a..1dcf49ac 100644 --- a/tests/testthat/test_plots_summary_twa.R +++ b/tests/testthat/test_plots_summary_twa.R @@ -16,11 +16,6 @@ # You should have received a copy of the GNU General Public License along with # this program. If not, see <http://www.gnu.org/licenses/> -models <- c("SFO", "FOMC", "DFOP", "HS") -fits <- mmkin(models, - list(FOCUS_C = FOCUS_2006_C, FOCUS_D = FOCUS_2006_D), - quiet = TRUE, cores = 1) - context("Calculation of maximum time weighted average concentrations (TWAs)") test_that("Time weighted average concentrations are correct", { @@ -49,7 +44,7 @@ test_that("Time weighted average concentrations are correct", { context("Summary") -test_that("The summary is reproducible", { +test_that("Summaries are reproducible", { fit <- fits[["DFOP", "FOCUS_C"]] test_summary <- summary(fit) test_summary$fit_version <- "Dummy 0.0 for testing" @@ -63,6 +58,19 @@ test_that("The summary is reproducible", { # and between Travis and my own Linux system test_summary$Corr <- signif(test_summary$Corr, 1) expect_known_output(print(test_summary), "summary_DFOP_FOCUS_C.txt") + + test_summary_2 <- summary(f_sfo_sfo,) + 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 + test_summary_2$Corr <- signif(test_summary_2$Corr, 1) + expect_known_output(print(test_summary_2), "summary_DFOP_FOCUS_D.txt") }) context("Plotting") @@ -71,11 +79,13 @@ test_that("Plotting mmkin objects is reproducible", { skip_on_cran() 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",]) 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) }) @@ -84,7 +94,8 @@ 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)), + 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") }) |