How to use configurable moving average for forecasting in R

北城以北 提交于 2020-07-23 06:46:04

问题


I've 36 months of demand data

dput(RawData)
structure(list(ModelNo = c("a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a"), Month_Year = structure(c(1498867200, 1501545600, 
1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 
1519862400, 1522540800, 1525132800, 1527811200, 1530403200, 1533081600, 
1535760000, 1538352000, 1541030400, 1543622400, 1546300800, 1548979200, 
1551398400, 1554076800, 1556668800, 1559347200, 1561939200, 1564617600, 
1567296000, 1569888000, 1572566400, 1575158400, 1577836800, 1580515200, 
1583020800, 1585699200, 1588291200, 1590969600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Quantity = c(9, 3, 4, 0, 2, 0, 0, 
7, 1, 4, 1, 2, 4, 2, 2, 0, 4, 1, 2, 0, 0, 0, 4, 0, 0, 0, 0, 0, 
1, 0, 0, 0, 2, 0, 2, 0)), row.names = c(NA, 36L), class = "data.frame")

I've created a time series using below code

    y_ts <- ts(RawData$Quantity,
                 start=c(year(min(Data_sort$Month_Year)), 
month(max(Data_sort$Month_Year))+1), frequency=12)

I wanted to used first 24 months of demand data as training set and the last 12 months as test set. Using Moving Average method (for first 24 months) I forecasted the values for 12 months using below code:

test<-rollapply(y_ts, list(-seq(24)), FUN = mean, fill = NA)
  cbind(RawData, data.frame(test))

The output looks like :

 ModelNo Month_Year Quantity     test
1        a 2017-07-01        9       NA
2        a 2017-08-01        3       NA
3        a 2017-09-01        4       NA
4        a 2017-10-01        0       NA
5        a 2017-11-01        2       NA
6        a 2017-12-01        0       NA
7        a 2018-01-01        0       NA
8        a 2018-02-01        7       NA
9        a 2018-03-01        1       NA
10       a 2018-04-01        4       NA
11       a 2018-05-01        1       NA
12       a 2018-06-01        2       NA
13       a 2018-07-01        4       NA
14       a 2018-08-01        2       NA
15       a 2018-09-01        2       NA
16       a 2018-10-01        0       NA
17       a 2018-11-01        4       NA
18       a 2018-12-01        1       NA
19       a 2019-01-01        2       NA
20       a 2019-02-01        0       NA
21       a 2019-03-01        0       NA
22       a 2019-04-01        0       NA
23       a 2019-05-01        4       NA
24       a 2019-06-01        0       NA
25       a 2019-07-01        0 2.166667
26       a 2019-08-01        0 1.791667
27       a 2019-09-01        0 1.666667
28       a 2019-10-01        0 1.500000
29       a 2019-11-01        1 1.500000
30       a 2019-12-01        0 1.458333
31       a 2020-01-01        0 1.458333
32       a 2020-02-01        0 1.458333
33       a 2020-03-01        2 1.166667
34       a 2020-04-01        0 1.208333
35       a 2020-05-01        2 1.041667
36       a 2020-06-01        0 1.083333

This is exactly what I want when I take all 24 months in historical data to predict forecast for 25th month onwards up to 36 month. However, My question 1 is how can I forecast using Moving Average for the most recent 15 months from the training period of 24 months to predict rolling moving average from 25th to 36th month (test period as avove). I want to keep the historical slices used to forecast configurable, it can be 15 months, 18 months or 24 months(as in current example) from 24th month backwards in time. To further clarify if I use 15 month moving average for example, then Month 25th forecast should be average of 10th value to 24th value, month 26th forecast will be average of 11th value to 25th value and so on...My question 2 should I write separate code to create a dataframe to predict/forecast actual values for next 12 months (i.e. for forecasting horizon) after I have compared the actual and forecast value for test data. If someone can help please. Thanks.


回答1:


I've been fortunately able to crack this myself with the help using some other R code available in this community, using below code.

MA_Horizon_FC<-data.frame(Part_Number = character(), Month_Year = Date(), HorizonFC_12m_Quantity = numeric())
train_test_FC_1<-NULL


history_slices=15 #####configurable historical slices, 15 or 24 or anything else
    listofdfs <- list()
      
      for (i in unique(MiniData$UpdatedPartNumber))
      {
        Selected_data<-subset(MiniData, UpdatedPartNumber==i)
        Data_sort<-Selected_data[order(Selected_data$Month_Year),]
        rownames(Data_sort) <- 1:36
      
        y_ts <- ts(Data_sort$Quantity,
                 start=c(year(min(Data_sort$Month_Year)), month(min(Data_sort$Month_Year))), frequency=12)
      
     
      test_FC<-rollapply(y_ts, list(-seq(history_slices)), FUN = mean, fill = NA)
      
      test_FC[1:24]<-NA
      listofdfs[[i]]<-cbind(Data_sort, data.frame(test_FC))
    
      
      ##############   Picking up the 37th month forecast  #####################
      forecast_only<-rollapply(y_ts ,history_slices, FUN = mean, align = "right") 
      FC_Value<-as.numeric(tail(as.zoo(forecast_only),1))
      
      ##############   calculating the month-year for the forecast   ###########
      date_1m_fwd <- as.POSIXlt(max(Data_sort$Month_Year))
      date_1m_fwd$mon <- date_1m_fwd$mon +1
      next_month_year<-date_1m_fwd
      
      #############creating a new data frame with parts and their forecasted value####
      MA_Horizon_FC <- rbind(MA_Horizon_FC, data.frame(Part_Number=i, Month_Year=next_month_year, HorizonFC_12m_Quantity=FC_Value))
      }  
      
      ##########   Melting the list to create a dataframe for Test-Train Values
    MA_Train_Test_FC <- listofdfs %>% reduce(bind_rows)


来源:https://stackoverflow.com/questions/62759128/how-to-use-configurable-moving-average-for-forecasting-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!