aboutsummaryrefslogtreecommitdiff
path: root/R/max_twa_parent.R
blob: 5129e3696076eb4c0dba9547b02c96b218eefb95 (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
# Copyright (C) 2016-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/>

max_twa_parent <- function(fit, windows) {
  parms.all <- c(fit$bparms.optim, fit$bparms.fixed)
  obs_vars <- fit$obs_vars
  if (length(obs_vars) > 1) {
    warning("Calculation of maximum time weighted average concentrations is",
            "currently only implemented for the parent compound using",
            "analytical solutions")
  }
  obs_var <- obs_vars[1]
  spec = fit$mkinmod$spec
  type = spec[[1]]$type

  M0 <- parms.all[paste0(obs_var, "_0")]

  if (type == "SFO") {
    k_name <- paste0("k_", obs_var)
    if (fit$mkinmod$use_of_ff == "min") {
      k_name <- paste0(k_name, "_sink")
    }
    k <- parms.all[k_name]
    twafunc <- function(t) {
      max_twa_sfo(M0, k, t)
    }
  }
  if (type == "FOMC") {
    alpha <- parms.all["alpha"]
    beta <- parms.all["beta"]
    twafunc <- function(t) {
      max_twa_fomc(M0, alpha, beta, t)
    }
  }
  if (type == "DFOP") {
    k1 <- parms.all["k1"]
    k2 <- parms.all["k2"]
    g <- parms.all["g"]
    twafunc <- function(t) {
      max_twa_dfop(M0, k1, k2, g, t)
    }
  }
  if (type == "HS") {
    k1 <- parms.all["k1"]
    k2 <- parms.all["k2"]
    tb <- parms.all["tb"]
    twafunc <- function(t) {
      ifelse(t <= tb,
        max_twa_sfo(M0, k1, t),
        max_twa_hs(M0, k1, k2, tb, t)
      )
    }
  }
  if (type %in% c("IORE", "SFORB")) {
    stop("Calculation of maximum time weighted average concentrations is currently ",
         "not implemented for the ", type, " model.")
  }
  res <- twafunc(windows)
  names(res) <- windows
  return(res)
}
max_twa_sfo <- function(M0 = 1, k, t) {
  M0 * (1 - exp(- k * t)) / (k * t)
}
max_twa_fomc <- function(M0 = 1, alpha, beta, t) {
  M0 * (beta)/(t * (1 - alpha)) * ((t/beta + 1)^(1 - alpha) - 1)
}
max_twa_dfop <- function(M0 = 1, k1, k2, g, t) {
  M0/t * ((g/k1) * (1 - exp(- k1 * t)) + ((1 - g)/k2) * (1 - exp(- k2 * t)))
}
max_twa_hs <- function(M0 = 1, k1, k2, tb, t) {
  (M0 / t) * (
    (1/k1) * (1 - exp(- k1 * tb)) +
    (exp(- k1 * tb) / k2) * (1 - exp(- k2 * (t - tb)))
  )
}

Contact - Imprint