R 区分股票价格矩阵:删除并应用

R Differencing a matrix of stock prices: Delt and apply

我有一个数据框,其中每一列都是股票的价格历史记录。我想从中创建这些价格的百分比变化矩阵,因此我尝试使用 quantmod 中的 Delt 函数。当我尝试使用下面的代码 apply Delt 函数时,结果中有不正确的更改。当我将 Delt 分别应用于每一列时,会出现正确的更改。我做错了什么?

这里是 data

> library(quantmod)
> m<-read.csv(file="C:/Users/Desktop/prices.csv", header=TRUE, sep=",", row.names=1)
> head(apply(m, 2, Delt, k=1))
                A           B             C           D            E
[1,]           NA          NA            NA          NA           NA
[2,]  0.013883218  0.02402696 -0.0028845060  0.10537081  0.065129843
[3,] -0.261075312 -0.27656116  0.0064643504 -0.03748629  0.003693638
[4,]  0.100354272  0.28212656 -0.0006447236  0.01986472  0.022154153
[5,] -0.033372547  0.11482705 -0.0023028136 -0.06562413  0.093813308
[6,] -0.004419677 -0.18058097  0.0007338350 -0.03160490 -0.010180472
> head(Delt(m[,1]))
     Delt.1.arithmetic
[1,]                NA
[2,]      -0.009326258
[3,]       0.015094952
[4,]       0.015403475
[5,]      -0.017321925
[6,]       0.019550238

这里是m的前30行:

               A        B        C      D        E
6/1/2006  186.57 15.16001 12.88992 128.04 63.23111
7/1/2006  184.83 13.93923 12.87614 127.91 63.17918
8/1/2006  187.62 14.40092 12.86571 128.26 63.33122
9/1/2006  190.51 13.87444 12.85661 128.34 63.60919
10/1/2006 187.21 12.84720 12.83384 126.74 62.97229
11/1/2006 190.87 13.52174 12.85892 127.57 64.05329
12/1/2006 198.13 13.81215 12.86256 133.37 64.81722
1/1/2007  195.92 14.24603 12.85711 132.01 65.18905
2/1/2007  196.68 13.95810 12.81181 130.22 65.17205
3/1/2007  195.84 13.74382 12.79951 131.86 65.47931
4/1/2007  196.78 13.76690 12.79787 133.54 65.93696
5/1/2007  199.87 14.22677 12.78429 136.04 65.77217
6/1/2007  198.24 14.10437 12.80410 134.49 65.31252
7/1/2007  200.87 14.19849 12.79181 135.41 65.34666
8/1/2007  203.25 14.08153 12.77221 136.67 65.90654
9/1/2007  201.71 13.99091 12.82413 136.30 65.55228
10/1/2007 204.35 14.62587 12.87034 142.31 67.68648
11/1/2007 208.01 15.15703 12.89241 144.25 68.97027
12/1/2007 205.63 14.69767 12.84175 146.33 69.04170
1/1/2008  198.64 14.58832 12.82002 145.92 69.43480
2/1/2008  196.51 13.58696 12.82676 148.02 70.73637
3/1/2008  198.91 12.77139 12.85116 151.79 71.66404
4/1/2008  197.59 12.52536 12.83977 156.14 72.56894
5/1/2008  197.48 13.14181 12.82874 154.74 73.44301
6/1/2008  198.23 13.14060 12.81345 155.54 73.41066
7/1/2008  199.49 12.67957 12.82051 157.93 73.46999
8/1/2008  197.50 13.85022 12.81246 155.64 72.86505
9/1/2008  180.14 12.89408 12.81132 146.17 70.21979
10/1/2008 176.97 12.13371 12.87266 140.09 69.55070
11/1/2008 160.76 10.22495 12.90323 127.26 67.40361

使用lapply

可能会更好
lst <- lapply(m, Delt, k=1)
head(data.frame(lst))
#Delt.1.arithmetic Delt.1.arithmetic.1 Delt.1.arithmetic.2 Delt.1.arithmetic.3 Delt.1.arithmetic.4
#1                NA                  NA                  NA                  NA                  NA
#2      -0.009326258         -0.08052690       -0.0010687188        -0.001015308       -0.0008213299
#3       0.015094952          0.03312212       -0.0008105454         0.002736299        0.0024065873
#4       0.015403475         -0.03655914       -0.0007071123         0.000623733        0.0043890342
#5      -0.017321925         -0.07403839       -0.0017710656        -0.012466885       -0.0100125948
#6       0.019550238          0.05250490        0.0019545514         0.006548840        0.0171662816

或者,如果我们使用 apply,则使用 as.vector

转换为 vector
head(apply(m, 2, function(x) Delt(as.vector(x), k = 1)))
#               A           B             C            D             E
#[1,]           NA          NA            NA           NA            NA
#[2,] -0.009326258 -0.08052690 -0.0010687188 -0.001015308 -0.0008213299
#[3,]  0.015094952  0.03312212 -0.0008105454  0.002736299  0.0024065873
#[4,]  0.015403475 -0.03655914 -0.0007071123  0.000623733  0.0043890342
#[5,] -0.017321925 -0.07403839 -0.0017710656 -0.012466885 -0.0100125948
#[6,]  0.019550238  0.05250490  0.0019545514  0.006548840  0.0171662816

取消命名整个数据集不会给出正确的结果

head(apply(unname(m), 2, quantmod::Delt))
#            [,1]        [,2]          [,3]        [,4]         [,5]
#[1,]           NA          NA            NA          NA           NA
#[2,]  0.013883218  0.02402696 -0.0028845060  0.10537081  0.065129843
#[3,] -0.261075312 -0.27656116  0.0064643504 -0.03748629  0.003693638
#[4,]  0.100354272  0.28212656 -0.0006447236  0.01986472  0.022154153
#[5,] -0.033372547  0.11482705 -0.0023028136 -0.06562413  0.093813308
#[6,] -0.004419677 -0.18058097  0.0007338350 -0.03160490 -0.010180472

但是,如果我们 unname 向量,它会给出正确的输出

head(apply(m, 2, function(x) Delt(unname(x), k=1)))
#                A           B             C            D             E
#[1,]           NA          NA            NA           NA            NA
#[2,] -0.009326258 -0.08052690 -0.0010687188 -0.001015308 -0.0008213299
#[3,]  0.015094952  0.03312212 -0.0008105454  0.002736299  0.0024065873
#[4,]  0.015403475 -0.03655914 -0.0007071123  0.000623733  0.0043890342
#[5,] -0.017321925 -0.07403839 -0.0017710656 -0.012466885 -0.0100125948
#[6,]  0.019550238  0.05250490  0.0019545514  0.006548840  0.0171662816

本质上,Delt 需要一个没有任何行名属性的 vectorapply 中的 as.vectorunname 删除这些属性,而 lapply 默认删除它。

确认一下

head(apply(`row.names<-`(m, NULL), 2, Delt, k=1))
#               A           B             C            D             E
#[1,]           NA          NA            NA           NA            NA
#[2,] -0.009326258 -0.08052690 -0.0010687188 -0.001015308 -0.0008213299
#[3,]  0.015094952  0.03312212 -0.0008105454  0.002736299  0.0024065873
#[4,]  0.015403475 -0.03655914 -0.0007071123  0.000623733  0.0043890342
#[5,] -0.017321925 -0.07403839 -0.0017710656 -0.012466885 -0.0100125948
#[6,]  0.019550238  0.05250490  0.0019545514  0.006548840  0.0171662816