将 HoltWinters 应用于矩阵时产生的 NaN

NaNs produced when applying HoltWinters to a matrix

我正在尝试使用 forecast 库建立指数平滑模型。

> library(forecast)
> dput(dat)
structure(list(a = c(142.8163942, 143.5711365, 145.3485827, 142.0577145, 
139.4326176, 140.1236581, 138.6560282, 136.405036, 133.9337229, 
133.8785538, 132.0608441, 130.0866307, 120.1320237, 119.6368882, 
114.3312943, 117.5084111, 114.4960017, 112.9124518, 112.8185478, 
112.3047916, 106.632639, 106.2107158, 106.8455028, 106.3879556, 
104.3451786, 102.9085952, 101.0967783, 101.7858278, 101.0749044, 
102.6441976, 102.0666152, 100, 97.14084104, 97.49972913, 96.91453836, 
96.05132443, 94.98057971, 92.78373451, 92.67526281, 91.82430571, 
91.4153859, 89.51740671, 89.01587176, 84.62259911, 91.48598494, 
89.12053042, 90.02364352, 90.92496121, 89.42963565, 91.93886583, 
88.83918306, 90.39513509, 87.54571761, 91.3386451, 87.7836994, 
91.79178376, 87.56903138, 87.77875755, 89.29938784), b = c(17703.7, 
17599.8, 17328.2, 17044, 17078.3, 16872.3, 16619.2, 16502.4, 
16332.5, 16268.9, 16094.7, 15956.5, 15785.3, 15587.1, 15460.9, 
15238.4, 15230.2, 15057.7, 14888.6, 14681.1, 14566.5, 14384.1, 
14340.4, 14383.9, 14549.9, 14843, 14813, 14668.4, 14685.3, 14569.7, 
14422.3, 14233.2, 14066.4, 13908.5, 13799.8, 13648.9, 13381.6, 
13205.4, 12974.1, 12813.7, 12562.2, 12367.7, 12181.4, 11988.4, 
11816.8, 11625.1, 11370.7, 11230.1, 11103.8, 11037.1, 10934.8, 
10834.4, 10701.3, 10639.5, 10638.4, 10508.1, 10472.3, 10357.4, 
10278.3), c = c(2160.080078, 2203.939941, 2500.850098, 2523.820068, 
2546.54, 2528.449951, 2223.97998, 2352.01001, 2401.21, 2089.73999, 
1975.349976, 2159.060059, 1891.68, 1947.849976, 2766.72998, 2882.179932, 
2947.24, 2541.629883, 2278.800049, 2634, 2495.56, 2637.280029, 
2098.649902, 1696.619995, 1750.83, 2767.76001, 3943.149902, 3765.909912, 
4512.98, 4527.299805, 4869.259766, 4645.5, 4463.47, 3868.27002, 
3745.719971, 4139.830078, 3667.03, 3457.449951, 3049.909912, 
2632.899902, 2431.38, 2042.869995, 1989.400024, 1866.76001, 1545.15, 
1351.890015, 1305.709961, 1163.109985, 1150.05, 1070.209961, 
1243.069946, 1289.16, 1140.36, 1084.069946, 1206.819946, 1186.540039, 
1073.3, 1161.160034, 1129.579956), d = c(363.81, 361.19, 362.35, 
359.09, 359.31, 355.8, 356.64, 353.83, 353.49, 348.92, 348.8, 
344.85, 343.48, 340.75, 341.1, 335.72, 331.29, 328.21, 328.95, 
325.92, 324.83, 322.83, 323.18, 321.66, 322.94, 323.14, 322.89, 
318.34, 315.85, 311.61, 311.3, 308.34, 306.1, 305.64, 305.58, 
302.91, 301.64, 300.24, 299.54, 298.58, 296.4, 293.87, 293.35, 
291.61, 289.43, 288.03, 287.69, 287.6, 285.95, 284.8, 284.63, 
282.62, 281.24, 280, 280.09, 277.65, 275.73, 273.12, 272.78), 
    e = c(109.1, 109.1, 108.8, 108.2, 107.6, 107.2, 107.3, 106.7, 
    106.4, 106, 105.9, 104.9, 103.8, 103.5, 103, 102.3, 101.3, 
    100.5, 99.6, 98.6, 97.43314, 96.68301, 95.84954, 95.18276, 
    94.76602, 94.01589, 92.84903, 91.18208, 89.76517, 89.18174, 
    88.51496, 87.76484, 86.68132, 85.93119, 85.18107, 84.51429, 
    83.76416, 83.43077, 83.26407, 82.93068, 82.46215, 82.14979, 
    81.83744, 81.05654, 80.43183, 80.35374, 80.27565, 79.9633, 
    79.72903, 79.57285, 79.57285, 79.26049, 79.02623, 79.10432, 
    79.02623, 78.71387, 78.4796, 78.24534, 77.93298), f = c(132.2, 
    133.9, 133.5, 126, 125, 122.6, 122.6, 123.8, 124.5, 120.2, 
    120.2, 123.5, 105.2, 116.4, 111.5, 116.4, 116.1, 114.3, 117, 
    117.9, 107.1, 104.5, 110.6, 110.5, 104.2, 105.4, 106.2, 110.3, 
    106.8, 111.4, 111.2, 108.5, 93.5, 101.5, 101.4, 101.3, 101.7, 
    96.8, 97.3, 100, 97.5, 99.4, 94.8, 93.8, 101.9, 97.4, 97.7, 
    98.4, 100.6, 100.1, 96.3, 98.1, 93.4, 99.3, 97.3, 99.6, 99.2, 
    97.8, 100.1), g = c(149.8, 151.9, 153.2, 150.7, 146.5, 151.5, 
    149.2, 147.3, 143.6, 144.8, 143.6, 143.7, 134.1, 129.7, 124.3, 
    127.5, 123.7, 122.2, 123.1, 123.8, 117.1, 118.6, 116.4, 118.4, 
    116.4, 114.6, 111.9, 112.2, 114.5, 114.6, 113.4, 111.3, 112.8, 
    110.1, 110.8, 109.5, 108.8, 106.1, 107.1, 106.1, 107, 102.1, 
    103, 94.5, 107.2, 102.5, 103.9, 105.1, 101.7, 107.8, 102.4, 
    104.8, 103.6, 107.6, 99.5, 107.4, 97.8, 98.4, 100.8), h = c(112.6, 
    112.7, 113.6, 110.7, 113.4, 127.1, 130.1, 135.7, 123.7, 123.2, 
    123, 125.5, 113.5, 120.2, 123.3, 128, 128.2, 124.6, 124, 
    125.8, 122.2, 124.8, 116.6, 120.4, 115.9, 120.6, 124, 120.6, 
    119, 120.1, 111.6, 114, 110.2, 111.6, 104.5, 107.9, 100.4, 
    104.7, 105, 106.9, 105.1, 105.8, 97.3, 96.6, 99.1, 101.1, 
    102.5, 105.2, 103, 101, 102.7, 100.5, 107.4, 110.1, 101.3, 
    105.7, 100.3, 104.1, 98.4), i = c(0.000887705, 0.007954085, 
    -0.025859667, 0.024097552, 0.114052787, 0.023329207, 0.042143181, 
    -0.092587287, -0.004050228, -0.001624696, 0.020121403, -0.100502922, 
    0.057354185, 0.025463388, 0.037409854, 0.001561281, -0.028482938, 
    -0.004827041, 0.014411779, -0.029034298, 0.021053409, -0.067963182, 
    0.032070259, -0.038091783, 0.039751534, 0.027802281, -0.027802281, 
    -0.013355791, 0.009201236, -0.073403679, 0.021277398, -0.033901552, 
    0.012624153, -0.065733979, 0.032017801, -0.072042665, 0.041936911, 
    0.002861232, 0.017933468, -0.01698154, 0.006638242, -0.08375153, 
    -0.007220248, 0.0255507, 0.019980685, 0.013752673, 0.026000502, 
    -0.021134312, -0.019608471, 0.0166916, -0.021654389, 0.066402455, 
    0.024828862, -0.083302632, 0.042518482, -0.052439198, 0.037186281, 
    -0.056311172, -0.012270093), j = c(0, -0.007558004, -0.015289567, 
    0, 0, -0.009609384, -0.019500305, 0, 0, -0.012458015, -0.025391532, 
    -0.000983501, 0, -0.00165265, -0.003313516, 0.000204576, 
    0, -0.004898564, -0.009869709, 0, 0, -0.010574012, -0.021489482, 
    0, 0, -0.011534651, -0.023476287, 0, 0, -0.00814845, -0.016498838, 
    0, 0, -0.0099856, -0.020275409, -0.002818337, 0, -0.007212389, 
    -0.014582736, 0, 0, -0.004121565, -0.008294445, 0, 0, -0.010766386, 
    -0.021886884, 0, 0, -0.010179741, -0.02067574, 0, 0, -0.011797067, 
    -0.024020039, -0.002017983, -0.007343864, -0.007398196, -0.014962644
    ), k = c(0.012777325, -0.002991775, -0.057819571, -0.00796817, 
    -0.019386714, 0, 0.009740337, 0.005638356, -0.035148694, 
    0, 0.027084134, -0.160377856, 0.101169235, -0.043007944, 
    0.043007944, -0.002580647, -0.015625318, 0.023347364, 0.007662873, 
    -0.09607383, -0.024575906, 0.056733018, -0.000904568, -0.058703392, 
    0.011450507, 0.007561473, 0.037879817, -0.032246, 0.042169401, 
    -0.001796946, -0.024580209, -0.148788737, 0.082097362, -0.000985707, 
    -0.00098668, 0.003940892, -0.049380309, 0.005151995, 0.027371197, 
    -0.025317808, 0.019299736, -0.047382704, -0.010604553, 0.082827084, 
    -0.04516573, 0.003075348, 0.007139245, 0.022111454, -0.004982571, 
    -0.038701368, 0.018519048, -0.049096021, 0.061254226, -0.020346582, 
    0.023363175, -0.00402415, -0.014213437, 0.023245109, 0.027587957
    )), .Names = c("a", "b", "c", "d", "e", "f", "g", "h", "i", 
"j", "k"), row.names = c(NA, -59L), class = "data.frame")
> 
> # converted to time series object
> ts <- ts(dat, start=c(2000, 2), end=c(2014, 4), frequency=4) 
> 
> # Exponential Smoothing
> #######################
> fit <- HoltWinters(ts)
> 
> # predict next three future values
> fc <- forecast(fit, 3)
Warning messages:
1: In sqrt(sapply(1L:n.ahead, vars)) : NaNs produced
2: In cbind(fit = fit, upr = if (prediction.interval) fit + int, lwr = if (prediction.interval) fit -  :
  number of rows of result is not a multiple of vector length (arg 1)
> (fc)
        Point Forecast       Lo 80         Hi 80       Lo 95        Hi 95
2162 Q3       1.035663   2.0279344  4.339088e-02   2.5532113   -0.4818861
2162 Q4       1.369902         NaN           NaN         NaN          NaN
2163 Q1      -2.293179         NaN           NaN         NaN          NaN
2163 Q2       1.035663         NaN           NaN         NaN          NaN
2163 Q3       1.369902   0.3964019  2.343402e+00  -0.1189380    2.8587424
2163 Q4      -2.293179   0.4764679 -5.062826e+00   1.9426305   -6.5289887
2164 Q1       1.035663   2.1153105 -4.398523e-02   2.6868416   -0.6155163
2164 Q2       1.369902   0.5702614  2.169543e+00   0.1469572    2.5928472
2164 Q3      -2.293179   0.5075722 -5.093930e+00   1.9902003   -6.5765585
2164 Q4       1.035663   2.3404760 -2.691508e-01   3.0312025   -0.9598772
2165 Q1       1.369902   0.6740083  2.065796e+00   0.3056244    2.4341800
2165 Q2      -2.293179         NaN           NaN         NaN          NaN
2165 Q3       1.035663 534.1230685 -5.320517e+02 816.3224948 -814.2511696
2165 Q4       1.369902 275.4577273 -2.727179e+02 420.5510575 -417.8112532
2166 Q1      -2.293179  36.2907545 -4.087711e+01  56.7158551  -61.3022133
2166 Q2       1.035663         NaN           NaN         NaN          NaN
2166 Q3       1.369902         NaN           NaN         NaN          NaN
2166 Q4      -2.293179         NaN           NaN         NaN          NaN
2167 Q1       1.035663         NaN           NaN         NaN          NaN
2167 Q2       1.369902         NaN           NaN         NaN          NaN
2167 Q3      -2.293179         NaN           NaN         NaN          NaN
2167 Q4       1.035663         NaN           NaN         NaN          NaN
2168 Q1       1.369902         NaN           NaN         NaN          NaN
2168 Q2      -2.293179 275.2906075 -2.798770e+02 422.2345880 -426.8209461
2168 Q3       1.035663 289.0380351 -2.869667e+02 441.4972817 -439.4259564
2168 Q4       1.369902  19.7729261 -1.703312e+01  29.5148985  -26.7750941
2169 Q1      -2.293179         NaN           NaN         NaN          NaN
2169 Q2       1.035663         NaN           NaN         NaN          NaN
2169 Q3       1.369902         NaN           NaN         NaN          NaN
2169 Q4      -2.293179   7.7255052 -1.231186e+01  13.0290763  -17.6154345
2170 Q1       1.035663         NaN           NaN         NaN          NaN
2170 Q2       1.369902         NaN           NaN         NaN          NaN
2170 Q3      -2.293179         NaN           NaN         NaN          NaN
2170 Q4       1.035663         NaN           NaN         NaN          NaN
2171 Q1       1.369902  36.4578743 -3.371807e+01  55.0323247  -52.2925203
2171 Q2      -2.293179  19.6058063 -2.419216e+01  31.1984289  -35.7847871
2171 Q3       1.035663   4.0175577 -1.946232e+00   5.5960776   -3.5247523
2171 Q4       1.369902         NaN           NaN         NaN          NaN
2172 Q1      -2.293179         NaN           NaN         NaN          NaN
2172 Q2       1.035663         NaN           NaN         NaN          NaN
2172 Q3       1.369902         NaN           NaN         NaN          NaN
2172 Q4      -2.293179         NaN           NaN         NaN          NaN
2173 Q1       1.035663         NaN           NaN         NaN          NaN
2173 Q2       1.369902         NaN           NaN         NaN          NaN
2173 Q3      -2.293179   0.2292821 -4.815640e+00   1.5645924   -6.1509506
2173 Q4       1.035663         NaN           NaN         NaN          NaN
2174 Q1       1.369902         NaN           NaN         NaN          NaN
2174 Q2      -2.293179         NaN           NaN         NaN          NaN
2174 Q3       1.035663   3.2475589 -1.176234e+00   4.4184661   -2.3471408
2174 Q4       1.369902   1.6773878  1.062417e+00   1.8401608    0.8996435
2175 Q1      -2.293179   0.4857532 -5.072111e+00   1.9568311   -6.5431893
2175 Q2       1.035663   3.4984352 -1.427110e+00   4.8021483   -2.7308230
2175 Q3       1.369902   2.4454550  2.943494e-01   3.0148183   -0.2750139
2175 Q4      -2.293179   2.2791427 -6.865501e+00   4.6995836   -9.2859418
2176 Q1       1.035663   4.1105874 -2.039262e+00   5.7383542   -3.6670289
2176 Q2       1.369902   0.6435877  2.096217e+00   0.2591001    2.4807043
2176 Q3      -2.293179         NaN           NaN         NaN          NaN
2176 Q4       1.035663         NaN           NaN         NaN          NaN
2177 Q1       1.369902         NaN           NaN         NaN          NaN
2177 Q2      -2.293179   1.5102680 -6.096626e+00   3.5236913   -8.1100495
2177 Q3       1.035663   3.6965071 -1.625182e+00   5.1050730   -3.0337478
2177 Q4       1.369902   0.8681394  1.871665e+00   0.6025222    2.1372822
2178 Q1      -2.293179   1.8254258 -6.411784e+00   4.0056835   -8.5920417
2178 Q2       1.035663   4.3755533 -2.304228e+00   6.1435847   -4.0722594
2178 Q3       1.369902   2.7123815  2.742291e-02   3.4230471   -0.6832427
2178 Q4      -2.293179   2.5440277 -7.130386e+00   5.1046903   -9.6910485
2179 Q1       1.035663   2.1153105 -4.398523e-02   2.6868416   -0.6155163
2179 Q2       1.369902         NaN           NaN         NaN          NaN
2179 Q3      -2.293179         NaN           NaN         NaN          NaN
2179 Q4       1.035663         NaN           NaN         NaN          NaN
2180 Q1       1.369902   0.6528730  2.086931e+00   0.2733006    2.4665037
2180 Q2      -2.293179   0.7010196 -5.287378e+00   2.2860527   -6.8724108
2180 Q3       1.035663   2.3417869 -2.704616e-01   3.0332072   -0.9618820
2180 Q4       1.369902   0.8366770  1.903127e+00   0.5544046    2.1853998
2181 Q1      -2.293179   1.0238935 -5.610252e+00   2.7798456   -7.3662038
2181 Q2       1.035663   2.8567227 -7.853975e-01   3.8207337   -1.7494084
2181 Q3       1.369902   1.1909803  1.548824e+00   1.0962648    1.6435396
2181 Q4      -2.293179   0.4031416 -4.989500e+00   1.8304876   -6.4168458
2182 Q1       1.035663         NaN           NaN         NaN          NaN
2182 Q2       1.369902   7.8926250 -5.152821e+00  11.3455459   -8.6057415
2182 Q3      -2.293179         NaN           NaN         NaN          NaN
2182 Q4       1.035663   3.4984352 -1.427110e+00   4.8021483   -2.7308230
2183 Q1       1.369902   1.9925455  7.472588e-01   2.3221530    0.4176513
2183 Q2      -2.293179   0.6695572 -5.255915e+00   2.2379350   -6.8242932
2183 Q3       1.035663   4.0466399 -1.975315e+00   5.6405550   -3.5692298
2183 Q4       1.369902   2.7454185 -5.614095e-03   3.4735728   -0.7337684
2184 Q1      -2.293179   2.5804905 -7.166849e+00   5.1604554   -9.7468135
2184 Q2       1.035663   4.4118169 -2.340492e+00   6.1990450   -4.1277197
2184 Q3       1.369902   0.6746919  2.065112e+00   0.3066699    2.4331345
2184 Q4      -2.293179         NaN           NaN         NaN          NaN
2185 Q1       1.035663         NaN           NaN         NaN          NaN
2185 Q2       1.369902         NaN           NaN         NaN          NaN
2185 Q3      -2.293179   2.2783352 -6.864693e+00   4.6983487   -9.2847069
2185 Q4       1.035663   4.3755533 -2.304228e+00   6.1435847   -4.0722594
2186 Q1       1.369902   1.1910132  1.548791e+00   1.0963152    1.6434892
2186 Q2      -2.293179   2.5782987 -7.164657e+00   5.1571032   -9.7434614
2186 Q3       1.035663   5.6968582 -3.625533e+00   8.1643461   -6.0930208
2186 Q4       1.369902   4.0332547 -1.293450e+00   5.4431484   -2.7033440
2187 Q1      -2.293179   3.8664354 -8.452794e+00   7.1271383  -11.7134965
2187 Q2       1.035663   2.3404760 -2.691508e-01   3.0312025   -0.9598772
2187 Q3       1.369902         NaN           NaN         NaN          NaN
2187 Q4      -2.293179         NaN           NaN         NaN          NaN
2188 Q1       1.035663         NaN           NaN         NaN          NaN
2188 Q2       1.369902   2.4462624  2.935419e-01   3.0160531   -0.2762488
2188 Q3      -2.293179   2.5452617 -7.131620e+00   5.1065775   -9.6929357
2188 Q4       1.035663   2.8567227 -7.853975e-01   3.8207337   -1.7494084
2189 Q1       1.369902   2.7476103 -7.805928e-03   3.4769249   -0.7371205
2189 Q2      -2.293179   3.8661349 -8.452493e+00   7.1266788  -11.7130370
2189 Q3       1.035663   5.6985089 -3.627184e+00   8.1668706   -6.0955454
2189 Q4       1.369902   4.0343828 -1.294578e+00   5.4448736   -2.7050692
2190 Q1      -2.293179   0.5068886 -5.093247e+00   1.9891548   -6.5755130
2190 Q2       1.035663         NaN           NaN         NaN          NaN
2190 Q3       1.369902         NaN           NaN         NaN          NaN
2190 Q4      -2.293179         NaN           NaN         NaN          NaN
2191 Q1       1.035663   4.1105874 -2.039262e+00   5.7383542   -3.6670289
2191 Q2       1.369902   2.7111475  2.865690e-02   3.4211598   -0.6813555
2191 Q3      -2.293179   1.0238605 -5.610219e+00   2.7797953   -7.3661534
2191 Q4       1.035663   4.4118169 -2.340492e+00   6.1990450   -4.1277197
2192 Q1       1.369902   4.0335552 -1.293751e+00   5.4436079   -2.7038035
2192 Q2      -2.293179   3.8672630 -8.453621e+00   7.1284040  -11.7147622
2192 Q3       1.035663   5.6991175 -3.627792e+00   8.1678013   -6.0964760

如您所见,我的时间序列对象未正确反映在预测中。

我的猜测是我对 ts 对象的定义是错误的。但是,关于如何定义时间序列的任何建议,即 quaterlyQ2 2000 开始一直持续到 Q4 2014?

感谢您的回复!

由于您的输入数据由 8 列('a' 到 'h')组成,您正在尝试对 8 个时间序列的矩阵应用 HoltWinters 算法。我不认为 HoltWinters 可以同时处理多个时间序列,所以它试图将它们端到端地结合起来。这就是为什么您会得到直到 2162 年的预测..

如果您的第一行代码是:

ts <- ts(dat$a, ..... 

然后你在 'a' 列上工作并且 returns 没有错误。