Search code examples
roptimizationr-portfolioanalytics

Error when trying to optimize with Portfolio Analytics


I'm trying to copy the code from a website to test Portfolio Analytics library in R. But I getting an error and I don't have a clue why. The error I am getting is: Error: "package:ROI" %in% search() || requireNamespace("ROI", quietly = TRUE) is not TRUE

library(PortfolioAnalytics)

data(edhec)
returns <- edhec[, 1:6]
funds <- colnames(returns)

init.portfolio <- portfolio.spec(assets = funds)

init.portfolio <- add.constraint(portfolio = init.portfolio, 
                                 type = "full_investment")

init.portfolio <- add.constraint(portfolio = init.portfolio, 
                                 type = "long_only")

minSD.portfolio <- add.objective(portfolio=init.portfolio, 
                                 type="risk", 
                                 name="StdDev")

minSD.opt <- optimize.portfolio(R = returns, portfolio = minSD.portfolio, 
                                optimize_method = "ROI", trace = TRUE)

Solution

  • You need to install ROI plugin then you should be able to do the optimization with ROI method. you can use the following commands to install ROI plugin,

    install.packages("ROI")
    install.packages("ROI.plugin.glpk")
    install.packages("ROI.plugin.quadprog")