From df916b91c90f80e2c68f3e136a7b6e07b8c6bae1 Mon Sep 17 00:00:00 2001 From: jranke Date: Mon, 26 Nov 2012 23:03:19 +0000 Subject: - Added an S3 plot method for mkinfit models - Fixed a bug in the mkinfit summary method which crashed on missing covariance matrices git-svn-id: svn+ssh://svn.r-forge.r-project.org/svnroot/kinfit/pkg/mkin@58 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- DESCRIPTION | 4 +-- R/mkinfit.R | 2 +- R/mkinplot.R | 38 ++--------------------------- R/plot.mkinfit.R | 45 ++++++++++++++++++++++++++++++++++ man/mkinplot.Rd | 34 +++----------------------- man/plot.mkinfit.Rd | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 70 deletions(-) create mode 100644 R/plot.mkinfit.R create mode 100644 man/plot.mkinfit.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8875c6e..494793c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Package: mkin Type: Package Title: Routines for fitting kinetic models with one or more state variables to chemical degradation data -Version: 0.9-07 -Date: 2012-09-20 +Version: 0.9-08 +Date: 2012-11-26 Author: Johannes Ranke, Katrin Lindenberger, René Lehmann Maintainer: Johannes Ranke Description: Calculation routines based on the FOCUS Kinetics Report (2006). diff --git a/R/mkinfit.R b/R/mkinfit.R index 0215596..3649b20 100644 --- a/R/mkinfit.R +++ b/R/mkinfit.R @@ -317,7 +317,7 @@ print.summary.mkinfit <- function(x, digits = max(3, getOption("digits") - 3), . print(x$SFORB, digits=digits,...) } - printcor <- !is.null(x$cov.unscaled) + printcor <- is.numeric(x$cov.unscaled) if (printcor){ Corr <- cov2cor(x$cov.unscaled) rownames(Corr) <- colnames(Corr) <- rownames(x$par) diff --git a/R/mkinplot.R b/R/mkinplot.R index 546c506..b9becfd 100644 --- a/R/mkinplot.R +++ b/R/mkinplot.R @@ -1,38 +1,4 @@ -mkinplot <- function(fit, xlab = "Time", ylab = "Observed", xlim = range(fit$data$time), ylim = range(fit$data$observed, na.rm = TRUE), legend = TRUE, ...) +mkinplot <- function(fit, ...) { - solution_type = fit$solution_type - fixed <- fit$fixed$value - names(fixed) <- rownames(fit$fixed) - parms.all <- c(fit$parms.all, fixed) - ininames <- c( - rownames(subset(fit$start, type == "state")), - rownames(subset(fit$fixed, type == "state"))) - odeini <- parms.all[ininames] - names(odeini) <- names(fit$mkinmod$diffs) - - outtimes <- seq(xlim[1], xlim[2], length.out=100) - - odenames <- c( - rownames(subset(fit$start, type == "deparm")), - rownames(subset(fit$fixed, type == "deparm"))) - odeparms <- parms.all[odenames] - - out <- mkinpredict(fit$mkinmod, odeparms, odeini, outtimes, - solution_type = solution_type, atol = fit$atol, rtol = fit$rtol, ...) - - # Plot the data and model output - plot(0, type="n", - xlim = xlim, ylim = ylim, - xlab = xlab, ylab = ylab, ...) - col_obs <- pch_obs <- lty_obs <- 1:length(fit$mkinmod$map) - names(col_obs) <- names(pch_obs) <- names(lty_obs) <- names(fit$mkinmod$map) - for (obs_var in names(fit$mkinmod$map)) { - points(subset(fit$data, variable == obs_var, c(time, observed)), - pch = pch_obs[obs_var], col = col_obs[obs_var]) - } - matlines(out$time, out[-1]) - if (legend == TRUE) { - legend("topright", inset=c(0.05, 0.05), legend=names(fit$mkinmod$map), - col=col_obs, pch=pch_obs, lty=lty_obs) - } + plot(fit, ...) } diff --git a/R/plot.mkinfit.R b/R/plot.mkinfit.R new file mode 100644 index 0000000..1858aa8 --- /dev/null +++ b/R/plot.mkinfit.R @@ -0,0 +1,45 @@ +plot.mkinfit <- function(x, fit = x, + xlab = "Time", ylab = "Observed", + xlim = range(fit$data$time), ylim = range(fit$data$observed, na.rm = TRUE), + col_obs = 1:length(fit$mkinmod$map), + pch_obs = col_obs, lty_obs = rep(1, length(fit$mkinmod$map)), + add = FALSE, legend = !add, ...) +{ + solution_type = fit$solution_type + fixed <- fit$fixed$value + names(fixed) <- rownames(fit$fixed) + parms.all <- c(fit$parms.all, fixed) + ininames <- c( + rownames(subset(fit$start, type == "state")), + rownames(subset(fit$fixed, type == "state"))) + odeini <- parms.all[ininames] + names(odeini) <- names(fit$mkinmod$diffs) + + outtimes <- seq(xlim[1], xlim[2], length.out=100) + + odenames <- c( + rownames(subset(fit$start, type == "deparm")), + rownames(subset(fit$fixed, type == "deparm"))) + odeparms <- parms.all[odenames] + + out <- mkinpredict(fit$mkinmod, odeparms, odeini, outtimes, + solution_type = solution_type, atol = fit$atol, rtol = fit$rtol, ...) + + # Set up the plot if not to be added to an existing plot + if (add == FALSE) { + plot(0, type="n", + xlim = xlim, ylim = ylim, + xlab = xlab, ylab = ylab, ...) + } + # Plot the data and model output + names(col_obs) <- names(pch_obs) <- names(lty_obs) <- names(fit$mkinmod$map) + for (obs_var in names(fit$mkinmod$map)) { + points(subset(fit$data, variable == obs_var, c(time, observed)), + pch = pch_obs[obs_var], col = col_obs[obs_var]) + } + matlines(out$time, out[-1], col = col_obs, lty = lty_obs) + if (legend == TRUE) { + legend("topright", inset=c(0.05, 0.05), legend=names(fit$mkinmod$map), + col=col_obs, pch=pch_obs, lty=lty_obs) + } +} diff --git a/man/mkinplot.Rd b/man/mkinplot.Rd index b72675a..4b0fef4 100644 --- a/man/mkinplot.Rd +++ b/man/mkinplot.Rd @@ -4,50 +4,22 @@ Plot the observed data and the fitted model of an mkinfit. } \description{ - Solves the differential equations with the optimised and fixed parameters - from a previous successful call to \code{\link{mkinfit}} and plots - the observed data together with the numerical solution of the fitted model. + Deprecated function. It now only calls the plot method \code{\link{plot.mkinfit}}. } \usage{ - mkinplot(fit, xlab = "Time", ylab = "Observed", - xlim = range(fit$data$time), ylim = range(fit$data$observed, na.rm=TRUE), - legend = TRUE, ...) + mkinplot(fit, ...) } \arguments{ \item{fit}{ an object of class \code{\link{mkinfit}}. } - \item{xlab}{ - label for the x axis. - } - \item{ylab}{ - label for the y axis. - } - \item{xlim}{ - plot range in x direction. - } - \item{ylim}{ - plot range in y direction. - } - \item{legend}{ - legend specifying if a legend should be included in the plot. - } \item{\dots}{ - further arguments passed to \code{\link{plot}}. + further arguments passed to \code{\link{plot.mkinfit}}. } } \value{ The function is called for its side effect. } -\examples{ -# One parent compound, one metabolite, both single first order. -SFO_SFO <- mkinmod( - parent = list(type = "SFO", to = "m1", sink = TRUE), - m1 = list(type = "SFO")) -# Fit the model to the FOCUS example dataset D using defaults -fit <- mkinfit(SFO_SFO, FOCUS_2006_D) -\dontrun{mkinplot(fit)} -} \author{ Johannes Ranke } diff --git a/man/plot.mkinfit.Rd b/man/plot.mkinfit.Rd new file mode 100644 index 0000000..01b8cec --- /dev/null +++ b/man/plot.mkinfit.Rd @@ -0,0 +1,70 @@ +\name{plot.mkinfit} +\alias{plot.mkinfit} +\title{ + Plot the observed data and the fitted model of an mkinfit object. +} +\description{ + Solves the differential equations with the optimised and fixed parameters + from a previous successful call to \code{\link{mkinfit}} and plots + the observed data together with the numerical solution of the fitted model. +} +\usage{ +\method{plot}{mkinfit}(x, fit = x, xlab = "Time", ylab = "Observed", + xlim = range(fit$data$time), ylim = range(fit$data$observed, na.rm = TRUE), + col_obs = 1:length(fit$mkinmod$map), pch_obs = col_obs, lty_obs = 1, + add = FALSE, legend = !add, ...) +} +\arguments{ + \item{x}{ + Alias for fit introduced for compatibility with the generic S3 method. +} + \item{fit}{ + an object of class \code{\link{mkinfit}}. +} + \item{xlab}{ + label for the x axis. + } + \item{ylab}{ + label for the y axis. + } + \item{xlim}{ + plot range in x direction. + } + \item{ylim}{ + plot range in y direction. + } + \item{col_obs}{ + colors used for plotting the observed data and the corresponding model prediction lines. +} + \item{pch_obs}{ + symbols to be used for plotting the data. +} + \item{lty_obs}{ + line types to be used for the model predictions. +} + \item{add}{ + should the plot be added to an existing plot? +} + \item{legend}{ + should a legend be added to the plot? +} + \item{\dots}{ + further arguments passed to \code{\link{plot}}. +} +} +\value{ + The function is called for its side effect. +} +\examples{ +# One parent compound, one metabolite, both single first order. +SFO_SFO <- mkinmod( + parent = list(type = "SFO", to = "m1", sink = TRUE), + m1 = list(type = "SFO")) +# Fit the model to the FOCUS example dataset D using defaults +fit <- mkinfit(SFO_SFO, FOCUS_2006_D) +\dontrun{plot(fit)} +} +\author{ + Johannes Ranke +} +\keyword{ hplot } -- cgit v1.2.1