aboutsummaryrefslogtreecommitdiff
path: root/tests/testthat/test_parent_only.R
blob: 572047b7d186e02d52c4cfcc1fe861ef6bbd98d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (C) 2015,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("Fitting of parent only models")

calc_dev.percent <- function(fitlist, reference, endpoints = TRUE, round_results = NULL) {
  dev.percent <- list()
  for (i in 1:length(fitlist)) {
    fit <- fitlist[[i]]
    if (endpoints) {
      results <- c(fit$bparms.optim,
                   endpoints(fit)$distimes$DT50,
                   endpoints(fit)$distimes$DT90)
    } else {
      results <- fit$bparms.optim
    }
    if (!missing(round_results)) results <- round(results, round_results)
    dev.percent[[i]] <- abs(100 * ((reference - results)/reference))
  }
  return(dev.percent)
}

SFO <- mkinmod(parent = list(type = "SFO"))
FOMC <- mkinmod(parent = list(type = "FOMC"))
DFOP <- mkinmod(parent = list(type = "DFOP"))
HS <- mkinmod(parent = list(type = "HS"))
SFORB <- mkinmod(parent = list(type = "SFORB"))

test_that("Fits for FOCUS A deviate less than 0.1% from median of values from FOCUS report", {
  fit.A.SFO <- list(mkinfit("SFO", FOCUS_2006_A, quiet = TRUE))

  median.A.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F,
                                        dataset == "A",
                                        c(M0, k, DT50, DT90)), "median"))

  dev.percent.A.SFO <- calc_dev.percent(fit.A.SFO, median.A.SFO)
  expect_equivalent(dev.percent.A.SFO[[1]] < 0.1, rep(TRUE, 4))

  # Fitting FOCUS A with FOMC is possible, but the correlation between
  # alpha and beta, when obtained, is 1.0000, and the fit does not
  # always converge using the Port algorithm (platform dependent), so
  # we need to suppress a potential warning
  suppressWarnings(fit.A.FOMC <- try(list(mkinfit("FOMC", FOCUS_2006_A, quiet = TRUE))))

  if (!inherits(fit.A.FOMC, "try-error")) {

    median.A.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F,
                                          dataset == "A",
                                          c(M0, alpha, beta, DT50, DT90)), "median"))

    dev.percent.A.FOMC <- calc_dev.percent(fit.A.FOMC, median.A.FOMC)
    # alpha and are beta ill-determined, do not compare those
    expect_equivalent(dev.percent.A.FOMC[[1]][c(1, 4, 5)] < 0.1, rep(TRUE, 3))
  }

  fit.A.DFOP <- list(mkinfit("DFOP", FOCUS_2006_A, quiet = TRUE))

  median.A.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B,
                                        dataset == "A",
                                        c(M0, k1, k2, f, DT50, DT90)), "median"))

  dev.percent.A.DFOP <- calc_dev.percent(fit.A.DFOP, median.A.DFOP)
  #expect_equivalent(dev.percent.A.DFOP[[1]] < 0.1, rep(TRUE, 6)) # g/f is ill-determined
  expect_equivalent(dev.percent.A.DFOP[[1]][c(1, 2, 3, 5, 6)] < 0.1, rep(TRUE, 5))

  fit.A.HS <- list(mkinfit("HS", FOCUS_2006_A, quiet = TRUE))

  median.A.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F,
                                        dataset == "A",
                                        c(M0, k1, k2, tb, DT50, DT90)), "median"))

  dev.percent.A.HS <- calc_dev.percent(fit.A.HS, median.A.HS)
  expect_equivalent(dev.percent.A.HS[[1]] < 0.1, rep(TRUE, 6))
})

test_that("Fits for FOCUS B deviate less than 0.1% from median of values from FOCUS report", {
  skip_on_cran()
  fit.B.SFO <- list(mkinfit("SFO", FOCUS_2006_B, quiet = TRUE))

  median.B.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F,
                                        dataset == "B",
                                        c(M0, k, DT50, DT90)), "median"))

  dev.percent.B.SFO <- calc_dev.percent(fit.B.SFO, median.B.SFO)
  expect_equivalent(dev.percent.B.SFO[[1]] < 0.1, rep(TRUE, 4))

  fit.B.FOMC <- list(mkinfit("FOMC", FOCUS_2006_B, quiet = TRUE))

  median.B.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F,
                                        dataset == "B",
                                        c(M0, alpha, beta, DT50, DT90)), "median"))

  dev.percent.B.FOMC <- calc_dev.percent(fit.B.FOMC, median.B.FOMC)
  expect_equivalent(dev.percent.B.FOMC[[1]] < 0.1, rep(TRUE, 5))

  fit.B.DFOP <- list(mkinfit("DFOP", FOCUS_2006_B, quiet = TRUE))

  median.B.DFOP <- as.numeric(lapply(subset(FOCUS_2006_DFOP_ref_A_to_B,
                                        dataset == "B",
                                        c(M0, k1, k2, f, DT50, DT90)), "median"))

  dev.percent.B.DFOP <- calc_dev.percent(fit.B.DFOP, median.B.DFOP)
  #expect_equivalent(dev.percent.B.DFOP[[1]] < 0.1, rep(TRUE, 6)) # g/f is ill-determined
  expect_equivalent(dev.percent.B.DFOP[[1]][c(1, 2, 3, 5, 6)] < 0.1, rep(TRUE, 5))

  fit.B.HS <- list(mkinfit("HS", FOCUS_2006_B, quiet = TRUE))

  median.B.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F,
                                        dataset == "B",
                                        c(M0, k1, k2, tb, DT50, DT90)),
                                   "median", na.rm = TRUE))

  dev.percent.B.HS <- calc_dev.percent(fit.B.HS, median.B.HS)
  expect_equivalent(dev.percent.B.HS[[1]] < 0.1, rep(TRUE, 6))

  fit.B.SFORB <- list(mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE))
  dev.percent.B.SFORB <- calc_dev.percent(fit.B.SFORB, median.B.DFOP)
  expect_equivalent(dev.percent.B.SFORB[[1]][c(1, 5, 6)] < 0.1, rep(TRUE, 3))
})

