# [R] Need Help with my Code for complex GARCH (GJR)

Lin23 linusholtermann at gmx.de
Sun Nov 27 13:48:43 CET 2011

```Hello,

i want to estimate a complex GARCH-model (see below).
http://r.789695.n4.nabble.com/file/n4112396/GJR_Garch.png
W stands for the Day of the Week Dummies. r stands for returns of stock
market indices. I stands for the GJR-term.
I need some help with three problems:
1.) implementation of the GJR-term in the variance equation
2.) compute robust covariance matrix (Bollerslev/Wooldbridge,1992) for
robust standard errors
3.) extract the residuals amd volatility of my estimation

First of all my GARCH-Code:

garch2<-function(par,x,Di,Mi,Do,Fr,y,z,d){
x<<-ts(x)
y<<-ts(y)
z<<-ts(z)
Di <<-ts(Di)
Mi<<-ts(Mi)
Do<<-ts(Do)
Fr<<-ts(Fr)

n<-length(x)

a <-par
di <- par
mi <- par
do <- par
fr <- par
b1 <- par
b2 <- par
b3 <- par
b4 <- par
dum <- par
alpha0<-par
alpha<-par
beta<-par

res<-array(length(x))
hh<-array(length(x))
ll <-numeric(length(x))

res <- x-a
for (i in 2:n){

res[i]<-x[i]-a-di*Di[i]-mi*Mi[i]-do*Do[i]-fr*Fr[i]-b1*y[i]-b2*z[i-1]-b3*x[i-1]-b4*d[i]*x[i-1]
#MEan Equation
}
res<-ts(res)
hh<-var(res)
for (i in 2:n){
hh[i]<-(alpha0+alpha*res[i-1]^2+beta*(hh[i-1]-alpha0))*(1+dum*d[i])
#Variance Equation
ll[i] <- -1/2*log(2*pi*hh[i]) - 1/2*res[i]^2/hh[i]                  #
LogLikelihood
}
hh<-ts(hh)
h<-sqrt(abs(hh))
ll <- sum(ll[i])

}

x <- dat2\$r_csi

Mean = mean(x); Var = var(x); S = 1e-6
param = c(reg\$coef, dum = 0, alpha0 = 0.1*Var,alpha = 0.1, beta = 0.8)
# start values
lowerB = c(a = -10*abs(Mean),di = S-1, mi = S-1, do = S-1, fr = S-1, b1
= S-1, b2 = S-1, b3= S-1, b4= S-1, dum = S-1, alpha0 = S^2, alpha = S, beta
= S)
upperB = c(a = 10*abs(Mean), di = 1-S, mi = 1-S, do = 1-S, fr =1-S, b1 =
1-S, b2 = 1-S,b3 = 1-S, b4 = 1-S, dum = 1-S, alpha0 = 100*Var, alpha = 1-S,
beta = 1-S)

fitt<-maxLik(start=param, logLik=garch2,method="BHHH",
x=dat2\$r_csi,Di=dat2\$Di,Mi=dat2\$Mi,Do=dat2\$Do,Fr=dat2\$Fr,y=dat2\$r_t,z=dat2\$r_sp,d=dat2\$f)

Note that optim always breaks down:
nlminb and the BFGS and BHHH algorithmus from the maxLik-package work fine.
The estimated coefficients are similiar to those of the EVIEWS Estimation.
So I guess, they are correct.
Is my Implementation of the  Dummy-Variabel in the VAriance-Equation
correct?

I failed to incorporate the GJR-term in the VAriance Equation. I tried to
modify the Variance Equation:
I=0
for(i in 2:n) {
I[i] <- if (res[i-1]<0){I[i] = 1}else{if(res[i-1]>=0){I[i] = 0}}
}
I<-ts(I)

hh<-alpha0
for (i in 2:n){

hh[i]<-alpha0+alpha*res[i-1]^2+beta*(hh[i-1]-alpha0)+gjr*I[i]*res[i-1]^2

The estimation results are different from those that EVIEWS suggested. So I
think I did something wrong here.

2.) Compute robust covariance (Bollerslev/Wooldbridge,1992). I need robust
standard errors, because the real innovations in my data are not normally
distributed. Is there a way to control for this aspect other than the robust
covariance from Bollerslev/Wooldbridge?

V = H^(-1) G' G H^(-1),

where V denotes the variance-covariance matrix, H stands for the Hessian and
G represents the matrix of contributions to the gradient, the elements of
which are defined as

G_{t,i} = derivative of l_{t} w.r.t. zeta_{i},

where l_{t} is the log likelihood of the t-th observation and zeta_{i} is
the i-th estimated parameter.

Thats a way to compute the robust covariance matrix. But how to I do this i
R??? Only the maxLik-package reports the Hessian-matrix, but not the
When using nlminb for optimization I dont know how to extract the gradient
and hessian.

3.) How can I extract the residuals of my GARCH-model and the
volatility(hh)? So that I can plot them or do a Box-test.

I hope someone can help me. That would be awesome. Thanks in advance.
Lin23

--
View this message in context: http://r.789695.n4.nabble.com/Need-Help-with-my-Code-for-complex-GARCH-GJR-tp4112396p4112396.html
Sent from the R help mailing list archive at Nabble.com.

```