将每小时动物园时间序列输入函数 stl() [英] Feeding an hourly zoo time-series into function stl()

查看:27
本文介绍了将每小时动物园时间序列输入函数 stl()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你问之前,是的,我需要展示这么多数据.stl() 需要两段数据.在这种情况下,一个周期是 24 个值,因此 stl() 至少需要 48 个值.

Before you ask, yes I need to show this much data. stl() requires two periods of data. In this case, one period is 24 values, so stl() wants at least 48 values.

此外,来自 stl() 帮助:

Also, from stl() help:

"....这应该是一个频率大于1的"ts"类的对象...."

"....This should be an object of class "ts" with a frequency greater than one...."

我正在升级一些旧的计算器,以便我的数据采用动物园格式.到目前为止,尽管存在 stl() 速度问题,但我已经升级了每月和每日数据而没有任何明显问题.我现在只能使用每小时数据.

I'm upgrading some old calcs so that my data is in the zoo format. So far, I've upgraded monthly and daily data without any noticeable problems, although there have been stl() speed issues. I'm now down to hourly data.

当您运行代码时,第一次尝试"和第二次尝试"失败,而第三次尝试"有效.为什么?我错过了什么非常明显的事情?从注意接下来会发生什么"部分来看,是不是 stl() 从 zoo 系列到 ts 系列的内部转换导致了这个问题?

When you run the code, the "First Try" and "Second Try" fail while the "Third Try" works. Why? What really obvious thing have I missed? From the "notice what happens next" section, is it stl()'s internal conversion from a zoo series to a ts series that is causing this problem?

library(zoo)   

texinp <- "
Date,Demand
2009-01-01 01:00:00,28099.1458685981
2009-01-01 02:00:00,27818.8853634983
2009-01-01 03:00:00,27542.1297303602
2009-01-01 04:00:00,27481.2230897352
2009-01-01 05:00:00,27863.5201611328
2009-01-01 06:00:00,28716.5855560981
2009-01-01 07:00:00,29542.9219102648
2009-01-01 08:00:00,29894.8927398003
2009-01-01 09:00:00,29984.6039306641
2009-01-01 10:00:00,30065.9059781901
2009-01-01 11:00:00,29885.6435823568
2009-01-01 12:00:00,29348.3372466363
2009-01-01 13:00:00,28547.9348426649
2009-01-01 14:00:00,27601.1429031033
2009-01-01 15:00:00,26784.0209678819
2009-01-01 16:00:00,26269.0193386502
2009-01-01 17:00:00,26196.6366764323
2009-01-01 18:00:00,27527.2755148655
2009-01-01 19:00:00,29723.9232834201
2009-01-01 20:00:00,29594.3683930122
2009-01-01 21:00:00,29089.5591764323
2009-01-01 22:00:00,28287.9040272352
2009-01-01 23:00:00,26863.3280593533
2009-01-02 00:00:00,25166.2158816189
2009-01-02 01:00:00,23708.111414388
2009-01-02 02:00:00,22905.427265625
2009-01-02 03:00:00,22517.7926079644
2009-01-02 04:00:00,22519.1570795356
2009-01-02 05:00:00,23065.7224479167
2009-01-02 06:00:00,24452.6789577908
2009-01-02 07:00:00,26450.5363346354
2009-01-02 08:00:00,27815.8465516493
2009-01-02 09:00:00,28402.8746001519
2009-01-02 10:00:00,29007.9232600911
2009-01-02 11:00:00,29333.0119395616
2009-01-02 12:00:00,29378.4373334418
2009-01-02 13:00:00,29145.4215820312
2009-01-02 14:00:00,29069.2706928168
2009-01-02 15:00:00,28900.4390755208
2009-01-02 16:00:00,28724.9172607422
2009-01-02 17:00:00,28523.1717095269
2009-01-02 18:00:00,29480.8798573134
2009-01-02 19:00:00,31168.033992513
2009-01-02 20:00:00,30736.5264789497
2009-01-02 21:00:00,29963.2411859809
2009-01-02 22:00:00,28915.5116943359
2009-01-02 23:00:00,27547.5425157335
2009-01-03 00:00:00,25756.0379166667
2009-01-03 01:00:00,24079.5182259115
2009-01-03 02:00:00,22927.8879052734"


tem <- read.zoo(textConnection(texinp), header = TRUE, sep = ",", tz = "", regular = TRUE)

#First try
frequency(tem)
tem.stl <- stl(tem, s.window = "periodic") #error, not the frequency it was expecting?

#Second try
frequency(tem) <- 24
tem.stl <- stl(tem, s.window = "periodic") #don't worry, it's not hung....
#r calculates for about a minute and comes back with an error

#Third try
temcor <- ts(coredata(tem), freq = 24)
temcor.stl <- stl(temcor, s.window = "periodic") #works fine
plot(temcor.stl)

#Also, notice what happens next
junk <- as.ts(tem) #again, it's not hung, it just takes a while
str(junk)
frequency(junk)
junk #hello

推荐答案

使用完整周期为 1 的时间表示,因此对于频率为 24 的每小时数据,时间单位应对应于一天.chron 是这样工作的:

Use a time representation for which is a full cycle is 1 so for hourly data with a frequency of 24 a unit of time should correspond to a day. chron works that way so:

library(zoo)
library(chron)

z <- read.zoo(text = texinp, header = TRUE, sep = ",", FUN = as.chron)
stl(z, "per")

这篇关于将每小时动物园时间序列输入函数 stl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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