2
當我嘗試使用ROI
運行optimize.portfolio
函數時,出現錯誤。PortfolioAnalytics包中的optimize.portfolio中的索引錯誤中的重複條目
Error in ROI::V_bound(li = seq.int(1L, N), lb = as.numeric(lb), ui = seq.int(1L, : duplicated entries in indices.
而當我使用DEoptim
我得到。
Error in sample.int(length(x), size, replace, prob) :
invalid first argument
當股票數據使用getSymbols
收集有一個關於退換貨物的長度警告。 research I did表明這不是問題。
我使用的代碼如下。任何幫助,將不勝感激。
library(quantmod)
library(PortfolioAnalytics)
library(DEoptim)
library(ROI)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
symbols <- c("MSFT", "MMM", "AMZN")
e <- new.env()
getSymbols(symbols, src="yahoo", env=e)
stocks <- do.call(merge, eapply(e, Cl)[symbols])
ret <- diff(log(df))
ret <- ret[2:(nrow(df)),]
portfolio <- portfolio.spec(ret)
portfolio
portfolio <- add.constraint(portfolio = portfolio, type = "weight_sum", min_sum=0.99,max_sum=1.01)
portfolio <- add.constraint(portfolio = portfolio, type = "long_only")
portfolio <- add.objective(portfolio=portfolio, type="risk", name="StdDev")
optimise <- optimize.portfolio(R = ret, portfolio = portfolio, optimize_method = "ROI")