且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ts和window功能中使用每小时数据

更新时间:2023-11-21 23:20:40

R中的时间序列

ts()对象具有一些限制.最值得注意的是,它不接受每次观察的时间戳.而是,它请求一个startfreq(end是可选的).此外,freq功能仅限于按季节查看数据.

Time Series in R

The ts() object has a few limitations. Most notably, it doesn't accept time stamps per observation. Instead, it requests a start and freq (the end is optional). Furthermore, the freq capabilities are limited to viewing data in terms of seasons.

Type      Frequency 
Annual     1
Quarterly  4
Monthly   12
Weekly    52

因此,要生成正确的季节",我们将必须计算每日季节性,其中freq=1440(= 24 * 60).之后,它变得更加复杂.

Thus, to generate the correct "season" we would have to calculate a daily seasonality where freq=1440 (=24*60). It gets a bit more complicated after that.

因此,我强烈建议使用xtszoo对象创建时间序列.

As a result, I would highly suggest creating the time series with an xts or zoo object.

接下来,出现窗口问题的原因之一是您提供的日期是字符串,而不是 POSIXct POSIXlt 目的.优先考虑.

Next up, one of the reasons for your windowing issues is the date you are supplying is a string and not a POSIXct or POSIXlt object. The prior of which is preferred.

可以找到完整的细分:

as.POSIXct之间的差异/as.POSIXlt和strptime,用于将字符向量转换为POSIXct/POSIXlt

处理R中的时间戳

话虽如此,第一步是将数据从字符格式转换为 POSIXct

With that being said, one of the first steps is to convert your data from character form to POSIXct

# Convert to POSXICT
SampleData$DateTime = as.POSIXct(strptime(SampleData$DateTime, format ="%Y-%m-%d %H:%M"))

开窗

从那里开始,如果我们创建xts()对象,则窗口问题变得微不足道.

Windowing

From there, the windowing issue becomes trivial if we create a xts() object.

# install.packages("xts")
require(xts)

# Create an XTS object to hold the time series
sdts = xts(SampleData$MedTime, order.by = SampleData$DateTime)

# Subset training
train = window(sdts,end= as.POSIXct('2015-01-21 23:00', format ="%Y-%m-%d %H:%M"))