Multiple time series forecasting in R using auto.arima

My dataset has the following 3 columns:

date client_id sales
01/01/2012 client 1 $1000
02/01/2012 client 1 $900
...
...
12/01/2014 client 1 $1000
01/01/2012 client 2 $300
02/01/2012 client 2 $450
...
..
12/01/2014 client 2 $375

      

etc. for 98 other customers (24 monthly datasets for each customer)

I have multiple clients (about 100) ... the data is in time series format for each client (24 monthly data)

how to automatically predict sales for all 100 customers using auto.arima in R? is there a by statement option? or do I need to use loops?

thank

+3


source to share


1 answer


You can always use lapply()

:

lapply(tsMat, function(x) forecast(auto.arima(x)))

      



A small example follows:

library(forecast)
#generate some time-series:
sales <- replicate(100, 
    arima.sim(n = 24, list(ar = c(0.8), ma = c(-0.2)), sd = sqrt(0.1)) 
)
dates <- seq(as.Date("2012/1/1"), by = "month", length.out=24)
df <- data.frame(date=rep(dates,100), client_id=rep(1:100,each=24), sales=c(sales))
#reshape and convert it to a proper time-series format like ts:
tsMat <- ts(reshape2::dcast(df, date~client_id), start=2012, freq=12)
#forecast by auto.arima:
output <- lapply(tsMat, function(x) forecast(auto.arima(x)))

      

+2


source







All Articles