Dear all, I have spent the last few days on a seemingly simple and previously documented rolling regression. I have a 60 year data set organized in a ts matrix. The matrix has 5 columns; cash_ret, epy1, ism1, spread1, unemp1 I have been able to come up with the following based on previous help threads. It seems to work fine. The trouble is I get regression coefficients but need the immediate next period forecast. cash_fit= rollapply(cash_data, width=60, function(x) coef(lm(cash_ret~epy1+ism1+spread1+unemp1, data = as.data.frame(x))), by.column=FALSE, align="right"); cash_fit I tried to replace "coef" above to "predict" but I get a whole bunch of results too big to be displayed. I would be grateful if someone could guide me on how to get the next period forecast after each regression. If there is a possibility of getting the significance of each regressor and the standard error in addition to R-sq without having to spend the next week, that would be helpful as well. Many thanks, Darius [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
It seems you don't really know how predict works. If you don't supply
new data, it will only return the least squares fit to the old data, which is the large data block you saw. Check the first example given in ?predict to see how this works for new (out of sample) data. More importantly, use of lm() gives a model for contemporaneous fitting of your data to cash_ret. You probably need to use a time series model that has forecasting built into it (unless you can somehow your independent variables before your dependent variables) Michael Weylandt On Sun, Oct 2, 2011 at 11:41 PM, Darius H <[hidden email]> wrote: > > Dear all, > > I have spent the last few days on a seemingly simple and previously documented rolling regression. > > I have a 60 year data set organized in a ts matrix. > The matrix has 5 columns; cash_ret, epy1, ism1, spread1, unemp1 > > I have been able to come up with the following based on previous help threads. It seems to work fine. > The trouble is I get regression coefficients but need the immediate next period forecast. > > cash_fit= rollapply(cash_data, width=60, > > function(x) coef(lm(cash_ret~epy1+ism1+spread1+unemp1, data = as.data.frame(x))), > > by.column=FALSE, align="right"); cash_fit > > > I tried to replace "coef" above to "predict" but I get a whole bunch of results too big to be displayed. I would be grateful > if someone could guide me on how to get the next period forecast after each regression. > > If there is a possibility of getting the significance of each regressor and the standard error in addition to R-sq > without having to spend the next week, that would be helpful as well. > > Many thanks, > Darius > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
Hi Michael, Many thanks for your reply. I am handicapped in 2 different areas; firstly R and most importantly statistical concepts! May I ask you to expand on the 2nd paragraph of your reply below. I was originally using dynlm but then changed back to lm to avoid complication. Also, what do you mean by: "You probably need to use a time series model that has forecasting built into it..." Many thanks, Darius > From: [hidden email] > Date: Mon, 3 Oct 2011 13:16:47 -0400 > Subject: Re: [R] rolling regression > To: [hidden email] > CC: [hidden email] > > It seems you don't really know how predict works. If you don't supply > new data, it will only return the least squares fit to the old data, > which is the large data block you saw. Check the first example given > in ?predict to see how this works for new (out of sample) data. > > More importantly, use of lm() gives a model for contemporaneous > fitting of your data to cash_ret. You probably need to use a time > series model that has forecasting built into it (unless you can > somehow your independent variables before your dependent variables) > > Michael Weylandt > > On Sun, Oct 2, 2011 at 11:41 PM, Darius H <[hidden email]> wrote: > > > > Dear all, > > > > I have spent the last few days on a seemingly simple and previously documented rolling regression. > > > > I have a 60 year data set organized in a ts matrix. > > The matrix has 5 columns; cash_ret, epy1, ism1, spread1, unemp1 > > > > I have been able to come up with the following based on previous help threads. It seems to work fine. > > The trouble is I get regression coefficients but need the immediate next period forecast. > > > > cash_fit= rollapply(cash_data, width=60, > > > > function(x) coef(lm(cash_ret~epy1+ism1+spread1+unemp1, data = as.data.frame(x))), > > > > by.column=FALSE, align="right"); cash_fit > > > > > > I tried to replace "coef" above to "predict" but I get a whole bunch of results too big to be displayed. I would be grateful > > if someone could guide me on how to get the next period forecast after each regression. > > > > If there is a possibility of getting the significance of each regressor and the standard error in addition to R-sq > > without having to spend the next week, that would be helpful as well. > > > > Many thanks, > > Darius > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [hidden email] 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. > > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by darius
Hello everyone, I would like assistance with updating a snippet I have written to do a recursive out-of-sample portfolio optimization. The trouble I am having is with the fact that the return on the riskless asset is time varying and so is different in each period. This is what I have written so far but it does not work; for (i in 1:648) optimized_port= rollapply(asset_forecast, width=1, function(x) portfolio.optim(data = as.data.frame(x), riskless=TRUE, shorts=TRUE, rf= cash_forecast[i,])), by.column = FALSE, by=1, align="right") -cash_forecast is a 648x1 matrix having forecasted risk-free returns -asset_forecast is a 648x7 matrix holding 7 forecasted asset class returns Can someone point out how to incorporate the changing riskfree in this model. Many thanks, Darius [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
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]] ______________________________________________ [hidden email] 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. |
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 <[hidden email]> 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]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |