#' Remove sin-curve fit over a year
#' @param dates vector of dates of TS
#' @param values vector of values of TS
#' @param max.p.value p.value up to which sin-curve is removed
#' @return vector vector of values
#' @export
remove_yearly_sin_fit <- function(dates, values, max.p.value = 0.1){
timeline <- data.frame(Date = seq.Date(from = min(dates), to = max(dates), by = "day"))
df.here <- timeline %>% left_join(
data.frame(Date = dates, Value = values)) %>% mutate(helper = 1) %>%
mutate(t = cumsum(helper)) %>% select(-helper)
models <- list()
for(i in 1:6){
df.use <- df.here
df.use$t <- df.here$t + i * (365.5/12)
df.use %<>% na.omit
models[[i]] <- lm(Value ~ sin(2*pi/365.5*t), data = df.use)
}
take <- map(models, AIC) %>% which.min
if(summary(models[[take]])$coefficients[2,4] > max.p.value){
return(values) } else {
res_plus_intercept = models[[take]]$residuals + models[[take]]$coefficients[1]
return(res_plus_intercept)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.