我花了几个小时试图了解成功利用 PortfolioAnalytics 包中的optimize.portfolio()函数需要什么,但我收到了多个错误,尽管尝试了各种优化方法(例如,“DEoptim”、“ROI”)。
安装 PortfolioAnalytics 后,我尝试在指定投资组合约束后运行 optimize.portfolio(),但收到以下错误:
错误:paste0("package:", plugin) %in% search() || requireNamespace(plugin, .... is not TRUE
尝试下载“插件”,但我收到:
Warning in install.packages : package ‘plugin’ is not available (for R version 3.3.1)
我首选的 optimize_method 是“ROI”,我已经安装了“ROI”包,但仍然收到需要“插件”的错误。
我尝试通过手动安装“DEoptim”来解决这个问题,但我仍然无法成功运行 optimize.portfolio():
pspec <- portfolio.spec(assets=names(fxreturns))
pspec <- add.constraint(pspec,type = "diversification", div_target = 0.5)
pspec <- add.constraint(pspec,type = "return",return_target=0.05)
pspec <- add.constraint(pspec,type = "leverage")
optimize.portfolio(fxreturns,portfolio = pspec,optimize_method = "DEoptim")
尽管下载了多个包(当我第一次安装“PortfolioAnalytics”时,为什么 R 不会自动安装所需的包?),当我运行“DEoptim”时收到以下错误:
seq.default 中的错误(from = round(min,rounding),to = round(max,rounding),:'from' 不能是 NA、NaN 或无限
作为参考,这里是我加载的所有包:
library(quantmod)
library(tseries)
library(PerformanceAnalytics)
library(PortfolioAnalytics)
library(xts)
library(timeSeries)
library(TTR)
require(Rblpapi)
require(reshape2)
require(xlsx)
require(Hmisc)
require(ROI)
require(data.table)
require(DEoptim)