Skip to content

Commit

Permalink
version 0.1-6
Browse files Browse the repository at this point in the history
  • Loading branch information
Claus Ekstrom authored and gaborcsardi committed Jun 25, 2011
1 parent 5db7315 commit ea5f00f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
@@ -1,5 +1,5 @@
Package: kulife
Version: 0.1-5
Version: 0.1-6
Title: Data sets and functions from the Faculty of Life Sciences,
University of Copenhagen
Author: Claus Ekstrom <ekstrom@life.ku.dk>, Ib M. Skovgaard
Expand All @@ -9,6 +9,6 @@ Enhances: XML
Description: Provides various functions and data sets from experiments
at the Faculty of Life Sciences, University of Copenhagen
License: GPL-2
Packaged: 2011-04-11 19:17:53 UTC; claus
Packaged: 2011-06-25 23:32:25 UTC; claus
Repository: CRAN
Date/Publication: 2011-04-12 06:22:23
Date/Publication: 2011-06-26 05:25:08
13 changes: 13 additions & 0 deletions R/auc.r
@@ -0,0 +1,13 @@
auc <- function(x, y, from=min(x), to=max(x), ...) {

if (length(x) != length(y))
stop("x and y must have the same length")

if (length(unique(x))<2)
return(NA)

values <- approx(x, y, xout=sort(unique(c(from, to, x))),...)

res <- .5*sum(diff(values$x)*(values$y[-1]+values$y[-length(values$y)]))
res
}
73 changes: 73 additions & 0 deletions man/auc.Rd
@@ -0,0 +1,73 @@
\name{auc}
\alias{auc}
\title{
Compute the area under the curve for two vectors.
}
\description{
Compute the area under the curve using linear interpolation for two
vectors where one corresponds to the x values and the other
corresponds to the y values.
}
\usage{
auc(x, y, from = min(x), to = max(x), ...)
}
\arguments{
\item{x}{a numeric vector of x values.
}
\item{y}{a numeric vector of y values of the same length as x.
}
\item{from}{The value from where to start calculating the area under
the curve. Defaults to the smallest x value.
}
\item{to}{
The value from where to end the calculation of the area under
the curve. Defaults to the smallest y value.
}
\item{\dots}{
additional arguments passed on to approxfun. In particular rule can be
set to determine how values outside the range of x is handled.
}
}
\details{
auc is implemented using the approx function together with
the composite trapezoid rule. approx creates a function that performs the linear
interpolation between points and the trapezoid rule calculates the numerical
integral, and by combining these we can handle unsorted time values,
missing observations, ties for the time values, and integrating over
part of the area or even outside the area.
}
\value{The value of the area under the curve.
}
%\references{
%% ~put references to the literature/web site here ~
%}
\author{
Claus Ekstrom \email{ekstrom@life.ku.dk}
}
%\note{
%% ~~further notes~~
%}

%% ~Make other sections like Warning with \section{Warning }{....} ~

\seealso{
\code{\link{approxfun}} and \code{\link{integrate}}
}
\examples{
x <- 1:4
y <- c(0, 1, 1, 5)
auc(x, y)

# AUC from 0 to max(x) where we allow for extrapolation
auc(x, y, from=0, rule=2)

# Use value 0 to the left
auc(x, y, from=0, rule=2, yleft=0)

# Use 1/2 to the left
auc(x, y, from=0, rule=2, yleft=.5)
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ ~kwd1 }
\keyword{ ~kwd2 }% __ONLY ONE__ keyword per line

0 comments on commit ea5f00f

Please sign in to comment.