Abdel_DAS
Abdel_DAS

Reputation: 13

How to forecast multiple time series in R

I have this dataset that contains multiple series (50 products). My dataset has 50 products (50 columns). each column has the daily sales of a product. I want to forecast these product using ets. So I have created this code below and when I run it I get only one time series and some information that I do not understand. Thanks in advance :)

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)

Upvotes: 0

Views: 885

Answers (1)

Rob Hyndman
Rob Hyndman

Reputation: 31820

This is what the fable package is designed to do. Here is an example using 50 series of monthly data from 2007. Although you say you have daily data, the code you provide assumes monthly data (frequency 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")

Created on 2021-08-05 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions