Compare Original x Seasonally adjusted data

Hi guys,

I'm trying to compare my original time series with my multiplicative seasonally adjusted, after that I will check additive to see which one is more smoother.

Here is my code, my issue is with the plot:

ibrary(forecast)
library(tsibble)
library(feasts)
library(tidyverse)

data_tsbl <- as_tsibble(ts(AirPassengers,
start = c(1949, 1),
frequency = 12))

data_tsbl_a <- data_tsbl %>%
model(STL(box_cox(value, lambda = 1))) %>%
components()

autoplot(data_tsbl) +
autolayer(data_tsbl_a$season_adjust, color = "red") +
labs(title = "Comparing original X multiplicative seasonal adjusted",
x = "Time",
y = "Number of Passengers")
theme_minimal()

Best
Lucas

ibrary(forecast)
#> Error in ibrary(forecast): could not find function "ibrary"
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(feasts)
#> Loading required package: fabletools
library(tidyverse)

data_tsbl <- as_tsibble(ts(AirPassengers,
                           start = c(1949, 1),
                           frequency = 12))

data_tsbl_a <- data_tsbl %>%
  model(STL(box_cox(value, lambda = 1))) %>%
  components()

data_tsbl["adjusted"] <- data_tsbl_a$season_adjust


data_tsbl %>% autoplot(vars(value,adjusted)) + theme_minimal()

data_tsbl %>% autoplot(value) +
  autolayer(vars(adjusted)) + theme_minimal()
#> Error: Objects of type quosures/list not supported by autolayer.

p <- ggplot(data_tsbl,aes(index))
p + geom_line(aes(,value, color = "Original")) +
    geom_line(aes(,adjusted, color = "Adjusted")) +
    labs(title = "Comparing original X multiplicative seasonal adjusted",
         x = "Time", y = "Number of Passengers") +
    theme_minimal() 

Thanks for your prompt reply, do you think there is a way to forecast a multiplicative seasonal adjust time series? in my research I just saw an automated way :

library(fpp3)

# AirPassengers is already in ts format
data_tsbl <- AirPassengers %>% as_tsibble()  

STL_add <- data_tsbl %>%
  model(STL(value)) %>%  # lambda = 1 is equivalent to no transformation
  components()

STL_add %>% 
  select(season_adjust) %>%  # avoid plot in usual components format
  autoplot(season_adjust, color = "blue") + 
  autolayer(data_tsbl, value, color = "gray") +
  xlab("Month") +
  ylab("Actual & STL Additive")

STL_mult <- data_tsbl %>%
  model(STL(log(value))) %>%  # log(value) is same as Box_Cox(, lambda = 0) & simpler
  components()

STL_mult %>% 
  select(season_adjust) %>%
  autoplot(exp(season_adjust), color = "blue") +  # back-transform logged values
  autolayer(data_tsbl, value, color = "gray") +
  xlab("Month") +
  ylab("Actual & STL Multiplicative")

Created on 2021-03-19 by the reprex package (v1.0.0)

2 Likes

Thanks for sharing a suitable solution, since the multiplicative is the smoother one I'm trying to fit with ses model according to the code below, and I got some issue:

library(forecast)
library(fable)
library(tsibble)
library(feasts)
library(tidyverse)

data_tsbl <- as_tsibble(ts(AirPassengers,
start = c(1949, 1),
frequency = 12))

dcmcp_spec_ad <- decomposition_model(
STL(log(value)),
ETS(value ~ error("A") + trend("N") + season("N"))
)

simple <- data_tsbl %>%
model(dcmcp_spec_ad) %>%
forecast(h = "1 year")

My warning message: Warning message:
1 error encountered for dcmcp_spec_ad
[1] Suitable defaults for these decomposition elements are not available: trend, remainder.
Please specify an appropriate model for these components

You want to use ETS on the seasonal_adjust variable, not the value (passengers) variable itself, and then tell it not to include a seasonal component.

I borrowed this from fpp3 and it seems to work for me. I am just unsure about the log transformation and when it gets back-transformed.

my_dcmp_spec <- decomposition_model(
STL(log(value)),
ETS(exp(season_adjust) ~ season("N"))
)
data_tsbl %>%
model(stl_ets = my_dcmp_spec) %>%
forecast(h = "2 years")

A small point, but why do you still transform a ts to a ts before transforming to a tsibble? Also, loading the fpp3 package will also load the other packages you need. It is much easier than loading each one individually.

I am grading finals right now so will not be checking in very often. Good luck!

Lucas,

An example in section 13.4 of fpp3 uses a log transformation to fit a multiplicative STL model, which yields the seasonal effects (for the forecast using SNAIVE) and the seasonally adjusted values, which it passes to an ETS model (without seasonality) but no explicit back-transformation, so it must handle that automatically.

my_dcmp_spec <- decomposition_model(
STL(log(value)),
ETS(season_adjust ~ season("N"))
)
data_tsbl %>%
model(stl_ets = my_dcmp_spec) %>%
forecast(h = "2 years")

Sorry to mislead you. Back to the exams!

Thanks for helping me with this topic, I could handle this issue, after your help.

Something that I what studying now is Machine learning methods for forecasts in tidyverse, you know some book that I could find relevant information about it?

Not something I have any experience with, and I am too close to retirement to start down that path!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.