PortfolioAnalytics
PortfolioAnalytics copied to clipboard
Missing imports with the new CVXR optimization method
Running the example from the vignette (see code below) produces an error: Error in Variable(N) : could not find function "Variable"
.
It appears that optimize.portfolio
uses Variable()
function from the CVXR package but does not import it, so when trying to call optimize.portfolio
without explicitly loading the library first in your environment, you get the error.
Rather than loading the CVXR library in a global environment, you could import the Variable()
function in the optimize.portfolio
function with @import
or @importFrom
to import a single function.
Not sure if this is a design intention, but it seems counterintuitive for new users (or even advanced users).
Example Code:
data(edhec)
# Use edhec for a returns object
ret_edhec <- tail(edhec, 60)
colnames(ret_edhec) <- c("CA", "CTAG", "DS", "EM", "EMN", "ED", "FIA",
"GM", "LSE", "MA", "RV", "SS", "FF")
print(head(ret_edhec, 5))
fund_edhec <- colnames(ret_edhec)
# Create portfolio object
pspec_maxret <- portfolio.spec(assets=fund_edhec)
# Add constraints to the portfolio object
pspec_maxret <- add.constraint(pspec_maxret, type="full_investment")
pspec_maxret <- add.constraint(portfolio = pspec_maxret, type = "box",
min = rep(0.02, 13),
max = c(rep(0.6, 4), rep(0.2, 9)))
# Add objective to the portfolio object
pspec_maxret <- add.objective(portfolio = pspec_maxret,
type = "return", name = "mean")
pspec_maxret
# Run the optimization with default solver
opt_maxret <- optimize.portfolio(R=ret_edhec, portfolio=pspec_maxret,
optimize_method="CVXR", trace=TRUE)