预测使用forecast.gts中的xreg参数,以及每个时间序列具有不同值的多个外部变量(hts包)

时间:2015-01-01 10:33:39

标签: r time-series forecasting

我目前正在使用forecast.gts (hts package)一个外部变量,它为所有单个时间序列保存相同的值,以便创建30天预测,但是我想要包含另一个外部回归量,每个人都拥有不同的价值观。

我已将问题简化如下:

我有4个ts预测(它们是每天为品牌和非盛大团体花费的保险付费搜索),分为两组:

enter image description here

有两个外部回归,我有兴趣申请:

  1. 开放注册期(具有更高的值) - 这适用于所有四个系列
  2. 关闭期间:这仅适用于两个非品牌ts - 对于2013年10月22日的两个期间 - 12/21/2013& 12/24/2013 - 01/27/2014他们暂停了,因此他们有〜0的价值。
  3. 因此,对于第一个外部回归量,我设法使用forecast.hts如下:

    library(ggplot2)
    library(plyr)
    library(hts)
    library(ts)
    library(data.table)
    library(reshape2)
    horizon<-30
    data1<-dcast(data, Date ~ Brand+Venue, sum , value.var="Spend")
    data[is.na(data)] <- 0
    abcd<-ts(as.matrix(data1[,2:5]),frequency=7)
    #abcd<-abcd+0.000001
    Brand <- c("Brand","Brand","NonBrand","NonBrand")
    Venue <- rep(c("Google","MSN"),  2)
    gcd <- rbind(Brand,Venue)
    rownames(gcd) <- c("Brand","Venue")
    x <- gts(abcd, groups = gcd)
    
    dates = data.frame(Date=seq.Date(min(data$Date,na.rm =T), max(data$Date,na.rm =T), by = "day"))
    dates$enrollment<-ifelse(dates$Date>=as.Date("2013-10-01") & dates$Date<=as.Date("2014-03-31"),1,
                             ifelse(dates$Date>=as.Date("2014-11-15") & dates$Date<=as.Date("2015-02-15"),1,0))
    
    enrollment_prediction<-seq.Date(max(dates$Date)+1, max(dates$Date)+horizon, by = "day")
    enrollment_prediction<-ifelse(enrollment_prediction>=as.Date("2013-10-01") & enrollment_prediction<=as.Date("2014-03-31"),1,
                                  ifelse(enrollment_prediction>=as.Date("2014-11-15") & enrollment_prediction<=as.Date("2015-02-15"),1,0))
    
    train<-cbind(dates[,2])
    predict<-cbind(enrollment_prediction)
    
        cost_fcasts <- forecast(x, h = horizon, method = "comb",fmethod = "arima",xreg=train,newxreg=predict)
    

    现在我的问题是:我如何创建另一个新的回归量(二元变量),它将为所有品牌ts分配1个值,并在2013年10月22日期间为非品牌ts分配0个值 - 12/21/2013&amp; 12/24/2013 - 2014年1月27日?

    数据的一个子集:

    dput(data)
    structure(list(Date = structure(c(16104, 16182, 16149, 16190, 
    16156, 16146, 16108, 16154, 16132, 16149, 16110, 16105, 16120, 
    16135, 16155, 16126, 16130, 16140, 16136, 16128, 16127, 16129, 
    16113, 16137, 16178, 16118, 16142, 16107, 16106, 16131, 16126, 
    16139, 16138, 16157, 16135, 16152, 16148, 16123, 16140, 16120, 
    16144, 16161, 16155, 16122, 16139, 16129, 16106, 16130, 16106, 
    16154, 16103, 16125, 16186, 16114, 16142, 16133, 16154, 16109, 
    16111, 16183, 16180, 16146, 16141, 16151, 16121, 16167, 16116, 
    16121, 16125, 16105, 16152, 16108, 16118, 16155, 16181, 16132, 
    16153, 16134, 16165, 16132, 16174, 16134, 16160, 16135, 16171, 
    16147, 16123, 16152, 16107, 16151, 16103, 16138, 16151, 16106, 
    16166, 16117, 16107, 16144, 16117, 16145, 16137, 16130, 16157, 
    16173, 16150, 16184, 16150, 16172, 16159, 16178, 16138, 16168, 
    16184, 16153, 16159, 16181, 16133, 16129, 16122, 16154, 16176, 
    16179, 16188, 16125, 16136, 16127, 16150, 16115, 16115, 16162, 
    16143, 16111, 16131, 16109, 16136, 16119, 16160, 16131, 16156, 
    16161, 16164, 16151, 16190, 16113, 16165, 16142, 16119, 16152, 
    16167, 16133, 16139, 16121, 16178, 16133, 16124, 16114, 16108, 
    16144, 16168, 16118, 16143, 16148, 16119, 16126, 16185, 16111, 
    16136, 16116, 16158, 16107, 16176, 16189, 16142, 16177, 16169, 
    16104, 16167, 16115, 16112, 16131, 16112, 16175, 16148, 16140, 
    16119, 16172, 16125, 16179, 16172, 16122, 16185, 16143, 16173, 
    16145, 16183, 16180, 16147, 16163, 16170, 16130, 16109, 16170, 
    16179, 16143, 16104, 16150, 16153, 16157, 16155, 16144, 16120, 
    16181, 16188, 16105, 16158, 16182, 16115, 16147, 16173, 16153, 
    16169, 16116, 16113, 16171, 16180, 16127, 16146, 16118, 16177, 
    16113, 16141, 16120, 16146, 16189, 16169, 16173, 16112, 16157, 
    16149, 16135, 16121, 16147, 16148, 16111, 16149, 16140, 16134, 
    16114, 16168, 16187, 16126, 16141, 16108, 16139, 16170, 16103, 
    16128, 16105, 16112, 16145, 16163, 16109, 16175, 16187, 16122, 
    16176, 16186, 16141, 16174, 16156, 16129, 16114, 16128, 16127, 
    16132, 16171, 16169, 16162, 16170, 16103, 16160, 16116, 16104, 
    16158, 16134, 16137, 16178, 16123, 16174, 16117, 16188, 16159, 
    16128, 16123, 16145, 16172, 16165, 16166, 16180, 16124, 16174, 
    16138, 16110, 16164, 16164, 16110, 16137, 16177, 16175, 16171, 
    16176, 16175, 16177, 16181, 16156, 16110, 16179, 16163, 16168, 
    16189, 16188, 16183, 16185, 16166, 16190, 16167, 16165, 16124, 
    16166, 16182, 16164, 16117, 16185, 16187, 16162, 16186, 16190, 
    16187, 16161, 16184, 16160, 16186, 16159, 16183, 16124, 16158, 
    16161, 16162, 16182, 16163, 16189, 16184), class = "Date"), Spend = c(15842.58, 
    4053.33, 12276.37, 3255.13, 34147.63, 10280.15, 1359.05, 10630.05, 
    19970.06, 8264.08, 633.13, 2018.73, 4336.53, 27650.11, 26951.01, 
    28489.2, 668.22, 33879.52, 21996.9, 19303.88, 23985.98, 1517.48, 
    1766.36, 14539.82, 2356.67, 13442.66, 9853.06, 1460.34, 1885.7, 
    9548.48, 6922.77, 29439.75, 14516.58, 15262.33, 3533.56, 1626.65, 
    11114.59, 585.64, 9125.84, 17940.8, 3477.37, 342.48, 9852.61, 
    2936.53, 8459.63, 15603.87, 1745.46, 3913.92, 14721.02, 4547.05, 
    757.44, 5125.04, 1523.12, 15526.4, 45516.4, 24888.82, 3190.53, 
    1065.25, 4356.01, 1142.47, 269.77, 38923.51, 39508.12, 690.53, 
    17041.63, 1513.47, 604.92, 3320.61, 1821.44, 14143.89, 8629.41, 
    8033.21, 1292.16, 2460.76, 706.36, 2804.23, 11942.11, 3084.68, 
    65.93, 9886.54, 3934.09, 26347.99, 903.66, 7629.38, 2468.47, 
    18017.85, 3720.39, 3023.53, 4375.57, 2851.58, 8886.96, 642.01, 
    6702.67, 4350.53, 1262.83, 496.95, 13424.99, 802.84, 858.65, 
    22996.32, 2929.39, 11457.34, 213.06, 1136.51, 9664.37, 1168.22, 
    2212.77, 1170.14, 356.39, 848.81, 3164.11, 3458.27, 3612.13, 
    2919.57, 7448.95, 1572.38, 8165.08, 1923.62, 15476.18, 27578.67, 
    3455.62, 279.58, 0, 30040.45, 1530.41, 6577.7, 1725.36, 1396.46, 
    1321.49, 3878.4, 7021.28, 8853.14, 1283.59, 1682.93, 5563.42, 
    1697.88, 3841.95, 3508.54, 9989.89, 1374.71, 2466.79, 1450.81, 
    1246.87, 17256.75, 1184.34, 4911.98, 4279.49, 842.95, 1509.02, 
    1978.1, 4056.48, 2312.5, 2674.83, 2795.75, 1047.46, 1365.23, 
    3367.7, 28421.9, 1208.81, 2975.87, 4314.07, 9691.88, 16614.88, 
    1710.66, 2870, 2287.66, 3298, 3248.6, 443.4, 1647.22, 2901.93, 
    3610.21, 1956.94, 2885.58, 3195.33, 2173.07, 3748.72, 2734.09, 
    9179.23, 571.99, 3893.73, 3600.8, 2361.42, 4801.02, 2242.37, 
    1249.61, 2885.95, 1097.31, 293.9, 2044.25, 1075.1, 1888.26, 413.3, 
    2594.21, 3729.84, 881.72, 2179.57, 31.98, 637.2, 1133.57, 520.98, 
    2940.35, 1942.11, 43361.33, 1837.33, 7429.81, 20603.36, 9059.43, 
    2776.64, 3027.94, 1803.23, 4175.03, 4361.84, 4780.86, 5334.76, 
    1266.33, 10430.92, 3988.57, 366.93, 3517.32, 294.78, 826.85, 
    1920, 3048.21, 1966.12, 2332.73, 5522.68, 1855.98, 3034.97, 4473.6, 
    9379.69, 2278.22, 2403.14, 1256.74, 1367.85, 1887.51, 1914.65, 
    0, 3268.07, 1899.46, 1456.54, 9883.6, 2106.05, 2364.99, 1895.83, 
    1836.96, 7785.42, 3784.51, 1379.38, 1542.7, 2937.07, 1620.28, 
    1658.6, 2033.13, 3081.68, 1435.15, 2945.92, 1802.12, 2084.01, 
    3324.74, 4623.88, 661.2, 4835.06, 342.56, 1398.99, 1131.91, 328.3, 
    4903.98, 4993.82, 0, 6923.42, 1591.52, 1692.26, 1607.41, 1898.13, 
    611.49, 1344.86, 1023.46, 1168.65, 532.7, 17004.37, 655.28, 5582.81, 
    1116.49, 1811.79, 4365.59, 657.33, 1225.97, 1150.57, 40.46, 1543.24, 
    1487.07, 7264.13, 528.31, 621, 333.81, 372.11, 446.69, 334.46, 
    528.69, 1353.67, 1881.9, 1040.74, 969.57, 92.97, 57.16, 611.54, 
    1104.2, 1478.91, 931.5, 582.02, 973.26, 753.14, 1365.5, 0, 1.61, 
    318.24, 1116.22, 124.04, 0, 0, 0, 0, 0, 0, 0, 0, 3.75, 26.52, 
    0, 0, 1.41, 0, 0, 0, 0, 0, 0, 0, 0, 18.57, 0, 2.7, 0, 24.74, 
    0, 0, 0, 72.7, 0, 0, 0), Brand = c("NonBrand", "Brand", "NonBrand", 
    "Brand", "NonBrand", "Brand", "NonBrand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "Brand", 
    "Brand", "NonBrand", "Brand", "NonBrand", "Brand", "NonBrand", 
    "Brand", "NonBrand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "NonBrand", "Brand", "Brand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "NonBrand", "Brand", 
    "Brand", "Brand", "NonBrand", "NonBrand", "Brand", "NonBrand", 
    "NonBrand", "Brand", "Brand", "Brand", "NonBrand", "NonBrand", 
    "NonBrand", "Brand", "Brand", "NonBrand", "NonBrand", "Brand", 
    "NonBrand", "NonBrand", "Brand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "Brand", "NonBrand", "NonBrand", "Brand", "Brand", "Brand", 
    "NonBrand", "Brand", "NonBrand", "Brand", "Brand", "Brand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "Brand", "NonBrand", "Brand", 
    "Brand", "NonBrand", "Brand", "NonBrand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "Brand", "Brand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", 
    "NonBrand", "Brand", "NonBrand", "Brand", "NonBrand", "Brand", 
    "Brand", "Brand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "NonBrand", "Brand", "NonBrand", "Brand", "Brand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "Brand", "Brand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "Brand", "NonBrand", "NonBrand", "Brand", 
    "Brand", "NonBrand", "NonBrand", "Brand", "Brand", "Brand", "NonBrand", 
    "NonBrand", "NonBrand", "Brand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "Brand", "NonBrand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", 
    "NonBrand", "Brand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "Brand", "Brand", "NonBrand", "Brand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "Brand", "Brand", "Brand", "Brand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", 
    "NonBrand", "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", 
    "NonBrand", "NonBrand", "Brand", "Brand", "Brand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "NonBrand", "NonBrand", "Brand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "NonBrand", "Brand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", 
    "Brand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", "Brand", 
    "Brand", "NonBrand", "Brand", "Brand", "NonBrand", "NonBrand", 
    "Brand", "NonBrand", "NonBrand", "Brand", "Brand", "Brand", "Brand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "NonBrand", "Brand", 
    "Brand", "Brand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "Brand", 
    "NonBrand", "NonBrand", "NonBrand", "Brand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", 
    "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand", "NonBrand"
    ), Venue = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 
    1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 
    2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 
    2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 
    1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 
    1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 
    2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 
    1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 
    2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 
    2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 
    2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Google", "MSN"), class = "factor")), .Names = c("Date", 
    "Spend", "Brand", "Venue"), row.names = c(16L, 24L, 31L, 54L, 
    69L, 72L, 85L, 100L, 107L, 115L, 118L, 134L, 137L, 156L, 184L, 
    195L, 198L, 226L, 284L, 289L, 290L, 321L, 345L, 358L, 359L, 361L, 
    389L, 397L, 418L, 456L, 458L, 478L, 486L, 524L, 550L, 560L, 576L, 
    588L, 598L, 600L, 625L, 649L, 735L, 775L, 782L, 791L, 891L, 896L, 
    901L, 907L, 973L, 987L, 1209L, 1266L, 1276L, 1282L, 1304L, 1326L, 
    1329L, 1350L, 1370L, 1537L, 1773L, 1775L, 1825L, 1921L, 2117L, 
    2476L, 2478L, 2555L, 2556L, 2726L, 2761L, 2949L, 2974L, 3339L, 
    3350L, 3438L, 3731L, 3843L, 3887L, 3890L, 3920L, 3922L, 3939L, 
    3944L, 4021L, 4049L, 4072L, 4105L, 4138L, 4149L, 4174L, 4180L, 
    4232L, 4261L, 4274L, 4281L, 4290L, 4316L, 4335L, 4417L, 4439L, 
    4514L, 4565L, 4567L, 4632L, 4778L, 4787L, 4849L, 4889L, 4996L, 
    5126L, 5199L, 5328L, 5342L, 5455L, 5466L, 5807L, 5932L, 6159L, 
    6242L, 6611L, 6926L, 6928L, 6934L, 6963L, 6969L, 7018L, 7036L, 
    7037L, 7039L, 7043L, 7121L, 7183L, 7186L, 7230L, 7237L, 7298L, 
    7333L, 7358L, 7394L, 7505L, 7515L, 7520L, 7615L, 7648L, 7655L, 
    7920L, 7964L, 8049L, 8079L, 8093L, 8364L, 8586L, 8596L, 8885L, 
    8896L, 8906L, 9299L, 9506L, 9561L, 9625L, 9647L, 9688L, 9700L, 
    9710L, 9722L, 9732L, 9754L, 9794L, 9798L, 9803L, 9868L, 9927L, 
    9947L, 9951L, 9955L, 9984L, 9987L, 9998L, 10146L, 10169L, 10213L, 
    10396L, 10409L, 10484L, 10505L, 10658L, 10678L, 10702L, 10710L, 
    10841L, 10846L, 10932L, 10952L, 11037L, 11048L, 11213L, 11258L, 
    11276L, 11282L, 11382L, 11432L, 11447L, 11777L, 11936L, 12024L, 
    12027L, 12079L, 12092L, 12132L, 12173L, 12180L, 12246L, 12446L, 
    12468L, 12501L, 12704L, 12736L, 12741L, 12780L, 12810L, 12816L, 
    12852L, 13200L, 13539L, 13659L, 13879L, 13969L, 13998L, 14022L, 
    14166L, 14537L, 14579L, 14708L, 15241L, 15712L, 15780L, 15784L, 
    15814L, 15829L, 15862L, 15880L, 15915L, 15941L, 15977L, 15999L, 
    16166L, 16494L, 16594L, 17368L, 17371L, 17376L, 17387L, 17401L, 
    17433L, 17447L, 17462L, 17463L, 17476L, 17482L, 17630L, 17797L, 
    17878L, 18051L, 18151L, 18461L, 18466L, 18488L, 18765L, 18766L, 
    18780L, 18830L, 18831L, 18884L, 18925L, 19006L, 19022L, 19023L, 
    19396L, 19510L, 19975L, 20000L, 20001L, 20043L, 20180L, 20587L, 
    20685L, 20849L, 20885L, 21174L, 21182L, 21325L, 21411L, 21414L, 
    21443L, 21735L, 21870L, 21969L, 22059L, 22279L, 22405L, 22620L, 
    22648L, 22892L, 23073L, 23143L, 23992L, 24744L, 25054L, 25460L, 
    25581L, 26298L, 26656L, 26684L, 26693L, 26858L, 28041L, 29034L, 
    29345L, 29468L, 29634L, 29657L, 29675L, 29926L, 29967L, 30091L, 
    30162L, 30203L, 30537L, 30569L, 30573L, 30762L, 30777L, 31110L, 
    31247L, 31328L, 31684L, 32036L, 32151L, 32192L, 32375L, 32540L, 
    32759L, 32896L, 33038L, 33643L, 33659L, 34136L, 34471L, 34652L
    ), class = "data.frame")
    

