R/remove_week.of.the.month_dummy_fit.R

Defines functions remove_week.of.month_dummy_fit

#' Removes mean-differences between first-, middle- and last-weeks of the months
#' @param dates vector of dates of TS
#' @param values vector of F-Value above which differences will be removed
#' @return vector of values
#' @export
remove_week.of.month_dummy_fit <- function(dates, values, min.F.value = 8){
  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(Day = day(Date), Week = week(Date), Month = month(Date), Year = year(Date)) %>%
    group_by(Month, Year) %>%
    mutate(weekclass = ifelse(Week == min(Week), "first",
                              ifelse(Week == max(Week), "last", "middle"))) %>%
    na.omit()
  df.here$weekclass <- as.factor(df.here$weekclass)
  model <- lm(Value ~ weekclass, data = df.here)

  if(summary(model)$fstatistic[1] < min.F.value){
    return(values) } else {
      res_plus_intercept = model$residuals + model$coefficients[1]
      return(res_plus_intercept)
    }
}
td-berlin/anomalizer documentation built on Feb. 21, 2020, 2:03 a.m.