1
#' Calculate the AIC for a column of an mmkin object
2
#'
3
#' Provides a convenient way to compare different kinetic models fitted to the
4
#' same dataset.
5
#'
6
#' @importFrom stats AIC BIC
7
#' @param object An object of class \code{\link{mmkin}}, containing only one
8
#'   column.
9
#' @param \dots For compatibility with the generic method
10
#' @param k As in the generic method
11
#' @return As in the generic method (a numeric value for single fits, or a
12
#'   dataframe if there are several fits in the column).
13
#' @author Johannes Ranke
14
#' @examples
15
#'
16
#'   \dontrun{ # skip, as it takes > 10 s on winbuilder
17
#'   f <- mmkin(c("SFO", "FOMC", "DFOP"),
18
#'     list("FOCUS A" = FOCUS_2006_A,
19
#'          "FOCUS C" = FOCUS_2006_C), cores = 1, quiet = TRUE)
20
#'   # We get a warning because the FOMC model does not converge for the
21
#'   # FOCUS A dataset, as it is well described by SFO
22
#'
23
#'   AIC(f["SFO", "FOCUS A"]) # We get a single number for a single fit
24
#'   AIC(f[["SFO", "FOCUS A"]]) # or when extracting an mkinfit object
25
#'
26
#'   # For FOCUS A, the models fit almost equally well, so the higher the number
27
#'   # of parameters, the higher (worse) the AIC
28
#'   AIC(f[, "FOCUS A"])
29
#'   AIC(f[, "FOCUS A"], k = 0) # If we do not penalize additional parameters, we get nearly the same
30
#'   BIC(f[, "FOCUS A"])        # Comparing the BIC gives a very similar picture
31
#'
32
#'   # For FOCUS C, the more complex models fit better
33
#'   AIC(f[, "FOCUS C"])
34
#'   BIC(f[, "FOCUS C"])
35
#'   }
36
#'
37
#' @export
38
AIC.mmkin <- function(object, ..., k = 2)
39
{
40
  # We can only handle a single column
41 2
  if (ncol(object) != 1) stop("Please provide a single column object")
42 2
  n.fits <- length(object)
43 2
  model_names <- rownames(object)
44

45 2
  code <- paste0("AIC(",
46 2
    paste0("object[[", 1:n.fits, "]]", collapse = ", "),
47 2
    ", k = k)")
48 2
  res <- eval(parse(text = code))
49 2
  if (n.fits > 1) rownames(res) <- model_names
50 2
  return(res)
51
}
52

53
#' @rdname AIC.mmkin
54
#' @export
55
BIC.mmkin <- function(object, ...)
56
{
57
  # We can only handle a single column
58 0
  if (ncol(object) != 1) stop("Please provide a single column object")
59 2
  n.fits <- length(object)
60 2
  model_names <- rownames(object)
61

62 2
  code <- paste0("BIC(",
63 2
    paste0("object[[", 1:n.fits, "]]", collapse = ", "),
64
    ")")
65 2
  res <- eval(parse(text = code))
66 2
  if (n.fits > 1) rownames(res) <- model_names
67 2
  return(res)
68
}

Read our documentation on viewing source code .

Loading