Skip to content

Instantly share code, notes, and snippets.

@lbui30
Last active November 28, 2022 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbui30/1b60f2f1ff4d391ca057700ac0d703f8 to your computer and use it in GitHub Desktop.
Save lbui30/1b60f2f1ff4d391ca057700ac0d703f8 to your computer and use it in GitHub Desktop.
My full reproducible code
#library(tidyverse)
library(config)
library(DBI)
library(odbc)
library(tibble)
library(dplyr)
library(tidyr)
library(lubridate)
library(tsibble)
library(readr)
library(feasts)
library(fable)
library(prophet)
library(fable.prophet)
library(reprex)
# testtraindata <- ts2 %>%
# select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
# testmodel <- model(.safely=TRUE,.data = testtraindata,
# prophetb = prophet(callvolume ~
# holiday(myholidays,prior_scale = 30))
# )
# saveRDS(ts2,"ts2.RDS")
# saveRDS(myholidays,"myholidays.RDS")
# saveRDS(testmodel,"testmodel.RDS")
mysqlconnect <- function(driver,server,database,user,password){
# odbc::odbcListDrivers() #To find drivers uncomment and run
con <- DBI::dbConnect(odbc::odbc(),
Driver = driver,
Server = server,
dbname = database,
UID = user,
PWD = password,
Port = 3306)
return(con)
}
create_holidays <- function(train_data) {
myconfig = config::get(config="mysql",file = "C:/Lechi/Automation/secrets/config.yml")
mysqlconn = mysqlconnect(myconfig$driver,myconfig$server,myconfig$database,
myconfig$username,myconfig$password)
res <- dbSendQuery(mysqlconn, "SELECT * FROM datawarehouse.extraholidays")
extraholidays <- dbFetch(res)
dbClearResult(res)
DBI::dbDisconnect(mysqlconn)
myyears <- train_data$incident_date%>% lubridate::year()
(myholidays <- prophet:::make_holidays_df(myyears,"US") %>%# dplyr::mutate(index=ds) %>%
dplyr::mutate(holiday=as.character(holiday)) %>%
dplyr::mutate(holiday=forcats::as_factor(holiday)) %>%
dplyr::mutate(
holiday=forcats::fct_relabel(holiday,
~ gsub(" ",".",gsub("[[:punct:]]+","",gsub(" \\(Observed\\)", "", .x)))
)) %>% dplyr::mutate(mymonth=months(ds)))#%>%
mycalander <- extraholidays %>%
mutate(year = lubridate::year(ds)) %>%
dplyr::filter(year %in% unique(myyears)) %>%
dplyr::filter(OnAFedHoliday == 'no') %>%
dplyr::select(!tidyr::any_of(c("Note","key","year","month","day","OnAFedHoliday"))) %>%
dplyr::distinct() %>%
dplyr::mutate(ds = lubridate::ymd(ds))
myholidays <- bind_rows(myholidays %>%
dplyr::select(!mymonth),mycalander) %>%
dplyr::mutate(ds=lubridate::round_date(ds, unit = 'week')) %>% distinct()
return(myholidays)
}
#' This errors out
library(config)
library(DBI)
library(odbc)
library(tibble)
library(dplyr)
library(readr)
library(feasts)
library(fable)
library(prophet)
library(fable.prophet)
library(reprex)
myprepfunction <- function() {
ts2 <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/ts2.RDS?raw=true"))
testmodel <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/testmodel.RDS?raw=true"))
sampledata <- ts2 %>% select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
# simplifying for reprex....
# myholidays <- create_holidays(sampledata)
myholidays <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/myholidays.RDS?raw=true"))
myforcast <- testmodel %>%
forecast(h = "4 weeks")
return(myforcast)
}
forecast = myprepfunction()
#' But this does not
ts2 <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/ts2.RDS?raw=true"))
testmodel <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/testmodel.RDS?raw=true"))
sampledata <- ts2 %>% select(!any_of(c("ds"))) %>% filter(station_id %in% c("04"))
myholidays <- readRDS(url("https://github.com/lbui30/reprexdata/blob/main/myholidays.RDS?raw=true"))
myforcast <- testmodel %>%
forecast(h = "4 weeks")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment