PranavMurali
PranavMurali

Reputation: 88

repeated forecasts gives same values

I have a monthly dataset of performance (in terms of %) of different sectors in a company in the form

   Date    |Sector  |Value
2016-01-01 |Sect 1  |-20
2016-02-01 |Sect 1  |10
2016-01-01 |Sect 2  |23
2016-02-01 |Sect 1  |10 

the data has 20 Sectors and monthly data till June 2018. Now I want to forecast Value for the next month. I used the below code:

combine_ts <- function(data, h=1, frequency= 12, start= c(2016,5), 
end=c(2018,6)) 
{
  results <- list()
  sectgrowthsub <- data[!duplicated(sectgrowthdf2[,2]),]
  sectgrowthts <- ts(sectgrowthsub[,3], frequency = frequency, start = start, 
  end = end)


  for (i in 1:(nrow(sectgrowthsub))) {    
  results[[i]] <- data.frame(Date = 
  format(as.Date(time(forecast(auto.arima(sectgrowthts), h)$mean)), "%b-%y"),
                         SectorName = rep(sectgrowthsub[,2], h),
                         PointEstimate = forecast(auto.arima(sectgrowthts), 
                         h=h)$mean[i])

    }

return(data.table::rbindlist(results)) 
}
fore <- combine_ts(sectgrowthsub) 

The problem in this case is that Value forecast is the same for all the Sectors. Help is much appreciated

Upvotes: 1

Views: 705

Answers (1)

Nate
Nate

Reputation: 10671

I took the liberty of simplifying the problem a little bit and removed the function to better show the process of modeling groups separately:

library(magrittr)
library(forecast)

dat <- data.frame(value = c(rnorm(36, 5),
                            rnorm(36, 50)),
                  group = rep(1:2, each = 36))

# make a list where each element is a group's timeseries
sect_list <- dat %>%
    split(dat$group) %>%
    lapply(function(x, frequency, start) {
        ts(x[["value"]], frequency = 12, start = 1 ) })

# then forecast on each groups timeseries
fc <- lapply(sect_list, function(x) { data.frame(PointEstimate = forecast(x, h=1)$mean ) }) %>%
    do.call(rbind, .) # turn into one big data.frame

fc

PointEstimate
1      5.120082
2     49.752510

Let me know if you get hung up on any parts of this.

Upvotes: 1

Related Questions