Hi,
I am trying to forecast Sales for next 5 years and I am also considering five other factors for now. The list of factors might increase later where I will try to find and use best 5 factors. But I am trying to figure out an efficient way to forecast these factors first as that is required before I can perform forecasting for Sales. As of now I was forecasting one factor at a time. But I would like to be able to forecast all the factors at once, probably using a function, or some other method that I am not aware of. Below was my trial on creating a function to do the task. But its not working the way I would like and throwing errors. Any help would be appreciated here.
Sample data --- Original data starts from 1996
#Libraries
library(tidyverse)
library(lubridate)
library(tsibble)
library(fable)
# Sample Data
df <- data.frame(
check.names = FALSE,
date = c("2019-01-01","2019-02-01","2019-03-01",
"2019-04-01","2019-05-01","2019-06-01","2019-07-01",
"2019-08-01","2019-09-01","2019-10-01",
"2019-11-01","2019-12-01","2020-01-01",
"2020-02-01","2020-03-01","2020-04-01",
"2020-05-01","2020-06-01","2020-07-01",
"2020-08-01","2020-09-01","2020-10-01","2020-11-01",
"2020-12-01"),
Sales = c(7183,
6610,8206,8565,9670,9787,9115,11883,
9466,9862,6275,9298,9010,8944,7036,2783,
5497,8595,7792,8965,12561,11450,8756,
9473),
`Factor 1` = c(19070,
20301,19056,18915,19546,20399,20388,
19126,18174,17104,17536,15443,15488,16651,
14724,15082,14371,14416,14865,14525,
13887,15968,18994,25454),
`Factor 2` = c(20364,
20545,20109,20080,21193,22256,22755,
25551,27215,28535,27251,28197,29524,29921,
34792,28715,26983,27009,26988,27232,
29185,30070,29144,27335),
`Factor 4` = c(423,
548,365,452,479,486,487,570,563,642,
904,730,994,1432,1846,1926,997,464,385,
331,470,328,405,412),
`Factor 3` = c(7099,
7830,6955,8396,10306,10054,9235,10597,
8507,8788,6705,9208,9057,10078,5327,
3185,4709,8895,8564,9496,12277,14540,
11984,15992),
`Factor 5` = c(5726,
6419,8685,8760,8639,8703,8407,9150,7849,
8721,7009,10320,7527,8518,7244,8512,
7181,8721,7467,9342,10824,11753,9449,
11373)
)
I would like to forecast all 5 factors at once.
My list of factors might increase later, but I will find the best 5 factors that can have impact on Sales. So, I will still need to use 3- 5 factors before predicting Sales
Below is my trial to forecast multiple factors at once - not correct. Any help here would be highly appreciated as this will reduce repetitive forecasts on 5 factors.
factor_fc <- function(x){
x <- x%>%
mutate(date = yearmonth(date))%>%
as_tsibble(index = date)%>%
model(factor_model = ARIMA(across(`Factor 1`: `Factor 2`))) %>%
forecast(h=60)
return(x)
}
fact_fc <- map(df, ~factor_fc(.))
Initial Working Approach -
Forecasting Individual Factors first - Repetitive and tedious if more factors are involved. Looking for a better way to handle all factors at once
#Forecasts for factor 1 and similarly for other 4 factors
factor_1 <- df%>%
select(date, `Factor 1`)
factor_1_fc <- factor_1%>%
model(factor_1_model = ARIMA(`Factor 1`)) %>%
forecast(h=60)
#Forecast plot for factor_1
factor_1_fc %>%
autoplot(factor_1, level = NULL) +
ggtitle("Forecasts - factor_1") +
xlab("Year") +
guides(colour = guide_legend(title = "Forecast - Factor 1"))+
theme_minimal()
factor_1_fc <- factor_1_fc%>%
rename("factor_11" = .mean)%>%
select(date, factor_11)%>%
rename("factor_1" = factor_11)
Thanks for your help!