让我提供任何预测数据:
df=structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), year = c(1973L, 1974L, 1975L, 1976L, 1977L, 1978L,
1973L, 1974L, 1975L, 1976L, 1977L, 1978L), Jan = c(9007L, 7750L,
8162L, 7717L, 7792L, 7836L, 9007L, 7750L, 8162L, 7717L, 7792L,
7836L), Feb = c(8106L, 6981L, 7306L, 7461L, 6957L, 6892L, 8106L,
6981L, 7306L, 7461L, 6957L, 6892L), Mar = c(8928L, 8038L, 8124L,
7767L, 7726L, 7791L, 8928L, 8038L, 8124L, 7767L, 7726L, 7791L
), Apr = c(9137L, 8422L, 7870L, 7925L, 8106L, 8192L, 9137L, 8422L,
7870L, 7925L, 8106L, 8192L), May = c(10017L, 8714L, 9387L, 8623L,
8890L, 9115L, 10017L, 8714L, 9387L, 8623L, 8890L, 9115L), Jun = c(10826L,
9512L, 9556L, 8945L, 9299L, 9434L, 10826L, 9512L, 9556L, 8945L,
9299L, 9434L), Jul = c(11317L, 10120L, 10093L, 10078L, 10625L,
10484L, 11317L, 10120L, 10093L, 10078L, 10625L, 10484L), Aug = c(10744L,
9823L, 9620L, 9179L, 9302L, 9827L, 10744L, 9823L, 9620L, 9179L,
9302L, 9827L), Sep = c(9713L, 8743L, 8285L, 8037L, 8314L, 9110L,
9713L, 8743L, 8285L, 8037L, 8314L, 9110L), Oct = c(9938L, 9129L,
8466L, 8488L, 8850L, 9070L, 9938L, 9129L, 8466L, 8488L, 8850L,
9070L), Nov = c(9161L, 8710L, 8160L, 7874L, 8265L, 8633L, 9161L,
8710L, 8160L, 7874L, 8265L, 8633L), Dec = c(8927L, 8680L, 8034L,
8647L, 8796L, 9240L, 8927L, 8680L, 8034L, 8647L, 8796L, 9240L
)), .Names = c("group", "year", "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = "data.frame", row.names = c(NA,
-12L))
我想分别对每个组执行预测(组var为group
(1和2)。
我将forecast
包和ets
函数用于自动选择参数。
library("forecast")
fit <- ets(df,model = "ZZZ")
该如何进行每个组的预测?
IE,我想要这样的输出。(两年)
group year jan jan..95. jan.95. feb feb..95. feb.95. X. X..1 X X.1 X.2
1 1 1973 pred.value - - pred.value - - NA NA NA NA NA
2 1 1974 pred.value - - pred.value - - NA NA NA NA NA
3 1 1975 pred.value - - pred.value - - NA NA NA NA NA
4 1 1976 pred.value - - pred.value - - NA NA NA NA NA
5 1 1977 pred.value - - pred.value - - NA NA NA NA NA
6 1 1978 pred.value - - pred.value - - NA NA NA NA NA
7 1 1979 pred.value - - pred.value - - NA NA NA NA NA
8 1 1980 pred.value - - pred.value - - NA NA NA NA NA
9 2 1973 pred.value - - pred.value - - NA NA NA NA NA
10 2 1974 pred.value - - pred.value - - NA NA NA NA NA
11 2 1975 pred.value - - pred.value - - NA NA NA NA NA
12 2 1976 pred.value - - pred.value - - NA NA NA NA NA
13 2 1977 pred.value - - pred.value - - NA NA NA NA NA
14 2 1978 pred.value - - pred.value - - NA NA NA NA NA
15 2 1979 pred.value - - pred.value - - NA NA NA NA NA
16 2 1980 pred.value - - pred.value - - NA NA NA NA NA
答案 0 :(得分:1)
首先,您需要将数据转换为ts
个对象的列表:
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")
最后,获得包含预测的表格:
lapply(lts, forecast)