在两列上使用Rollapply

在两列上使用Rollapply,r,time-series,xts,zoo,R,Time Series,Xts,Zoo,我正试着做一些类似于我要求的事情,但不幸的是我无法解决 这是我的数据框(数据),是价格的时间序列: Date Price Vol 1998-01-01 200 0.3 1998-01-02 400 0.4 1998-01-03 600 -0.2 1998-01-04 100 0.1 ... 1998-01-20 100 0.1 1998-01-21 200 -0.4 19

我正试着做一些类似于我要求的事情,但不幸的是我无法解决

这是我的数据框(数据),是价格的时间序列:

Date          Price   Vol
1998-01-01     200      0.3
1998-01-02     400      0.4
1998-01-03     600     -0.2
1998-01-04     100      0.1
...
1998-01-20     100      0.1
1998-01-21     200     -0.4
1998-01-21     500      0.06
....
1998-02-01     100      0.2
1998-02-02     200      0.4
1998-02-03     500      0.3
1998-02-04     100      0.1
etc.
我想告诉R,去

  • 取“Vol”的第一个值除以“Price”的第20个值,然后
  • 取“Vol”的2st值除以“Price”的第21个值,然后
  • 取“Vol”的第3个值除以“Price”的第22个值,然后
  • 等等
在我的另一篇文章中,我能够使用此函数计算20天持有期内的回报:

> data.xts <- xts(data[, -1], data[, 1])
> hold <- 20
> f <- function(x) log(tail(x, 1)) - log(head(x, 1))
> data.xts$returns.xts <- rollapply(data.xts$Price, FUN=f, 
  width=hold+1, align="left", na.pad=T)

我知道一定会有NA的结果,但只针对最后20次观察,而不是前20次。上面提到的公式计算正确的值,但是将它们从第21行而不是第一行开始。你知道我怎样才能改变吗

事实上比那容易。只要这样做:

data.xts <- xts(data[, -1], data[, 1])
hold <- 20
returns.xts = data.xts[,2] / lag(data.xts[,1], hold)
data.xts您只需使用

data.xts$quo <- data.xts[,2] / lag( data.xts[,1], -hold)

data.xts$quo在
rollapply
中使用
by.column=FALSE
。为了使用公布的数据,我们将第一行的数量除以第三行的价格,以此类推,以便于再现:

library(zoo)

Lines <- "Date          Price   Vol
1998-01-01     200      0.3
1998-01-02     400      0.4
1998-01-03     600     -0.2
1998-01-04     100      0.1
1998-01-20     100      0.1
1998-01-21     200     -0.4
1998-01-21     500      0.06
1998-02-01     100      0.2
1998-02-02     200      0.4
1998-02-03     500      0.3
1998-02-04     100      0.1"


# read in and use aggregate to remove all but last point in each day.
# In reality we would replace textConnection(Lines) with something 
#  like "myfile.dat"

z <- read.zoo(textConnection(Lines), header = TRUE, 
       aggregate = function(x) tail(x, 1))

# divide Volume by the Price of the point 2 rows ahead using by.column = FALSE
# Note use of align = "left" to align with the volume.
# If we used align = "right" it would align with the price.

rollapply(z, 3, function(x) x[1, "Vol"] / x[3, "Price"], by.column = FALSE,
    align = "left")

# and this is the same as rollapply with align = "left" as above
z$Vol / lag(z$Price, 2)

# this is the same as using rollapply with align = "right"
lag(z$Vol, -2) / z$Price
图书馆(动物园)

线路很酷,我不得不做一个小的改变,计算值是正确的。由于一个新问题,我更新了我的Q。你知道吗?谢谢对于你们接下来的问题,你们能不能用滞后(quo,-保持)将quo向后移动。由于Grothendieck's是目前为止唯一的一家,因此我担心滞后信号可能需要转换。
data.xts <- xts(data[, -1], data[, 1])
hold <- 20
returns.xts = data.xts[,2] / lag(data.xts[,1], hold)
data.zoo<- zoo(data[, -1], data[, 1])
hold <- 20
returns.zoo = data.zoo[,2] / lag(data.zoo[,1], -hold)
data.xts$quo <- data.xts[,2] / lag( data.xts[,1], -hold)
library(zoo)

Lines <- "Date          Price   Vol
1998-01-01     200      0.3
1998-01-02     400      0.4
1998-01-03     600     -0.2
1998-01-04     100      0.1
1998-01-20     100      0.1
1998-01-21     200     -0.4
1998-01-21     500      0.06
1998-02-01     100      0.2
1998-02-02     200      0.4
1998-02-03     500      0.3
1998-02-04     100      0.1"


# read in and use aggregate to remove all but last point in each day.
# In reality we would replace textConnection(Lines) with something 
#  like "myfile.dat"

z <- read.zoo(textConnection(Lines), header = TRUE, 
       aggregate = function(x) tail(x, 1))

# divide Volume by the Price of the point 2 rows ahead using by.column = FALSE
# Note use of align = "left" to align with the volume.
# If we used align = "right" it would align with the price.

rollapply(z, 3, function(x) x[1, "Vol"] / x[3, "Price"], by.column = FALSE,
    align = "left")

# and this is the same as rollapply with align = "left" as above
z$Vol / lag(z$Price, 2)

# this is the same as using rollapply with align = "right"
lag(z$Vol, -2) / z$Price