[R-sig-finance] tips and tricks for rolling regressions?
Gregor.gawron at rmf.ch
Gregor.gawron at rmf.ch
Wed Aug 25 16:11:35 CEST 2004
There is a 'running' function in the gregmisc package which I slightly
changed to suit my calcualtions. Below the code for doing one-step ahead
GARCH(1,1) estimations. This is just an extraction of a longer code for
various purposes, so it is surely not the nicest one. However, you can
easily replace the 'garch' function with 'lm' and adjust the output to
your needs.
-----------------------------------------
#moving one-step-ahead GARCH(1,1)
library(tseries)
width<-1500 #the size of the sample window
data(EuStockMarkets) #data taken from package 'tseries'
X<- diff(log(EuStockMarkets))[,"DAX"]
n<-length(X)
from<-sapply((1:n)-width+1,function(x) max(x,1))
to<-1:n
all.elements<-apply(cbind(from,to),1,function(x) seq(x[1],x[2]))
good.elements<-all.elements[width:length(all.elements)]
better.elements<-as.data.frame(good.elements)
#estimating GARCH(1,1) and extracting the fitted coefficients
myfun<-function(i){
out<-garch(X[better.elements[,i]])
garchsum<-summary(out)[[2]][1:3]
names(garchsum)<-NULL
return(garchsum)
}
res<-sapply(1:length(better.elements),function(i) myfun(i))
#adjusting the output
RES<-matrix(unlist(res))
dim(RES)<-dim(res)
dimnames(RES)<-list(c("a0","a1","b1"),NULL)
(RES<-t(RES))
---------------------------------------------------
-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Taher Khan
Sent: Mittwoch, 25. August 2004 13:32
To: r-sig-finance
Subject: [R-sig-finance] tips and tricks for rolling regressions?
Greetings!
I want to find out if there is a clever way to do a one-step-ahead
moving window rolling regression using a vectorized operation like with
apply, rather than a for loop.
Does anyone have a code snippet they would like to share for how they do
rolling regressions?!?!
Thanks in advance!
_______________________________________________
R-sig-finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Any information in this communication is confidential and ma...{{dropped}}
More information about the R-sig-finance
mailing list