Learn R Programming

bayestestR (version 0.2.0)

area_under_curve: Area under the Curve (AUC)

Description

Based on the DescTools AUC function. It calculates 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

area_under_curve(x, y, method = c("trapezoid", "step", "spline"), ...)

auc(x, y, method = c("trapezoid", "step", "spline"), ...)

Arguments

x

Vector of x values.

y

Vector of y values.

method

Can be "trapezoid" (default), "step" or "spline".

...

Arguments passed to or from other methods.

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).

See Also

DescTools

Examples

Run this code
# NOT RUN {
library(bayestestR)
posterior <- distribution_normal(1000)

dens <- as.data.frame(density(posterior))
dens <- dens[dens$x > 0, ]
x <- dens$x
y <- dens$y

area_under_curve(x, y, method = "trapezoid")
area_under_curve(x, y, method = "step")
area_under_curve(x, y, method = "spline")
# }

Run the code above in your browser using DataLab