#' 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)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.