R中多个时间序列的霍尔特预测
Holt forecast for multiple timeseries in R
我正在尝试对多个时间序列进行 Holt 的预测,并将它们与我的原始预测相结合 data.frame。考虑以下 data.frame,其中我有两个人口组:
library("forecast")
d <- data.frame(SEX = c("MALE","MALE","MALE","FEMALE","FEMALE","FEMALE"),
EDUCATION = c("01","01","01","01","01","01"),
TIME = c("2000","2001","2002","2000","2001","2002"),
VALUE = c(120,150,140,90,75,60))
然后我对两个时间序列做霍尔特预测:
male <- ts(as.numeric(d[1:3,]$VALUE),start=c(2000))
female <- ts(as.numeric(d[4:6,]$VALUE),start=c(2000))
forecastmale <- holt(male,h = 3,damped = FALSE)
forecastfemale <- holt(female,h = 3,damped = FALSE)
然后我保存结果并与我的原始结果结合data.frame:
forecastmale <- data.frame(forecastmale[["mean"]])
forecastfemale <- data.frame(forecastfemale[["mean"]])
forecastmale$SEX <- c("MALE","MALE","MALE")
forecastmale$EDUCATION <- c("01","01","01")
forecastmale$TIME <- c("2003","2004","2005")
colnames(forecastmale)[1] <- "VALUE"
forecastmale <- forecastmale[, c(2,3,4,1)]
forecastfemale$SEX <- c("FEMALE","FEMALE","FEMALE")
forecastfemale$EDUCATION <- c("01","01","01")
forecastfemale$TIME <- c("2003","2004","2005")
colnames(forecastfemale)[1] <- "VALUE"
forecastfemale <- forecastfemale[, c(2,3,4,1)]
d <- rbind(d,forecastmale,forecastfemale)
这适用于我只有两个时间序列的情况。但是如果我有 100 个必须预测的时间序列,那么这不是一种非常有效的方法。任何人都可以帮助提高编码器的效率,所以如果我在我的 data.frame 中包含一个额外的人口组,那么我的代码没有任何改变吗?
这就是 fable
包旨在处理的问题。这是一个使用与您相同的数据结构的示例。
library(dplyr)
library(tsibble)
library(fable)
# Artifical data
df <- expand.grid(
education = 1:3,
sex = c("male","female"),
year = 1990:2002
) %>%
as_tsibble(index=year, key=c(sex,education)) %>%
mutate(value = rnorm(78))
# Fit Holt's method to each series and forecast 3 years ahead
df %>%
model(holt = ETS(value ~ trend("A"))) %>%
forecast(h=3)
#> # A fable: 18 x 6 [1Y]
#> # Key: sex, education, .model [6]
#> sex education .model year value .mean
#> <fct> <int> <chr> <dbl> <dist> <dbl>
#> 1 male 1 holt 2003 N(0.14, 1.7) 0.137
#> 2 male 1 holt 2004 N(0.17, 1.7) 0.171
#> 3 male 1 holt 2005 N(0.21, 1.7) 0.205
#> 4 male 2 holt 2003 N(-0.75, 1.5) -0.749
#> 5 male 2 holt 2004 N(-0.84, 1.8) -0.837
#> 6 male 2 holt 2005 N(-0.93, 2) -0.926
#> 7 male 3 holt 2003 N(0.51, 0.7) 0.514
#> 8 male 3 holt 2004 N(0.53, 0.7) 0.530
#> 9 male 3 holt 2005 N(0.55, 0.7) 0.546
#> 10 female 1 holt 2003 N(0.44, 0.98) 0.445
#> 11 female 1 holt 2004 N(0.47, 0.98) 0.470
#> 12 female 1 holt 2005 N(0.5, 0.98) 0.495
#> 13 female 2 holt 2003 N(0.13, 0.89) 0.127
#> 14 female 2 holt 2004 N(0.15, 0.89) 0.148
#> 15 female 2 holt 2005 N(0.17, 0.89) 0.168
#> 16 female 3 holt 2003 N(0.78, 1.8) 0.781
#> 17 female 3 holt 2004 N(0.88, 1.8) 0.880
#> 18 female 3 holt 2005 N(0.98, 1.8) 0.978
由 reprex package (v0.3.0)
于 2020-09-05 创建
我正在尝试对多个时间序列进行 Holt 的预测,并将它们与我的原始预测相结合 data.frame。考虑以下 data.frame,其中我有两个人口组:
library("forecast")
d <- data.frame(SEX = c("MALE","MALE","MALE","FEMALE","FEMALE","FEMALE"),
EDUCATION = c("01","01","01","01","01","01"),
TIME = c("2000","2001","2002","2000","2001","2002"),
VALUE = c(120,150,140,90,75,60))
然后我对两个时间序列做霍尔特预测:
male <- ts(as.numeric(d[1:3,]$VALUE),start=c(2000))
female <- ts(as.numeric(d[4:6,]$VALUE),start=c(2000))
forecastmale <- holt(male,h = 3,damped = FALSE)
forecastfemale <- holt(female,h = 3,damped = FALSE)
然后我保存结果并与我的原始结果结合data.frame:
forecastmale <- data.frame(forecastmale[["mean"]])
forecastfemale <- data.frame(forecastfemale[["mean"]])
forecastmale$SEX <- c("MALE","MALE","MALE")
forecastmale$EDUCATION <- c("01","01","01")
forecastmale$TIME <- c("2003","2004","2005")
colnames(forecastmale)[1] <- "VALUE"
forecastmale <- forecastmale[, c(2,3,4,1)]
forecastfemale$SEX <- c("FEMALE","FEMALE","FEMALE")
forecastfemale$EDUCATION <- c("01","01","01")
forecastfemale$TIME <- c("2003","2004","2005")
colnames(forecastfemale)[1] <- "VALUE"
forecastfemale <- forecastfemale[, c(2,3,4,1)]
d <- rbind(d,forecastmale,forecastfemale)
这适用于我只有两个时间序列的情况。但是如果我有 100 个必须预测的时间序列,那么这不是一种非常有效的方法。任何人都可以帮助提高编码器的效率,所以如果我在我的 data.frame 中包含一个额外的人口组,那么我的代码没有任何改变吗?
这就是 fable
包旨在处理的问题。这是一个使用与您相同的数据结构的示例。
library(dplyr)
library(tsibble)
library(fable)
# Artifical data
df <- expand.grid(
education = 1:3,
sex = c("male","female"),
year = 1990:2002
) %>%
as_tsibble(index=year, key=c(sex,education)) %>%
mutate(value = rnorm(78))
# Fit Holt's method to each series and forecast 3 years ahead
df %>%
model(holt = ETS(value ~ trend("A"))) %>%
forecast(h=3)
#> # A fable: 18 x 6 [1Y]
#> # Key: sex, education, .model [6]
#> sex education .model year value .mean
#> <fct> <int> <chr> <dbl> <dist> <dbl>
#> 1 male 1 holt 2003 N(0.14, 1.7) 0.137
#> 2 male 1 holt 2004 N(0.17, 1.7) 0.171
#> 3 male 1 holt 2005 N(0.21, 1.7) 0.205
#> 4 male 2 holt 2003 N(-0.75, 1.5) -0.749
#> 5 male 2 holt 2004 N(-0.84, 1.8) -0.837
#> 6 male 2 holt 2005 N(-0.93, 2) -0.926
#> 7 male 3 holt 2003 N(0.51, 0.7) 0.514
#> 8 male 3 holt 2004 N(0.53, 0.7) 0.530
#> 9 male 3 holt 2005 N(0.55, 0.7) 0.546
#> 10 female 1 holt 2003 N(0.44, 0.98) 0.445
#> 11 female 1 holt 2004 N(0.47, 0.98) 0.470
#> 12 female 1 holt 2005 N(0.5, 0.98) 0.495
#> 13 female 2 holt 2003 N(0.13, 0.89) 0.127
#> 14 female 2 holt 2004 N(0.15, 0.89) 0.148
#> 15 female 2 holt 2005 N(0.17, 0.89) 0.168
#> 16 female 3 holt 2003 N(0.78, 1.8) 0.781
#> 17 female 3 holt 2004 N(0.88, 1.8) 0.880
#> 18 female 3 holt 2005 N(0.98, 1.8) 0.978
由 reprex package (v0.3.0)
于 2020-09-05 创建