Reputation: 315
My series has 3 different columns, first ID tag identifying the first outlet, then time tag, and finally the measurement.
I need to create forecasts for 100 different series (outlets). First I need to subset ID for the first outlet, then predict arima functions and finally collect 7 days ahead forecasts for every outlet. Moreover, I also need hourly, weekly, daily dummies in my model. So I need to xregs to the auto.arima procedure.
However, I am incapable create the code bellow with a loop that would run for all 100 different IDs.
df11 <-subset(df10,ID==288)%>%select(Tag,Measure)
sales.xts <- xts(df11[ ,c(-1)],order.by = df11$Tag)
sales.xts_m<-sales.xts["2020-07-22/2020-10-04"]
dummies<- xts(Seasonaldummies_all[,-1],order.by = Seasonaldummies_all$Tag)
dummies_hd_m<-dummies_hd["2020-07-22/2020-10-04"]
model<-auto.arima(sales.xts_m,xreg=dummies_hd_m, biasadj = TRUE,max.p=7,max.q=7,seasonal=FALSE,test=c("kpss"),lambda = "auto",num.cores=15,stationary = TRUE)
Can you show me a quick way to do that job by apply or loop functions?
Upvotes: 0
Views: 716
Reputation: 405
You if you want to use forecast
package need to convert your data into a ts
(mts
) object. To do that fist transform your data from long format to wide format (from the image you post above I assume your data is in a long format). Then by using ts()
function to create a ts()
object, see the example below.
sales.xts_m <- ts(data.frame(AA = arima.sim(list(order=c(1,0,0), ar=.5), n=100,
mean = 12),
AB = arima.sim(list(order=c(1,0,0), ar=.5), n=100,
mean = 12),
AC = arima.sim(list(order=c(1,0,0), ar=.5), n=100,
mean = 11),
BA = arima.sim(list(order=c(1,0,0), ar=.5), n=100,
mean = 10),
BB = arima.sim(list(order=c(1,0,0), ar=.5), n=100,
mean = 14)), start = c(2000, 1),
frequency = 12)
nts <- ncol(sales.xts_m) # number of time series
h <- 12 # forecast horizon
dummies_hd_m <- forecast::seasonaldummy(sales.xts_m[,1])
dummies_hd_m_future <- forecast::seasonaldummy(sales.xts_m[,1], h = h)
mylist <- list()
fc <- matrix(nrow = h, ncol = nts)
models will be in mylist and point forecast in fc for each ts
for (i in 1:nts) {
mylist[[i]] <- auto.arima(sales.xts_m[,i],xreg=dummies_hd_m, biasadj = TRUE,
max.p=7,max.q=7,seasonal= FALSE,test=c("kpss"),
lambda = "auto",num.cores=15,stationary = TRUE )
fc[,i] <- forecast(mylist[[i]], h=h, xreg = dummies_hd_m_future)$mean
}
#ts names
colnames(fc) <- colnames(sales.xts_m)
fc <- matrix(nrow = h, ncol = nts)
for (i in 1:nts) {
fc[,i] <- forecast(auto.arima(sales.xts_m[,i],xreg=dummies_hd_m, biasadj = TRUE,
max.p=7,max.q=7,seasonal=FALSE,test=c("kpss"),
lambda = "auto",num.cores=15,stationary = TRUE ), h=h,
xreg = dummies_hd_m_future)$mean
}
#ts names
colnames(fc) <- colnames(sales.xts_m)
If you want to use ML models for your projects
devtools::install_github("Akai01/caretForecast")
library(caretForecast)
nts <- ncol(sales.xts_m) # mumber of time series
h <- 12 # forecast horizon
fc <- matrix(nrow = h, ncol = nts)
example: Support Vector Machines with Linear Kernel. You need to change only caret_method argument to use another model, for example caret_method = "ridge" or caret_method = "rf" etc. Ref: https://github.com/Akai01/caretForecast
for (i in 1:nts) {
fc[,i] <- forecast(ARml(sales.xts_m[,i], maxlag = 12, xreg = dummies_hd_m,
caret_method = "svmLinear", seasonal = FALSE ),
h=h, xreg = dummies_hd_m_future)$mean
}
colnames(fc) <- colnames(sales.xts_m)
Upvotes: 1