XTS 分裂的艰难时期
Hard times with split in XTS
先生Ulrichs xts 包总是惊人的。我一直使用普通的 5 分钟、15 分钟、30 分钟拆分。从来没有问题。
现在我卡住了。
#Setup test data
my.time <- seq(from = as.POSIXct('2000-01-01 00:00:00'),
to = as.POSIXct('2000-01-01 1:00:00'),
by = '1 sec')
my.data <- rep(10, length = length(my.time))
my.xts <- as.xts(my.data, order.by = my.time)
#Now splitting and checking endtimes of first split
tail((split(my.xts, f="minutes", k=20))[[1]])
tail((split(my.xts, f="minutes", k=30))[[1]])
#2000-01-01 00:19:59 10 #All good
#2000-01-01 00:29:59 10 #All good
tail((split(my.xts, f="minutes", k=22))[[1]])
#2000-01-01 00:11:59 10 #Hmmm, what am I missing. Expectimg 00:21:59
#As endpoints is used by split I also checked this behaviour
endpoints(my.xts, on="minutes", k=20)
#[1] 0 1200 2400 3600 3601 #All good
endpoints(my.xts, on="minutes", k=30)
#[1] 0 1800 3600 3601 #All good
endpoints(my.xts, on="minutes", k=22)
#[1] 0 720 2040 3360 3601 #Hmmm
为了理解这一点,我在 https://github.com/joshuaulrich/xts/blob/master/src/endpoints.c
进一步研究了 XTS 代码
在那里我发现这个应该更有效
c(0,which(diff(_x%/%on%/%k+1) != 0),NROW(_x))
所以我尝试了这个
which(diff(.index(my.xts) %/% 60 %/% 20 +1) != 0)
#[1] 1200 2400 3600 #As expected
which(diff(.index(my.xts) %/% 60 %/% 21 +1) != 0)
#[1] 1080 2340 3600 #Expecting 1260 2520...
which(diff(.index(my.xts) %/% 60 %/% 22 +1) != 0)
#[1] 720 2040 3360 #Expecting 1320 2640...
which(diff(.index(my.xts) %/% 60 %/% 23 +1) != 0)
#[1] 720 2100 3480 #Expecting 1380 2760...
which(diff(.index(my.xts) %/% 60 %/% 24 +1) != 0)
#[1] 1440 2880 #As expected
which(diff(.index(my.xts) %/% 60 %/% 30 +1) != 0)
#[1] 1800 3600 #As expected
这是我的大脑过热的地方,我在这里发帖。我确定我只是遗漏了一些东西,所以我没有将其作为错误发布在 Github。请帮助解释发生了什么。为什么我没有得到预期的结果?
编辑:
所以,快速思考一下,我猜这与所有基于 Unix 时间开始的函数和使用不能被一小时整除的时基有关。这是我理解的正确引导吗?
编辑2:
在最终了解端点和拆分应该如何工作后,在下面发布我的答案...
当然,split
(和endpoints
)按照它们应该在 xts 中工作的方式工作。 IE。在决定间隔时以 1970-01-01 00:00:00 作为起点进行分割。
是的,我误用拆分作为在我的时间序列 xts 数据中的一个小时的任意起点拆分的简单方法。
无论如何,我通过编写这个 "resets" 第一个时间戳到 1970-01-01 00:00:00 的短函数解决了我的小问题。
## contuation from code snippet above
#So, I realised it was all about resetting the first time
#to 1970-01-01 00:00:0,
#so that I could do easily my "strange" splitting.
#Please note that this is not to be used for dates,
#only when working on hour, mins or secs
#and don't care that you break the index.
startMovedSplit <- function(x, ...) {
startIndex <- head(.index(x), n=1)
.index(x) <- .index(x) - startIndex
split(x, ...)
}
#New Try
tail((startMovedSplit(my.xts, f="minutes", k=20))[[1]])
tail((startMovedSplit(my.xts, f="minutes", k=30))[[1]])
#1970-01-01 00:19:59 10 #Good enough for my purposes
#1970-01-01 00:29:59 10 #Good enough for my purposes
tail((startMovedSplit(my.xts, f="minutes", k=22))[[1]])
#1970-01-01 00:21:59 10 #Good enough for my purposes
我对 xts 库的所有误解中最好的部分是什么?我现在知道如何处理函数中的省略号 (...)
和子函数的调用!
先生Ulrichs xts 包总是惊人的。我一直使用普通的 5 分钟、15 分钟、30 分钟拆分。从来没有问题。
现在我卡住了。
#Setup test data
my.time <- seq(from = as.POSIXct('2000-01-01 00:00:00'),
to = as.POSIXct('2000-01-01 1:00:00'),
by = '1 sec')
my.data <- rep(10, length = length(my.time))
my.xts <- as.xts(my.data, order.by = my.time)
#Now splitting and checking endtimes of first split
tail((split(my.xts, f="minutes", k=20))[[1]])
tail((split(my.xts, f="minutes", k=30))[[1]])
#2000-01-01 00:19:59 10 #All good
#2000-01-01 00:29:59 10 #All good
tail((split(my.xts, f="minutes", k=22))[[1]])
#2000-01-01 00:11:59 10 #Hmmm, what am I missing. Expectimg 00:21:59
#As endpoints is used by split I also checked this behaviour
endpoints(my.xts, on="minutes", k=20)
#[1] 0 1200 2400 3600 3601 #All good
endpoints(my.xts, on="minutes", k=30)
#[1] 0 1800 3600 3601 #All good
endpoints(my.xts, on="minutes", k=22)
#[1] 0 720 2040 3360 3601 #Hmmm
为了理解这一点,我在 https://github.com/joshuaulrich/xts/blob/master/src/endpoints.c
进一步研究了 XTS 代码在那里我发现这个应该更有效
c(0,which(diff(_x%/%on%/%k+1) != 0),NROW(_x))
所以我尝试了这个
which(diff(.index(my.xts) %/% 60 %/% 20 +1) != 0)
#[1] 1200 2400 3600 #As expected
which(diff(.index(my.xts) %/% 60 %/% 21 +1) != 0)
#[1] 1080 2340 3600 #Expecting 1260 2520...
which(diff(.index(my.xts) %/% 60 %/% 22 +1) != 0)
#[1] 720 2040 3360 #Expecting 1320 2640...
which(diff(.index(my.xts) %/% 60 %/% 23 +1) != 0)
#[1] 720 2100 3480 #Expecting 1380 2760...
which(diff(.index(my.xts) %/% 60 %/% 24 +1) != 0)
#[1] 1440 2880 #As expected
which(diff(.index(my.xts) %/% 60 %/% 30 +1) != 0)
#[1] 1800 3600 #As expected
这是我的大脑过热的地方,我在这里发帖。我确定我只是遗漏了一些东西,所以我没有将其作为错误发布在 Github。请帮助解释发生了什么。为什么我没有得到预期的结果?
编辑: 所以,快速思考一下,我猜这与所有基于 Unix 时间开始的函数和使用不能被一小时整除的时基有关。这是我理解的正确引导吗?
编辑2: 在最终了解端点和拆分应该如何工作后,在下面发布我的答案...
当然,split
(和endpoints
)按照它们应该在 xts 中工作的方式工作。 IE。在决定间隔时以 1970-01-01 00:00:00 作为起点进行分割。
是的,我误用拆分作为在我的时间序列 xts 数据中的一个小时的任意起点拆分的简单方法。
无论如何,我通过编写这个 "resets" 第一个时间戳到 1970-01-01 00:00:00 的短函数解决了我的小问题。
## contuation from code snippet above
#So, I realised it was all about resetting the first time
#to 1970-01-01 00:00:0,
#so that I could do easily my "strange" splitting.
#Please note that this is not to be used for dates,
#only when working on hour, mins or secs
#and don't care that you break the index.
startMovedSplit <- function(x, ...) {
startIndex <- head(.index(x), n=1)
.index(x) <- .index(x) - startIndex
split(x, ...)
}
#New Try
tail((startMovedSplit(my.xts, f="minutes", k=20))[[1]])
tail((startMovedSplit(my.xts, f="minutes", k=30))[[1]])
#1970-01-01 00:19:59 10 #Good enough for my purposes
#1970-01-01 00:29:59 10 #Good enough for my purposes
tail((startMovedSplit(my.xts, f="minutes", k=22))[[1]])
#1970-01-01 00:21:59 10 #Good enough for my purposes
我对 xts 库的所有误解中最好的部分是什么?我现在知道如何处理函数中的省略号 (...)
和子函数的调用!