Blaze Tama
Blaze Tama

Reputation: 10948

R: ts object shows weekly seasonality, but not xts (with same data and frequency parameter)

I have a dataframe which captures daily data:

$dt: Date, format: "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04"
$new_user_growth: num  NA -0.0254 -0.0469 -0.1257 0.3125 

I converted the dataframe above to ts by:

ts_h7_2019 <- ts(data=df$new_user_growth, frequency = 7)

I set frequency to 7 because I want to focus on weekly seasonality. When I decompose the data using mstl (automatic stl algorithm), it shows Seasonal7 trend.

So far so good.

But then, I found working with xts is easier, so I created an xts object:

df_xts <- xts(x=df$new_user_growth, order.by=df$dt, frequency=7)

or alternatively, I also tried:

df_xts2 <- xts(x=df$new_user_growth, order.by=df$dt, deltat=7)

Notice that both ts object (ts_h7_2019) and xts object (df_xts, df_xts2) are derived from a same dataframe (df). However, the mstl decomposition return no seasonality and consequently, the manual stl can't be run on the xts objects with this error:

y is not a seasonal ts object

What's wrong here? both xts and ts should have exactly same seasonality as both are derived from a single dataframe.

Why does the frequency parameter works on ts but not on xts?

Upvotes: 1

Views: 280

Answers (1)

GWD
GWD

Reputation: 1464

Have you tried using the mstsclass (taken from: https://otexts.com/fpp2/complexseasonality.html).
Potentially something like this:

forecast::mstl(msts(data = xts(df$....), seasonal.periods = 7))

Upvotes: 1

Related Questions