Not having played with portfolio.opim() much, I can't guarantee this will fix it, but if it requires a matrix rather than a vector and you are sure about the rest of the syntax, this might do the trick:
asset_forecast[i, , drop = FALSE] This is because: R> x = matrix(1:9, 3) R> is.matrix(x[,1]) FALSE R> is.matrix(x[,1,drop=FALSE]) TRUE Michael On Mon, Oct 10, 2011 at 9:33 PM, Darius H <xeno...@hotmail.com> wrote: > > > > > > Hello everyone, > > I would like assistance with a snippet I have written to do a recursive > portfolio optimization given time-varying return forecasts. > > In my case, I have forecast the monthly returns for nearly 55 years out on 8 > asset classes. > I need to calculate the weights for the optimal (tangency) portfolio based on > my monthly forecasts and an arbitrary covariance matrix > Getting these weights have proven difficult. > > # these are forecast (out of sample) returns; each is a 648x1 matrix > cash_forecast2=as.ts(cash_forecast) > > larg_forecast2=as.ts(larg_forecast) > > valu_forecast2=as.ts(valu_forecast) > > grow_forecast2=as.ts(grow_forecast) > > smal_forecast2=as.ts(smal_forecast) > > tres_forecast2=as.ts(tres_forecast) > > cred_forecast2=as.ts(cred_forecast) > > comm_forecast2=as.ts(comm_forecast) > > > > # make a matrix of all expected returns > # each line corresponds to forecast monthly returns for each asset class; > this is a 648x8 matrix > > asset_forecast=ts.intersect(cash_forecast2, larg_forecast2,valu_forecast2, > > grow_forecast2, smal_forecast2, tres_forecast2, cred_forecast2, > comm_forecast2) > > > > # make a covariance matrix based on the entire data > > actual_ret=cbind(cash_ret, > larg_ret,valu_ret,grow_ret,smal_ret,tres_ret,cred_ret,comm_ret) > > > cov_matrix=cov(actual_ret) > > > > opt_port = ts(matrix(,nrow=648,ncol=8)) > > > for (i in 1:648) opt_port[i,]= portfolio.optim(asset_forecast[i,], > > riskless=FALSE, shorts=TRUE, covmat = "cov_matrix", > > by.column = FALSE, by=1, align="right") > > > I get the following error message; "Error in > portfolio.optim.default(asset_forecast[i, ], shorts = TRUE, covmat = > "cov_matrix", : x is not a matrix" > > So clearly, asset_forecast[i,] is not a matrix. So I need another method to > do this. Can anyone suggest a solution that would allow my to set sail in the > right direction? > > Many thanks, > Bond, Jamesss....sorry that's my screen name... Darius :) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.