使用列表时将国家假期添加到先知模型
Add country holidays to prophet model when using lists
我希望将美国假期添加到我的数据中,以便使用 Facebook 先知功能进行建模。
我可以在模特试穿时添加一个预定义的假期列表,即 NFL 季后赛和超级碗日期,但似乎国家假期需要在模特试穿之前进行试穿。
从 documentation 可以看出,国家假期已添加到先知对象中,然后进行拟合,但我不确定在使用地图作为需要指定的数据对象 (.x) 时如何执行此操作使用 map
时
如何使用数据框列表将美国假期添加到我的模型中?
到目前为止我的代码是这样的:
#install.packages("pacman")
#library(pacman)
# LOAD LIBRARIES
pacman::p_load(tidyquant,tidyverse,prophet,purrr)
# SPECIFY STOCKS TO PULL
tickers = c("AAPL","AMZN")
# GET STOCK PRICE INFO
getSymbols(tickers,
from = '2013-01-01',
to = '2016-06-30',
warnings = FALSE,
auto.assign = TRUE)
# MAP DATAFRAMES TO A LIST
dfList <- mget(tickers) %>%
map(~ fortify.zoo(.x) %>%
select(1,5) %>%
rename(ds = 1,
y = 2))
# SPECIFY NFL IMPORTANT DATES
playoffs <- tibble(
holiday = 'playoff',
ds = as.Date(c('2008-01-13', '2009-01-03', '2010-01-16',
'2010-01-24', '2010-02-07', '2011-01-08',
'2013-01-12', '2014-01-12', '2014-01-19',
'2014-02-02', '2015-01-11', '2016-01-17',
'2016-01-24', '2016-02-07')),
lower_window = 0,
upper_window = 1
)
superbowls <- tibble(
holiday = 'superbowl',
ds = as.Date(c('2010-02-07','2011-02-06','2012-02-05','2013-02-03','2014-02-02','2015-02-01','2016-02-07','2017-02-05','2018-02-04','2019-02-03','2020-02-02')),
lower_window = 0,
upper_window = 1
)
holidays <- bind_rows(playoffs, superbowls) %>%
filter(ds >= min(dfList$AAPL$ds) & ds <= max(dfList$AAPL$ds))
# CALL THE PROPHET FUNCTION TO FIT THE MODEL
model_list <- map(dfList,
prophet,
holidays = holidays)
# TAKES THE MODEL ABOVE AND THE SPECIFIED FORECAST PERIOD TO PRODUCE A SUITABLE
# DATA FRAME
future_list <- map(model_list,
make_future_dataframe,
periods = 365)
# USE THE GENERIC PREDICT FUNCTION TO GET OUR FORECAST
forecast_list <- map2(model_list,
future_list,
predict)
我们可以使用匿名函数调用(function(x)
简写为~
)
model_list <- map(dfList,
~ {
m <- prophet(holidays = holidays)
m <- add_country_holidays(m, country_name = 'US')
m <- fit.prophet(m, .x)
return(m)
})
一旦我们得到预测,就可以检查组件
pdf("plottesting.pdf")
p1 <- map2(model_list, forecast_list, prophet_plot_components)
dev.off()
我希望将美国假期添加到我的数据中,以便使用 Facebook 先知功能进行建模。
我可以在模特试穿时添加一个预定义的假期列表,即 NFL 季后赛和超级碗日期,但似乎国家假期需要在模特试穿之前进行试穿。
从 documentation 可以看出,国家假期已添加到先知对象中,然后进行拟合,但我不确定在使用地图作为需要指定的数据对象 (.x) 时如何执行此操作使用 map
时如何使用数据框列表将美国假期添加到我的模型中?
到目前为止我的代码是这样的:
#install.packages("pacman")
#library(pacman)
# LOAD LIBRARIES
pacman::p_load(tidyquant,tidyverse,prophet,purrr)
# SPECIFY STOCKS TO PULL
tickers = c("AAPL","AMZN")
# GET STOCK PRICE INFO
getSymbols(tickers,
from = '2013-01-01',
to = '2016-06-30',
warnings = FALSE,
auto.assign = TRUE)
# MAP DATAFRAMES TO A LIST
dfList <- mget(tickers) %>%
map(~ fortify.zoo(.x) %>%
select(1,5) %>%
rename(ds = 1,
y = 2))
# SPECIFY NFL IMPORTANT DATES
playoffs <- tibble(
holiday = 'playoff',
ds = as.Date(c('2008-01-13', '2009-01-03', '2010-01-16',
'2010-01-24', '2010-02-07', '2011-01-08',
'2013-01-12', '2014-01-12', '2014-01-19',
'2014-02-02', '2015-01-11', '2016-01-17',
'2016-01-24', '2016-02-07')),
lower_window = 0,
upper_window = 1
)
superbowls <- tibble(
holiday = 'superbowl',
ds = as.Date(c('2010-02-07','2011-02-06','2012-02-05','2013-02-03','2014-02-02','2015-02-01','2016-02-07','2017-02-05','2018-02-04','2019-02-03','2020-02-02')),
lower_window = 0,
upper_window = 1
)
holidays <- bind_rows(playoffs, superbowls) %>%
filter(ds >= min(dfList$AAPL$ds) & ds <= max(dfList$AAPL$ds))
# CALL THE PROPHET FUNCTION TO FIT THE MODEL
model_list <- map(dfList,
prophet,
holidays = holidays)
# TAKES THE MODEL ABOVE AND THE SPECIFIED FORECAST PERIOD TO PRODUCE A SUITABLE
# DATA FRAME
future_list <- map(model_list,
make_future_dataframe,
periods = 365)
# USE THE GENERIC PREDICT FUNCTION TO GET OUR FORECAST
forecast_list <- map2(model_list,
future_list,
predict)
我们可以使用匿名函数调用(function(x)
简写为~
)
model_list <- map(dfList,
~ {
m <- prophet(holidays = holidays)
m <- add_country_holidays(m, country_name = 'US')
m <- fit.prophet(m, .x)
return(m)
})
一旦我们得到预测,就可以检查组件
pdf("plottesting.pdf")
p1 <- map2(model_list, forecast_list, prophet_plot_components)
dev.off()