jranke / mkin
1
#' Function to plot the confidence intervals obtained using mkinfit
2
#' 
3
#' This function plots the confidence intervals for the parameters fitted using
4
#' \code{\link{mkinfit}}.
5
#' 
6
#' @param object A fit represented in an \code{\link{mkinfit}} object.
7
#' @return Nothing is returned by this function, as it is called for its side
8
#'   effect, namely to produce a plot.
9
#' @author Johannes Ranke
10
#' @examples
11
#' 
12
#' \dontrun{
13
#' model <- mkinmod(
14
#'   T245 = mkinsub("SFO", to = c("phenol"), sink = FALSE),
15
#'   phenol = mkinsub("SFO", to = c("anisole")),
16
#'   anisole = mkinsub("SFO"), use_of_ff = "max")
17
#' fit <- mkinfit(model, subset(mccall81_245T, soil == "Commerce"), quiet = TRUE)
18
#' mkinparplot(fit)
19
#' }
20
#' @export
21
mkinparplot <- function(object) {
22 1
  state.optim = rownames(subset(object$start, type == "state"))
23 1
  deparms.optim = rownames(subset(object$start, type == "deparm"))
24 1
  fractions.optim = grep("^f_", deparms.optim, value = TRUE)
25 1
  N.optim = grep("^N_", deparms.optim, value = TRUE)
26 0
  if ("g" %in% deparms.optim) fractions.optim <- c("g", fractions.optim)
27 1
  rates.optim.unsorted = setdiff(deparms.optim, union(fractions.optim, N.optim))
28 1
  rates.optim <- rownames(object$start[rates.optim.unsorted, ])
29 1
  n.plot <- c(state.optim = length(state.optim),
30 1
              rates.optim = length(rates.optim),
31 1
	      N.optim = length(N.optim),
32 1
              fractions.optim = length(fractions.optim))
33 1
  n.plot <- n.plot[n.plot > 0]
34

35 1
  oldpar <- par(no.readonly = TRUE)
36 1
  on.exit(par(oldpar, no.readonly = TRUE))
37 1
  layout(matrix(1:length(n.plot), ncol = 1), heights = n.plot + 1)
38

39 1
  s <- summary(object)
40 1
  bpar <- data.frame(t(s$bpar[, c("Estimate", "Lower", "Upper")]))
41 1
  par(mar = c(2.1, 2.1, 0.1, 2.1))
42 1
  par(cex = 1)
43 1
  for (type in names(n.plot)) {
44 1
    parnames <- get(type)
45 1
    values <- bpar[parnames]
46 1
    values_with_confints <- data.frame(t(subset(data.frame(t(values)), !is.na("Lower"))))
47 1
    xlim = switch(type,
48 1
                  state.optim = range(c(0, unlist(values)),
49 1
                                      na.rm = TRUE, finite = TRUE),
50 1
                  rates.optim = range(c(0, unlist(values)),
51 1
                                      na.rm = TRUE, finite = TRUE),
52 1
                  N.optim = range(c(0, 1, unlist(values)),
53 1
                                      na.rm = TRUE, finite = TRUE),
54 1
                  fractions.optim = range(c(0, 1, unlist(values)),
55 1
                                          na.rm = TRUE, finite = TRUE))
56 1
    parname_index <- length(parnames):1 # Reverse order for strip chart
57

58 1
    stripchart(values["Estimate", ][parname_index],
59 1
               xlim = xlim,
60 1
               ylim = c(0.5, length(get(type)) + 0.5),
61 1
               yaxt = "n")
62 1
    if (type %in% c("rates.optim", "fractions.optim")) abline(v = 0, lty = 2)
63 0
    if (type %in% c("N.optim", "fractions.optim")) abline(v = 1, lty = 2)
64 1
    position <- ifelse(values["Estimate", ] < mean(xlim), "right", "left")
65 1
    text(ifelse(position == "left", min(xlim), max(xlim)),
66 1
         parname_index, parnames,
67 1
         pos = ifelse(position == "left", 4, 2))
68

69 1
    values.upper.nonInf <- ifelse(values["Upper", ] == Inf, 1.5 * xlim[[2]], values["Upper", ])
70
    # Suppress warnings for non-existing arrow lengths
71 1
    suppressWarnings(arrows(as.numeric(values["Lower", ]), parname_index,
72 1
	   as.numeric(values.upper.nonInf), parname_index,
73 1
	   code = 3, angle = 90, length = 0.05))
74
  }
75
}

Read our documentation on viewing source code .

Loading