decompose and tsibbles

Hello,

I tried to decompose a time series which is saved as a tsibble. I get an error when trying to perform the decompose functions or stl in many syntax variations. The error is: "Time series has no or less then 2 periods".
I have read: 3.1 Transformations and adjustments | Forecasting: Principles and Practice (3rd ed),

My used data set is this: Ethereum Price | ETH Price Index and Live Chart — CoinDesk 20 - All and then export as .csv

library(feasts)
library(dplyr)
library(ggplot2)
library(lubridate)
library(tsibble)

csv_data_1 <- read.csv("ETH_USD_CoinDesk.csv")

z_eth <- mutate(csv_data_1, Currency = NULL, X24h.High..USD. = NULL, X24h.Low..USD.=NULL, X24h.Open..USD.=NULL) %>%
mutate(Date = ymd(Date)) %>% as_tsibble(index = Date)

z_eth %>%
autoplot() +
labs(y = "Closing price", x="Date")

switch tsibble to monthly

z_eth_monthly <- z_eth %>% index_by(month = ~ floor_date(.,unit="monthly")) %>% group_by(month) %>% summarise(avgPM= mean(Closing.Price..USD.))

autoplot(z_eth_monthly)

decompose(z_eth_monthly, type="additive")

If I do the following code:

x <- z_eth_monthly %>%
model(stl = STL(avgPM))

components(dcmp)

It return an Null model.

I figured it out and write this post to clarify if someone finds this in the archives.

For tsibbles it is helpful to work with the Date functions provided and documented in tsibble: https://cran.r-project.org/web/packages/tsibble/tsibble.pdf, P.4

The correct code to transform to monthly data would therefore be:
z_eth <- mutate(csv_data_1, Currency = NULL, X24h.High..USD. = NULL, X24h.Low..USD.=NULL, X24h.Open..USD.=NULL) %>%
mutate(Date = yearmonth(Date)) %>% group_by(Date) %>% summarise(avgPM = mean(Closing.Price..USD.)) %>% as_tsibble(index = Date)

With this change, the autoplot works and the tsibble is correct transformed, also shown through the top [M] via console output.

Instead of decompose, I used the STL function from the feasts package.

z_eth %>% model(STL(avgPM)) %>% components() %>% autoplot()

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.