# Copyright (C) 2014-2015,2018,2019 Johannes Ranke
# Contact: [email protected]

# 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("Complex test case from Schaefer et al. (2007) Piacenza paper")

test_that("Complex test case from Schaefer (2007) can be reproduced (10% tolerance)", {
  schaefer07_complex_model <- mkinmod(
    parent = list(type = "SFO", to = c("A1", "B1", "C1"), sink = FALSE),
    A1 = list(type = "SFO", to = "A2"),
    B1 = list(type = "SFO"),
    C1 = list(type = "SFO"),
    A2 = list(type = "SFO"), use_of_ff = "max", quiet = TRUE)

  schaefer07_long <- mkin_wide_to_long(schaefer07_complex_case, time = "time")

  fit.default <- mkinfit(schaefer07_complex_model, schaefer07_long, quiet = TRUE)

  s <- summary(fit.default)
  r <- schaefer07_complex_results

  with(as.list(fit.default$bparms.optim), {
    r$mkin <<- c(
      s$distimes["parent", "DT50"],
      s$distimes["A1", "DT50"],
      s$distimes["B1", "DT50"],
      s$distimes["C1", "DT50"],
      s$distimes["A2", "DT50"])
  r$means <- (r$KinGUI + r$ModelMaker)/2
  r$mkin.deviation <- abs(round(100 * ((r$mkin - r$means)/r$means), digits=1))
  expect_equal(r$mkin.deviation < 10, rep(TRUE, 14))

  # In previous versions of mkinfit, if we used optimisation algorithm 'Marq'
  # we got a local minimum with a sum of squared residuals of 273.3707
  # When using 'Marq', we needed to give a good starting estimate e.g. for k_A2 in
  # order to get the optimum with sum of squared residuals 240.5686
  ssr <- sum(fit.default$data$residual^2)
  expect_equal(round(ssr, 4), 240.5686)
jranke/mkin documentation built on Feb. 16, 2020, 9:14 p.m.