6/18/12 6:17 PM
> setwd("C:/Users/rst/teaching/bs41202/sp2010")
### Compare skew-Student-t and Student-t distributions
### in fGarch packet, the commands are sstd and std, respectively.
> x1=rstd(5000,0,1,5) #(nobs, mean, sd, nu
> x2=rsstd(5000,0,1,5,0.8) # skew to the left (nobs, mean,sd,nu,xi) where xi is the skew
parameter.
> x3=rsstd(5000,0,1,5,1.5) # skew to the right
> par(mfcol=c(1,2))
> hist(x1,nclass=50)
> hist(x2,nclass=50)
> hist(x1,nclass=50)
> hist(x3,nclass=50)
### Below are commands for Ox-G@RCH estimation ###
> da=read.table("m-intc7303.txt",header=T)
> intc=log(da[,2]+1)
> source("garchoxfit_R.txt") %Need to download the file garchoxfit_R.txt
> x=ts(intc,frequency=12,start=c(1973,1))
> plot(x)
> acf(intc)
> acf(intc^2)
> Box.test(intc^2,lag=10,type='Ljung')
> pacf(intc^2,lag=10)
> m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,3),series=intc)
> m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,1),series=intc)
> names(m1)
> par(mfcol=c(2,1))
> plot(intc,type='l')
> plot(sqrt(m1$condvars),type='l')
> par(mfcol=c(1,1))
> sresi=m1$residuals/sqrt(m1$condvars) % Compute standardized residuals
> qqnorm(sresi) % Obtain normal prob. plot
> qqline(sresi)
> Box.test(sresi,10,type='Ljung')
> Box.test(sresi^2,10,type='Ljung')
% Use Student-t innovations
> m1=garchOxFit(formula.mean=~arma(0,0),formula.var=~garch(0,1),series=intc,cond.dist="t")
> sresi=m1$residuals/sqrt(m1$condvars) % Compute standardized residuals
> qqplot(rt(10000,6.0),sresi) % t-prob plot.
> qqline(sresi)
> #### Use fGarch package of RMetrics in R ####
> library(fGarch)
> m2=garchFit(~garch(3,0),data=intc) % lots of output
> m2=garchFit(~garch(3,0),data=intc,trace=F) %no output printed.
> summary(m2) % Obtain results and model checking statististics
> sresi=residuals(m2,standardize=T) % Obtain standardized residuals - epsilon(t)-hat
> sigma.t=volatility(m2) % obtain the fitted volatility sigma_t.
> qqnorm(sresi)
> qqline(sresi)
http://faculty.chicagobooth.edu/ruey.tsay/teaching/bs41202/sp2010/Rcommands_lec4.txt Page 1 of 2
6/18/12 6:17 PM
> m2=garchFit(~garch(1,0),data=intc,trace=F)
> summary(m2)
> predict(m2,6) % prediction
# use Student-t innovations
> m2=garchFit(~garch(1,0),data=intc,trace=F,cond.dist=c("std"))
> summary(m2)
# use skewed Student-t innovations
> m3=garchFit(~garch(1,0),data=intc,trace=F,cond.dist=c("sstd"))
> summary(m3)
# Use ARMA(p,q)+GARCH(1,1) model with normal innovations
> m4=garchFit(~arma(p,q)+garch(1,0),data=intc,trace=F)
> summary(m4)
http://faculty.chicagobooth.edu/ruey.tsay/teaching/bs41202/sp2010/Rcommands_lec4.txt Page 2 of 2