Question
I'm getting the below error message in R when I'm trying to use the portfolio.optim function in to determine the weights of an optimum portfolio
I'm getting the below error message in R when I'm trying to use the portfolio.optim function in to determine the weights of an optimum portfolio for a group of stocks.
"Error in portfolio.optim.default(my_portfolio, pm = 0.05, shorts = TRUE) : x is not a matrix"
To simplify how I created the portfolio I'll only use 3 stocks. The code I wrote to create the portfolio is below:
#Step 1: upload following packages (all previously installed in my R Studio):
ibrary(quantmod) library(tseries) library(xts) library(astsa)
#Step 2: Download daily stock prices:
getSymbols("IDXX", src="yahoo", from='2012-01-01', to='2018-12-31') getSymbols("INTC", src="yahoo", from='2012-01-01', to='2018-12-31') getSymbols("JKHY", src="yahoo", from='2012-01-01', to='2018-12-31')
# Step 3 - Obtain monthly stock returns for each stock
IDXX.rtn <- monthlyReturn(IDXX$IDXX.Adjusted, subset=NULL, type='log', leading=TRUE) INTC.rtn <- monthlyReturn(INTC$INTC.Adjusted, subset=NULL, type='log', leading=TRUE) JKHY.rtn <- monthlyReturn(JKHY$JKHY.Adjusted, subset=NULL, type='log', leading=TRUE)
#Step 4 - Create portfolio
my_portfolio <- c(IDXX.rtn, INTC.rtn, JKHY.rtn)
#Step 5 - Find weights of each stock that would create most efficient portfolio
my_eff_portfolio <- portfolio.optim(my_portfolio, pm = 0.05, shorts = TRUE)
I'm fairly certain the issue is only in Step 4 of my procedure - what code do I need to I input for the portfolio.optim R function to be executable on my created object "my_portfolio"? How do I manipulate "my_portfolio" to be in the correct format?
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started