[R] fGarch: how to use garchFit() in loop?

Yohan Chalabi chalabi at phys.ethz.ch
Mon Aug 16 13:10:01 CEST 2010


On Aug 15, 2010, at 11:14 PM, Marius Hofert wrote:

> Dear David,
> 
> thanks for the quick response. Yes, I tried "formula", but this gives the error "Multivariate data inputs require lhs for the formula."
> 
> library(fGarch)
> spec <- garchSpec(model = list(alpha = 0.1, beta = c(0.4, 0.4)))
> data <- garchSim(spec, n = 100)
> x <- list()
> for(q in 1:3){
> 	print(q)
> 	x[q] <- list(garchFit(as.formula(paste("~garch(1,",q,")",sep="")),data=x,trace=FALSE))
> }
> 
> Cheers,
> 
> Marius
> 



library(fGarch)
spec <- garchSpec(model = list(alpha = 0.1, beta = c(0.4, 0.4)))
Xt <- garchSim(spec, n = 100)
x <- list()
for(q in 1:3){
	print(q)
	x[q] <- list(garchFit(substitute(~garch(1,beta), list(beta =q)) , data = Xt, trace = FALSE))
}

should do the trick

HTH,
Yohan


> On 2010-08-15, at 22:32 , David Winsemius wrote:
> 
>> 
>> On Aug 15, 2010, at 4:26 PM, Marius Hofert wrote:
>> 
>>> Dear expeRts,
>>> 
>>> How can I specify the order p,q of a GARCH(p,q) model within a loop? Here's a minimal example showing that an "Formula and data units do not match"-error appears:
>>> 
>>> library(fGarch)
>>> spec <- garchSpec(model = list(alpha = 0.1, beta = c(0.4, 0.4)))
>>> data <- garchSim(spec, n = 100)
>>> x <- list()
>>> for(q in 1:3){
>>> 	print(q)
>>> 	x[q] <- list(garchFit(~garch(1,q),data=data,trace=FALSE))
>>> }
>>> 
>> 
>> Have you tried building a:
>> 
>> form<-formula( paste( , q, sep=""  ) ) # solution?
>> 
>> See:
>> ?formula
>> 
>> -- 
>> David Winsemius, MD
>> West Hartford, CT
> 
> ______________________________________________
> R-help at 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.
> 

--
PhD candidate
Swiss Federal Institute of Technology
Zurich

www.ethz.ch



More information about the R-help mailing list