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

问题描述 投票:1回答:1

目前我尝试获取数据的季节性组件。为此,我通过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日未开始的数据分解(以获取季节性成分)呢?

r time-series xts
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 

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

© www.soinside.com 2019 - 2024. All rights reserved.