问题描述
我进行TS分析.我的dput().
I conduct TS analysis.My dput().
df=structure(list(month = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L), .Label = c("Apr", "Aug", "Dec", "Feb", "Jan", "Jul",
"Jun", "Mar", "May", "Nov", "Oct", "Sep"), class = "factor"),
year = c(1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L,
1979L, 1979L, 1979L, 1979L, 1979L, 1980L, 1980L, 1980L, 1980L,
1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1984L,
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1984L,
1984L, 1984L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L, 1986L, 1986L, 1986L, 1986L,
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1987L,
1987L, 1987L, 1987L), cake = structure(c(54L, 33L, 62L, 70L,
92L, 97L, 3L, 99L, 80L, 88L, 73L, 83L, 46L, 26L, 53L, 61L,
87L, 94L, 2L, 96L, 74L, 82L, 65L, 77L, 38L, 20L, 45L, 52L,
81L, 90L, 1L, 93L, 66L, 76L, 56L, 69L, 30L, 15L, 37L, 44L,
75L, 85L, 100L, 89L, 57L, 68L, 47L, 60L, 23L, 11L, 29L, 36L,
67L, 79L, 98L, 84L, 48L, 59L, 39L, 51L, 18L, 8L, 22L, 28L,
58L, 72L, 95L, 78L, 40L, 50L, 31L, 43L, 14L, 6L, 17L, 21L,
49L, 64L, 91L, 71L, 32L, 42L, 24L, 35L, 10L, 5L, 13L, 16L,
41L, 55L, 86L, 63L, 25L, 34L, 19L, 27L, 7L, 4L, 9L, 12L), .Label = c("10154.696",
"10379.224", "10603.752", "5689.504", "5914.032", "6138.560",
"6238.622", "6363.087", "6442.272", "6463.150", "6587.615",
"6650.772", "6666.799", "6687.677", "6812.142", "6875.300",
"6891.327", "6912.205", "6998.555", "7036.670", "7099.828",
"7115.855", "7136.733", "7223.083", "7248.663", "7261.198",
"7297.533", "7324.355", "7340.382", "7361.260", "7447.610",
"7473.191", "7485.725", "7509.326", "7522.060", "7548.883",
"7564.910", "7585.788", "7672.138", "7697.719", "7705.501",
"7733.854", "7746.588", "7773.411", "7789.438", "7810.315",
"7896.666", "7922.246", "7930.029", "7958.381", "7971.115",
"7997.938", "8013.965", "8034.843", "8106.171", "8121.193",
"8146.774", "8154.557", "8182.909", "8195.643", "8222.466",
"8238.493", "8304.060", "8330.699", "8345.721", "8371.301",
"8379.084", "8407.436", "8420.171", "8446.994", "8528.588",
"8555.227", "8570.249", "8595.829", "8603.612", "8631.964",
"8644.698", "8753.116", "8779.754", "8820.357", "8828.140",
"8856.492", "8869.226", "8977.643", "9004.282", "9032.058",
"9052.667", "9081.019", "9202.171", "9228.809", "9256.586",
"9277.195", "9426.698", "9453.337", "9481.114", "9651.226",
"9677.865", "9705.641", "9875.754", "9930.169"), class = "factor"),
icecream = structure(c(79L, 63L, 81L, 84L, 95L, 97L, 100L,
98L, 88L, 91L, 82L, 87L, 62L, 48L, 67L, 72L, 89L, 93L, 99L,
94L, 76L, 83L, 70L, 74L, 50L, 33L, 52L, 58L, 78L, 85L, 96L,
90L, 65L, 73L, 57L, 64L, 38L, 24L, 41L, 46L, 68L, 75L, 92L,
80L, 54L, 61L, 47L, 53L, 28L, 14L, 30L, 36L, 56L, 66L, 86L,
71L, 42L, 49L, 37L, 43L, 19L, 9L, 21L, 25L, 45L, 55L, 77L,
60L, 31L, 39L, 27L, 32L, 12L, 5L, 13L, 17L, 35L, 44L, 69L,
51L, 22L, 29L, 18L, 23L, 7L, 2L, 8L, 10L, 26L, 34L, 59L,
40L, 15L, 20L, 11L, 16L, 3L, 1L, 4L, 6L), .Label = c("3385.834",
"3778.830", "3948.779", "4124.808", "4178.019", "4319.547",
"4342.230", "4517.359", "4585.426", "4711.662", "4736.627",
"4742.014", "4915.965", "5004.123", "5014.631", "5021.709",
"5109.700", "5131.026", "5150.224", "5261.328", "5322.590",
"5410.013", "5415.633", "5439.154", "5515.562", "5527.707",
"5532.057", "5570.050", "5656.213", "5740.192", "5812.339",
"5816.038", "5899.870", "5914.260", "5925.189", "5932.101",
"5941.954", "6006.774", "6057.884", "6084.031", "6173.592",
"6224.003", "6225.088", "6311.199", "6330.308", "6363.928",
"6364.167", "6407.747", "6468.652", "6470.314", "6479.920",
"6631.617", "6646.099", "6648.738", "6715.593", "6745.815",
"6804.502", "6819.387", "6826.069", "6882.919", "6892.105",
"6984.050", "7046.898", "7084.607", "7092.952", "7130.098",
"7134.058", "7176.124", "7222.477", "7274.188", "7295.503",
"7316.767", "7334.341", "7551.164", "7558.942", "7569.422",
"7626.165", "7629.133", "7648.558", "7721.562", "7752.579",
"7801.250", "7807.326", "7917.988", "8009.607", "8039.695",
"8071.334", "8112.267", "8121.785", "8167.836", "8341.913",
"8467.124", "8497.833", "8647.995", "8708.185", "8915.546",
"9071.327", "9199.960", "9399.616", "9961.726"), class = "factor"),
kex = structure(c(37L, 28L, 46L, 55L, 91L, 4L, 21L, 13L,
73L, 90L, 66L, 82L, 41L, 32L, 50L, 59L, 100L, 8L, 24L, 16L,
78L, 95L, 69L, 85L, 43L, 35L, 53L, 62L, 2L, 10L, 27L, 19L,
81L, 97L, 71L, 87L, 45L, 36L, 54L, 63L, 3L, 11L, 26L, 18L,
80L, 96L, 70L, 86L, 44L, 34L, 52L, 61L, 1L, 9L, 25L, 17L,
77L, 94L, 68L, 84L, 42L, 33L, 51L, 60L, 99L, 7L, 23L, 15L,
76L, 92L, 67L, 83L, 40L, 31L, 49L, 58L, 98L, 6L, 22L, 14L,
74L, 89L, 65L, 79L, 39L, 30L, 48L, 57L, 93L, 5L, 20L, 12L,
72L, 88L, 64L, 75L, 38L, 29L, 47L, 56L), .Label = c("10012.354",
"10027.147", "10031.100", "10284.403", "10298.083", "10350.199",
"10394.860", "10408.841", "10429.411", "10448.012", "10449.622",
"10524.089", "10551.548", "10577.256", "10623.312", "10654.457",
"10659.784", "10682.780", "10685.561", "11238.047", "11245.777",
"11290.695", "11336.062", "11358.832", "11371.587", "11393.214",
"11393.847", "7924.553", "7993.174", "8049.233", "8099.100",
"8114.649", "8140.749", "8171.107", "8173.470", "8185.131",
"8421.128", "8528.465", "8584.070", "8633.341", "8636.581",
"8674.186", "8701.261", "8703.415", "8715.747", "8724.407",
"8759.735", "8816.240", "8866.689", "8893.873", "8909.120",
"8940.573", "8947.258", "8956.228", "8975.999", "8981.998",
"9038.938", "9089.956", "9128.165", "9133.149", "9165.665",
"9176.489", "9182.893", "9260.483", "9315.139", "9339.247",
"9363.164", "9402.322", "9417.254", "9429.165", "9437.884",
"9482.695", "9528.447", "9536.369", "9573.356", "9583.098",
"9620.489", "9622.236", "9628.487", "9644.810", "9649.651",
"9667.118", "9677.137", "9717.143", "9738.233", "9745.187",
"9755.734", "9757.324", "9811.494", "9820.126", "9846.204",
"9858.879", "9883.296", "9897.166", "9905.657", "9922.767",
"9929.587", "9934.869", "9978.806", "9983.549"), class = "factor"),
bread = structure(c(84L, 74L, 85L, 88L, 96L, 97L, 100L, 98L,
91L, 92L, 83L, 89L, 68L, 57L, 71L, 75L, 87L, 93L, 99L, 94L,
79L, 81L, 72L, 77L, 54L, 41L, 58L, 62L, 78L, 82L, 95L, 86L,
66L, 70L, 60L, 64L, 40L, 28L, 46L, 49L, 67L, 73L, 90L, 76L,
53L, 59L, 47L, 52L, 29L, 19L, 34L, 38L, 55L, 63L, 80L, 65L,
43L, 48L, 36L, 42L, 20L, 11L, 23L, 27L, 44L, 51L, 69L, 56L,
32L, 37L, 25L, 31L, 12L, 6L, 15L, 17L, 33L, 39L, 61L, 45L,
22L, 26L, 16L, 21L, 7L, 2L, 8L, 10L, 24L, 30L, 50L, 35L,
14L, 18L, 9L, 13L, 3L, 1L, 4L, 5L), .Label = c("2166.345",
"2648.523", "2736.609", "2898.017", "3085.471", "3140.171",
"3219.482", "3379.514", "3539.234", "3566.302", "3644.388",
"3712.041", "3816.960", "3832.007", "3870.272", "4023.558",
"4056.190", "4071.310", "4165.873", "4217.487", "4300.558",
"4317.832", "4373.292", "4374.852", "4518.024", "4556.376",
"4558.044", "4712.338", "4740.699", "4753.932", "4794.067",
"4814.280", "4863.891", "4893.102", "4908.819", "5026.050",
"5051.821", "5076.228", "5242.140", "5289.752", "5298.084",
"5300.797", "5325.007", "5364.609", "5395.421", "5437.073",
"5552.911", "5561.179", "5617.793", "5658.289", "5741.750",
"5825.819", "5855.726", "5879.818", "5881.214", "5892.896",
"5955.957", "6018.704", "6089.938", "6107.488", "6145.685",
"6195.500", "6256.822", "6377.603", "6405.031", "6416.234",
"6420.458", "6546.651", "6644.214", "6647.421", "6668.263",
"6706.952", "6793.825", "6814.597", "6837.318", "6937.775",
"6972.282", "6994.417", "7026.075", "7157.797", "7251.932",
"7364.200", "7394.167", "7444.071", "7495.351", "7501.435",
"7629.006", "7637.950", "7648.955", "7692.634", "7737.427",
"7950.654", "7992.020", "8116.917", "8259.580", "8406.970",
"8750.245", "8842.216", "8881.043", "9621.858"), class = "factor"),
pie = structure(c(69L, 67L, 72L, 79L, 6L, 25L, 56L, 39L,
95L, 9L, 91L, 2L, 74L, 68L, 81L, 86L, 21L, 41L, 60L, 47L,
7L, 19L, 99L, 12L, 80L, 70L, 84L, 93L, 29L, 46L, 61L, 50L,
14L, 26L, 4L, 20L, 82L, 71L, 88L, 97L, 35L, 48L, 62L, 54L,
17L, 32L, 11L, 23L, 83L, 73L, 92L, 1L, 38L, 49L, 63L, 55L,
22L, 36L, 13L, 28L, 85L, 75L, 94L, 3L, 42L, 51L, 64L, 57L,
24L, 37L, 15L, 31L, 87L, 76L, 96L, 5L, 44L, 52L, 65L, 58L,
27L, 40L, 16L, 33L, 89L, 77L, 98L, 8L, 45L, 53L, 66L, 59L,
30L, 43L, 18L, 34L, 90L, 78L, 100L, 10L), .Label = c("10021.538",
"10089.497", "10090.666", "10134.899", "10143.466", "10147.420",
"10165.583", "10184.298", "10211.385", "10216.074", "10240.421",
"10317.115", "10318.226", "10326.369", "10377.197", "10422.607",
"10437.822", "10457.876", "10461.052", "10462.739", "10476.329",
"10519.485", "10565.467", "10581.157", "10605.485", "10616.507",
"10628.549", "10641.434", "10661.863", "10665.320", "10699.108",
"10724.935", "10743.562", "10778.105", "10786.766", "10804.639",
"10864.942", "10876.955", "10909.292", "10911.331", "10914.655",
"10944.505", "10947.342", "10996.167", "11036.150", "11093.419",
"11185.536", "11214.739", "11302.686", "11351.962", "11368.703",
"11419.258", "11458.410", "11466.567", "11550.255", "11585.645",
"11613.335", "11661.755", "11699.302", "11877.405", "12049.813",
"12167.703", "12253.485", "12318.013", "12367.487", "12405.828",
"8156.854", "8566.438", "8625.615", "8775.256", "8911.947",
"8981.635", "9009.357", "9073.980", "9081.786", "9136.948",
"9179.541", "9212.663", "9256.038", "9291.757", "9359.667",
"9432.769", "9532.766", "9560.171", "9606.922", "9607.613",
"9663.313", "9692.747", "9706.817", "9740.635", "9746.330",
"9787.663", "9800.377", "9858.417", "9903.287", "9912.382",
"9929.028", "9954.085", "9984.490", "9986.527"), class = "factor")), .Names = c("month",
"year", "cake", "icecream", "kex", "bread", "pie"), class = "data.frame", row.names = c(NA,
-100L))
因此,对于5个职位(蛋糕",冰淇淋","kex",面包",派"),我要执行预测!
So for 5 positions ( "cake", "icecream", "kex", "bread", "pie"), i want perform forecast!
library(forecast)
ld <- lapply(df, function(x) {ts(c(t(x[,-1])), start = min(x[,1]), frequency = 12)})
lts <- lapply(ld, ets, model = "ZZZ")
lapply(lts, forecast)
之后,发生错误.
Error in x[, -1] : incorrect number of dimensions
我认为处理我的数据结构,但是我无法解释到底是什么错误?
I think the deal with my data structure, but i can't explane what namely wrong?
我怎么理解,为什么我不能进行分析.也许我把分组弄错了吗?
How can i understand, why i can't conduct analysis. Maybe i wrong did the grouping?
最后的结果,我想要
month year cake icecream kex bread
Jan 1988 1226 1019 1149 1220
Feb 1988 1627 1530 1649 1341
(它是预测值)
推荐答案
这是该问题的继续:
用于分析的代码是相同的,但是数据的结构是不同的,这就是问题所在,如果将数据转换为具有与前一个类似的结构,就可以解决问题.
The code for the analysis is the same, but the structure of the data is different and that is the problem, if you transform the data to have a structure like the previous one, the problem is solved.
library(reshape)
df <- cast(melt(df, id=c("month", "year"), na.rm=TRUE),
variable + year ~ month)[, c("variable", "year", substring(month.name, 1, 3))]
colnames(df)[1] <- "group"
df[, substring(month.name, 1, 3)] <- lapply(df[, substring(month.name, 1, 3)],
function(x) as.numeric(as.character(x)))
现在这项工作:
library(forecast)
ld <- split(df[, -1], df$group)
ld <- lapply(ld, function(x) {ts(c(t(x[,-1])), start = min(x[,1]), frequency = 12)})
lts <- lapply(ld, ets, model = "ZZZ")
lf <- lapply(lts, forecast)
lf <- sapply(1:length(lf),
function(x) {d <- as.data.frame(lf[[x]])[, 1, drop = F]
colnames(d) <- names(lf)[x]
d},
simplify = FALSE)
do.call(cbind, lf)
cake icecream kex bread pie
May 1987 7480.974 5134.009 9830.727 3903.814 11068.371
Jun 1987 7881.644 5519.485 10254.112 4285.591 11490.782
Jul 1987 8807.531 6429.317 11193.300 5194.330 12436.630
Aug 1987 8079.533 5685.421 10483.109 4451.265 11729.948
Sep 1987 7024.136 4615.184 9446.217 3383.871 10695.142
...
这篇关于Forecat:R中的维数不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!