Search code examples
rpredictionarimaforecast

R - Predict values for multiple categories of same outcome


Lets take , I have data of tickets sold in each category for a number of years by months. Like this:

Year         Premium     Silver     Budget
Jan2016      112354      36745      456563       
Feb2016      1233445     234322     4533345
Mar2016      13456544    346755     34564422

I have this data till Feb 2019 for each month. This is the code I use to apply arima for each category separately. I import the count of each column and do the below:

> count <-data.frame(mytickets$Premium)
> tickets<-ts(count, frequency = 12, start = c(2016, 1),end=c(2018,6)) 
> pi=auto.arima(tickets) 
> summary(pi) 
> q=forecast(pi,h=12)

I want to predict how many tickets will be sold next year evey month. Is it possible to apply auto ARIMA in the same shot? I have been applying the model separately so far.


Solution

  • You can always try lapply when you want to calculate multiple things in a similar way:

    
    dt <- read.table(text ="Year         Premium     Silver     Budget
    Jan2016      112354      36745      456563       
    Feb2016      1233445     234322     4533345
    Mar2016      13456544    346755     34564422", header = TRUE)
    
    library(data.table)
    dt <- data.table(dt)
    res <- lapply(c("Premium", "Silver", "Budget"), function(x) {
      count <- dt[, get(x)]
      tickets <-
        ts(
          count,
          frequency = 12,
          start = c(2016, 1),
          end = c(2018, 6)
        )
      pi = auto.arima(tickets)
      forecast(pi, h = 12)
    })