我有一个频率为52(周)和104条记录(2年的数据)的时间序列数据集,我在下面给出了一个样本。
我正在使用hts包在r(链接)
它有一个预测包,我们可以在其中指定一个用户定义的函数,该函数将用于创建预测。我使用了 holtwters (在stlf的行上创建了一个修改后的函数,也在下面的代码中给出)
我得到两个错误:
-.default
(y, fits): non-conformable arrays
附加:警告信息:在holtwints (x)中:优化困难:ERROR: ABNORMAL_TERMINATION_IN_LNSRCH
我已经尝试对返回的fcast变量进行矢量化,但它没有帮助。请告诉我哪里做错了。我不能使用ets,因为它不能处理超过24的频率,而且stl需要超过2个周期。
dataTS<-ts(matrix(c(696,1363.5,1769.5,901.5,679.5,653,626.5,633.5,626.5,3224.5,3733.5,1782.5,693.5,692,638,659.5,711,1495,1629.5,955.5,545.5,846.5,978.5,603,589.5,522.5,523,495.5,427,409,341,402,642,585,481,851,1235,885,608,588.5,617,660,622.5,625,538.5,1012,1361,929.5,596.5,637,597.5,537,480,862,1012.5,717,472.5,505,456,661.5,855.5,740.5,559.5,487.5,629,797,902.5,1341,1567.5,995.5,660.5,661.5,674.5,571,561.5,596,906,771.5,573,555.5,565,533,425.5,561.5,652,662.5,620.5,645.5,651,637.5,692.5,735,695,711.5,651.5,831.5,720.5,758.5,2046,1165.5,648,782,681,621,832.5,1805,2216.5,1073.5,652,686.5,697.5,1028,1226.5,759,454.5,399,550.5,600,541.5,599,643,1486,1556,968,647,700,658,1204,844,618.5,586,571.5,500,510,371,441.5,707.5,682.5,640.5,1304.5,1736,1092,756,670.5,555.5,673.5,699,621,565.5,1344,1786.5,1109.5,631.5,730,684,657,619,1057,1189,782.5,565.5,682.5,671.5,1150.5,1278,851.5,476.5,396.5,447.5,519,603,1205,1628.5,1049,637.5,782.5,812.5,788,748.5,789.5,1362.5,996.5,782.5,691,680,666,491,1026,2045.5,1255,822.5,782,841.5,744,675.5,774,839.5,821,796,958.5,832,866,2511,1136,525,643.5,603,539,2120.25,1296.25,963,1453.5,1853.25,1669.25,1874,2491.75,2344.25,1391.25,569.25,438.25,471,2110.5,3440.75,1788,739.5,675.5,621.5,704.25,766.25,3209.25,4072.25,541,2131,2850.75,803.5,675.75,582.75,2390.25,1752.75,1300,648.5,768,885.5,2291.5,2953.5,1410.5,576.5,724,819.25,2550,3035.25,693.75,661.5,826,965,2891.75,3323,1846.5,582.25,626.75,600,822.5,699.5,584.25,519.25,2331.5,3332,2168.75,835.5,790.25,672.5,449,458.5,2335,3860.75,1855.5,711.5,645.5,588.5,697.75,913.25,4202.25,1762.25,578.5,2982.5,1598,887.25,668,707.75,2538.25,1929,1769.25,657.75,839.5,931,2628.25,3376.25,1603.25,767.75,938,1130,3366,1717,792.5,1057.75,1101.75,3730.25,1660.25,702,1008.25,1070.5,906.75,1489,1032.5,785.5,1032.75,1321.75,1340,1586.75,1680.5,1594.75,1028.5,454.75,346,377.5,1853,2877.75,1485.75,665.5,576.75,559.25,595.75,650.25,2619.25,3420.75,497,1526.75,2005.5,640.5,529,515.5,1863.5,1406.5,1013.25,536.5,609,662.25,1840,2211.75,1088,467.5,539.25,618.25,1764.75,2172,580.5,561.75,607.5,656.25,2290.25,2864.5,1560.25,479,558.25,489.25,586.75,461,403.25,371,1668.5,2427.5,1473.75,551.25,527.5,486,342.25,328.25,1638.5,2791,1408.25,579.5,465.75,378,495.75,604.5,2932.75,1350.25,385.25,2178.75,1185,571.5,374,418.5,1854.25,1406.5,1287.5,427,551.75,612,1813.75,2448,1093.75,473.25,637.75,772.5,2260.25,1178.25,530.25,677.25,759,2345.25,1035.5,403,657,683.75,632.25,7971,7773,8437.5,7658.5,8002,8629,8119.5,8555,7631,6093,5593,5353,6055.5,6845,7153.5,7819,7665,7533.5,7593,7293,7123.5,7151,6974.5,7068.5,7327,7532.5,6950,6611.5,6245.5,6143,5938,5551,6956,7091.5,7182,6893.5,6490,6588.5,6635,6702.5,7155,7832,7884.5,7340.5,7550,7811,7922,7664.5,6787.5,8055,8488,8116,7903.5,8744.5,7582.5,8509.5,8926,9952,8800.5,8838,7861.5,6851.5,6697,5941.5,6442,6826.5,7384,7356.5,7524,7617.5,7088.5,7358,7433.5,6965,6812.5,7026.5,7136.5,7012,6485,6638,6529,6434.5,5795.5,6496.5,6805.5,7089,7078.5,7076.5,7099,6297,5864,6673,7228.5,7094,6945.5,7137.5,6645.5,6450,7386.5,7233.5,6493,6976.5,5659,6611,1685.5,1539,1580,1028.5,948,941.5,874.5,879.5,826,921.5,809,773,1099.5,1076,990,1008.5,989,939.5,863.5,899,883,871,796.5,965.5,902,827,848.5,803,685,754.5,567.5,610.5,971,905,911.5,839,801,781,823,1472,2072.5,1451.5,994,897,914.5,899,940,918.5,828,933.5,1019.5,937.5,791.5,975.5,947,855.5,865.5,929.5,831,982,858,733,693,560,688,865,929.5,873.5,813,813,851.5,851.5,885.5,782,818.5,822,844.5,868.5,849.5,743.5,814.5,767.5,596,699,811.5,875.5,919.5,899,896,872,905,1326.5,1566.5,892.5,894.5,769,947.5,957.5,904.5,902,831.5,1004.5,970,864.5),ncol = 6,nrow=104),frequency = 52)
colnames(dataTS)<-c("001001001", "001001002", "002002096", "002002097", "003002010", "003002036")
myhTS <- hts(dataTS, characters = c(3,3,3))
myHWfn<-function (x, h = frequency(x) * 2, lambda = NULL, ...)
{
if (!is.null(lambda)) {
origx <- x
x <- BoxCox(x, lambda)}
fit <- HoltWinters(x)
fcast <- forecast.HoltWinters(fit, h = h, lambda = lambda, ...)
return(fcast)
}
myFCST<-forecast(myhTS,h=52, method = "comb", FUN = myHWfn)
这里的问题是HoltWinters
没有以易于访问的形式返回拟合值,并且forecast.hts
没有正确检查返回的对象是否正确表述。
我将在未来的版本中改进检查,以提供更有意义的错误消息。
下面的代码将使用hw
函数做完全相同的事情,这比HoltWinters
要好得多。
library(hts)
myHWfn <- function (x, h = frequency(x) * 2, lambda = NULL, ...)
{
fcast <- hw(x, h = h, lambda = lambda, initial='simple', ...)
return(fcast)
}
myFCST <- forecast(myhTS,h=52, method = "comb", FUN = myHWfn)