diff options
Diffstat (limited to 'tests/testthat')
-rw-r--r-- | tests/testthat/setup_script.R | 21 | ||||
-rw-r--r-- | tests/testthat/test_FOCUS_chi2_error_level.R | 11 | ||||
-rw-r--r-- | tests/testthat/test_confidence.R | 3 | ||||
-rw-r--r-- | tests/testthat/test_from_max_mean.R | 3 | ||||
-rw-r--r-- | tests/testthat/test_mkinfit_errors.R | 8 | ||||
-rw-r--r-- | tests/testthat/test_mkinpredict_SFO_SFO.R | 20 | ||||
-rw-r--r-- | tests/testthat/test_residuals.R | 4 | ||||
-rw-r--r-- | tests/testthat/test_tests.R | 25 |
8 files changed, 43 insertions, 52 deletions
diff --git a/tests/testthat/setup_script.R b/tests/testthat/setup_script.R index fc972a3d..9becdd2a 100644 --- a/tests/testthat/setup_script.R +++ b/tests/testthat/setup_script.R @@ -47,6 +47,9 @@ SFO_SFO <- mkinmod(parent = list(type = "SFO", to = "m1"), SFO_SFO.ff <- mkinmod(parent = list(type = "SFO", to = "m1"), m1 = list(type = "SFO"), use_of_ff = "max", quiet = TRUE) +SFO_SFO.ff.nosink <- mkinmod( + parent = mkinsub("SFO", "m1", sink = FALSE), + m1 = mkinsub("SFO"), quiet = TRUE, use_of_ff = "max") f_sfo_sfo_desolve <- mkinfit(SFO_SFO, subset(FOCUS_2006_D, value != 0), @@ -76,8 +79,6 @@ m_synth_DFOP_par <- mkinmod(parent = mkinsub("DFOP", c("M1", "M2")), use_of_ff = "max", quiet = TRUE) fit_nw_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE) -fit_nw_1_ML <- mkinfit(m_synth_SFO_lin, SFO_lin_a, quiet = TRUE, - error_model = "const", error_model_algorithm = "direct") # We know direct optimization is OK and direct needs 4 sec versus 5.5 for threestep and 6 for IRLS fit_obs_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "obs", quiet = TRUE, @@ -85,19 +86,3 @@ fit_obs_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "obs", quiet = TR # We know threestep is OK, and threestep (and IRLS) need 4.8 se versus 5.6 for direct fit_tc_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "tc", quiet = TRUE, error_model_algorithm = "threestep") - -# We know direct optimization is OK and direct needs 8 sec versus 11 sec for threestep -f_tc_2 <- mkinfit(m_synth_DFOP_par, DFOP_par_c, error_model = "tc", - error_model_algorithm = "direct", quiet = TRUE) - -# Experimental data for UBA -dfop_sfo_sfo <- mkinmod( - parent = mkinsub("DFOP", to = "A1"), - A1 = mkinsub("SFO", to = "A2"), - A2 = mkinsub("SFO"), - use_of_ff = "max" -) - -f_soil_1_tc <- mkinfit(dfop_sfo_sfo, - experimental_data_for_UBA_2019[[1]]$data, - error_model = "tc", quiet = TRUE) diff --git a/tests/testthat/test_FOCUS_chi2_error_level.R b/tests/testthat/test_FOCUS_chi2_error_level.R index d7f8d9e7..d091cb95 100644 --- a/tests/testthat/test_FOCUS_chi2_error_level.R +++ b/tests/testthat/test_FOCUS_chi2_error_level.R @@ -18,10 +18,6 @@ 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", quiet = TRUE) - errmin.FOCUS_2006_D_rounded = data.frame( err.min = c(0.0640, 0.0646, 0.0469), n.optim = c(4, 2, 2), @@ -36,16 +32,13 @@ errmin.FOCUS_2006_E_rounded = data.frame( test_that("Chi2 error levels for FOCUS D are as in mkin 0.9-33", { - fit <- expect_warning(mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE), - "Observations with value of zero") - - - expect_equal(round(mkinerrmin(fit), 4), + expect_equal(round(mkinerrmin(f_sfo_sfo.ff), 4), errmin.FOCUS_2006_D_rounded) }) test_that("Chi2 error levels are independent of setting parms.ini that are not in the model", { + skip_on_cran() fit.2 <- expect_warning(mkinfit(SFO_SFO.ff, FOCUS_2006_D, quiet = TRUE, parms.ini = c(tb = 5)), "Observations with value of zero") diff --git a/tests/testthat/test_confidence.R b/tests/testthat/test_confidence.R index 2443fa66..a2bf1401 100644 --- a/tests/testthat/test_confidence.R +++ b/tests/testthat/test_confidence.R @@ -73,7 +73,8 @@ test_that("Likelihood profile based confidence intervals work", { } f_mle <- stats4::mle(f_nll, start = as.list(parms(f)), nobs = nrow(FOCUS_2006_C)) - ci_mkin_1_p_0.95 <- confint(f, method = "profile", level = 0.95, quiet = TRUE) + ci_mkin_1_p_0.95 <- confint(f, method = "profile", level = 0.95, + cores = n_cores, quiet = TRUE) # Magically, we get very similar boundaries as stats4::mle # (we need to capture the output to avoid printing this while testing as diff --git a/tests/testthat/test_from_max_mean.R b/tests/testthat/test_from_max_mean.R index c4d6bfe4..3edf5969 100644 --- a/tests/testthat/test_from_max_mean.R +++ b/tests/testthat/test_from_max_mean.R @@ -19,8 +19,7 @@ context("Test fitting the decline of metabolites from their maximum") test_that("Fitting from maximum mean value works", { - SFO_SFO <- mkinmod(parent = mkinsub("SFO", "m1"), - m1 = mkinsub("SFO")) + expect_warning( expect_error(mkinfit(SFO_SFO, FOCUS_2006_D, from_max_mean = TRUE), "only implemented for models with a single observed variable"), diff --git a/tests/testthat/test_mkinfit_errors.R b/tests/testthat/test_mkinfit_errors.R index 8adb05c1..3a04e9c6 100644 --- a/tests/testthat/test_mkinfit_errors.R +++ b/tests/testthat/test_mkinfit_errors.R @@ -18,14 +18,6 @@ context("Special cases of mkinfit calls") -SFO_SFO.ff.nosink <- mkinmod( - parent = mkinsub("SFO", "m1", sink = FALSE), - m1 = mkinsub("SFO"), quiet = TRUE, use_of_ff = "max") - -SFO_SFO.ff <- mkinmod( - parent = mkinsub("SFO", "m1"), - m1 = mkinsub("SFO"), quiet = TRUE, use_of_ff = "max") - test_that("mkinfit stops to prevent and/or explain user errors", { expect_error(mkinfit("foo", FOCUS_2006_A)) expect_error(mkinfit(3, FOCUS_2006_A)) diff --git a/tests/testthat/test_mkinpredict_SFO_SFO.R b/tests/testthat/test_mkinpredict_SFO_SFO.R index 3fcdb3a2..d0d856fa 100644 --- a/tests/testthat/test_mkinpredict_SFO_SFO.R +++ b/tests/testthat/test_mkinpredict_SFO_SFO.R @@ -31,20 +31,20 @@ test_that("Variants of model predictions for SFO_SFO model give equivalent resul m1 = list(type = "SFO"), use_of_ff = "max", quiet = TRUE)
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"),
+ 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"),
+ 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"),
+ 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"),
+ 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
diff --git a/tests/testthat/test_residuals.R b/tests/testthat/test_residuals.R index 275b33b9..35a143f6 100644 --- a/tests/testthat/test_residuals.R +++ b/tests/testthat/test_residuals.R @@ -4,5 +4,7 @@ test_that("Residuals are correctly returned", { f <- fits[["FOMC", "FOCUS_C"]] expect_equal(residuals(f)[1:3], c(-0.7748906, 2.7090589, -1.9451989)) - expect_equivalent(residuals(f_tc_2, standardized = TRUE)[1:3], c(0.52579103, 0.40714911, 1.66394233), tolerance = 0.0001) + expect_equivalent( + residuals(f, standardized = TRUE)[1:3], + c(-0.4171812, 1.4584875, -1.0472450), tolerance = 0.0001) }) diff --git a/tests/testthat/test_tests.R b/tests/testthat/test_tests.R index d8b97815..6edb4dfa 100644 --- a/tests/testthat/test_tests.R +++ b/tests/testthat/test_tests.R @@ -2,7 +2,7 @@ context("Hypothesis tests") test_that("The likelihood ratio test works", { - expect_error(lrtest(fit_tc_1, f_tc_2), "not been fitted to the same data") + expect_error(lrtest(f_1_mkin_trans, f_2_mkin), "not been fitted to the same data") res <- lrtest(fit_nw_1, fit_tc_1) expect_equal(res[["2", "Pr(>Chisq)"]], 1, tolerance = 1e-5) @@ -10,13 +10,27 @@ test_that("The likelihood ratio test works", { }) test_that("We can conveniently fix parameters using 'fixed_parms'", { + f_k2_fixed <- mkinfit("DFOP", FOCUS_2006_C, fixed_parms = c(k2 = 0.05), quiet = TRUE) expect_equivalent(f_k2_fixed$bparms.ode["k2"], 0.05) + }) test_that("Updating fitted models works", { - f_dfop_const <- mkinfit("DFOP", FOCUS_2006_C, quiet = TRUE) - f_dfop_tc <- update(f_dfop_const, error_model = "tc") + + skip_on_cran() + f_dfop_tc <- update(f_2_mkin, error_model = "tc") + + dfop_sfo_sfo <- mkinmod( + parent = mkinsub("DFOP", to = "A1"), + A1 = mkinsub("SFO", to = "A2"), + A2 = mkinsub("SFO"), + use_of_ff = "max" + ) + + f_soil_1_tc <- mkinfit(dfop_sfo_sfo, + experimental_data_for_UBA_2019[[1]]$data, + error_model = "tc", quiet = TRUE) f_soil_1_nw <- update(f_soil_1_tc, error_model = "const") f_soil_1_nw_A2 <- update(f_soil_1_nw, fixed_parms = c(k_A2 = 0)) @@ -25,3 +39,8 @@ test_that("Updating fitted models works", { test_nw_A2 <- lrtest(f_soil_1_nw, f_soil_1_nw_A2) expect_equivalent(test_nw_A2[["2", "Pr(>Chisq)"]], 1, tolerance = 1e-4) }) + +test_that("We can do a likelihood ratio test using an update specification", { + test_2_mkin_k2 <- lrtest(f_2_mkin, fixed_parms = c(k2 = 0)) + expect_equivalent(test_2_mkin_k2[["2", "Pr(>Chisq)"]], 1.139e-6, tolerance = 1e-8) +}) |