首页 > 解决方案 > 如何在R中预测多个时间序列

问题描述

我有这个包含多个系列(50 个产品)的数据集。我的数据集有 50 个产品(50 列)。每列都有一个产品的每日销售额。我想使用 ets 预测这些产品。所以我在下面创建了这段代码,当我运行它时,我只得到一个时间序列和一些我不明白的信息。提前致谢 :)

y<- read.csv("QAO2.csv", header=FALSE, fileEncoding = "latin1")
y <- ts(y[,-1],f=12,s=c(2007, 1))

ns <- ncol(y)
for(i in 1:ns)
fit.ets <- ets(y[,i])
print(fit.ets)

f.ets <- forecast(fit.ets,h=12)

print(f.ets)

plot(f.ets)

标签: rggplot2time-seriesforecastingets

解决方案


这就是 fable 包的设计目的。这是一个使用 2007 年的 50 系列月度数据的示例。尽管您说您有每日数据,但您提供的代码假定为月度数据(频率 12)。

library(fable)
library(dplyr)
library(tidyr)
library(ggplot2)

y <- ts(matrix(rnorm(175*50), ncol=50), frequency=12, start=c(2007,1)) %>%
  as_tsibble() %>%
  rename(Month = index, Sales=value)
y
#> # A tsibble: 8,750 x 3 [1M]
#> # Key:       key [50]
#>       Month key        Sales
#>       <mth> <chr>      <dbl>
#>  1 2007 Jan Series 1  1.06  
#>  2 2007 Feb Series 1  0.495 
#>  3 2007 Mar Series 1  0.332 
#>  4 2007 Apr Series 1  0.157 
#>  5 2007 May Series 1 -0.120 
#>  6 2007 Jun Series 1 -0.0846
#>  7 2007 Jul Series 1 -0.743 
#>  8 2007 Aug Series 1  0.714 
#>  9 2007 Sep Series 1  1.73  
#> 10 2007 Oct Series 1 -0.212 
#> # … with 8,740 more rows
fit.ets <- y %>% model(ETS(Sales))  
fit.ets
#> # A mable: 50 x 2
#> # Key:     key [50]
#>    key       `ETS(Sales)`
#>    <chr>          <model>
#>  1 Series 1  <ETS(A,N,N)>
#>  2 Series 10 <ETS(A,N,N)>
#>  3 Series 11 <ETS(A,N,N)>
#>  4 Series 12 <ETS(A,N,N)>
#>  5 Series 13 <ETS(A,N,N)>
#>  6 Series 14 <ETS(A,N,N)>
#>  7 Series 15 <ETS(A,N,N)>
#>  8 Series 16 <ETS(A,N,N)>
#>  9 Series 17 <ETS(A,N,N)>
#> 10 Series 18 <ETS(A,N,N)>
#> # … with 40 more rows
f.ets <- forecast(fit.ets, h=12)
f.ets
#> # A fable: 600 x 5 [1M]
#> # Key:     key, .model [50]
#>    key      .model        Month          Sales   .mean
#>    <chr>    <chr>         <mth>         <dist>   <dbl>
#>  1 Series 1 ETS(Sales) 2021 Aug N(-0.028, 1.1) -0.0279
#>  2 Series 1 ETS(Sales) 2021 Sep N(-0.028, 1.1) -0.0279
#>  3 Series 1 ETS(Sales) 2021 Oct N(-0.028, 1.1) -0.0279
#>  4 Series 1 ETS(Sales) 2021 Nov N(-0.028, 1.1) -0.0279
#>  5 Series 1 ETS(Sales) 2021 Dec N(-0.028, 1.1) -0.0279
#>  6 Series 1 ETS(Sales) 2022 Jan N(-0.028, 1.1) -0.0279
#>  7 Series 1 ETS(Sales) 2022 Feb N(-0.028, 1.1) -0.0279
#>  8 Series 1 ETS(Sales) 2022 Mar N(-0.028, 1.1) -0.0279
#>  9 Series 1 ETS(Sales) 2022 Apr N(-0.028, 1.1) -0.0279
#> 10 Series 1 ETS(Sales) 2022 May N(-0.028, 1.1) -0.0279
#> # … with 590 more rows
f.ets %>% 
  filter(key == "Series 1") %>%
  autoplot(y) +
  labs(title = "Series 1")

reprex 包于 2021-08-05 创建 (v2.0.0 )


推荐阅读