Lu Fan, You shoud write something like:
fct_computeCoeffAndStats<-function(zooObject) { Reg_lm = lm(Y~X,data=...) Reg_coeff = Reg_lm$coefficients Tstat = summary(Reg_lm)$tstat #check names of the summary to find the right #"tstat" name to apply to summary() i think it is #Pr<... But i cant remember exactly. Moreover #the result of this will propably be a list() so #check with class() how to correctly work with #this. If it is a list you can use unlist() to have #an as.numeric format dataToReturn = cbind(Reg_coeff,Tstat) Return(as.numeric(dataToReturn)) } In the rollapply you use this function Rollapply(data, width, fct_computeCoeffandStats,...) Instead of the function you were using. Sorry for the short message an the uncomplete answer but im not home. Hope it helps for the moment. Regards Sent using BlackBerry® from Orange -----Original Message----- From: Lu Fan <lfa...@gmail.com> Date: Tue, 17 May 2011 16:15:08 To: <p.desma...@gmail.com> Cc: <r-sig-finance@r-project.org> Subject: Re: [R-SIG-Finance] rolling regression estimate std. error / t value Hi Des Mazis, Thank you for your reply. Yes, I am looking for that function too. I tried to use summary(lm(y~f1+f2+f3+f4+f5, data=as.data.frame(x)) instead of coef(lm(y~f1+f2+f3+f4+f5, data=as.data.frame(x)) , but unfortunately it didn't work. -Lu On Tue, May 17, 2011 at 4:04 PM, des Mazis, Pierre-Alexandre < p.desma...@gmail.com> wrote: > Hi Lu Fan, > > I think you should change the FUN function in your rollapply to something > that computes the coefficients and the stats you are interested in and > returns the concatenation of the results using as.numeric format. > > Regards > Sent using BlackBerry® from Orange > > -----Original Message----- > From: Lu Fan <lfa...@gmail.com> > Sender: r-sig-finance-bounces@r-project.orgDate: Tue, 17 May 2011 14:41:03 > To: <r-sig-finance@r-project.org> > Subject: [R-SIG-Finance] rolling regression estimate std. error / t value > > Dear all, > > The following codes is for a multi-factor rolling regression with rolling > window = 60: > > > z<-read.table("C:/.../dataset.txt") > > library(zoo) > > mydata=zoo(z) > > coef=rollapply(mydata,width=60,function(x) coef(lm(y~f1+f2+f3+f4+f5, > data=as.data.frame(x))),by.column=FALSE,align="right") > > The result is shown below: > > (Intercept) f1 f2 f3 f4 f5 > 60 2.61433094 0.16136881 -0.72852878 -1.62169901 -28.7663294 > -1.0079586 > 61 2.36795263 0.14779184 -0.72893841 -1.42712190 -28.9783777 > -1.0877425 > 62 1.80016092 0.13134766 -0.75010570 -0.92525342 -27.2138634 > -1.0736837 > 63 1.76904728 0.05141441 -0.94569512 -0.34299478 -19.7857978 > -0.6615184 > 64 2.29169442 0.13725741 -0.65519163 -1.44045686 -24.1101822 > -0.8517188 > 65 2.19904020 0.16520775 -0.58812883 -1.52981468 -24.9350513 > -0.9234610 > 66 1.84645778 0.23440231 -0.54350214 -1.42709733 -21.7792819 > -0.8785267 > 67 1.60558115 0.30913187 -0.25909438 -1.82122203 -21.1839368 > -0.8866916 > 68 1.49679459 0.31441766 -0.40952404 -1.52307138 -18.5434502 > -1.0317272 > 69 2.02789774 0.20500137 -0.65737973 -1.34524065 -17.1654126 > -0.8685087 > 70 2.11604869 0.09416856 -0.93294824 -0.74672358 -16.8795155 > -0.8370804 > 71 1.73134487 -0.01723606 -1.41291550 0.53813772 -15.0169149 > -0.9722847 > > So these are the regression coefficients; my question is how can I get > estimate std. error or t-value for these coefficients on a rolling basis? > Can I also add multicollinearity testing in this code? > > Thank you in advance! > > [[alternative HTML version deleted]] > >_______________________________________________ > R-SIG-Finance@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-finance > -- Subscriber-posting only. If you want to post, subscribe first. > -- Also note that this is not the r-help list where general R questions > should go. > [[alternative HTML version deleted]]
_______________________________________________ R-SIG-Finance@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.