用奇数的起始月份来分解ts

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用奇数的起始月份来分解ts相关的知识,希望对你有一定的参考价值。

目前我尝试获取数据的季节性组件。为此,我通过ts从一组日期和值创建一个tk_ts。不幸的是,我的数据集开始于2011-07-01并且运行到2018-05-01(缺少数据,我已经用pad lib填充了padr)。

由于tsfrequency = 12必须从1月1日开始,我无法用ts对这些数据进行建模。所以我尝试从我的数据创建一个xts并将其投射到ts,但要么我不能使频率工作或数据关闭。

这是我的MWE:

library(tidyquant)
library(timetk)

raw_data <- tibble(Date = c(as.Date("2011-07-01"), as.Date("2011-08-01"),
                   as.Date("2011-09-01"), as.Date("2011-10-01"),
                   as.Date("2011-11-01"), as.Date("2011-12-01"),
                   as.Date("2012-01-01"), as.Date("2012-02-01")),
                  Value = c(1,4,1,4,1,4,1,4))
                  # And so on, till 2018-05-01 and with reasonable values

tk_ts(raw_data, select = Value, start = 2011, frequency = 12)
# Leads to:
# 
#      Jan Feb Mar Apr May Jun Jul Aug
# 2011   1   4   1   4   1   4   1   4
#
# which is bad since my first date is 2011-07-01 not 2011-01-01.

xts_data <- xts(raw_data$Value, order.by = raw_data$Date, frequency = 12)
# xts_data Leads to, which is fine:
# 
# [,1]
# 2011-07-01    1
# 2011-08-01    4
# 2011-09-01    1
# 2011-10-01    4
# 2011-11-01    1
# 2011-12-01    4
# 2012-01-01    1
# 2012-02-01    4

as.ts(xts_data, start = start(xts_data), end = end(xts_data))
# Leads to:
# 
# Time Series:
# Start = 15156 
# End = 15371 
# Frequency = 1 
# [1] 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1
# [52] 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4
# [103] 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1
# [154] 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4
# [205] 1 4 1 4 1 4 1 4 1 4 1 4
#
# Which is totaly bad since there are more than the original 8 values.

as.ts(xts_data, start = start(xts_data))
# Leads to:
# 
# Time Series:
#   Start = 15156 
# End = 15163 
# Frequency = 1 
# [1] 1 4 1 4 1 4 1 4
#
# Which is bad since the Frequency is off
#  and I need it to be ok for the decompose.

as.ts(xts_data, start = start(xts_data), end = end(xts_data), frequency = 12)
# Leads to:
# 
# Error in ts(coredata(x), frequency = frequency(x), ...) : 
#   formal argument "frequency" matched by multiple actual arguments

attr(xts_data, 'frequency') <- 12
as.ts(xts_data, start = start(xts_data))
# Leads to:
# 
# Jan Feb Mar Apr May Jun Jul Aug
# 15156   1   4   1   4   1   4   1   4
#
# Which is as bad as the first example

那么如何生成1月1日未开始的数据分解(以获取季节性成分)呢?

答案

您可以尝试对start参数进行简单的添加,同时指定月份编号(本例中为07)。

raw_data <- tibble(Date = c(as.Date("2011-07-01"), as.Date("2011-08-01"),
                            as.Date("2011-09-01"), as.Date("2011-10-01"),
                            as.Date("2011-11-01"), as.Date("2011-12-01"),
                            as.Date("2012-01-01"), as.Date("2012-02-01")),
                   Value = c(1,4,1,4,1,4,1,4))
# And so on, till 2018-05-01 and with reasonable values

tk_ts(raw_data, select = Value, start = c(2011,07), frequency = 12)

这导致以下输出:

tk_ts(raw_data, select = Value, start = c(2011,07), frequency = 12)
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2011                           1   4   1   4   1   4
2012   1   4 

希望有助于您在后续步骤中尝试实现的目标。

以上是关于用奇数的起始月份来分解ts的主要内容,如果未能解决你的问题,请参考以下文章

已解决在react+ts中 atnd 用 upload 组件报错Failed to execute ‘readAsArrayBuffer,param 1 is notof type Blob(代码片段

[原][算法]分解计算三维空间抛物线

替换从 VAST 代码返回的多个 HLS VOD 片段

获得已过月份起始和结束时间及当月的起始时间到当前时间

JavaScript获得当前月份起止日期

《Vue3+TS》开发一个自己的起始页