6

I would like to calculate the area between the blue line and the gray diagonal line.

enter image description here

I can calculate the area under the blue line as:

library(zoo)    
id <- order(x)
AUC <- sum(diff(x[id])*rollmean(y[id],2))

(thanks to Calculate the Area under a Curve in R)

Any ideas how to find the area between the blue and gray line?

Thanks in advance EC

Community
  • 1
  • 1
ECII
  • 10,297
  • 18
  • 80
  • 121
  • 1
    I'm missing something here: did you miss the explanations at your link? If your blue[j] and gray[j] are data, just calculate `sum(blue[j]-gray[j])` (not real R code), adjusted by the distance between values (your diff(x[id])). If blue[j] is data and gray is a function, just calculate gray at the x-values in blue. – Carl Witthoft Oct 23 '11 at 19:09
  • The gray line is the diagonal. So should it be something like z<-abs(y-x); id <- order(x); AUC<- sum(diff(x[id])*rollmean(z[id],2)) ; – ECII Oct 23 '11 at 19:39
  • You have to indicate (after thinking about it for a second) whether you want the area to be counted positively both when the blue line is above and below the grey line. – Nick Sabbe Oct 24 '11 at 06:42
  • Both should be counted as positive. Essentially i require the area between the gray and blue line regardless of the side (below or above the gray line). The gray line is always the diagonal (intercept=0, slope=1). – ECII Oct 24 '11 at 10:47

3 Answers3

4

The area between curves is the modulus(area(under blue line) - area(under gray line)) Area under blue line is 1/2(b-a)(f(b)-f(a)) where a and b are Xs of limits and f(a),f(b) are Ys of limits. Finding area under gray line can be done using simpson's rule. see this http://en.wikipedia.org/wiki/Simpson%27s_rule They have given rule as well as implementation + accuracy provided you have arrays of xs and ys.

I hope it helps, Saurabh

Saurabh Ghorpade
  • 1,137
  • 5
  • 15
  • 22
2

Stumbled across this one, it seemed another possible solution, so I'll add it:

library(geiger)
geiger:::.area.between.curves(x, f1, f2)
# x is the vector of x-axis values
# f1 the y-axis values for the first line
# f2 the y-axis values for the second line
RLave
  • 8,144
  • 3
  • 21
  • 37
1

Just to ensure there is an acceptable answer, but mainly summarizing other peoples' suggestions:

'Mathematically', what you want is the area under the curve abs(blue - grey).

These values (i.e. the absolute differences) are easily obtained, and once you have them, you can use any integration-like method (like Simpson's rule or what is suggested in the article you link to) to obtain the surface.

As an alternative, and somewhat like @Saurabh suggested, you can use the fact that the blue line is a step function (or isn't it?) and the other is a straight line. For this, you sum up all x values for which either the blue and grey lines cross, or a new value is presented (a 'step' is taken) in the blue line. Divididing the area like this will lead only to triangles and trapeziums, and you can simply take the absolute value before summing them all up.

Even if the blue line isn't a simple step function, but still made up of straight lines, this should work, although you now have to account for some additional cases...

Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
  • I think I got it using the Simpson's Rule library(Bolstad2); z<-abs(y-x); AUC<-sintegral(x,z)$int; – ECII Oct 24 '11 at 12:07