r - Error in to.period.contributions() from PerformanceAnalytics sandbox -
i've been trying calculate portfolio returns , individual stock contributions. stumbled along this post, appears guy write performanceanalytics.
at end of article posts link r-forge sandbox file functions.
so i'm trying convert daily returns summed monthly returns via to.monthly.contributions()
function i'm running xts error!
here's code:
library(performanceanalytics) library(quantmod) stock.weights <- c(.15, .20, .25, .225, .175) symbols <- c("goog", "amzn", "ba", "fb", "aapl") getsymbols(symbols, src = 'google', = "2016") #xts daily closing of each stock merged.closing <- merge(goog[,4], amzn[,4], ba[,4], fb[,4], aapl[,4]) #xts 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)
however when run last line following error message:
error in inherits(x, "xts") : argument "contributions" missing, 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)
i'm guessing error has portfolio.returns$contributions
not being xts? i'm not sure how around that.
on side note, if has better ideas or sources calculating portfolio returns months/quarters/years i'm keen hear, bearing in mind need account weight changes, re-balances , contributions changes!
note performanceanalytics (and many other packages in r-forge repo) have moved brian peterson's github account. there see changes sandbox/to.period.contributions.r year ago. might causing issue(s).
another issue object returned return.portfolio()
not have contributions
element. element name want contribution
(singular).
after addressing 2 issues, to.monthly.contributions()
call works.
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
Comments
Post a Comment