From c5878e0c561a93f36f8fdc64d5839db2a0527572 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Tue, 7 Apr 2020 20:25:03 +0200 Subject: Add some tests for the nlme convenience functions --- tests/testthat/FOCUS_2006_D.csf | 2 +- tests/testthat/test_nlme.R | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test_nlme.R (limited to 'tests') diff --git a/tests/testthat/FOCUS_2006_D.csf b/tests/testthat/FOCUS_2006_D.csf index fe5e481a..f492a57f 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-01 +Date: 2020-04-07 Optimiser: IRLS [Data] diff --git a/tests/testthat/test_nlme.R b/tests/testthat/test_nlme.R new file mode 100644 index 00000000..b3c41f00 --- /dev/null +++ b/tests/testthat/test_nlme.R @@ -0,0 +1,63 @@ +context("Nonlinear mixed-effects models") + +test_that("nlme_function works correctly", { + + sampling_times = c(0, 1, 3, 7, 14, 28, 60, 90, 120) + m_SFO <- mkinmod(parent = mkinsub("SFO")) + d_SFO_1 <- mkinpredict(m_SFO, + c(k_parent_sink = 0.1), + c(parent = 98), sampling_times) + d_SFO_1_long <- mkin_wide_to_long(d_SFO_1, time = "time") + d_SFO_2 <- mkinpredict(m_SFO, + c(k_parent_sink = 0.05), + c(parent = 102), sampling_times) + d_SFO_2_long <- mkin_wide_to_long(d_SFO_2, time = "time") + d_SFO_3 <- mkinpredict(m_SFO, + c(k_parent_sink = 0.02), + c(parent = 103), sampling_times) + d_SFO_3_long <- mkin_wide_to_long(d_SFO_3, time = "time") + + d1 <- add_err(d_SFO_1, function(value) 3, n = 1, seed = 123456) + d2 <- add_err(d_SFO_2, function(value) 2, n = 1, seed = 234567) + d3 <- add_err(d_SFO_3, function(value) 4, n = 1, seed = 345678) + ds <- c(d1 = d1, d2 = d2, d3 = d3) + + f <- mmkin("SFO", ds, cores = 1, quiet = TRUE) + + mean_dp <- mean_degparms(f) + grouped_data <- nlme_data(f) + nlme_f <- nlme_function(f) + + m_nlme_raw <- nlme(value ~ SSasymp(time, 0, parent_0, log_k_parent_sink), + data = grouped_data, + fixed = parent_0 + log_k_parent_sink ~ 1, + random = pdDiag(parent_0 + log_k_parent_sink ~ 1), + start = mean_dp) + + m_nlme_mkin <- nlme(value ~ nlme_f(name, time, parent_0, log_k_parent_sink), + data = grouped_data, + fixed = parent_0 + log_k_parent_sink ~ 1, + random = pdDiag(parent_0 + log_k_parent_sink ~ 1), + start = mean_dp) + + expect_equal(m_nlme_raw$coefficients, m_nlme_mkin$coefficients) + + m_nlme_raw_up_1 <- update(m_nlme_raw, random = log_k_parent_sink ~ 1) + # The following two calls give an error although they should + # do the same as the call above + # The error occurs in the evaluation of the modelExpression in the + # call to .C(fit_nlme, ...) + # m_nlme_mkin_up_1 <- update(m_nlme_mkin, random = log_k_parent_sink ~ 1) + # m_nlme_mkin <- nlme(value ~ nlme_f(name, time, parent_0, log_k_parent_sink), + # data = grouped_data, + # fixed = parent_0 + log_k_parent_sink ~ 1, + # random = log_k_parent_sink ~ 1, + # start = mean_dp) + + m_nlme_raw_up_2 <- update(m_nlme_raw, random = parent_0 ~ 1) + m_nlme_mkin_up_2 <- update(m_nlme_mkin, random = parent_0 ~ 1) + expect_equal(m_nlme_raw_up_2$coefficients, m_nlme_mkin_up_2$coefficients) + + # update(m_nlme_mkin) # works + +}) -- cgit v1.2.1