From b4739ba14c5472a23cb3e334d55989f7fbb0afe2 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Mon, 28 Apr 2014 17:59:10 +0200 Subject: Option to add residual plot to plot.mkinfit --- R/plot.mkinfit.R | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'R/plot.mkinfit.R') diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R index 80cf45f4..23eb30fa 100644 --- a/R/plot.mkinfit.R +++ b/R/plot.mkinfit.R @@ -21,13 +21,20 @@ plot.mkinfit <- function(x, fit = x, obs_vars = names(fit$mkinmod$map), xlab = "Time", ylab = "Observed", xlim = range(fit$data$time), - ylim = c(0, max(subset(fit$data, variable %in% obs_vars)$observed, na.rm = TRUE)), + ylim = "default", col_obs = 1:length(fit$mkinmod$map), pch_obs = col_obs, lty_obs = rep(1, length(fit$mkinmod$map)), add = FALSE, legend = !add, + show_residuals = FALSE, maxabs = "auto", lpos = "topright", inset = c(0.05, 0.05), ...) { + if (add && show_residuals) stop("If adding to an existing plot we can not show residuals") + + if (ylim == "default") { + ylim = c(0, max(subset(fit$data, variable %in% obs_vars)$observed, na.rm = TRUE)) + } + solution_type = fit$solution_type parms.all <- c(fit$bparms.optim, fit$bparms.fixed) @@ -52,6 +59,10 @@ plot.mkinfit <- function(x, fit = x, # Set up the plot if not to be added to an existing plot if (add == FALSE) { + if (show_residuals) { + layout(matrix(c(1, 2), 2, 1), heights = c(2, 1.3)) + par(mar = c(3, 4, 4, 2) + 0.1) + } plot(0, type="n", xlim = xlim, ylim = ylim, xlab = xlab, ylab = ylab, ...) @@ -67,4 +78,19 @@ plot.mkinfit <- function(x, fit = x, legend(lpos, inset= inset, legend = obs_vars, col = col_obs[obs_vars], pch = pch_obs[obs_vars], lty = lty_obs[obs_vars]) } + # Show residuals if requested + if (show_residuals) { + par(mar = c(5, 4, 0, 2) + 0.1) + residuals <- subset(fit$data, variable %in% obs_vars, residual) + if (maxabs == "auto") maxabs = max(abs(residuals), na.rm = TRUE) + plot(0, type="n", + xlim = xlim, + ylim = c(-1.2 * maxabs, 1.2 * maxabs), + xlab = xlab, ylab = ylab) + for(obs_var in obs_vars){ + residuals_plot <- subset(fit$data, variable == obs_var, c("time", "residual")) + points(residuals_plot, pch = pch_obs[obs_var], col = col_obs[obs_var]) + } + abline(h = 0, lty = 2) + } } -- cgit v1.2.1