分解 xts 每小时时间序列 [英] Decompose xts hourly time series

查看:25
本文介绍了分解 xts 每小时时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 decomposeetsstl 或任何函数分解每小时的时间序列.这是一个示例代码及其输出:

require(xts)需要(预测)time_index1 <- seq(from = as.POSIXct("2012-05-15 07:00"),to = as.POSIXct("2012-05-17 18:00"), by="小时")头(时间索引1 <-格式(时间索引1,格式=%Y-%m-%d %H:%M:%S",tz="UTC", usetz=TRUE)# [1] "2012-05-15 05:00:00 UTC" "2012-05-15 06:00:00 UTC"# [3] "2012-05-15 07:00:00 UTC" "2012-05-15 08:00:00 UTC"# [5] "2012-05-15 09:00:00 UTC" "2012-05-15 10:00:00 UTC"头(时间索引<- as.POSIXct(时间索引1))# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"

为什么 time_index 的时区改回 CEST?

set.seed(1)值 <- rnorm(n = length(time_index1))eventdata1 <- xts(value, order.by = time_index)tzone(事件数据1)# [1] ""头(索引(事件数据1))# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"ets(事件数据1)# ETS(A,N,N)## 称呼:# ets(y = eventdata1)## 平滑参数:# alpha = 1e-04## 初始状态:# l = 0.1077##西格玛:0.8481## AIC AICc BIC# 229.8835 230.0940 234.0722分解(事件数据1)# 分解错误(eventdata1):# 时间序列没有或少于 2 个周期stl(事件数据1)# stl(eventdata1) 中的错误:# 系列不是周期性的或少于两个周期

当我调用 tzoneindexTZ 时,没有时区,但 index 清楚地表明时间是用时区定义的.

另外,为什么只有 ets 有效?可以用来分解时间序列吗?

解决方案

为什么 time_index 的时区改回 CEST?

因为您没有在对 as.POSIXct 的调用中指定 tz=.如果它是由 UTC 的偏移量(例如 -0800)指定的,它只会从字符串中提取时区.参见 ?strptime.

R>head(time_index <- as.POSIXct(time_index1, "UTC"))[1] "2012-05-15 12:00:00 UTC" "2012-05-15 13:00:00 UTC"[3] "2012-05-15 14:00:00 UTC" "2012-05-15 15:00:00 UTC"[5] "2012-05-15 16:00:00 UTC" "2012-05-15 17:00:00 UTC"

<块引用>

当我调用 tzoneindexTZ 时,没有时区,但 index 清楚地表明时间是用时区定义的.

所有 POSIXct 对象都有一个时区."" 时区仅表示 R 无法确定特定时区,因此它使用操作系统指定的时区.参见 ?timezone.

只有 ets 函数有效,因为您的 xts 对象没有正确定义的 frequency 属性.这是 xts 对象的一个​​已知限制,我计划在接下来的几个月内解决这些问题.您可以通过在调用 xts 构造函数后显式指定 frequency 属性来解决当前问题.

R>set.seed(1)R>值 <- rnorm(n = length(time_index1))R>eventdata1 <- xts(value, order.by = time_index)R>attr(eventdata1, 'frequency') <- 24 # 设置频率属性R>分解(as.ts(eventdata1)) # 分解期望一个 'ts' 对象

I want to decompose hourly time series with decompose, ets, or stl or whatever function. Here is an example code and its output:

require(xts)
require(forecast)
time_index1 <- seq(from = as.POSIXct("2012-05-15 07:00"),
                   to = as.POSIXct("2012-05-17 18:00"), by="hour")
head(time_index1 <- format(time_index1, format="%Y-%m-%d %H:%M:%S",
                           tz="UTC", usetz=TRUE)
# [1] "2012-05-15 05:00:00 UTC" "2012-05-15 06:00:00 UTC"
# [3] "2012-05-15 07:00:00 UTC" "2012-05-15 08:00:00 UTC"
# [5] "2012-05-15 09:00:00 UTC" "2012-05-15 10:00:00 UTC"
head(time_index <- as.POSIXct(time_index1))
# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"
# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"
# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"

Why does the timezone for time_index change back to CEST?

set.seed(1)
value <- rnorm(n = length(time_index1))
eventdata1 <- xts(value, order.by = time_index)
tzone(eventdata1)
# [1] ""
head(index(eventdata1))
# [1] "2012-05-15 05:00:00 CEST" "2012-05-15 06:00:00 CEST"
# [3] "2012-05-15 07:00:00 CEST" "2012-05-15 08:00:00 CEST"
# [5] "2012-05-15 09:00:00 CEST" "2012-05-15 10:00:00 CEST"
ets(eventdata1)
# ETS(A,N,N) 
# 
# Call:
#  ets(y = eventdata1) 
# 
#   Smoothing parameters:
#     alpha = 1e-04 
# 
#   Initial states:
#     l = 0.1077 
# 
#   sigma:  0.8481
# 
#      AIC     AICc      BIC 
# 229.8835 230.0940 234.0722
decompose(eventdata1)
# Error in decompose(eventdata1) : 
#   time series has no or less than 2 periods
stl(eventdata1)
# Error in stl(eventdata1) : 
#   series is not periodic or has less than two periods

When I call tzone or indexTZ there is no timezone but the index clearly show that the times are defined with a timezone.

Also, why does only ets work? Can it be used to decompose a time series?

解决方案

Why does the timezone for time_index change back to CEST?

Because you didn't specify tz= in your call to as.POSIXct. It will only pick up the timezone from the string if it's specified by offset from UTC (e.g. -0800). See ?strptime.

R> head(time_index <- as.POSIXct(time_index1, "UTC"))
[1] "2012-05-15 12:00:00 UTC" "2012-05-15 13:00:00 UTC"
[3] "2012-05-15 14:00:00 UTC" "2012-05-15 15:00:00 UTC"
[5] "2012-05-15 16:00:00 UTC" "2012-05-15 17:00:00 UTC"

When I call tzone or indexTZ there is no timezone but the index clearly show that the times are defined with a timezone.

All POSIXct objects have a timezone. A timezone of "" simply means R wasn't able to determine a specific timezone, so it is using the timezone specified by your operating system. See ?timezone.

Only the ets function works because your xts object doesn't have a properly defined frequency attribute. This is a known limitation of xts objects, and I have plans to address them over the next several months. You can work around the current issues by explicitly specifying the frequency attribute after calling the xts constructor.

R> set.seed(1)
R> value <- rnorm(n = length(time_index1))
R> eventdata1 <- xts(value, order.by = time_index)
R> attr(eventdata1, 'frequency') <- 24  # set frequency attribute
R> decompose(as.ts(eventdata1))  # decompose expects a 'ts' object

这篇关于分解 xts 每小时时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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