如何使用可配置的移动平均线在 R 中进行预测 [英] How to use configurable moving average for forecasting in R

查看:14
本文介绍了如何使用可配置的移动平均线在 R 中进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 36 个月的需求数据

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)

我想将前 24 个月的需求数据用作训练集,将后 12 个月的需求数据用作测试集.使用移动平均法(前 24 个月),我使用以下代码预测了 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))

输出如下:

 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

这正是我在使用历史数据中的所有 24 个月来预测第 25 个月至 36 个月的预测时所需要的.但是,我的问题 1 是如何使用移动平均线预测从 24 个月的训练期开始的最近 15 个月的移动平均线,以预测从 25 到 36 个月的滚动移动平均线(测试期为 avove).我想保持用于预测的历史切片可配置,可以是15个月18个月24个月(如当前示例) 从第 24 个月开始倒推.为了进一步澄清,如果我使用 15 个月移动平均线,那么第 25 个月的预测应该是第 10 个值到第 24 个值的平均值,第 26 个月的预测将是第 11 个值到第 25 个值的平均值,依此类推...我的问题 2 在比较测试数据的实际值和预测值后,我是否应该编写单独的代码来创建一个数据框来预测/预测未来 12 个月的实际值(即预测范围).如果有人可以帮忙.谢谢.

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.

推荐答案

幸运的是,我能够使用社区中提供的其他一些 R 代码,使用以下代码,在帮助下自己破解这个问题.

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(ModelNumber = 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$ModelNumber))
      {
        Selected_data<-subset(MiniData, ModelNumber==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(ModelNumber=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)

这篇关于如何使用可配置的移动平均线在 R 中进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