所以几天前我问了一个关于如何预测使用tslm函数的问题。
Forecasting with `tslm` returning dimension error
事实证明,我传递的函数没有任何新数据可以预测,因此我收到了错误。
但是,我现在有了一些新的数据,我仍然在努力,即使我传递新数据,我也会遇到与之前相同的错误。我已经意识到由于没有创建正确的数据帧而导致的问题,因此我已将所有内容都转换为数据框。
这是一些与我尝试的非常类似的R代码: -
library("forecast")
ts.in <- ts(input) #data vector for input shown below question
ts.out <- ts(output) #data vector for output variable shown below question
df.oldData <- data.frame(ts.in, ts.out)
new.ts <- ts(newInput) # these are my projected values
df.newData <- data.frame(new.ts)
fit1 <- tslm(ts.out ~ ts.in)
projection <- forecast.lm(fit1, newdata = df.newData)
但我仍然遇到尺寸错误: -
Error in forecast.lm(fit1, newdata = df.newData) :
Variables not found in newdata
In addition: Warning messages:
1: 'newdata' had 10 rows but variables found have 696 rows
2: 'newdata' had 10 rows but variables found have 696 rows
如何让R看到新数据并正确预测?我的智慧结束了。即使我只是将新数据粘贴到原始数据并将数据集拆分为2行,我仍然会出现维度错误。
下面提供了一些示例数据。
这里输入变量的dput
和输出数据作为时间序列的data.frame: -
>dput(df.oldData)
structure(list(ts.in = structure(c(0, 22, 0, 145, 113, 118, 155,
323, 103, 151, 311, 143, 106, 25, 3, 0, 4, 0, 4, 0, 0, 0, 0,
0, 0, 0, 0, 96, 128, 229, 112, 159, 154, 70, 260, 110, 12, 56,
6, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 102, 320, 168, 83,
409, 203, 235, 83, 134, 195, 59, 0, 5, 0, 5, 0, 0, 0, 0, 0, 0,
0, 0, 259, 165, 101, 211, 387, 185, 193, 314, 61, 82, 70, 52,
0, 11, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 178, 236, 377, 169, 233,
444, 303, 457, 325, 5, 102, 43, 0, 0, 4, 18, 0, 0, 0, 0, 0, 4,
0, 2, 139, 744, 319, 618, 265, 387, 672, 366, 273, 527, 55, 8,
0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 146, 256, 189, 201, 204,
383, 399, 177, 151, 38, 20, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0,
0, 145, 103, 172, 172, 277, 253, 153, 393, 97, 188, 42, 0, 21,
6, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 152, 124, 249, 254, 206, 242,
138, 307, 349, 63, 336, 118, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 97, 78, 134, 101, 177, 246, 316, 315, 158, 24, 62, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, 136, 118, 187, 181, 179, 143,
165, 184, 228, 54, 0, 0, 5, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 292,
232, 283, 323, 159, 190, 187, 137, 148, 22, 174, 57, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 8, 88, 341, 564, 263, 102, 348, 234,
154, 208, 135, 17, 11, 0, 5, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 95,
154, 188, 111, 158, 499, 678, 266, 249, 128, 132, 29, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 20, 0, 49, 60, 46, 106, 179, 487, 203, 266,
49, 25, 46, 0, 4, 3, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 93, 98, 145,
265, 56, 227, 178, 179, 244, 13, 150, 33, 47, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 132, 225, 106, 165, 259, 110, 430, 457, 29,
4, 2, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 87, 225, 338, 185,
129, 316, 437, 561, 321, 46, 218, 53, 0, 0, 18, 0, 0, 0, 0, 0,
0, 0, 0, 0, 111, 226, 580, 271, 235, 128, 210, 534, 218, 232,
28, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151, 446, 305, 152,
213, 223, 392, 280, 132, 320, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 479, 599, 465, 290, 560, 977, 963, 1062, 1240, 138,
819, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 178, 378, 116,
180, 159, 225, 507, 155, 270, 392, 17, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 191, 140, 175, 208, 244, 374, 193, 344, 799, 573,
54, 5, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 6, 0, 223, 274, 184, 252,
538, 381, 160, 558, 336, 205, 233, 49, 0, 0, 0, 8, 0, 0, 0, 0,
0, 0, 2, 4, 283, 177, 157, 532, 214, 311, 508, 440, 481, 139,
422, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 296, 413, 392, 295,
285, 189, 371, 819, 444, 110, 56, 4, 0, 0, 0, 6, 0, 0, 0, 0,
0, 5, 0, 0, 433, 368, 299, 295, 372, 531, 534, 471, 254, 51,
2, 5, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 13, 29, 110, 83, 102, 115,
123, 254, 130, 233, 288, 117, 14, 11, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 51, 34, 39, 85, 113, 132, 154, 334, 138, 132, 117, 17,
76, 70, 0, 0, 0, 0, 0, 0, 0, 0), .Tsp = c(1, 696, 1), class = "ts"),
ts.out = structure(c(115, 331, 558, 867, 1066, 1172, 1212,
1214, 1175, 992, 1077, 1054, 1192, 1257, 1746, 1781, 1525,
606, 329, 117, 57, 74, 38, 82, 182, 313, 478, 762, 950, 1120,
1511, 1549, 1501, 1483, 1637, 1369, 1414, 1795, 1958, 2176,
1843, 879, 365, 165, 122, 103, 66, 88, 184, 2053, 1951, 2253,
2235, 2053, 2020, 1976, 1879, 1657, 1750, 1770, 1615, 1777,
2247, 2079, 1643, 1004, 405, 179, 66, 80, 54, 82, 239, 415,
703, 983, 1246, 1255, 1467, 1660, 1605, 1290, 1388, 1193,
1174, 1218, 1462, 1524, 1124, 831, 360, 173, 115, 61, 50,
76, 168, 385, 911, 1096, 1327, 1459, 1449, 1450, 1630, 1622,
1697, 1913, 1520, 1253, 1411, 1221, 911, 619, 408, 200, 63,
81, 58, 66, 106, 343, 719, 1204, 1668, 1680, 1698, 1473,
1583, 1808, 1988, 2124, 2010, 2220, 2413, 2024, 1436, 852,
292, 125, 92, 92, 67, 88, 195, 1124, 1493, 1797, 1850, 1844,
1800, 2131, 1723, 1554, 1814, 1762, 1756, 2037, 2639, 2511,
1957, 875, 435, 160, 85, 58, 56, 104, 230, 462, 704, 969,
1318, 1337, 1366, 1665, 1461, 1500, 1391, 1306, 1281, 1593,
2020, 2046, 1633, 838, 339, 158, 78, 54, 57, 79, 171, 398,
624, 952, 1110, 1224, 1248, 1246, 1184, 1096, 1261, 1093,
1129, 1323, 1650, 1927, 1648, 809, 330, 116, 86, 72, 46,
99, 154, 365, 637, 818, 1067, 1037, 1156, 1275, 1009, 1046,
1225, 1232, 1111, 1347, 1693, 1677, 1367, 791, 240, 123,
82, 94, 70, 115, 217, 434, 586, 951, 987, 1133, 1023, 1087,
1174, 1063, 1079, 948, 975, 1058, 1194, 1143, 982, 588, 328,
168, 81, 34, 57, 69, 156, 436, 670, 931, 1082, 1074, 944,
883, 1014, 1093, 1010, 1169, 860, 846, 1033, 998, 979, 594,
291, 142, 72, 59, 48, 65, 118, 394, 778, 1048, 1152, 1268,
1375, 1159, 1076, 1361, 1487, 1676, 1523, 1941, 2201, 2254,
1597, 672, 264, 127, 78, 31, 53, 98, 178, 1378, 1541, 1870,
2016, 1869, 2114, 2373, 2041, 1877, 2066, 1707, 1668, 2327,
3088, 2818, 2395, 1155, 439, 156, 109, 83, 58, 113, 282,
521, 830, 1219, 1598, 1844, 1795, 2003, 1728, 1905, 1528,
1568, 1467, 1863, 2619, 2668, 2070, 1094, 413, 129, 70, 59,
37, 118, 206, 585, 817, 1243, 1418, 1421, 1400, 1507, 1399,
1309, 1374, 1408, 1216, 1421, 1932, 2277, 1636, 895, 302,
157, 82, 70, 73, 70, 181, 470, 673, 1118, 1331, 1425, 1428,
1536, 1453, 1442, 1517, 1316, 1285, 1400, 1730, 1712, 1646,
805, 291, 169, 112, 80, 50, 75, 257, 374, 694, 1182, 1222,
1268, 1253, 1454, 1269, 1337, 1514, 1302, 1220, 1195, 1433,
1383, 1061, 740, 386, 161, 70, 89, 72, 73, 174, 979, 1594,
1903, 1797, 1963, 1863, 1767, 1804, 1933, 2015, 1771, 1647,
1526, 1540, 1344, 1154, 661, 370, 224, 117, 58, 66, 70, 136,
337, 806, 1141, 1747, 1691, 1762, 1468, 1529, 1645, 1668,
1565, 1894, 1565, 1758, 1653, 1291, 810, 311, 147, 107, 67,
95, 88, 164, 337, 797, 1419, 1920, 2187, 2149, 1868, 2092,
2343, 2379, 2765, 2230, 2305, 2633, 2286, 1667, 800, 358,
142, 86, 43, 41, 124, 141, 1149, 1534, 2058, 2083, 2004,
2253, 2223, 2081, 2131, 2247, 1937, 1964, 2301, 2577, 2444,
1957, 954, 509, 204, 91, 54, 64, 87, 196, 423, 906, 1412,
1540, 1695, 1632, 1805, 1637, 1774, 1762, 1727, 1697, 1914,
2217, 2354, 1810, 976, 486, 233, 98, 88, 77, 126, 166, 454,
832, 1279, 1638, 1709, 1672, 1839, 1607, 2451, 5066, 4860,
4496, 5362, 6230, 6373, 4830, 2555, 1366, 405, 315, 196,
188, 221, 458, 1280, 2157, 3170, 3420, 3983, 4142, 4126,
3814, 4263, 3947, 3840, 3632, 4006, 4274, 4160, 4068, 2180,
1073, 537, 326, 164, 228, 276, 515, 1149, 2056, 3350, 3617,
3645, 3258, 3326, 3130, 3795, 3984, 4140, 3255, 3332, 3965,
3479, 2927, 1855, 968, 448, 280, 110, 134, 216, 413, 937,
2065, 3290, 4263, 4392, 4091, 4200, 4479, 4567, 5086, 5749,
5339, 6093, 6957, 6898, 4582, 2386, 1038, 356, 260, 222,
191, 301, 468, 3184, 4062, 4982, 5516, 5382, 5829, 5915,
5983, 4826, 5443, 5758, 6113, 6609, 8605, 7956, 6102, 3105,
1341, 453, 169, 189, 151, 305, 305, 686, 1314, 2146, 3674,
4702, 4444, 4913, 5196, 4148, 4380, 4432, 4413, 4558, 5131,
6462, 6775, 5836, 2948, 1125, 495, 222, 126, 138), .Tsp = c(1,
696, 1), class = "ts")), .Names = c("ts.in", "ts.out"), row.names = c(NA,
-696L), class = "data.frame")
然后是输入数据的一些预测值: -
> dput(df.newData)
structure(list(new.ts = structure(c(52, 79, 105, 130, 151, 167,
179, 185, 186, 182), .Tsp = c(1, 10, 1), class = "ts")), .Names = "new.ts", row.names = c(NA,
-10L), class = "data.frame")
答案 0 :(得分:3)
进行预测时,模型中用作预测变量的所有列的名称必须与新数据中的列相同。
使用上面的示例data.frames,这应该可行
#change name to match the model data
names(df.newData)<-"ts.in"
#this should be true
# > names(df.oldData)
# [1] "ts.in" "ts.out"
# > names(df.newData)
# [1] "ts.in"
#use formala syntax with data.frame and correct variable names
fit1 <- tslm(ts.out ~ ts.in, df.oldData)
#now predict using ts.in values from df.newData
projection <- forecast.lm(fit1, newdata = df.newData)