通过rowMeans计算等权重组合

时间:2015-09-29 23:45:16

标签: r

我的数据的头部(Pri)在下面,

      Date     AAPL      GOOGL     BAC      WFC      WMT      SP500
  1 2011-01-03 44.90084  308.5285  13.84028 27.94722 47.98998 1271.50
  2 2011-01-10 46.55196  312.4024  14.81153 29.05624 48.63777 1293.24
  3 2011-01-18 43.64513  306.2212  13.84028 28.84330 49.45417 1283.35
  4 2011-01-24 44.89817  300.7958  13.20897 28.24887 50.31493 1276.34
  5 2011-01-31 46.28746  305.7958  13.87913 29.10863 49.72038 1310.87
  6 2011-02-07 47.67007  312.5626  14.34533 29.99717 49.41867 1329.15

按股票价格计算股票收益后的头(ret)如下所示。

ret<-sapply(Pri[2:7], function(x) x[-1] / x[-length(x)] - 1)
head(ret)
   AAPL         GOOGL        BAC          WFC          WMT          SP500    
[1,]0.03677256  0.012555957  0.070175516  0.039682516  0.013498507  0.017097908
[2,]-0.06244261 -0.019785965 -0.065573838 -0.007328272 0.016785269 -0.007647470
[3,]0.02870957  -0.017717306 -0.045613952 -0.020608977 0.017405369  -0.005462275
[4,]0.03094320  0.016622572  0.050735209  0.030435196  -0.011816591 0.027053943
[5,]0.02987016  0.022128341  0.033589994  0.030525034  -0.006068175 0.013944960
[6,]-0.01762647 0.008935126  -0.001354099 -0.033175357 -0.005566520 0.010427706

然后,我尝试添加一个新列ret $ equ_port,该列由前5个股票AAPL,GOOGL,BAC,WFC和WMT逐行计算。

 ret$equ_port<-rowMeans(ret[,1:5])

然而,它不起作用。

 Warning message:
 In ret$equ_port <- rowMeans(ret[, 1:5]) : Coercing LHS to a list

头部(ret)在下方,

[[1]]
[1] 0.03677256

[[2]]
[1] -0.06244261

[[3]]
[1] 0.02870957

[[4]]
[1] 0.0309432

[[5]]
[1] 0.02987016

[[6]]
[1] -0.01762647

> dim(ret)
NULL

我该如何解决这个问题?我需要的数据如下,

       Date        AAPL        GOOGL          BAC          WFC          WMT
 2 2011-01-10  0.03677256  0.012555957  0.070175516  0.039682516  0.013498507
 3 2011-01-18 -0.06244261 -0.019785965 -0.065573838 -0.007328272  0.016785269
 4 2011-01-24  0.02870957 -0.017717306 -0.045613952 -0.020608977  0.017405369
 5 2011-01-31  0.03094320  0.016622572  0.050735209  0.030435196 -0.011816591
 6 2011-02-07  0.02987016  0.022128341  0.033589994  0.030525034 -0.006068175
 7 2011-02-14 -0.01762647  0.008935126 -0.001354099 -0.033175357 -0.005566520
          SP500     equ_port
 2  0.017097908  0.034537012
 3 -0.007647470 -0.027669084
 4 -0.005462275 -0.007565059
 5  0.027053943  0.023383917
 6  0.013944960  0.022009072
 7  0.010427706 -0.009757463

1 个答案:

答案 0 :(得分:2)

你的逻辑是正确的。只需先向ret <- as.data.frame(ret) ret$equ_port<-rowMeans(ret[,1:5]) ret # V1 V2 V3 V4 V5 V6 # 1 1.750000 -0.7500000 -Inf -1.0204082 1.8235294 -1.6326531 # 2 -1.000000 4.1666667 -0.3043478 -21.0000000 -0.2291667 -0.3548387 # 3 -Inf -1.8064516 -1.5000000 -3.2000000 -0.7027027 -0.5500000 # 4 0.600000 0.4800000 -1.4375000 -0.6818182 3.0000000 -6.5555556 # 5 -1.541667 -0.3783784 4.1428571 -3.1428571 -0.3863636 -1.5000000 # 6 2.000000 -1.6956522 -0.1666667 -2.5333333 -2.3333333 -2.5200000 # V7 equ_port # 1 10.5000000 -Inf # 2 0.3043478 -3.6733696 # 3 0.1333333 -Inf # 4 -0.1470588 0.3921364 # 5 -0.8620690 -0.2612817 # 6 -8.2500000 -0.9457971 添加强制:

names(ret)[1:6] <- names(Pri[2:7])

如果您还希望通过try:

转移名称
def countChars(str: String, chr: Char): Int = str.filter(_ == chr).length