aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/testthat/test_error_models.R (renamed from tests/testthat/test_irls.R)106
1 files changed, 71 insertions, 35 deletions
diff --git a/tests/testthat/test_irls.R b/tests/testthat/test_error_models.R
index f61f793d..bda8ca7f 100644
--- a/tests/testthat/test_irls.R
+++ b/tests/testthat/test_error_models.R
@@ -16,8 +16,7 @@
# 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("Iteratively reweighted least squares (IRLS) fitting")
-
+context("Error model fitting")
m_synth_SFO_lin <- mkinmod(parent = mkinsub("SFO", "M1"),
M1 = mkinsub("SFO", "M2"),
@@ -33,19 +32,53 @@ SFO_lin_a <- synthetic_data_for_UBA_2014[[1]]$data
DFOP_par_c <- synthetic_data_for_UBA_2014[[12]]$data
-test_that("Reweighting method 'obs' works", {
+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 <- summary(fit_const_1)$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
+k_parent 0.7390 0.6770 0.8070
+k_M1 0.2990 0.2560 0.3490
+k_M2 0.0202 0.0176 0.0233
+f_parent_to_M1 0.7690 0.6640 0.8480
+f_M1_to_M2 0.7230 0.6030 0.8180",
+col.names = c("parameter", "estimate", "lower", "upper"))
+
+ expect_equivalent(signif(bpar_1[1:6, "Estimate"], 3), bpar_1_mkin_0.9$estimate)
+ # Relative difference of lower bound of confidence is < 0.02
+ rel_diff <- function(v1, v2) {
+ (v1 - v2)/v2
+ }
+ expect_equivalent(rel_diff(bpar_1[1:6, "Lower"],
+ bpar_1_mkin_0.9$lower),
+ rep(0, 6), tolerance = 0.02)
+})
+
+test_that("Error model 'obs' works", {
skip_on_cran()
- fit_irls_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, reweight.method = "obs", quiet = TRUE)
- parms_1 <- round(fit_irls_1$bparms.optim, c(1, 4, 4, 4, 4, 4))
- expect_equivalent(parms_1, c(102.1, 0.7389, 0.2982, 0.0203, 0.7677, 0.7246))
+ fit_obs_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "obs", quiet = TRUE)
+ parms_2 <- round(fit_obs_1$bparms.optim, c(1, 4, 4, 4, 4, 4))
+ expect_equivalent(parms_2, c(102.1, 0.7389, 0.2982, 0.0203, 0.7677, 0.7246))
})
-test_that("Reweighting method 'tc' works", {
- fit_irls_2 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, reweight.method = "tc", quiet = TRUE)
- parms_2 <- round(fit_irls_2$bparms.optim, c(1, 4, 4, 4, 4, 4))
- expect_equivalent(parms_2, c(102.1, 0.7393, 0.2992, 0.0202, 0.7687, 0.7229))
+test_that("Error model 'tc' works", {
+ skip_on_cran()
+ fit_tc_1 <- mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "tc", quiet = TRUE)
+ parms_3 <- round(fit_tc_1$bparms.optim, c(1, 4, 4, 4, 4, 4))
+ expect_equivalent(parms_3, c(102.1, 0.7393, 0.2992, 0.0202, 0.7687, 0.7229))
+})
+
+test_that("Error model 'obs_tc' works", {
+ skip_on_cran()
+ fit_obs_tc_1 <- expect_warning(mkinfit(m_synth_SFO_lin, SFO_lin_a, error_model = "obs_tc", quiet = TRUE), "NaN")
+ # Here the error model is overparameterised
+ expect_warning(summary(fit_obs_tc_1), "singular system")
+})
+
+test_that("Reweighting method 'tc' produces reasonable variance estimates", {
- skip("Too much trouble with datasets that are randomly generated")
# I need to make the tc method more robust against that
# skip_on_cran()
@@ -57,42 +90,46 @@ test_that("Reweighting method 'tc' works", {
sampling_times = c(0, 1, 3, 7, 14, 28, 60, 90, 120)
parms_DFOP <- c(k1 = 0.2, k2 = 0.02, g = 0.5)
parms_DFOP_optim <- c(parent_0 = 100, parms_DFOP)
+
d_DFOP <- mkinpredict(DFOP,
parms_DFOP, c(parent = 100),
sampling_times)
d_2_10 <- add_err(d_DFOP,
sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07),
- n = 10, reps = 2, digits = 5, LOD = -Inf)
+ n = 10, reps = 2, digits = 5, LOD = -Inf, seed = 123456)
d_100_1 <- add_err(d_DFOP,
sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07),
- n = 1, reps = 100, digits = 5, LOD = -Inf)
+ n = 1, reps = 100, digits = 5, LOD = -Inf, seed = 123456)
- f_2_10 <- mmkin("DFOP", d_2_10, quiet = TRUE,
+ # Unweighted fits
+ f_2_10 <- mmkin("DFOP", d_2_10, error_model = "const", quiet = TRUE,
cores = if (Sys.getenv("TRAVIS") != "") 1 else 15)
parms_2_10 <- apply(sapply(f_2_10, function(x) x$bparms.optim), 1, mean)
parm_errors_2_10 <- (parms_2_10 - parms_DFOP_optim) / parms_DFOP_optim
- expect_true(all(abs(parm_errors_2_10) < 0.45))
+ expect_true(all(abs(parm_errors_2_10) < 0.12))
- f_2_10_tc <- mmkin("DFOP", d_2_10, reweight.method = "tc", quiet = TRUE,
+ f_2_10_tc <- mmkin("DFOP", d_2_10, error_model = "tc", quiet = TRUE,
cores = if (Sys.getenv("TRAVIS") != "") 1 else 15)
parms_2_10_tc <- apply(sapply(f_2_10_tc, function(x) x$bparms.optim), 1, mean)
parm_errors_2_10_tc <- (parms_2_10_tc - parms_DFOP_optim) / parms_DFOP_optim
- expect_true(all(abs(parm_errors_2_10_tc) < 0.25))
+ expect_true(all(abs(parm_errors_2_10_tc) < 0.05))
- tcf_2_10_tc <- apply(sapply(f_2_10_tc, function(x) x$tc_fitted), 1, mean, na.rm = TRUE)
+ tcf_2_10_tc <- apply(sapply(f_2_10_tc, function(x) x$errparms), 1, mean, na.rm = TRUE)
tcf_2_10_error_model_errors <- (tcf_2_10_tc - c(0.5, 0.07)) / c(0.5, 0.07)
- expect_true(all(abs(tcf_2_10_error_model_errors) < 0.5))
+ expect_true(all(abs(tcf_2_10_error_model_errors) < 0.2))
- f_tc_100_1 <- suppressWarnings(mkinfit(DFOP, d_100_1[[1]], reweight.method = "tc", quiet = TRUE))
+ # When we have 100 replicates in the synthetic data, we can roundtrip
+ # the parameters with < 2% precision
+ f_tc_100_1 <- mkinfit(DFOP, d_100_1[[1]], error_model = "tc", quiet = TRUE)
parm_errors_100_1 <- (f_tc_100_1$bparms.optim - parms_DFOP_optim) / parms_DFOP_optim
- expect_true(all(abs(parm_errors_100_1) < 0.1))
+ expect_true(all(abs(parm_errors_100_1) < 0.02))
- tcf_100_1_error_model_errors <- (f_tc_100_1$tc_fitted - c(0.5, 0.07)) /
+ tcf_100_1_error_model_errors <- (f_tc_100_1$errparms - c(0.5, 0.07)) /
c(0.5, 0.07)
- # Even with 100 (or even 1000, not shown) replicates at each observation time
- # we only get a precision of 15% to 30% for the error model components
- expect_true(all(abs(tcf_100_1_error_model_errors) < 0.3))
+ # When maximising the likelihood directly (not using IRLS), we get
+ # a precision of < 2% for the error model componentes as well
+ expect_true(all(abs(tcf_100_1_error_model_errors) < 0.02))
# Parent and two metabolites
m_synth_DFOP_lin <- mkinmod(parent = list(type = "DFOP", to = "M1"),
@@ -111,32 +148,31 @@ test_that("Reweighting method 'tc' works", {
d_met_2_15 <- add_err(d_synth_DFOP_lin,
sdfunc = function(x) sigma_twocomp(x, 0.5, 0.07),
- n = 15, reps = 1000, digits = 5, LOD = -Inf)
+ n = 15, reps = 100, digits = 5, LOD = -Inf, seed = 123456)
- # For a single fit, we get a relative error of less than 30% in the error
+ # For a single fit, we get a relative error of less than 10% in the error
# model components
f_met_2_tc_e4 <- mkinfit(m_synth_DFOP_lin, d_met_2_15[[1]], quiet = TRUE,
- reweight.method = "tc", reweight.tol = 1e-4)
- parm_errors_met_2_tc_e4 <- (f_met_2_tc_e4$tc_fitted - c(0.5, 0.07)) / c(0.5, 0.07)
- expect_true(all(abs(parm_errors_met_2_tc_e4) < 0.3))
+ error_model = "tc")
+ parm_errors_met_2_tc_e4 <- (f_met_2_tc_e4$errparms - c(0.5, 0.07)) / c(0.5, 0.07)
+ expect_true(all(abs(parm_errors_met_2_tc_e4) < 0.1))
# Doing more takes a lot of computing power
skip_on_travis()
f_met_2_15_tc_e4 <- mmkin(list(m_synth_DFOP_lin), d_met_2_15, quiet = TRUE,
- reweight.method = "tc", reweight.tol = 1e-4,
- cores = 14)
+ error_model = "tc", cores = 15)
parms_met_2_15_tc_e4 <- apply(sapply(f_met_2_15_tc_e4, function(x) x$bparms.optim), 1, mean)
parm_errors_met_2_15_tc_e4 <- (parms_met_2_15_tc_e4[names(parms_DFOP_lin_optim)] -
parms_DFOP_lin_optim) / parms_DFOP_lin_optim
expect_true(all(abs(parm_errors_met_2_15_tc_e4) < 0.01))
- tcf_met_2_15_tc <- apply(sapply(f_met_2_15_tc_e4, function(x) x$tc_fitted), 1, mean, na.rm = TRUE)
+ tcf_met_2_15_tc <- apply(sapply(f_met_2_15_tc_e4, function(x) x$errparms), 1, mean, na.rm = TRUE)
tcf_met_2_15_tc_error_model_errors <- (tcf_met_2_15_tc - c(0.5, 0.07)) /
c(0.5, 0.07)
- # Here we only get a precision < 30% for retrieving the original error model components
+ # Here we get a precision < 15% for retrieving the original error model components
# from 15 datasets
- expect_true(all(abs(tcf_met_2_15_tc_error_model_errors) < 0.3))
+ expect_true(all(abs(tcf_met_2_15_tc_error_model_errors) < 0.15))
})

Contact - Imprint