This worked example introduces estimation in panel data in R using example on partisan governments and economics measures for a panel of OECD states.

Setup

This tutor

library("lmtest")
library("car")
library("dplyr")
library("ggplot2")

The panel data specific packages used are plm and pcse:

library("plm")
library("pcse")

Data

This example data was the data from Garrett (1989) Partisan Politics in the Global Economy. The analyses are similar (but not the same) as those run in King, Wittenberg and Tomz (2000), Bailey and Katz, and Beck and Katz (2011). The replication data is from Beck and Katz (2011).

data_path <- "http://pols503.github.io/pols_503_sp15/data/garrett1998.csv"
garrett1998 <- read.csv(data_path, stringsAsFactors = FALSE)

The data consist of 350 observations for 0 countries for 25 years (1966 – 1990 with the following variables

country

A country identifier (number)

year

Year

unem

unemployment

inf

inflation

gdp

Growth in GDP

leftlab

Political power of left parties in the government. Index from 0–5.

corp

A measure of corporatism or how encompassing the labor movement, e.g. encompassing unions. Index from 0–4.

capmob

Capital mobility. Index from 0–4.

oild

Oil dependency

demand

OECD demand (growth rate in all OECD countries)

countryname

The country name

Effect of the left parties, corporatism and the interaction of the two.

Garret’s theories involve the combination of left-leaning parties in government and encompassing trade unions.

Models estimated with LM

Pooled time-series, cross-section

mod1 <- lm(gdp ~ corp * leftlab + demand + oild + trade, data = garrett1998)

Controlling for country

mod2 <- lm(gdp ~ corp * leftlab + demand + oild + trade + countryname,
           data = garrett1998)

Controlling for country and year

mod3 <- lm(gdp ~ corp * leftlab + demand + oild + trade + countryname + factor(year),
           data = garrett1998)

Lagged dependent variable

mod_ldv <- lm(gdp ~ gdpl + corp * leftlab + demand + oild + trade + year,
              data = garrett1998)
mod_ldv
## 
## Call:
## lm(formula = gdp ~ gdpl + corp * leftlab + demand + oild + trade + 
##     year, data = garrett1998)
## 
## Coefficients:
##  (Intercept)          gdpl          corp       leftlab        demand  
##    80.061094      0.293309     -0.403889     -0.617747      0.008225  
##         oild         trade          year  corp:leftlab  
##    -4.890031     -0.029882     -0.038558      0.176266

Estimating panel-corrected standard errors after a lagged dependent variable model.

pcse(mod_ldv, groupN = garrett1998$country, groupT = garrett1998$year)
## $vcov
##               X.Intercept.          gdpl          corp       leftlab
## X.Intercept. 1836.07724475 -8.924833e-01 -8.923448e-01 -1.273943e+00
## gdpl           -0.89248334  4.478574e-03  1.994741e-03  3.057399e-03
## corp           -0.89234485  1.994741e-03  4.242712e-02  3.088963e-02
## leftlab        -1.27394332  3.057399e-03  3.088963e-02  5.725434e-02
## demand         -0.01518286 -6.371204e-06 -1.063662e-06  2.820008e-06
## oild          -29.92733871  4.525943e-02  1.336451e-01  2.754657e-02
## trade           0.08391008  4.116534e-05 -3.151312e-04 -7.239340e-05
## year           -0.92611645  4.391191e-04  4.148240e-04  5.941080e-04
## corp.leftlab    0.37348774 -6.823238e-04 -1.435762e-02 -1.581397e-02
##                     demand          oild         trade          year
## X.Intercept. -1.518286e-02 -2.992734e+01  8.391008e-02 -9.261165e-01
## gdpl         -6.371204e-06  4.525943e-02  4.116534e-05  4.391191e-04
## corp         -1.063662e-06  1.336451e-01 -3.151312e-04  4.148240e-04
## leftlab       2.820008e-06  2.754657e-02 -7.239340e-05  5.941080e-04
## demand        2.179216e-06  6.272688e-04 -6.702467e-06  7.665887e-06
## oild          6.272688e-04  1.878762e+01 -5.358596e-03  1.493209e-02
## trade        -6.702467e-06 -5.358596e-03  5.530314e-05 -4.329927e-05
## year          7.665887e-06  1.493209e-02 -4.329927e-05  4.672369e-04
## corp.leftlab  1.644199e-06 -2.245614e-02  1.168068e-04 -1.758201e-04
##               corp.leftlab
## X.Intercept.  3.734877e-01
## gdpl         -6.823238e-04
## corp         -1.435762e-02
## leftlab      -1.581397e-02
## demand        1.644199e-06
## oild         -2.245614e-02
## trade         1.168068e-04
## year         -1.758201e-04
## corp.leftlab  6.166286e-03
## 
## $pcse
## X.Intercept.         gdpl         corp      leftlab       demand 
## 42.849471931  0.066922150  0.205978457  0.239278794  0.001476217 
##         oild        trade         year corp.leftlab 
##  4.334469083  0.007436608  0.021615663  0.078525705 
## 
## $b
##  (Intercept)         gdpl         corp      leftlab       demand 
## 80.061094407  0.293309090 -0.403889158 -0.617746797  0.008224868 
##         oild        trade         year corp:leftlab 
## -4.890031278 -0.029882287 -0.038558261  0.176266133 
## 
## $tstats
##  (Intercept)         gdpl         corp      leftlab       demand 
##     1.868427     4.382840    -1.960832    -2.581703     5.571586 
##         oild        trade         year corp:leftlab 
##    -1.128173    -4.018268    -1.783811     2.244693 
## 
## $df
## [1] 341
## 
## $pval
##  (Intercept)         gdpl         corp      leftlab       demand 
## 6.255993e-02 1.561865e-05 5.071226e-02 1.024822e-02 5.130916e-08 
##         oild        trade         year corp:leftlab 
## 2.600400e-01 7.217957e-05 7.534352e-02 2.542850e-02 
## 
## $pairwise
## [1] FALSE
## 
## $nobs
## [1] 350
## 
## $nmiss
## [1] 0
## 
## $call
## pcse(object = mod_ldv, groupN = garrett1998$country, groupT = garrett1998$year)
## 
## attr(,"class")
## [1] "pcse"

Panel Models using plm

The functions in plm need to know which variables correspond to id and time in the data frame. So use the function pdata.frame to create a data frame friendly with the plm functions.

pgarrett <- pdata.frame(garrett1998, index = c("countryname", "year"))

Within (fixed-effects) model

mod_within <- plm(gdp ~ corp * leftlab + demand + oild + trade + year,
                  data = pgarrett, model = "within")
summary(mod_within)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = gdp ~ corp * leftlab + demand + oild + trade + 
##     year, data = pgarrett, model = "within")
## 
## Balanced Panel: n=14, T=25, N=350
## 
## Residuals :
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -5.2100 -1.0200  0.0369  1.0000  4.6900 
## 
## Coefficients :
##                Estimate Std. Error t-value  Pr(>|t|)    
## corp          0.5669585  0.5685383  0.9972 0.3194451    
## leftlab      -0.6490591  0.3874089 -1.6754 0.0948802 .  
## demand       -0.0080754  0.0021784 -3.7070 0.0002489 ***
## oild         -8.4801198  6.2795243 -1.3504 0.1778728    
## trade         0.0374468  0.0167635  2.2338 0.0262167 *  
## year1967     -1.0224698  0.6636366 -1.5407 0.1244215    
## year1968      0.6208874  0.6420478  0.9670 0.3342866    
## year1969      2.0101661  0.6418841  3.1317 0.0019062 ** 
## year1970      0.1680420  0.6725343  0.2499 0.8028603    
## year1971     -1.5533423  0.6667365 -2.3298 0.0204686 *  
## year1972      0.4941926  0.6511567  0.7589 0.4484692    
## year1973      1.5523570  0.6517948  2.3817 0.0178457 *  
## year1974     -4.1051472  0.8180272 -5.0184 8.857e-07 ***
## year1975     -6.9624994  0.9505922 -7.3244 2.141e-12 ***
## year1976      0.2075046  0.6640872  0.3125 0.7548993    
## year1977     -2.8350941  0.6890417 -4.1145 4.992e-05 ***
## year1978     -1.8088430  0.6766242 -2.6733 0.0079131 ** 
## year1979     -1.3251267  0.6991356 -1.8954 0.0589845 .  
## year1980     -4.1802011  0.8343638 -5.0100 9.218e-07 ***
## year1981     -5.2619782  0.8083506 -6.5095 3.083e-10 ***
## year1982     -6.6518768  0.9856099 -6.7490 7.465e-11 ***
## year1983     -3.3253888  0.7473295 -4.4497 1.206e-05 ***
## year1984     -1.1318565  0.7041248 -1.6075 0.1089835    
## year1985     -1.8905450  0.7322675 -2.5818 0.0102944 *  
## year1986     -2.9050938  0.7536212 -3.8548 0.0001412 ***
## year1987     -2.4052672  0.6997451 -3.4373 0.0006688 ***
## year1988     -0.9238201  0.6783068 -1.3620 0.1742153    
## year1989     -2.0653889  0.7054172 -2.9279 0.0036687 ** 
## year1990     -3.1559856  0.7564154 -4.1723 3.933e-05 ***
## corp:leftlab  0.2758505  0.1437601  1.9188 0.0559365 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1793.5
## Residual Sum of Squares: 881.08
## R-Squared      :  0.50873 
##       Adj. R-Squared :  0.44477 
## F-statistic: 10.5625 on 30 and 306 DF, p-value: < 2.22e-16

Recover the fixed effects

fixef(mod_within)
##     Austria     Belgium      Canada     Denmark     Finland      France 
##   0.1183194   1.6125709   4.0759096   0.4528291   2.1303994   4.9929682 
##     Germany       Italy       Japan Netherlands     Norway       Sweden 
##   1.5925973   2.5675705   6.4440796   2.5177996   1.9605038  -0.1088838 
##          UK          US 
##   2.0211691   3.7676501

Random effects model

mod_re <- plm(gdp ~ corp * leftlab + demand + oild + trade,
                  data = pgarrett, model = "random")
summary(mod_re)
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = gdp ~ corp * leftlab + demand + oild + trade, data = pgarrett, 
##     model = "random")
## 
## Balanced Panel: n=14, T=25, N=350
## 
## Effects:
##                 var std.dev share
## idiosyncratic 3.909   1.977 0.773
## individual    1.145   1.070 0.227
## theta:  0.6533  
## 
## Residuals :
##    Min. 1st Qu.  Median 3rd Qu.    Max. 
## -5.9900 -1.2400 -0.0819  1.0800  6.9700 
## 
## Coefficients :
##                 Estimate  Std. Error t-value  Pr(>|t|)    
## (Intercept)    5.9879526   1.1294870  5.3015 2.062e-07 ***
## corp          -0.5845495   0.4308488 -1.3567   0.17576    
## leftlab       -0.9230097   0.4168615 -2.2142   0.02747 *  
## demand         0.0086524   0.0010632  8.1383 7.468e-15 ***
## oild         -10.8127060   5.5343674 -1.9537   0.05154 .  
## trade         -0.0500292   0.0091757 -5.4524 9.523e-08 ***
## corp:leftlab   0.3400063   0.1454336  2.3379   0.01997 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    1826.1
## Residual Sum of Squares: 1334.9
## R-Squared      :  0.26899 
##       Adj. R-Squared :  0.26361 
## F-statistic: 21.0351 on 6 and 343 DF, p-value: < 2.22e-16

Compare the random effects model with the fixed effects model using a Hausman test

phtest(mod_within, mod_re)
## 
##  Hausman Test
## 
## data:  gdp ~ corp * leftlab + demand + oild + trade + year
## chisq = 97.216, df = 6, p-value < 2.2e-16
## alternative hypothesis: one model is inconsistent

If we can reject the null, then we should use the fixed effects estimator.