我一直在尝试计算我的投资组合回报和个人股票投入。我偶然发现了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?但我不确定如何解决这个问题。
请注意,如果任何人有任何更好的想法或资料来计算投资组合收益按月/季/年计算,我很想听听他们的记录,他们需要考虑体重变化,重新平衡和贡献改变!
你有两次Google,'merged.closing'没有Apple。这可能吗? – lebelinoz
我找不到'to.monthly.contributions'。那是哪个包? – lebelinoz
我无法重现教程。我遇到了一个定义函数中的日期的错误,并且我遇到了同样的问题。 – Katerina