| 1 | ## File: createCalendar.r |
| 2 | ## Description: Create the calendar transitions to be used on KWF predictor |
| 3 | ## Author: jc @ September 2015 |
| 4 | |
| 5 | ## Function: irishCalendar |
| 6 | ## Description: Create the calendar with day of the week and daytype. |
| 7 | ## (It is the first step, then we need to obtain the transitions) |
| 8 | irishCalendar <- function(from, to){ |
| 9 | require(timeDate) |
| 10 | y0 <- as.character(substr(from, 1, 4)) |
| 11 | y1 <- as.character(substr(to, 1, 4)) |
| 12 | years <- y0:y1 |
| 13 | IRholidays <- c("NewYearsDay", |
| 14 | #saint patrick |
| 15 | "EasterMonday", |
| 16 | "GBMayDay", # may day |
| 17 | #june holiday |
| 18 | "GBSummerBankHoliday", # august holiday |
| 19 | #october holiday |
| 20 | "ChristmasDay" |
| 21 | #, st stephen day |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | jf <- holiday(year = years, Holiday = IRholidays) |
| 26 | jf <- sort(unique(jf)) |
| 27 | dts <- timeSequence(from = paste(y0, '01', '01', sep = '-'), |
| 28 | to = paste(y1, '12', '31', sep = '-')) |
| 29 | wday <- dayOfWeek(dts) |
| 30 | wdayf <- unname(wday) |
| 31 | for(d in 1:length(wdayf)){ |
| 32 | if(d == 1) fpos <- 1 |
| 33 | if(dts[d] == jf[fpos]){ |
| 34 | wdayf[d] <- "Hol" |
| 35 | if(fpos < length(jf)) fpos <- fpos + 1 |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | res <- data.frame(dow = wday, daytype = wdayf) |
| 40 | i0 <- which(rownames(res) == from) |
| 41 | i1 <- which(rownames(res) == to) |
| 42 | |
| 43 | return(res[i0:i1, ]) |
| 44 | } |
| 45 | |
| 46 | |
| 47 | # Call irishCalendar to create the calendar. |
| 48 | myCal <- irishCalendar(from="2011-01-01", to= "2013-12-31") |
| 49 | n <- nrow(myCal) |
| 50 | |
| 51 | # Obtention of the transitions |
| 52 | gr <- paste(myCal$daytype[1:(n - 1)], myCal$daytype[2:n], sep = '_') |
| 53 | gr[gr == "Hol_Hol"] <- "Sun_Hol" |
| 54 | myCal$gr <- c(gr, "Sat_Sun") |
| 55 | |
| 56 | # Write output to a file |
| 57 | # write.table(file = "calendar_ir.txt", myCal) |
| 58 | |