## 4.5 Panel Data

To estimate panel data model, we need to install and load package plm.

install.packages("plm")
library(plm)

We have the following panel data

time <- c(1,2,1,2,1,2,1,2,1,2,1,2)
firm <-c(1,1,2,2,3,3,4,4,5,5,6,6)
x <- c(1, 2, 4, 3, 5, 4, 2, 5,5,6,7,8)
y <- c(2, 3, 5, 4, 3, 2, 4, 7,7,8,5,7)
p.df <- data.frame(time, firm, x, y)
p.df <- pdata.frame(p.df, index = c("firm", "time"), drop.index = F, row.names = T)

### 4.5.1 Clustering

We first estimate the model based on pooled OLS.

pooled.plm <- plm(formula=y~x, data=p.df, model="pooling") 

Then we calculate the variance-covariance matrix to be clustered by group.

coeftest(pooled.plm, vcov=vcovHC(pooled.plm,
type="sss",
cluster="group"))  
##
## t test of coefficients:
##
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.81164    0.68521  2.6439 0.024568 *
## x            0.67808    0.17151  3.9536 0.002715 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Then we calculate the variance-covariance matrix to be clustered by time.

coeftest(pooled.plm, vcov=vcovHC(pooled.plm,
type="sss",
cluster="time")) 
##
## t test of coefficients:
##
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.81164    0.71388  2.5377 0.029478 *
## x            0.67808    0.21088  3.2154 0.009246 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Two-way clustering of both time and group:

coeftest(pooled.plm, vcov=vcovDC(pooled.plm,
type="sss"))
##
## t test of coefficients:
##
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.81164    0.81130  2.2330  0.04959 *
## x            0.67808    0.22838  2.9691  0.01407 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

### 4.5.2 Fixed Effect Model

We first estimate the model based on pooled OLS.

fe.plm <- plm(formula=y~x, data=p.df, model="within")

Then we calculate the variance-covariance matrix to be clustered by group.

coeftest(fe.plm, vcov=vcovHC(fe.plm,
type="sss",
cluster="group")) 
##
## t test of coefficients:
##
##   Estimate Std. Error t value  Pr(>|t|)
## x 1.071429   0.089074  12.028 7.008e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Then we calculate the variance-covariance matrix to be clustered by time.

coeftest(fe.plm, vcov=vcovHC(fe.plm,
type="sss",
cluster="time")) 
##
## t test of coefficients:
##
##     Estimate Std. Error    t value  Pr(>|t|)
## x 1.0714e+00 4.6089e-10 2324719242 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1