The AUC is quite easily approximated by looking at the set of trapezoid figures, each time connected between x_i , x_{i+1} , y{i+1} and y_i . Using the rollmean of the zoo package, you can do:
library(zoo) x <- 1:10 y <- 3*x+25 id <- order(x) AUC <- sum(diff(x[id])*rollmean(y[id],2))
Make sure you order x values, or your result does not make sense. If you have negative values โโsomewhere along the y axis, you need to figure out exactly how you want to determine the area under the curve and adjust accordingly (for example, using abs() )
As for your follow-up: if you don't have an official function, how would you build it? Therefore, if you have only values, the only thing you can approximate is a certain integral. Even if you have a function in R, you can only calculate certain integrals with integrate() . The construction of a formal function is possible only if you can also define it.
Joris Meys Feb 10 '11 at 9:07 2011-02-10 09:07
source share