1
#' Summary method for class "mkinfit"
2
#'
3
#' Lists model equations, initial parameter values, optimised parameters with
4
#' some uncertainty statistics, the chi2 error levels calculated according to
5
#' FOCUS guidance (2006) as defined therein, formation fractions, DT50 values
6
#' and optionally the data, consisting of observed, predicted and residual
7
#' values.
8
#'
9
#' @param object an object of class \code{\link{mkinfit}}.
10
#' @param x an object of class \code{summary.mkinfit}.
11
#' @param data logical, indicating whether the data should be included in the
12
#'   summary.
13
#' @param distimes logical, indicating whether DT50 and DT90 values should be
14
#'   included.
15
#' @param alpha error level for confidence interval estimation from t
16
#'   distribution
17
#' @param digits Number of digits to use for printing
18
#' @param \dots optional arguments passed to methods like \code{print}.
19
#' @importFrom stats qt pt cov2cor
20
#' @return The summary function returns a list with components, among others
21
#'   \item{version, Rversion}{The mkin and R versions used}
22
#'   \item{date.fit, date.summary}{The dates where the fit and the summary were
23
#'     produced}
24
#'   \item{diffs}{The differential equations used in the model}
25
#'   \item{use_of_ff}{Was maximum or minimum use made of formation fractions}
26
#'   \item{bpar}{Optimised and backtransformed
27
#'     parameters}
28
#'   \item{data}{The data (see Description above).}
29
#'   \item{start}{The starting values and bounds, if applicable, for optimised
30
#'     parameters.}
31
#'   \item{fixed}{The values of fixed parameters.}
32
#'   \item{errmin }{The chi2 error levels for
33
#'     each observed variable.}
34
#'   \item{bparms.ode}{All backtransformed ODE
35
#'     parameters, for use as starting parameters for related models.}
36
#'   \item{errparms}{Error model parameters.}
37
#'   \item{ff}{The estimated formation fractions derived from the fitted
38
#'      model.}
39
#'   \item{distimes}{The DT50 and DT90 values for each observed variable.}
40
#'   \item{SFORB}{If applicable, eigenvalues of SFORB components of the model.}
41
#'   The print method is called for its side effect, i.e. printing the summary.
42
#' @author Johannes Ranke
43
#' @references FOCUS (2006) \dQuote{Guidance Document on Estimating Persistence
44
#'   and Degradation Kinetics from Environmental Fate Studies on Pesticides in
45
#'   EU Registration} Report of the FOCUS Work Group on Degradation Kinetics,
46
#'   EC Document Reference Sanco/10058/2005 version 2.0, 434 pp,
47
#'   \url{http://esdac.jrc.ec.europa.eu/projects/degradation-kinetics}
48
#' @examples
49
#'
50
#'   summary(mkinfit(mkinmod(parent = mkinsub("SFO")), FOCUS_2006_A, quiet = TRUE))
51
#'
52
#' @export
53
summary.mkinfit <- function(object, data = TRUE, distimes = TRUE, alpha = 0.05, ...) {
54 2
  param  <- object$par
55 2
  pnames <- names(param)
56 2
  bpnames <- names(object$bparms.optim)
57 2
  epnames <- names(object$errparms)
58 2
  p      <- length(param)
59 2
  mod_vars <- names(object$mkinmod$diffs)
60 2
  covar  <- try(solve(object$hessian), silent = TRUE)
61 2
  covar_notrans  <- try(solve(object$hessian_notrans), silent = TRUE)
62 2
  rdf <- object$df.residual
63

64 2
  if (!is.numeric(covar) | is.na(covar[1])) {
65 0
    covar <- NULL
66 0
    se <- lci <- uci <- rep(NA, p)
67
  } else {
68 2
    rownames(covar) <- colnames(covar) <- pnames
69 2
    se     <- sqrt(diag(covar))
70 2
    lci    <- param + qt(alpha/2, rdf) * se
71 2
    uci    <- param + qt(1-alpha/2, rdf) * se
72
  }
73

74 2
  beparms.optim <- c(object$bparms.optim, object$par[epnames])
75 2
  if (!is.numeric(covar_notrans) | is.na(covar_notrans[1])) {
76 2
    covar_notrans <- NULL
77 2
    se_notrans <- tval <- pval <- rep(NA, p)
78
  } else {
79 2
    rownames(covar_notrans) <- colnames(covar_notrans) <- c(bpnames, epnames)
80 2
    se_notrans <- sqrt(diag(covar_notrans))
81 2
    tval  <- beparms.optim / se_notrans
82 2
    pval  <- pt(abs(tval), rdf, lower.tail = FALSE)
83
  }
84

85 2
  names(se) <- pnames
86

87 2
  param <- cbind(param, se, lci, uci)
88 2
  dimnames(param) <- list(pnames, c("Estimate", "Std. Error", "Lower", "Upper"))
89

90 2
  bparam <- cbind(Estimate = beparms.optim, se_notrans,
91 2
                  "t value" = tval, "Pr(>t)" = pval, Lower = NA, Upper = NA)
92

93
  # Transform boundaries of CI for one parameter at a time,
94
  # with the exception of sets of formation fractions (single fractions are OK).
95 2
  f_names_skip <- character(0)
96 2
  for (box in mod_vars) { # Figure out sets of fractions to skip
97 2
    f_names <- grep(paste("^f", box, sep = "_"), pnames, value = TRUE)
98 2
    n_paths <- length(f_names)
99 2
    if (n_paths > 1) f_names_skip <- c(f_names_skip, f_names)
100
  }
101

102 2
  for (pname in pnames) {
103 2
    if (!pname %in% f_names_skip) {
104 2
      par.lower <- param[pname, "Lower"]
105 2
      par.upper <- param[pname, "Upper"]
106 2
      names(par.lower) <- names(par.upper) <- pname
107 2
      bpl <- backtransform_odeparms(par.lower, object$mkinmod,
108 2
                                            object$transform_rates,
109 2
                                            object$transform_fractions)
110 2
      bpu <- backtransform_odeparms(par.upper, object$mkinmod,
111 2
                                            object$transform_rates,
112 2
                                            object$transform_fractions)
113 2
      bparam[names(bpl), "Lower"] <- bpl
114 2
      bparam[names(bpu), "Upper"] <- bpu
115
    }
116
  }
117 2
  bparam[epnames, c("Lower", "Upper")] <- param[epnames, c("Lower", "Upper")]
118

119 2
  ans <- list(
120 2
    version = as.character(utils::packageVersion("mkin")),
121 2
    Rversion = paste(R.version$major, R.version$minor, sep="."),
122 2
    date.fit = object$date,
123 2
    date.summary = date(),
124 2
    solution_type = object$solution_type,
125 2
    warnings = object$summary_warnings,
126 2
    use_of_ff = object$mkinmod$use_of_ff,
127 2
    error_model_algorithm = object$error_model_algorithm,
128 2
    df = c(p, rdf),
129 2
    covar = covar,
130 2
    covar_notrans = covar_notrans,
131 2
    err_mod = object$err_mod,
132 2
    niter = object$iterations,
133 2
    calls = object$calls,
134 2
    time = object$time,
135 2
    par = param,
136 2
    bpar = bparam)
137

138 2
  if (!is.null(object$version)) {
139 2
    ans$fit_version <- object$version
140 2
    ans$fit_Rversion <- object$Rversion
141 2
    if (ans$fit_version >= "0.9.49.6") {
142 2
      ans$AIC = AIC(object)
143 2
      ans$BIC = BIC(object)
144 2
      ans$logLik = logLik(object)
145
    }
146
  }
147

148 2
  ans$diffs <- object$mkinmod$diffs
149 2
  if(data) ans$data <- object$data
150 2
  ans$start <- object$start
151 2
  ans$start_transformed <- object$start_transformed
152

153 2
  ans$fixed <- object$fixed
154

155 2
  ans$errmin <- mkinerrmin(object, alpha = 0.05)
156

157 2
  if (object$calls > 0) {
158 2
    if (!is.null(ans$covar)){
159 2
      Corr <- cov2cor(ans$covar)
160 2
      rownames(Corr) <- colnames(Corr) <- rownames(ans$par)
161 2
      ans$Corr <- Corr
162
    } else {
163 0
      warning("Could not calculate correlation; no covariance matrix")
164
    }
165
  }
166

167 2
  ans$bparms.ode <- object$bparms.ode
168 2
  ans$shapiro.p <- object$shapiro.p
169 2
  ep <- endpoints(object)
170 2
  if (length(ep$ff) != 0)
171 2
    ans$ff <- ep$ff
172 2
  if (distimes) ans$distimes <- ep$distimes
173 2
  if (length(ep$SFORB) != 0) ans$SFORB <- ep$SFORB
174 0
  if (!is.null(object$d_3_message)) ans$d_3_message <- object$d_3_message
175 2
  class(ans) <- c("summary.mkinfit", "summary.modFit")
176 2
  return(ans)
177
}
178