test_that("Fits for FOCUS C deviate less than 0.1% from median of values from FOCUS report", {
  fit.C.SFO <- list(mkinfit("SFO", FOCUS_2006_C, quiet = TRUE))

  median.C.SFO <- as.numeric(lapply(subset(FOCUS_2006_SFO_ref_A_to_F,
                                        dataset == "C",
                                        c(M0, k, DT50, DT90)), "median"))

  dev.percent.C.SFO <- calc_dev.percent(fit.C.SFO, median.C.SFO)
  expect_equivalent(dev.percent.C.SFO[[1]] < 0.1, rep(TRUE, 4))

  fit.C.FOMC <- list(mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE))

  median.C.FOMC <- as.numeric(lapply(subset(FOCUS_2006_FOMC_ref_A_to_F,
                                        dataset == "C",
                                        c(M0, alpha, beta, DT50, DT90)), "median"))

  dev.percent.C.FOMC <- calc_dev.percent(fit.C.FOMC, median.C.FOMC,
                                         round_results = 2) # Not enough precision in FOCUS results
  expect_equivalent(dev.percent.C.FOMC[[1]] < 0.1, rep(TRUE, 5))

  fit.C.HS <- list(mkinfit("HS", FOCUS_2006_C, quiet = TRUE))

  median.C.HS <- as.numeric(lapply(subset(FOCUS_2006_HS_ref_A_to_F,
                                        dataset == "C",
                                        c(M0, k1, k2, tb, DT50, DT90)), "median"))

  dev.percent.C.HS <- calc_dev.percent(fit.C.HS, median.C.HS, round_results = c(2, 4, 6, 2, 2))
  # Not enouth precision in k2 available
  expect_equivalent(dev.percent.C.HS[[1]] < c(0.1, 0.1, 0.3, 0.1, 0.1, 0.1), rep(TRUE, 6))
})

test_that("SFO fits give approximately (0.001%) equal results with different solution methods", {
  skip_on_cran()
  fit.A.SFO.default <- mkinfit("SFO", FOCUS_2006_A, quiet = TRUE)$bparms.optim

  fits.A.SFO <- list()
  fits.A.SFO[[1]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE)
  fits.A.SFO[[2]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE, solution_type = "eigen")
  fits.A.SFO[[3]] <- mkinfit(SFO, FOCUS_2006_A, quiet = TRUE, solution_type = "deSolve")

  dev.percent <- calc_dev.percent(fits.A.SFO, fit.A.SFO.default, endpoints = FALSE)
  expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 2))
  expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 2))
  expect_equivalent(dev.percent[[3]] < 0.001, rep(TRUE, 2))
})

test_that("FOMC fits give approximately (0.001%) equal results with different solution methods", {
  skip_on_cran()
  fit.C.FOMC.default <- mkinfit("FOMC", FOCUS_2006_C, quiet = TRUE)$bparms.optim

  fits.C.FOMC <- list()
  fits.C.FOMC[[1]] <- mkinfit(FOMC, FOCUS_2006_C, quiet = TRUE)
  fits.C.FOMC[[2]] <- mkinfit(FOMC, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve")

  dev.percent <- calc_dev.percent(fits.C.FOMC, fit.C.FOMC.default, endpoints = FALSE)
  expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 3))
  expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 3))
})

test_that("DFOP fits give approximately (0.001%) equal results with different solution methods", {
  skip_on_cran()
  fit.C.DFOP.default <- mkinfit("DFOP", FOCUS_2006_C, quiet = TRUE)$bparms.optim

  fits.C.DFOP <- list()
  fits.C.DFOP[[1]] <- mkinfit(DFOP, FOCUS_2006_C, quiet = TRUE)
  fits.C.DFOP[[2]] <- mkinfit(DFOP, FOCUS_2006_C, quiet = TRUE, solution_type = "deSolve")

  dev.percent <- calc_dev.percent(fits.C.DFOP, fit.C.DFOP.default, endpoints = FALSE)
  expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 4))
  expect_equivalent(dev.percent[[2]] < 0.001, rep(TRUE, 4))
})

test_that("SFORB fits give approximately (0.002%) equal results with different solution methods", {
  skip_on_cran()
  fit.B.SFORB.default <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE)$bparms.optim

  fits.B.SFORB <- list()
  fits.B.SFORB[[1]] <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE, solution_type = "eigen")
  fits.B.SFORB[[2]] <- mkinfit(SFORB, FOCUS_2006_B, quiet=TRUE, solution_type = "deSolve")
  dev.percent <- calc_dev.percent(fits.B.SFORB, fit.B.SFORB.default, endpoints = FALSE)
  expect_equivalent(dev.percent[[1]] < 0.001, rep(TRUE, 4))
  expect_equivalent(dev.percent[[2]] < 0.002, rep(TRUE, 4))
})

Contact - Imprint