-1

I have two columns X1 ,X2 .I want to find area under X1 and area under X2.

X = c(1,2,3,4,5)
Y1 = c(2,3,4,2,3)
Y2 = c(6,6,6,6,6)

plot(X,Y1) #Need to get area under this curve i.e auc(Y1)
plot(X,Y2) #Need to get area under this curve i.e auc(Y2)

I need this so as to I need to compare both areas i.e Y1 and Y2 by taking the ratio AUC(Y1)/AUC(Y2)

I need commulative area at each point.

Praveen Chougale
  • 373
  • 3
  • 11

1 Answers1

5

Assuming that by auc(Y1) you mean the area under the curve seen in

plot(1:5,Y1,type = "l")

you can just use the trapezoidal rule, which with step size 1 can be computed like thus:

auc <- function(y){
  n <- length(y)
  0.5*(y[1]+y[n]+2*sum(y[-c(1,n)]))
}

For example:

> auc(Y1)
[1] 11.5
John Coleman
  • 51,337
  • 7
  • 54
  • 119