![](/img/trans.png)
[英]How to turn (interpolate) this irregularly spaced time series into a regularly spaced one in R or Matlab?
[英]How to handle irregularly spaced timeseries and returns a regularly spaced one
我有一個一段時間內股票價格的逐筆報價數據集,我想將高頻、不規則間隔的數據轉換為較低頻率、規則間隔的時間序列,以便稍后進行數據分析。 我在這里使用 R。
該數據以 1 秒的頻率跟蹤每筆交易/報價的特定股票的價值。 例如,在日期時間2009-07-16 13:30:01
(參考下面的數據),在這一秒內有兩個報價分別為145.88
和145.89
。
Date Value
2009-07-16T13:30:01.000 145.88
2009-07-16T13:30:01.000 145.89
2009-07-16T13:30:02.000 145.57
2009-07-16T13:30:02.000 145.75
2009-07-16T13:30:03.000 145.65
2009-07-16T13:30:03.000 145.84
2009-07-16T13:30:03.000 145.776
2009-07-16T13:30:04.000 145.74
2009-07-16T13:30:04.000 145.68
2009-07-16T13:30:04.000 145.68
2009-07-16T13:30:04.000 145.76
2009-07-16T13:30:04.000 145.68
.
.
.
首先,我想將數據轉換為規則間隔的時間序列,它只顯示每秒股票的最新價值:
Date Value
2009-07-16T13:30:01.000 145.89
2009-07-16T13:30:02.000 145.75
2009-07-16T13:30:03.000 145.776
2009-07-16T13:30:04.000 145.68
2009-07-16T13:30:05.000 145.76
2009-07-16T13:30:06.000 145.85
2009-07-16T13:30:07.000 145.8
2009-07-16T13:30:08.000 145.62
2009-07-16T13:30:09.000 145.85
2009-07-16T13:30:10.000 145.64
.
.
.
但最重要的是,我想將數據轉換為規則間隔和較低頻率的時間序列,比如 1 分鍾,它顯示每分鍾股票的最新價值:
Date Value
2009-07-16T13:31:00.000 145.89
2009-07-16T13:32:00.000 145.53
2009-07-16T13:33:00.000 145.68
2009-07-16T13:34:00.000 145.14
2009-07-16T13:35:00.000 145.7
2009-07-16T13:36:00.000 145.83
2009-07-16T13:37:00.000 145.88
2009-07-16T13:38:00.000 145.62
2009-07-16T13:39.00.000 145.84
2009-07-16T13:40:00.000 145.63
.
.
.
我曾嘗試使用highfrequency
包中的aggregatets()
,但它沒有返回我想要的結果。 日期時間不是規則間隔的,也不是較低的頻率,即使我已經指定了 1 分鍾,如我的代碼所示。
library(lubridate)
library(dplyr)
data$Date <- ymd_hms(data$Date)
library(xts)
data_xts <- as.xts(data[,"Value"], order.by=data[,"Date"])
library(highfrequency)
data_new <- aggregatets(data_xts, on="minutes", k=1)
我如何在 R 中做到這一點?
先做聚合。
你得到的是這個。
> head(df1, 10)
date value
1 2019-02-02T13:59:38.000 145.8922
2 2019-02-02T13:59:38.000 145.8820
3 2019-02-02T13:59:38.000 145.7998
4 2019-02-02T13:59:39.000 145.8122
5 2019-02-02T13:59:39.000 145.7355
6 2019-02-02T13:59:39.000 145.7822
7 2019-02-02T13:59:40.000 145.7078
8 2019-02-02T13:59:41.000 145.7133
9 2019-02-02T13:59:41.000 145.6906
10 2019-02-02T13:59:41.000 145.8749
現在我們使用aggregate()
來獲取每一秒的最新值(即每一秒的最高行數)。
df1.sec <- aggregate(value ~ date, df1, FUN=function(x) x[length(x)])
> head(df1.sec, 10)
date value
1 2019-02-02T13:59:38.000 145.7998
2 2019-02-02T13:59:39.000 145.7822
3 2019-02-02T13:59:40.000 145.7078
4 2019-02-02T13:59:41.000 145.8749
5 2019-02-02T13:59:42.000 145.7630
6 2019-02-02T13:59:43.000 145.7921
7 2019-02-02T13:59:44.000 145.6459
8 2019-02-02T13:59:45.000 145.7680
9 2019-02-02T13:59:46.000 145.7966
10 2019-02-02T13:59:47.000 145.8542
然后我們對分鍾做同樣的事情,用substr()
減少他的秒數。
df1.min <- aggregate(value ~ substr(date, 1, 16), df1.sec, FUN=function(x) x[length(x)])
> head(df1.min, 10)
substr(date, 1, 16) value
1 2019-02-02T13:59 145.8073
2 2019-02-02T14:00 145.6909
3 2019-02-02T14:01 145.8617
4 2019-02-02T14:02 145.7452
5 2019-02-02T14:03 145.7080
6 2019-02-02T14:04 145.8530
7 2019-02-02T14:05 145.9772
8 2019-02-02T14:06 145.8247
9 2019-02-02T14:07 145.9125
10 2019-02-02T14:08 145.6915
(注意:如果重要的話,為了防止奇怪的列名"substr(date, 1, 16)"
我們也可以這樣做:)
# with(df1.sec, aggregate(list(value=value), by=list(date=substr(date, 1, 16)),
# FUN=function(x) x[length(x)]))
# # date value
# # 1 2019-02-03T09:43 146.0894
# # 2 2019-02-03T09:44 145.7456
# # ...
xts()
例如POSIXct
格式,因此我們將其轉換。
df1.min$date.POSIX <- as.POSIXct(df1.min$`substr(date, 1, 16)`, format="%FT%H:%M")
現在我們可以在干凈的數據上設置xts
對象。
library(xts)
data_xts <- xts(df1.min$value, order.by=df1.min$date.POSIX)
結果
> data_xts
[,1]
2019-02-02 13:59:00 145.8073
2019-02-02 14:00:00 145.6909
2019-02-02 14:01:00 145.8617
2019-02-02 14:02:00 145.7452
2019-02-02 14:03:00 145.7080
2019-02-02 14:04:00 145.8530
2019-02-02 14:05:00 145.9772
2019-02-02 14:06:00 145.8247
2019-02-02 14:07:00 145.9125
2019-02-02 14:08:00 145.6915
玩具數據
set.seed(42)
date <- as.POSIXct(unlist(sapply(as.matrix(1:1000), function(x)
rep(x, sample(1:3, 1))))[1:1000], origin=Sys.time())
df1 <- data.frame(date=date,
value=rnorm(1000, 145.8, 0.08962))
df1$date <- strftime(df1$date, format="%FT%H:%M:%S.000")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.