R: Area Under the Curve

AUC {DescTools} | R Documentation

## Area Under the Curve

### Description

Calculate the area under the curve with a naive algorithm and with a more elaborated spline approach. The curve must be given by vectors of xy-coordinates.

### Usage

    AUC(x, y, method = c("trapezoid", "step", "spline"), na.rm = FALSE)

### Arguments

`x, y` | the xy-points of the curve
`method` | can be `"trapezoid"` (default), `"step"` or `"spline"`.
`na.rm` | logical, indicating whether `NA` values should be stripped before the computation proceeds. In this case only the complete.cases of x and y will be used. `na.rm` defaults to `FALSE`.

### Details

If method is set to `"trapezoid"` then the curve is formed by connecting all points by a direct line (composite trapezoid rule). If `"step"` is chosen then a stepwise connection of two points is used. For calculating the area under a spline interpolation the `splinefun` function is used in combination with `integrate`.
The AUC function will handle unsorted x values (by sorting x) and ties for the x values (by ignoring duplicates).

### Value

Numeric value of the area under the curve.

### Author(s)

Andri Signorell <andri@signorell.net>, spline part by Claus Ekstrom <claus@rprimer.dk>

### See Also

`integrate`, `splinefun`

### Examples

    AUC(x=c(1,3), y=c(1,1))

    AUC(x=c(1,2,3), y=c(1,2,4), method="trapezoid")
    AUC(x=c(1,2,3), y=c(1,2,4), method="step")

    plot(x=c(1,2,2.5), y=c(1,2,4), type="l", col="blue", ylim=c(0,4))
    lines(x=c(1,2,2.5), y=c(1,2,4), type="s", col="red")

    x <- seq(0, pi, length.out=200)
    AUC(x=x, y=sin(x))
    AUC(x=x, y=sin(x), method="spline")

[Package DescTools version 0.99.28 Index]