-
Notifications
You must be signed in to change notification settings - Fork 1
/
panel-data.Rmd
161 lines (105 loc) · 3.43 KB
/
panel-data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Panel Data"
author: "Jeffrey B. Arnold"
date: "06/01/2015"
output: html_document
---
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
```{r message = FALSE}
library("lmtest")
library("car")
library("dplyr")
library("ggplot2")
```
The panel data specific packages used are **plm** and **pcse**:
```{r message = FALSE}
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).
```{r}
data_path <- "data/garrett1998.csv"
garrett1998 <- read.csv(data_path, stringsAsFactors = FALSE)
```
The data consist of `r nrow(garrett1998)` observations for `r length(unique(garrett1998$county))` countries for `r length(unique(garrett1998$year))` years (`r min(garrett1998$year)` -- `r max(garrett1998$year)` 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
```{r}
mod1 <- lm(gdp ~ corp * leftlab + demand + oild + trade, data = garrett1998)
```
Controlling for country
```{r}
mod2 <- lm(gdp ~ corp * leftlab + demand + oild + trade + countryname,
data = garrett1998)
```
Controlling for country and year
```{r}
mod3 <- lm(gdp ~ corp * leftlab + demand + oild + trade + countryname + factor(year),
data = garrett1998)
```
Lagged dependent variable
```{r}
mod_ldv <- lm(gdp ~ gdpl + corp * leftlab + demand + oild + trade + year,
data = garrett1998)
mod_ldv
```
Estimating panel-corrected standard errors after a lagged dependent variable model.
```{r}
pcse(mod_ldv, groupN = garrett1998$country, groupT = garrett1998$year)
```
# 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.
```{r}
pgarrett <- pdata.frame(garrett1998, index = c("countryname", "year"))
```
Within (fixed-effects) model
```{r}
mod_within <- plm(gdp ~ corp * leftlab + demand + oild + trade + year,
data = pgarrett, model = "within")
summary(mod_within)
```
Recover the fixed effects
```{r}
fixef(mod_within)
```
Random effects model
```{r}
mod_re <- plm(gdp ~ corp * leftlab + demand + oild + trade,
data = pgarrett, model = "random")
summary(mod_re)
```
Compare the random effects model with the fixed effects model using a Hausman test
```{r}
phtest(mod_within, mod_re)
```
If we can reject the null, then we should use the fixed effects estimator.