R:ts 对象显示每周季节性,但不显示 xts(具有相同的数据和频率参数) [英] R: ts object shows weekly seasonality, but not xts (with same data and frequency parameter)

查看:26
本文介绍了R:ts 对象显示每周季节性,但不显示 xts(具有相同的数据和频率参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dataframe 用于捕获 daily 数据:

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 

我通过以下方式将上面的 dataframe 转换为 ts:

I converted the dataframe above to ts by:

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

我将 frequency 设置为 7,因为我想关注每周的季节性.当我使用mstl(自动stl 算法)分解数据时,它显示 Seasonal7 趋势.

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.

到目前为止一切顺利.

但后来,我发现使用 xts 更容易,所以我创建了一个 xts 对象:

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)

或者,我也尝试过:

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

请注意,ts 对象(ts_h7_2019)和 xts 对象(df_xts, df_xts2)均源自相同 dataframe (df).但是,mstl 分解返回无季节性,因此无法在 xts 上运行手动 stl出现此错误的对象:

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

这里有什么问题?xtsts 都应该具有完全相同的季节性,因为它们都来自单个 dataframe.

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

为什么 frequency 参数适用于 ts 而不适用于 xts?

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

推荐答案

您是否尝试过使用 msts 类(取自:https://otexts.com/fpp2/complexseasonality.html).
可能是这样的:

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))

这篇关于R:ts 对象显示每周季节性,但不显示 xts(具有相同的数据和频率参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