R区分股票价格矩阵:Delt并申请

时间:2016-06-19 04:20:59

标签: r quantmod

我有一个数据框,其中每列是股票的价格历史。我想从中创建这些价格的百分比变化矩阵,因此我尝试使用Delt中的quantmod函数。当我使用下面的代码尝试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

1 个答案:

答案 0 :(得分:4)

使用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,请使用vector转换为as.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

unnaming 整个数据集无法提供正确的结果

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需要vector而没有任何rowname属性。 as.vector中的unnameapply会删除这些属性,而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