r - To calculate equal-weight portfolio by rowMeans -
the head(pri) of data below,
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
the head(ret) after calculating stock returns stock price shown below.
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
then, try add new column, ret$equ_port, calculated row row first 5 stocks, aapl, googl, bac, wfc,and wmt.
ret$equ_port<-rowmeans(ret[,1:5])
however, not work.
warning message: in ret$equ_port <- rowmeans(ret[, 1:5]) : coercing lhs list
and head(ret) below,
[[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
how can solve problem? data need below,
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
your logic correct. add coercion data.frame
first:
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
if you'd names transfer on try:
names(ret)[1:6] <- names(pri[2:7])
Comments
Post a Comment