I used the R-package PortfolioAnalytics for portfolio optimization. In the portfolio optimization part. I used the function optimize.portfolio to set up my optimization. However, here was the error just showed that
Error in optimize.portfolio(R = initial_weights, portfolio = p, optimize_method = "random", : unused arguments (R = initial_weights, portfolio = p, optimize_method = "random", rp = rp, trace = TRUE)
I checked my code and I made sure everything was fine beside this step. So I am wondering this function is not working? Anybody knows how to fix this situation or recommend other package to use?
Thank you so much.
Here is my code:
library(PortfolioAnalytics)
library(quantmod)
library(PerformanceAnalytics)
library(zoo)
library(plotly)
# Get data of the stock
getSymbols(c("SWX","SPY","ICUI","MSFT"), src = 'yahoo', from = '2016-01-01')
Adjusted_price <- merge.zoo(SWX[,6], SPY[,6], ICUI[,6], MSFT[,6])
#Get Returns of portfolio
returns.portfolio <- CalculateReturns(Adjusted_price)
returns.portfolio <- na.omit(returns.portfolio)
#Set names of each stocks
colnames(returns.portfolio) <- c("Southwest Gas Holdings","S&P 500","ICU Medical","Microsoft")
# Meanreturns
meanReturns <- colMeans(returns.portfolio)
covMat <- cov(returns.portfolio)
#Initial weights
portfolio.spec(assets = c("SWX","SPY","ICUI","MSFT") )
initial_weights <- c("SWX" = 0.25,"SPX" = 0.25,"ICUI"=0.25,"MSFT"=0.25)
port <- intial_weights
portfolio.spec(assets = initial_weights)
#Initialize Portfolio specification
p <- portfolio.spec(assets = initial_weights)
#Add constraint
p <- add.constraint(portfolio = p, type = "weight_sum", min_sum = 1, max_sum = 1)
#Add objective
p <- add.objective(portfolio = p, type = "risk", name = "StdDev")
p <- add.objective(portfolio = p, type = "return", name = "mean")
#optimization
opt_single <- optimize.portfolio(R = initial_weights, portfolio = p, optimize_method = "random", rp = rp, trace = TRUE)
Error in optimize.portfolio(R = initial_weights, portfolio = p, optimize_method = "random", :
unused arguments (R = initial_weights, portfolio = p, optimize_method = "random", rp = rp, trace = TRUE)
port <- intial_weights
does produce an error as you called this variable initial_weights one line before. $\endgroup$