我正在尝试使用以下代码优化edhec数据,但只获得NAs作为优化权重
library(openxlsx)
library(PortfolioAnalytics)
library(PerformanceAnalytics)
library(plyr)
library(dplyr)
library(reshape2)
library(ROI)
library(ggplot2)
library(plotly)
library(car)
library(quantmod)
library(quadprog)
library(ROI.plugin.symphony)
require(ROI.plugin.glpk)
require(ROI.plugin.quadprog)
library(ROI.plugin.lpsolve)
library(Rglpk)
library(DEoptim)
library(fGarch)
library(pso)
library(GenSA)
library(nloptr)
data(edhec)
returns <- edhec[, 1:4]
colnames(returns) <- c("CA", "CTAG", "DS", "EM")
funds <- colnames(returns)
portf_maxret <- portfolio.spec(assets=funds)
portf_maxret <- add.constraint(portfolio=portf_maxret, type="return", return_target=.0075)
portf_maxret<-add.objective(portfolio=portf_maxret, type="risk", name="StdDev")
opt_maxret <- optimize.portfolio(R=returns, portfolio=portf_maxret,optimize_method="ROI", trace=TRUE)
opt_maxret的输出是
***********************************
PortfolioAnalytics Optimization
***********************************
Call:
optimize.portfolio(R = returns, portfolio = portf_maxret, optimize_method = "ROI",
trace = TRUE)
Optimal Weights:
CA CTAG DS EM
NA NA NA NA
Objective Measure:
StdDev
NA
任何数据都会发生同样的情况,只有当我尝试在type="risk"
中设置optimize.portfolio
时才会发生这种情况,即根据与之相关的风险尝试优化投资组合。
答案 0 :(得分:1)
当你试图在你的投资组合中给出相同的名称时,可能会引起这个问题:
portf_maxret<-add.objective(portfolio=portf_maxret, type="risk", name="StdDev")
尝试将portf_maxret
更改为其他内容,并将其用作优化中的投资组合名称