2
votes

I have this kind of data:

dat
# A tibble: 34 x 2
   date_block_num  sales
            <int>  <dbl>
 1              0 131479
 2              1 128090
 3              2 147142
 4              3 107190
 5              4 106970
 6              5 125381
 7              6 116966
 8              7 125291
 9              8 133332
10              9 127541
# ... with 24 more rows

date_block_num is the month of each year. sales are the sales of a product. For example, in the original data,date_block_num 0 has 63,224 rows/cases since the sales are day wise and they refer to different Items in different shops. It would be also interesting to analyze the data on a daily basis but R canĀ“t handle this amount of data.

I want to decompose the time series in order to analyze the trend, the seasonality and the random components. Overall, the time series has 33 months (start: 01.01.2013, end, 01.10.2015).

This is my approach.

library(forecast)
ts(dat, frequency = 12) %>%
  decompose() %>%
  autoplot()

enter image description here

However, this seems not right compare the first of the four plots above and this one:

plot(dat, type = "l")

enter image description here

structure(list(date_block_num = 0:33, sales = c(131479, 128090, 
147142, 107190, 106970, 125381, 116966, 125291, 133332, 127541, 
130009, 183342, 116899, 109687, 115297, 96556, 97790, 97429, 
91280, 102721, 99208, 107422, 117845, 168755, 110971, 84198, 
82014, 77827, 72295, 64114, 63187, 66079, 72843, 71056)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -34L))
2

2 Answers

2
votes

The issue arises from passing both columns of dat instead of just the sales one:

ts(dat$sales, frequency = 12) %>%
  decompose() %>%
  autoplot()

enter image description here

2
votes

The problem is that ts(dat)creates a two dimensional time series:

ts(dat, frequency = 12)
      date_block_num  sales
Jan 1              0 131479
Feb 1              1 128090

And afterwards only the first column (date_block_num) is decomposed. Try this

ts(dat$sales, frequency = 12) %>% 
  decompose() %>%
  autoplot()