179
#' @rdname summary.mkinfit
180
#' @export
181
print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), ...) {
182 2
  if (is.null(x$fit_version)) {
183 0
    cat("mkin version:   ", x$version, "\n")
184 0
    cat("R version:      ", x$Rversion, "\n")
185
  } else {
186 2
    cat("mkin version used for fitting:   ", x$fit_version, "\n")
187 2
    cat("R version used for fitting:      ", x$fit_Rversion, "\n")
188
  }
189

190 2
  cat("Date of fit:    ", x$date.fit, "\n")
191 2
  cat("Date of summary:", x$date.summary, "\n")
192

193 2
  cat("\nEquations:\n")
194 2
  nice_diffs <- gsub("^(d.*) =", "\\1/dt =", x[["diffs"]])
195 2
  writeLines(strwrap(nice_diffs, exdent = 11))
196 2
  df  <- x$df
197 2
  rdf <- df[2]
198

199 2
  cat("\nModel predictions using solution type", x$solution_type, "\n")
200

201 2
  cat("\nFitted using", x$calls, "model solutions performed in", x$time[["elapsed"]],  "s\n")
202

203 2
  if (!is.null(x$err_mod)) {
204 2
    cat("\nError model: ")
205 2
    cat(switch(x$err_mod,
206 2
               const = "Constant variance",
207 2
               obs = "Variance unique to each observed variable",
208 2
               tc = "Two-component variance function"), "\n")
209

210 2
    cat("\nError model algorithm:", x$error_model_algorithm, "\n")
211 0
    if (!is.null(x$d_3_message)) cat(x$d_3_message, "\n")
212
  }
213

214 2
  cat("\nStarting values for parameters to be optimised:\n")
215 2
  print(x$start)
216

217 2
  cat("\nStarting values for the transformed parameters actually optimised:\n")
218 2
  print(x$start_transformed)
219

220 2
  cat("\nFixed parameter values:\n")
221 2
  if(length(x$fixed$value) == 0) cat("None\n")
222 2
  else print(x$fixed)
223

224
  # We used to only have one warning - show this for summarising old objects
225 0
   if (!is.null(x[["warning"]])) cat("\n\nWarning:", x$warning, "\n\n")
226

227 2
  if (length(x$warnings) > 0) {
228 0
    cat("\n\nWarning(s):", "\n")
229 0
    cat(x$warnings, sep = "\n")
230
  }
231

232 2
  if (!is.null(x$AIC)) {
233 2
    cat("\nResults:\n\n")
234 2
    print(data.frame(AIC = x$AIC, BIC = x$BIC, logLik = x$logLik,
235 2
      row.names = " "))
236
  }
237

238 2
  cat("\nOptimised, transformed parameters with symmetric confidence intervals:\n")
239 2
  print(signif(x$par, digits = digits))
240

241 2
  if (x$calls > 0) {
242 2
    cat("\nParameter correlation:\n")
243 2
    if (!is.null(x$covar)){
244 2
      print(x$Corr, digits = digits, ...)
245
    } else {
246 0
      cat("No covariance matrix")
247
    }
248
  }
249

250 2
  cat("\nBacktransformed parameters:\n")
251 2
  cat("Confidence intervals for internally transformed parameters are asymmetric.\n")
252 2
  if ((x$version) < "0.9-36") {
253 0
    cat("To get the usual (questionable) t-test, upgrade mkin and repeat the fit.\n")
254 0
    print(signif(x$bpar, digits = digits))
255
  } else {
256 2
    cat("t-test (unrealistically) based on the assumption of normal distribution\n")
257 2
    cat("for estimators of untransformed parameters.\n")
258 2
    print(signif(x$bpar[, c(1, 3, 4, 5, 6)], digits = digits))
259
  }
260

261 2
  cat("\nFOCUS Chi2 error levels in percent:\n")
262 2
  x$errmin$err.min <- 100 * x$errmin$err.min
263 2
  print(x$errmin, digits=digits,...)
264

265 2
  printSFORB <- !is.null(x$SFORB)
266 2
  if(printSFORB){
267 2
    cat("\nEstimated Eigenvalues of SFORB model(s):\n")
268 2
    print(x$SFORB, digits=digits,...)
269
  }
270

271 2
  printff <- !is.null(x$ff)
272 2
  if(printff){
273 2
    cat("\nResulting formation fractions:\n")
274 2
    print(data.frame(ff = x$ff), digits=digits,...)
275
  }
276

277 2
  printdistimes <- !is.null(x$distimes)
278 2
  if(printdistimes){
279 2
    cat("\nEstimated disappearance times:\n")
280 2
    print(x$distimes, digits=digits,...)
281
  }
282

283 2
  printdata <- !is.null(x$data)
284 2
  if (printdata){
285 2
    cat("\nData:\n")
286 2
    print(format(x$data, digits = digits, ...), row.names = FALSE)
287
  }
288

289 2
  invisible(x)
290
}

Read our documentation on viewing source code .

Loading