Skip to content
This repository has been archived by the owner on Dec 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #18 from CDU-data-science-team/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ChrisBeeley authored Jul 25, 2021
2 parents ffbf03e + 59ba13f commit ca22325
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 14 deletions.
54 changes: 41 additions & 13 deletions R/data_and_forecast.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@

# data and forecasts

make_tsibble <- function(data, frequency = "Daily"){
make_tsibble <- function(data, frequency = "Daily", remove_weekends = FALSE){

if(frequency == "Weekly"){

data <- data %>%
dplyr::mutate(Date = lubridate::floor_date(Date, "week"),
Date = tsibble::yearweek(Date))
return(
data %>%
dplyr::mutate(Date = lubridate::floor_date(Date, "week"),
Date = tsibble::yearweek(Date)) %>%
dplyr::filter(Total_Qty >= 0) %>%
dplyr::group_by(Date) %>%
dplyr::summarise(quantity = sum(Total_Qty, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
head(-1) %>% # remove the last row in case it isn't a complete week
tsibble::tsibble(index = Date) %>%
tsibble::fill_gaps(quantity = 0)
)

} else {

if(remove_weekends){

return(
data %>%
dplyr::filter(Total_Qty >= 0,
!lubridate::wday(Date, label = TRUE) %in% c("Sat", "Sun")) %>%
dplyr::group_by(Date) %>%
dplyr::summarise(quantity = sum(Total_Qty, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
dplyr::mutate(observation = dplyr::row_number()) %>%
tsibble::tsibble(key = observation) %>%
tsibble::fill_gaps(quantity = 0)
)
} else {

return(
data %>%
dplyr::filter(Total_Qty >= 0) %>%
dplyr::group_by(Date) %>%
dplyr::summarise(quantity = sum(Total_Qty, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
tsibble::tsibble(index = Date) %>%
tsibble::fill_gaps(quantity = 0)
)
}
}

data %>%
dplyr::filter(Total_Qty >= 0) %>%
dplyr::group_by(Date) %>%
dplyr::summarise(quantity = sum(Total_Qty, na.rm = TRUE)) %>%
dplyr::ungroup() %>%
head(-1) %>% # remove the last row in case it isn't a complete week
tsibble::tsibble(index = Date) %>%
tsibble::fill_gaps(quantity = 0)
}

forecast_series <- function(data, horizon, frequency = "Daily"){
Expand Down
45 changes: 45 additions & 0 deletions R/data_prep_functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Calculate mode
#'
#' @description Funnily enough, R doesn't have a mode, so this is it.
#' Be careful because ties just return whichever is the furthest to the
#' beginning of the vector with no warning given
#'
#' @param v Vector of numerical values
#' @return number- mode of the supplied vector
#' @export

calc_mode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}

#' Get holidays
#'
#' @description Download English bank and other holidays from gov.uk
#'
#' @return vector of dates as string YYYY-MM-DD
#' @export
get_holidays <- function(){

jsonlite::fromJSON(
"https://www.gov.uk/bank-holidays.json")$`england-and-wales`$events$date
}

#' return number of weekdays
#'
#' @description return the number of weekdays between two days, excluding
#' weekends (obviously) and holidays (user defined)
#' @param from Date. Date to count from (inclusive)
#' @param to Date. Date to count to (inclusive)
#' @param holidays. vector of dates as string YYYY-MM-DD. You can make this
#' (for England) with \code{\link{get_holidays}}
#'
#' @return integer. Number of weekdays between two dates
#' @export

n_weekdays <- function(from, to, holidays) {

possible_days <- seq(from, to, "days")
# Count all days that are not weekend and are not holidays
sum(!weekdays(possible_days) %in% c("Saturday", "Sunday") & !possible_days %in% holidays)
}
Binary file removed data/simulated_forecast.rda
Binary file not shown.
19 changes: 19 additions & 0 deletions man/calc_mode.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/forecast_series.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/get_holidays.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/make_tsibble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/n_weekdays.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/plot_forecast.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/show_accuracy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-data_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("Data produced correctly", {

weekly_data <- make_tsibble(test_data, frequency = "Weekly")

testthat::expect_equal(nrow(daily_data), 2312)
testthat::expect_equal(nrow(daily_data), 2313)

testthat::expect_equal(nrow(weekly_data), 330)

Expand Down

0 comments on commit ca22325

Please sign in to comment.