Reputation: 131
I have a curve as the data below and I would like to calculate the area under the curve (integral) of this curve made by x and y points. Is it correct to do in the way below? and how can I calculate the area under the curve within a specific interval without knowing the function of curve? I am going to repeat this with the rest of my data for each individual with repeated measurements. Also, I need to calculate the area under the curve for the first and second derivative values. should I do the same? Thank you for your advice.
structure(list(x = c("76.37", "76.89", "77.36", "77.86", "78.86",
"79.37", "79.87", "80.36", "80.5", "81.2"),
y = c("2.1", "2.29", "2.47", "2.65", "2.82", "2.99", "3.16", "3.31", "3.46", "3.5")),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,-10L))
library(pracma)
AUC = trapz(data$x,data$y)
Upvotes: 1
Views: 646
Reputation: 79228
You could approximate the function thus use approxfun
with a linear interpolation
integrate(approxfun(df$x, df$y), min(df$x), max(df$x))
13.78885 with absolute error < 0.00049
Note that a rough approximation of the area yields a number closer to this. This assumes the region is trapezoidal.
prod(sapply(df, \(x)diff(range(x))))/2 + diff(range(df$x)) * min(df$y)
[1] 13.524
Upvotes: 1