【问题标题】:How do I add periods to time series in R after aggregation聚合后如何在 R 中的时间序列中添加句点
【发布时间】:2015-04-13 10:15:24
【问题描述】:

我在从 2004 年 7 月 9 日到 2014 年 12 月 31 日的十年期间的每日销售额 R 中有两个变量数据框 (df)。并非每个日期都以十年为单位表示,但几乎是周一到周五的大多数日子。

我的目标是按季度汇总销售额,转换为时间序列对象,并运行季节性分解和其他时间序列预测。

我在转换时遇到了问题,因为最终我收到了一个错误:

time series has no or less than 2 periods

这是我的代码结构。

# create a time series object
library(xts)
x <- xts(df$amount, df$date)
# create a time series object aggregated by quarter
q.x <- apply.quarterly(x, sum)

当我尝试运行时

fit <- stl(q.x, s.window = "periodic")  

我收到错误消息

series is not periodic or has less than two periods  

当我尝试运行时

q.x.components <- decompose(q.x)  
# or  
decompose(x)  

我收到错误消息

time series has no or less than 2 periods  

那么,我如何获取包含日期变量和金额变量(销售额)的原始数据框,将季度聚合为时间序列对象,然后运行时间序列分析?

【问题讨论】:

标签: r time-series xts forecasting


【解决方案1】:

我想我能够回答我自己的问题。我这样做了。谁能确认这种结构是否有意义?

library(lubridate)
# add a new variable indicating the calendar year.quarter (i.e. 2004.3) of each observation
df$year.quarter <- quarter(df$date, with_year = TRUE)

library(plyr)
# summarize gift amount by year.quarter
new.data <- ddply(df, .(year.quarter), summarize,
              sum = round(sum(amount), 2))

# convert the new data to a quarterly time series object beginning
# in July 2004 (2004, Q3) and ending in December 2014 (2014, Q4)
nd.ts <- ts(new.data$sum, start = c(2004,3), end = c(2014,4), frequency = 4)

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2022-01-19
    • 2011-06-28
    • 2011-04-28
    • 2020-02-08
    • 1970-01-01
    • 2016-06-03
    • 2013-12-13
    • 2018-05-18
    相关资源
    最近更新 更多