分解起始月份为奇数的 ts
Decompose ts with odd starting month
目前我正在尝试获取数据的季节性成分。
为此,我通过 tk_ts
从一系列日期和值中创建了一个 ts
。
不幸的是,我的数据集从 2011-07-01 开始运行到 2018-05-01(缺少数据,我已经从 padr
库中填充了 pad
)。
由于 ts
和 frequency = 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
希望对您在后续步骤中尝试实现的目标有所帮助。
目前我正在尝试获取数据的季节性成分。
为此,我通过 tk_ts
从一系列日期和值中创建了一个 ts
。
不幸的是,我的数据集从 2011-07-01 开始运行到 2018-05-01(缺少数据,我已经从 padr
库中填充了 pad
)。
由于 ts
和 frequency = 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
希望对您在后续步骤中尝试实现的目标有所帮助。