2017-09-12 49 views
0

我一直在尝试计算我的投资组合回报和个人股票投入。我偶然发现了this post,这似乎来自帮助编写PerformanceAnalytics的人。PerformanceAnalytics沙箱中的to.period.contributions()错误

在文章的最后,他发布了一些功能link to r-forge with a sandbox file

因此,我试图通过to.monthly.contributions()函数将我的日常回报转换为总计的每月回报,但我遇到了xts错误!

这里是我的代码:

library(PerformanceAnalytics) 
library(quantmod) 

stock.weights <- c(.15, .20, .25, .225, .175) 
symbols <- c("GOOG", "AMZN", "BA", "FB", "AAPL") 
getSymbols(symbols, src = 'google', from = "2016") 
#xts with daily closing of each stock 
merged.closing <- merge(GOOG[,4], AMZN[,4], BA[,4], FB[,4], AAPL[,4]) 
#xts with returns 
merged.return <- na.omit(Return.calculate(merged.closing)) 
# weighted returns rebalanced quartely 
portfolio.returns = Return.portfolio(merged.return, weights = stock.weights, 
            rebalance_on = "quarters", verbose = TRUE) 
#to monthly contributions function 
to.monthly.contributions(portfolio.returns$contributions) 

然而,当我运行的最后一行,我得到了以下错误消息:

Error in inherits(x, "xts") : 
    argument "Contributions" is missing, with no default 
5. inherits(x, "xts") 
4. is.xts(x) 
3. checkData(Contributions) 
2. to.period.contributions(contributions = contributions, period = "months") 
1. to.monthly.contributions(portfolio.returns$contributions) 

我猜的错误有事情做与portfolio.returns$contributions不是一个xts?但我不确定如何解决这个问题。

请注意,如果任何人有任何更好的想法或资料来计算投资组合收益按月/季/年计算,我很想听听他们的记录,他们需要考虑体重变化,重新平衡和贡献改变!

+0

你有两次Google,'merged.closing'没有Apple。这可能吗? – lebelinoz

+0

我找不到'to.monthly.contributions'。那是哪个包? – lebelinoz

+0

我无法重现教程。我遇到了一个定义函数中的日期的错误,并且我遇到了同样的问题。 – Katerina

回答

0

请注意,PerformanceAnalytics(以及该R-Forge回购中的许多其他软件包)已移至Brian Peterson's GitHub account。大约一年前你会看到sandbox/to.period.contributions.R的一些变化。这可能会导致你一些问题。

另一个问题是由Return.portfolio()返回的对象没有contributions元素。你想要的元素名称是contribution(单数)。

解决了这两个问题后,您的to.monthly.contributions()调用起作用。

R> to.monthly.contributions(portfolio.returns$contribution) 
       GOOG.Close AMZN.Close  BA.Close  FB.Close AAPL.Close Portfolio Return 
2016-01-29 0.0002244419 -0.0156956938 -0.036245552 0.0219893367 -1.330565e-02  -0.043033115 
2016-02-29 -0.0095461956 -0.0113127380 -0.003625779 -0.0121676134 -1.128288e-03  -0.037780614 
2016-03-31 0.0103601952 0.0140210157 0.016927654 0.0171632715 2.218899e-02  0.080661130 
2016-04-29 -0.0104584200 0.0222188532 0.015479754 0.0068624014 -2.448619e-02  0.009616397 
2016-05-31 0.0085179936 0.0210895602 -0.016873347 0.0024024015 9.732993e-03  0.024869602 
2016-06-30 -0.0084883795 -0.0023345382 0.007080427 -0.0086331655 -6.610526e-03  -0.018986182 
2016-07-29 0.0166211530 0.0120706520 0.007295757 0.0190190760 1.576098e-02  0.070767622 
2016-08-31 -0.0003521895 0.0027014233 -0.007568643 0.0040084230 3.231073e-03  0.002020086 
2016-09-30 0.0020684771 0.0177517727 0.004108611 0.0039452914 1.185750e-02  0.039731657 
2016-10-31 0.0013990917 -0.0113434690 0.020286170 0.0047711858 7.585139e-04  0.015871492 
2016-11-30 -0.0050340240 -0.0092287866 0.015187074 -0.0217047070 -4.601884e-03  -0.025382327 
2016-12-30 0.0026858660 -0.0001688763 0.009813394 -0.0059705490 8.286484e-03  0.014646319 
2017-01-31 0.0048528154 0.0196327363 0.012429342 0.0298631030 8.355638e-03  0.075133635 
2017-02-28 0.0047757941 0.0053484794 0.025108019 0.0094951963 2.198006e-02  0.066707545 
2017-03-31 0.0010760715 0.0096512663 -0.004718775 0.0111011780 8.787645e-03  0.025897386 
2017-04-28 0.0138145523 0.0086741715 0.011265973 0.0129883844 -1.218154e-05  0.046730900 
2017-05-31 0.0101747490 0.0150069699 0.003781232 0.0018310137 1.060194e-02  0.041395909 
2017-06-30 -0.0093108126 -0.0055092033 0.013123207 -0.0006974798 -9.767034e-03  -0.012161323 
2017-07-31 0.0035934766 0.0040867769 0.056523388 0.0272271162 5.723163e-03  0.097153921 
2017-08-31 0.0013284632 -0.0013521084 -0.003226369 0.0036945746 1.691168e-02  0.017356239 
2017-09-19 -0.0025908954 -0.0019880088 0.014497492 0.0007343197 -5.737005e-03  0.004915902