# e-TA 7: Granger Causality

Welcome to a new issue of e-Tutorial. This issue focuses on time series models, with special emphasis on the tests of Granger causality. We would like to remark that the theoretical background given in class is essential to proceed with the computational exercise below. Thus, I recommend you to study Prof. Koenker’s Lectures Note 9 as you go through the tutorial.^{1}

# Data

The first thing you need is to download the data in text format from the Econ 508 web site. Save it in your preferred directory (I will save my as “C:/eggs.csv”.) The next step is inserting the Data in R:

```
Thurman<-read.table("C:/eggs1.txt", header=T, sep=",")
head(Thurman)
```

If you don’t want to do the previous you can call your data from the web:

` Thurman<-read.table("http://www.econ.uiuc.edu/~econ508/data/eggs.csv", header=T, sep=",")`

The next step is to declare chickens and eggs as time series:

```
year<-ts(Thurman$year)
chic<-ts(Thurman$chic)
egg<-ts(Thurman$egg)
```

# Granger Causality

In problem set 3 you will be asked to replicate the results of Thurman and Fisher’s (1988), Table 1. I recommend you to sketch the Granger test, explain the NULL and the ALTERNATIVE hypotheses, and run the test for the causality for all lags, and both directions. At each round, collect the F-test statistics, p-values, and R-squares. *At the end, please provide a table in the same format of Thurman and Fisher’s (1988), containing your results, along with a graphical analysis*.

In R: There is a code for the Granger test as follows:

```
"granger" <-function(d, L, k = 1) {
#d is a bivariate time-series: regress d[,k] on L lags of d[,1] and d[,2].
#This is a modified version for R, in which the command ts.matrix was substituted by ts.intersect.
names.d <- dimnames(d)[[2]]
D <- d
for(i in 1:L)
{
D <-ts.intersect(D, lag(d, - i))
#don't you find -1 bizarre here and it annoying to need the loop
}
dimnames(D)[[2]] <- paste(rep(names.d, L + 1), "_", rep(0:L, times = rep(2, L + 1)), sep = "")
y <- D[, k]
n <- length(y)
x1 <- D[, - (1:2)]
x0 <- x1[, ((1:L) * 2) - (k %% 2)]
z1 <- lm(y ~ x1)
z0 <- lm(y ~ x0)
S1 <- sum(z1$resid^2)
S0 <- sum(z0$resid^2)
ftest <- ((S0 - S1)/L)/(S1/(n - 2 * L - 1))
list(ftest = ftest, p.val = 1 - pf(ftest, L, n - 2 * L - 1), R2 = summary(z1)$r.squared)
}
```

This code is available at the rountines tab, under the name granger.R. Your job is to copy the routine above and past it in the R console. This will create a function called “granger” that does the test for you. Next you should start running the Granger causality test for each of the lags and directions. If you don’t feel like downloading it and doing the copy pasting you can directly source it from the web page

` source("http://www.econ.uiuc.edu/~econ508/routines/granger.R")`

For example, to test if chickens Granger cause eggs, using 1 lag, you type:

` granger(cbind(egg,chic), L=1) `

```
$ftest
[1] 0.007306
$p.val
[1] 0.9321
$R2
[1] 0.9763
```

and obviously, to test in the reverse direction, you type:

`granger(cbind(chic, egg), L=1) `

```
$ftest
[1] 0.0753
$p.val
[1] 0.7846
$R2
[1] 0.7633
```

Do that for lags 1,2,3, and 4, and provide a table like Thurman and Fisher’s (1988), containing your results, plus a graphical analysis.

Please send comments to bottan2@illinois.edu or srmntbr2@illinois.edu↩