Re: [R] efficient use of lm over a matrix vs. using apply over rows
Good Bless you Duncan. Your explanation is crisp and to the point. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/efficient-use-of-lm-over-a-matrix-vs-using-apply-over-rows-tp870810p3742043.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] efficient use of lm over a matrix vs. using apply over rows
Good Bless you Duncan. Your explanation is crisp and to the point. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/efficient-use-of-lm-over-a-matrix-vs-using-apply-over-rows-tp870810p3742058.html Sent from the R help mailing list archive at Nabble.com. __ 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] fPortfolio-portfolio optimization
Hello everyone, I have been spending many hours on a seemingly simple portfolio optimization problem using the package fPortfolio. My optimization problem is slightly different than a standard one such that I have a known set of asset returns. My problem is how to collect this information into my functions and pass them onto the optimization function. I have written my own covariance estimation function using the "shrinkEstimator" as template. I will use the shrunk estimation of the covariance matrix with my own set of predicted returns. My code is below. Many thanks, Darius --- b=ts(ret.forecast[1,]) mu.pred=b myEstimator=function(x) { stopifnot(inherits(x, "timeSeries")) x.mat = x mu = mu.pred Sigma = .cov.shrink(x = x.mat, verbose = FALSE, ...) attr(Sigma, "lambda.var") <- NULL attr(Sigma, "lambda.var.estimated") <- NULL list(mu = mu, Sigma = Sigma) } portfolio1=portfolioSpec() a=ts(ret.mat[(1:60),(1:n.assets)]) setEstimator(portfolio1)="myEstimator" portfolio2=tangencyPortfolio(data=a, spec=portfolio1) [[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] I need help in seeing the code
Hi there, I am Darius and I am taking the R Programming course in Coursera. I have a problem that I had spent so much looking for the problem. I wrote my code and I believe that the code works perfectly fine because it produces the result as what the course demanded. However, when I tried to submit it, it says that my code is wrong. I do believe I make mistake, but I cannot seem to find it. the code is as follow: Complete.R complete <- function(directory, id = 1:332) { file <- list.files(directory, full.names=TRUE) nobs <- c() for (i in id){ file1 <- read.csv(file[i]) nobs1 <- sum(complete.cases(file1)) nobs <- c(nobs, nobs1) df <- data.frame(nobs) } return(data.frame(id,df)) } Please give me a hint where I should look at. Thank you very much for your time and concern. I look forward hearing back from you. Sincerely, Darius Mulia. [[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] AIC or AICc for a system of equations
Hi all, does anyone know of a function that would calculate AIC or AICc for a system of equations. I have several systems and I have individual AIC values but I need a global one to assess amongst several systems of equations. The systems are very similar to, but not exactly like VAR groups of equations. I have the printout from one of the systems below. Many thanks in advance, Darius. > selMod(calibresult1, Order = "AICc") modelLL K N2KAIC 1 ism1 + per1 + spread1 + unemp1 70.69719 6 19.8 -129.39438 7 ism1 + per1 + spread1 + unemp1 -28.42095 6 19.8 68.84191 5 ism1 + per1 + spread1 + unemp1 -31.00702 6 19.8 74.01404 6 ism1 + per1 + spread1 + unemp1 -31.69314 6 19.8 75.38629 4 ism1 + per1 + spread1 + unemp1 -35.55851 6 19.8 83.11702 2 ism1 + per1 + spread1 + unemp1 -49.64393 6 19.8 111.28787 3 ism1 + per1 + spread1 + unemp1 -52.75274 6 19.8 117.50547 8 ism1 + per1 + spread1 + unemp1 -56.14871 6 19.8 124.29742 deltAIC w_i AICc deltAICc w_ic 1 0. 1 -128.64438 0.1 7 198.2363 0 69.59191 198.23630 5 203.4084 0 74.76404 203.40840 6 204.7807 0 76.13629 204.78070 4 212.5114 0 83.86702 212.51140 2 240.6822 0 112.03787 240.68220 3 246.8999 0 118.25547 246.89990 8 253.6918 0 125.04742 253.69180 [[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] predict() function on a list made up of a system of equations
Hi everyone, Does anyone know how I can use the predict() function or anything similar in a various packages to forecast future values of a system of equations in a list? I keep getting an error message when I try to use the predict function and I cannot find anything on the help archives. I have also tried unlist() as to no avail. Many thanks, 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] Writing multiple regression in one function
Hello all, I am newbie to R and have not been able to find too much stuff on a version of VAR(p) I am working on. Would someone be able to tell me if there is a more elegant way of writing A function for the following? Many thanks in advance. Darius I am regressing returns of 8 asset classes on lagged values of 4 state variables and so I have 8 equations like the following: cash_lag1= dynlm (cal_cash ~ lag(cal_ism,-1) +lag(cal_per,-1) +lag(cal_spread,-1) +lag(cal_unemp,-1)) Is there a way to write pack the 8 regressions in one function? I have already tried ts.union to join the 8 returns into one vector and also as a data frame but I get error messages. [[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] rolling regression
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]] __ 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.
Re: [R] rolling regression
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: michael.weyla...@gmail.com > Date: Mon, 3 Oct 2011 13:16:47 -0400 > Subject: Re: [R] rolling regression > To: xeno...@hotmail.com > CC: r-help@r-project.org > > 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 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]] > > > > __ > > 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. > > [[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.
Re: [R] rolling regression
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]] __ 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] Rolling optimization
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] Bug or not
I have this sample from help. I try use command erase.screen() but in split mode sreen dont erase. Why? split.screen(c(2,1)) # split display into two screens split.screen(c(1,2),2) # split bottom half in two plot(1:10) # screen 3 is active, draw plot erase.screen() # forgot label, erase and redraw IN THIS PLACE SCREEN DONT ERASE plot(1:10, ylab= "ylab 3") IN THIS PLACE COMMAND PLOT ON TOP screen(1) # prepare screen 1 for output __ 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.