从另一列在数据框中构建股票变量

Build Stock Variable in Data Frame from another column

我正在寻找一种方便快捷的方法来构建一个新列,该列的库存变量取决于之前对新列的观察结果和旧列中的值。

所以 B 列应该是 ColumnBt = 0.01 * Bt-1 + ColumnAt

数据看起来应该是

ColumnA      ColumnB
1            1
0            0.01
0            0.0001
4            4.000001
5            5.04000001
0            0.504

在某些时候,能够确定因子 (0.01) 作为交替结转的变量甚至会很方便。

有什么建议可以轻松实现吗?

非常感谢任何帮助或建议!非常感谢!

关键是为差分方程建立一个合适的单位响应矩阵 ColumnB[k] = 0.01 * ColumnB[k-1] + ColumnA[k]:

> z
      [,1]  [,2]  [,3]  [,4] [,5] [,6]
[1,] 1e+00 0e+00 0e+00 0e+00 0.00    0
[2,] 1e-02 1e+00 0e+00 0e+00 0.00    0
[3,] 1e-04 1e-02 1e+00 0e+00 0.00    0
[4,] 1e-06 1e-04 1e-02 1e+00 0.00    0
[5,] 1e-08 1e-06 1e-04 1e-02 1.00    0
[6,] 1e-10 1e-08 1e-06 1e-04 0.01    1

然后,通过叠加ColumnB <- z %*% ColumnA。要构建此矩阵:

lag <- 0.01  ## This is your parameter
r <- lag^(seq_len(length(ColumnA))-1)
m <- matrix(rep(r,length(ColumnA)),nrow=length(ColumnA))

z <- matrix(0,nrow=length(ColumnA),ncol=length(ColumnA))
z[lower.tri(z,diag=TRUE)] <- m[row(m) <= (length(ColumnA)+1-col(m))]
##      [,1]  [,2]  [,3]  [,4] [,5] [,6]
##[1,] 1e+00 0e+00 0e+00 0e+00 0.00    0
##[2,] 1e-02 1e+00 0e+00 0e+00 0.00    0
##[3,] 1e-04 1e-02 1e+00 0e+00 0.00    0
##[4,] 1e-06 1e-04 1e-02 1e+00 0.00    0
##[5,] 1e-08 1e-06 1e-04 1e-02 1.00    0
##[6,] 1e-10 1e-08 1e-06 1e-04 0.01    1

我们可以把它放到一个函数中:

constructZ <- function(lag, N) {
  r <- lag^(seq_len(N)-1)
  m <- matrix(rep(r,N),nrow=N)
  z <- matrix(0,nrow=N,ncol=N)
  z[lower.tri(z,diag=TRUE)] <- m[row(m) <= (N+1-col(m))]
  z
}

然后,

df <- data.frame(ColumnA=c(1,0,0,4,5,0))
df$ColumnB <- constructZ(0.01,nrow(df)) %*% df$ColumnA
print(df)
##  ColumnA  ColumnB
##1       1 1.000000
##2       0 0.010000
##3       0 0.000100
##4       4 4.000001
##5       5 5.040000
##6       0 0.050400

按每个指标值应用函数的更新答案

假设您有数据:

df <- structure(list(ColumnA = c(1L, 0L, 0L, 4L, 5L, 0L, 4L, 0L, 2L
), Indicator = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), Time = c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L)), .Names = c("ColumnA", "Indicator", 
"Time"), class = "data.frame", row.names = c(NA, -9L))
##  ColumnA Indicator Time
##1       1         1    1
##2       0         1    2
##3       0         1    3
##4       4         2    1
##5       5         2    2
##6       0         2    3
##7       4         3    1
##8       0         3    2
##9       2         3    3

您要在其中为每个 Indicator 值分别计算 ColumnA 中所有观察值(在所有 Time 中)的响应。然后您可以使用 constructZ:

执行以下操作
df$ColumnB <- unlist(by(df,df$Indicator,function(df) constructZ(0.5,nrow(df)) %*% df$ColumnA))

在这里,我们使用 by 在被 Indicator 值分割的数据帧 df 上单独计算提供的函数。提供的函数只是 constructZ(0.5,nrow(df)) %*% df$ColumnA) 和以前一样,其中滞后参数是 0.5by 的输出是一个列表,我们 unlist 然后设置为 df$ColumnB。结果符合预期:

print(df)
##  ColumnA Indicator Time ColumnB
##1       1         1    1    1.00
##2       0         1    2    0.50
##3       0         1    3    0.25
##4       4         2    1    4.00
##5       5         2    2    7.00
##6       0         2    3    3.50
##7       4         3    1    4.00
##8       0         3    2    2.00
##9       2         3    3    3.00