我试图以累积的方式将xts时间序列数据转换为较低的周期性。
例如,使用to.weekly来自xts包的样本数据(sample_matrix),我得到了这个:
library(xts)
data(sample_matrix)
to.weekly(as.xts(sample_matrix), name="")
> to.weekly(as.xts(sample_matrix), name="")
.Open .High .Low .Close
2007-01-08 50.03978 50.42188 49.95041 49.98806
2007-01-15 49.99489 50.68583 49.80454 50.48912
.....
我希望能够使用稍微不同的函数to.weekly.cumulative,而不是返回这个:
> to.weekly.cumulative(as.xts(sample_matrix), name="")
.Open .High .Low .Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03978 50.42188 49.95041 49.98806
2007-01-09 49.99489 49.99489 49.80454 49.91333
2007-01-10 49.99489 50.13053 49.80454 49.97246
2007-01-11 49.99489 50.23910 49.80454 50.23910
2007-01-12 49.99489 50.35980 49.80454 50.28519
2007-01-13 49.99489 50.48000 49.80454 50.41286
2007-01-14 49.99489 50.62395 49.80454 50.60145
2007-01-15 49.99489 50.68583 49.80454 50.48912
....
此函数不会仅为端点返回数据,而是返回xts对象中的所有行。例如,我想要得到的是每日酒吧的每周酒吧(或1分钟酒吧15分钟酒吧),以便每天(或每分钟)我获得当前(从那天/分钟)开发每周(15分钟)的酒吧。因此,在星期一,我会得到一周开盘价,高点,低点,收盘价与周一完全相同,周二开盘价将从周一开始,收盘价将从周二开始,如果高于周一高点,则高点将是周二高位。如果低于周一低点,那么周二将是低点......周五我将从周一开始,高点为全周高点,低点整周的低点和周五的收盘价。星期五的数据应该是相同的,如果我将使用来自xts的.weekly函数。
所以,基本上,这不仅仅是端点上的数据(比如xts to.weekly),而且还包括原始xts对象周期中可用的时间步长。所以不知何故一部关于每周酒吧开发日期的电影(每天我都会看到每周酒吧在每周结束时的位置)。
怎么做(如何写函数to.weekly.cumulative?)?
如何做到这一点的例子受到高度赞赏。
编辑:尝试根据DWin评论解释一下。
答案 0 :(得分:2)
不确定这是否是您想要的,但也许您可以通过以下方式完成这项工作:
wday
)。rollapplyr
作为窗口宽度使用wday
。这是一个例子:
z <- as.xts(sample_matrix)
wday <- .indexwday(z) + 1
wday <- (wday-3)%%7 + 1 # rotate the wday index if need.
z2 <- data.frame(
Open = rollapplyr(z$Open, wday, function(x) x[1], partial = TRUE),
High = rollapplyr(z$High, wday, max, partial = TRUE),
Low = rollapplyr(z$Low, wday, min, partial = TRUE),
Close = z$Close
)
z2
是这样的:
> head(z2, 15)
Open High Low Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03978 50.42188 49.95041 49.98806
2007-01-09 49.99489 49.99489 49.80454 49.91333
2007-01-10 49.99489 50.13053 49.80454 49.97246
2007-01-11 49.99489 50.23910 49.80454 50.23910
2007-01-12 49.99489 50.35980 49.80454 50.28519
2007-01-13 49.99489 50.48000 49.80454 50.41286
2007-01-14 49.99489 50.62395 49.80454 50.60145
2007-01-15 49.99489 50.68583 49.80454 50.48912
2007-01-16 50.62024 50.73731 50.56627 50.67835
答案 1 :(得分:1)
拆分“周”,将自定义函数应用于每周的数据,然后rbind结果
to.weekly.cumulative <- function(xts.obj, name="") {
out <- do.call(rbind,
lapply(split(xts.obj, 'weeks'),
function(x) cbind(rep(first(x[,1]), NROW(x[,1])),
cummax(x[,2]), cummin(x[,3]), x[,4])))
colnames(out) <- paste(name, c("Open", "High", "Low", "Close"), sep=".")
out
}
> library(quantmod)
> data(sample_matrix)
> myxts <- as.xts(sample_matrix)
> head(to.weekly.cumulative(myxts), 15)
.Open .High .Low .Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.03978 50.42188 49.95041 50.39767
2007-01-04 50.03978 50.42188 49.95041 50.33236
2007-01-05 50.03978 50.42188 49.95041 50.33459
2007-01-06 50.03978 50.42188 49.95041 50.18112
2007-01-07 50.03978 50.42188 49.95041 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806
2007-01-09 50.03555 50.10363 49.80454 49.91333
2007-01-10 50.03555 50.13053 49.80454 49.97246
2007-01-11 50.03555 50.23910 49.80454 50.23910
2007-01-12 50.03555 50.35980 49.80454 50.28519
2007-01-13 50.03555 50.48000 49.80454 50.41286
2007-01-14 50.03555 50.62395 49.80454 50.60145
2007-01-15 50.61724 50.68583 50.47359 50.48912
2007-01-16 50.61724 50.73731 50.47359 50.67835