1 个答案:

答案 0 :(得分:3)

我不建议在您的示例中使用forecast.gts(),因为针对不同时间序列处理不同的xreg仍在开发中。但另一种方法就是让你到达那里。

您需要首先使用forecast.gts()生成预测,然后将forecast::auto.arima应用于这些预测以获得已对帐的预测,而不是直接使用combinef()

# Create a binary variable called "shutdown"
shutdown <- ifelse(dates$Date >= as.Date("2013-10-22") &
                   dates$Date <= as.Date("2013-12-21"), 0,
                   ifelse(dates$Date >= as.Date("2013-12-24") &
                   dates$Date <= as.Date("2014-01-27"), 0, 1))
pred.shutdown <- rep(1, horizon) # Assume it's open in the future
brand.xregs <- data.frame(enrollment = train)
pbrand.xregs <- data.frame(enrollment = predict)
nonbrand.xregs <- data.frame(enrollment = train, shutdown = shutdown)
pnonbrand.xregs <- data.frame(enrollment = predict, shutdown = pred.shutdown)
basef <- matrix(, nrow = horizon, ncol = 4) # bottom level
for (i in 1:2) { # brand
  basef[, i] <- forecast(auto.arima(abcd[, i], xreg = brand.xregs),
                     h = horizon, xreg = pbrand.xregs)$mean
}
for (i in 3:4) { # non brand
  basef[, i] <- forecast(auto.arima(abcd[, i], xreg = nonbrand.xregs), 
                     h = horizon, xreg = pnonbrand.xregs)$mean
 }
aggx <- aggts(x, levels = 0:2)
aggbasef <- matrix(, nrow = horizon, ncol = ncol(aggx)) # aggregated level
for (i in 1:ncol(aggx)) { # brand
  aggbasef[, i] <- forecast(auto.arima(aggx[, i]), h = horizon)$mean
}
allbasef <- cbind(aggbasef, basef) # All base forecasts across levels
rf <- combinef(allbasef, groups = x$groups)

希望这会有所帮助。