1
utils::globalVariables(c("variable", "residual"))
2

3
#' Function to plot residuals stored in an mkin object
4
#'
5
#' This function plots the residuals for the specified subset of the observed
6
#' variables from an mkinfit object. A combined plot of the fitted model and
7
#' the residuals can be obtained using \code{\link{plot.mkinfit}} using the
8
#' argument \code{show_residuals = TRUE}.
9
#'
10
#' @importFrom stats residuals
11
#' @param object A fit represented in an \code{\link{mkinfit}} object.
12
#' @param obs_vars A character vector of names of the observed variables for
13
#'   which residuals should be plotted. Defaults to all observed variables in
14
#'   the model
15
#' @param xlim plot range in x direction.
16
#' @param xlab Label for the x axis.
17
#' @param standardized Should the residuals be standardized by dividing by the
18
#'   standard deviation given by the error model of the fit?
19
#' @param ylab Label for the y axis.
20
#' @param maxabs Maximum absolute value of the residuals. This is used for the
21
#'   scaling of the y axis and defaults to "auto".
22
#' @param legend Should a legend be plotted?
23
#' @param lpos Where should the legend be placed? Default is "topright". Will
24
#'   be passed on to \code{\link{legend}}.
25
#' @param col_obs Colors for the observed variables.
26
#' @param pch_obs Symbols to be used for the observed variables.
27
#' @param frame Should a frame be drawn around the plots?
28
#' @param \dots further arguments passed to \code{\link{plot}}.
29
#' @return Nothing is returned by this function, as it is called for its side
30
#'   effect, namely to produce a plot.
31
#' @author Johannes Ranke
32
#' @seealso \code{\link{mkinplot}}, for a way to plot the data and the fitted
33
#'   lines of the mkinfit object, and \code{\link{plot_res}} for a function
34
#'   combining the plot of the fit and the residual plot.
35
#' @examples
36
#'
37
#' model <- mkinmod(parent = mkinsub("SFO", "m1"), m1 = mkinsub("SFO"))
38
#' fit <- mkinfit(model, FOCUS_2006_D, quiet = TRUE)
39
#' mkinresplot(fit, "m1")
40
#'
41
#' @export
42
mkinresplot <- function (object,
43
  obs_vars = names(object$mkinmod$map),
44
  xlim = c(0, 1.1 * max(object$data$time)),
45
  standardized = FALSE,
46
  xlab = "Time", ylab = ifelse(standardized, "Standardized residual", "Residual"),
47
  maxabs = "auto", legend = TRUE, lpos = "topright",
48
  col_obs = "auto", pch_obs = "auto",
49
  frame = TRUE,
50
  ...)
51
{
52 0
  obs_vars_all <- as.character(unique(object$data$variable))
53

54 0
  if (length(obs_vars) > 0){
55 0
      obs_vars <- intersect(obs_vars_all, obs_vars)
56 0
  } else obs_vars <- obs_vars_all
57

58 0
  if (standardized) {
59 0
    res_col <- "standardized"
60 0
    object$data[[res_col]] <- residuals(object, standardized = TRUE)
61
  } else {
62 0
    res_col <- "residual"
63
  }
64 0
  res <- subset(object$data, variable %in% obs_vars)[res_col]
65

66 0
  if (maxabs == "auto") maxabs = max(abs(res), na.rm = TRUE)
67

68
  # Set colors and symbols
69 0
  if (col_obs[1] == "auto") {
70 0
    col_obs <- 1:length(obs_vars)
71
  }
72

73 0
  if (pch_obs[1] == "auto") {
74 0
    pch_obs <- 1:length(obs_vars)
75
  }
76 0
  names(col_obs) <- names(pch_obs) <- obs_vars
77

78 0
  plot(0, type = "n", frame = frame,
79 0
       xlab = xlab, ylab = ylab,
80 0
       xlim = xlim,
81 0
       ylim = c(-1.2 * maxabs, 1.2 * maxabs), ...)
82

83 0
  for(obs_var in obs_vars){
84 0
    residuals_plot <- subset(object$data, variable == obs_var, c("time", res_col))
85 0
    points(residuals_plot, pch = pch_obs[obs_var], col = col_obs[obs_var])
86
  }
87

88 0
  abline(h = 0, lty = 2)
89

90 0
  if (legend == TRUE) {
91 0
    legend(lpos, inset = c(0.05, 0.05), legend = obs_vars,
92 0
      col = col_obs[obs_vars], pch = pch_obs[obs_vars])
93
  }
94
}

Read our documentation on viewing source code .

Loading