I have the following data series consisting of a series of measurements taken at monthly intervals.
library(dplyr)
library(flux)
library(ModelMetrics)
date = c("2016-01-10","2016-02-10","2016-03-10","2016-04-10","2016-05-10",
"2016-06-10","2016-07-10","2016-08-10","2016-09-10","2016-10-10",
"2016-11-10","2016-12-10","2017-01-10","2017-02-10","2017-03-10",
"2017-04-10","2017-05-10","2017-06-10","2017-07-10","2017-08-10",
"2017-09-10","2017-10-10","2017-11-10","2017-12-10")
date = as.POSIXlt(date)
value <- c(0.1, 0.6, 0.3, 0.35, -0.37, -0.35, 0.3, 0.1, -0.5, -.6, -0.4, 0.2,
0.1, 0.2, 0.3, 0.35, 0.32, 0.31, 0.28, 0.1, -0.1, -.3, -0.38, 0.15)
df = data.frame(date, value)
df$year <- format(df$date,format="%Y")
where the data looks like:
I need to calculate the total area under the curve for each year that is greater than zero (i.e, only positive or above zero parts of the curve). I have been trying to use the package flux
and the 'thresh
' argument in the function auc
, but cant get it to work. Any help will be appreciated.
df %>%
group_by(year) %>%
summarize(auc = auc(value, thresh=0))