/
introduction.Rmd
389 lines (239 loc) · 12.6 KB
/
introduction.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
---
title: "getspanel: Getting Started"
output: rmarkdown::html_vignette
#output: prettydoc::html_pretty
# code_download: true
vignette: >
%\VignetteIndexEntry{getspanel: Getting Started}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = NA,
echo = TRUE,
message = FALSE,
error = TRUE,
eval = TRUE,
out.width = "100%",
fig.width = 7,
fig.height = 5,
dev = "png",
dpi = 300
)
```
The `getspanel` package can be downloaded and installed from CRAN [here](https://cran.r-project.org/package=getspanel) by simply using:
```{r, eval=FALSE}
install.packages("getspanel")
```
The source code of the package is on [GitHub](https://github.com/moritzpschwarz/getspanel) and the development version can be installed using:
# install.packages("devtools")
devtools::install_github("moritzpschwarz/getspanel", ref = "devel")
Once installed we need to load the library:
```{r setup}
library(getspanel)
library(fixest)
```
Currently the package is called **getspanel** to align with the **gets** package, but it's main function of course remains the **isatpanel** function.
The **isatpanel** function implements the empirical break detection algorithm that is described in a [paper by Felix Pretis and Moritz Schwarz](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4022745) and was applied to a study by Nico Koch and colleagues on EU Road CO~2~ emissions, which was [published in Nature Energy in 2022](https://www.nature.com/articles/s41560-022-01095-6).
**A quick overview over what has changed:**
- We can now use the function approach as well as the traditional gets approach. This means that we can specify a model using `y` and `mxreg` as well as `time` and `id` as vectors, but we can now also simply supply a `data.frame` and a `function` in the form `y ~ x + z + I(x^2)` to e.g. specify polynomials. This means we will then need an `index` argument, which specifies the
- The `ar` argument now works
- We can now use the `fixest` package to speed up model estimation with large `i` (for short panels, the default method is still faster).The package can be activated using the new `engine` argument.
- Using the `fixest` package also allows us to calculate **clustered standard errors**.
- We can now be certain that unbalanced panels would work as intended, which was not the case before.
- The `mxbreak` and `break.method` arguments have been removed. Instead the function now produces the break matrix itself. This now implements the following saturation methods in a user friendly way:
- **iis**: Impulse Indicator Saturation
- **jsis**: **Joint** Step Indicator Saturation (Common Breaks over time)
- **csis**: **Coefficient** Step Indicator Saturation (Common Coefficient Breaks over time)
- **fesis**: **Fixed Effect** Step Indicator Saturation (Breaks in the Group Fixed Effect over time)
- **cfesis**: **Coefficient Fixed Effect** Step Indicator Saturation (Breaks in the coefficient for each individual)
# The isatpanel function
We first load some data of EU CO2 Emissions in the housing sector.
```{r}
data("EUCO2residential")
head(EUCO2residential)
# let's subset this a little bit to speed this up
EUCO2residential <- EUCO2residential[EUCO2residential$year > 2000 &
EUCO2residential$country %in% c("Germany", "Austria",
"Belgium", "Italy",
"Sweden", "Denmark"),]
# let's create a log emissions per capita variable
EUCO2residential$lagg.directem_pc <- log(EUCO2residential$agg.directem/EUCO2residential$pop)
# and let's also turn off printing the intermediate output from isatpanel
options(print.searchoutput = FALSE)
```
Let's look at how we input what we want to model. Each `isatpanel` command takes:
## Basics
- A specification of the source data, the group and time variable and the group-time characteristics. This can be entered into the function in two ways:
i. In the **gets** package style i.e. using vectors and matrices to specify `y`, `mxreg`, `time` and `id`
ii. But also in a form that resembles the `lm` and `plm` specification i.e. inputting a `data.frame` (or `matrix` or `tibble`), a `formula` argument as well as character vectors for `index` (in the form `c("group_variable_name", "time_variable_name")`)
- A an argument for the Fixed Effect Specification using `effect`.
This already means that the following two commands will give the same result:
Using the new method
```{r}
is_lm <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE)
```
Using the traditional method
```{r}
is_gets <- isatpanel(y = EUCO2residential$lagg.directem_pc,
mxreg = EUCO2residential$lgdp,
time = EUCO2residential$year,
id = EUCO2residential$country,
effect = "twoways",
fesis = TRUE)
```
From here onwards, I will use the `lm` notation.
## Plotting
We can plot these simply using the default plotting methods (rely on the **ggplot2** package):
```{r}
plot(is_lm)
```
```{r}
plot_grid(is_lm)
```
```{r}
plot_counterfactual(is_lm)
```
<!-- ![](getspanel_plot.png) -->
## Saturation Methods
### Impulse Indicator Saturation
This argument works just as in the **gets** package. The method simply adds a `0` and `1` dummy for each observation.
Simply set `iis = TRUE`.
```{r}
iis_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
iis = TRUE,
fesis = TRUE)
```
```{r}
plot(iis_example)
```
### Step Indicator Saturation
Traditional Step Indicator Saturation does not make sense in a panel setting. Therefore, the **gets** function of `sis` is disabled.
### Joint Step Indicator Saturation
It is possible, however, to consider Step Indicator Saturation with common breaks across individuals. Such indicators would be collinear, if `effects = c("twoways")` or `effects = c("time")` i.e. if Time Fixed Effects are included.
If, however, `effect = "individual"` then we can use `jsis = TRUE` to select over all individual time fixed effects.
```{r}
jsis_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "individual",
jsis = TRUE)
```
### Coefficient Step Indicator Saturation
**Note:** This method has only been tested using the `lm` implementation (using `data`, `formula`, and `index`).
This method allows detection of coefficient breaks that are common across all groups. It is the interaction between `jsis` and the relevant coefficient.
To illustrate this, as well as the advantages of using the `lm` approach, we include a non-linear term of the lgdp variable using `I(lgdp^2)`:
```{r}
csis_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
t.pval = 0.05,
csis = TRUE)
```
By default, all coefficients will be interacted and added to the indicator list - but his can be controlled using the `csis_var`, which takes a character vector of column names i.e. `csis_var = "lgdp"`.
```{r}
csis_example2 <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
csis = TRUE,
csis_var = "lgdp")
```
### Fixed Effect Step Indicator Saturation
This is equivalent to supplying a constant to the mxbreak argument in the old method. This essentially breaks the group-specific intercept i.e. the individual fixed effect.
```{r}
fesis_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE)
```
```{r}
plot(fesis_example)
```
Similar to the `csis_var` idea, we can specify the `fesis` method for a subset of individuals as well using the `fesis_id` variable, which takes a character vector of individuals. In this case we can use e.g. `fesis_id = c("Austria","Denmark")`.
```{r}
fesis_example2 <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE,
fesis_id = c("Austria","Denmark"))
```
```{r}
plot(fesis_example2)
```
## Post-selection robustness
The options for the `robust_isatpanel` are to use HAC Standard Errors, use a standard White Standard Error Correction (with the option of clustering the S.E. within groups or time):
```{r}
robust_isatpanel(fesis_example, HAC = TRUE, robust = TRUE, cluster = "group")
```
### Coefficient Fixed Effect Step Indicator Saturation
This method combines the `csis` and the `fesis` approach and detects whether coefficients for individual units break over time.
This means we can also combine the subsetting in both the variable and in the individual units using `cfesis_id` and `cfesis_var`.
```{r}
cfesis_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
cfesis = TRUE,
cfesis_id = c("Belgium","Germany"),
cfesis_var = "lgdp",
t.pval = 0.001)
```
```{r}
plot(cfesis_example)
```
## The `ar` argument
It is now possible to specify an argument to include autoregressive coefficients, using the `ar` argument.
```{r}
fesis_ar1_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE,
ar = 1)
```
## The `engine` argument
Another new argument is also the `engine` argument. This allows us to use an external package to estimate our models. At this stage, the **fixest** package can be used.
This also means that we can now use an argument to cluster Standard Errors using `cluster`. The following few chunks are not executed by default in the vignette.
```{r,eval = FALSE}
fixest_example <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE,
engine = "fixest",
cluster = "none")
```
We can verify that, using no clustering of Standard Errors at all, using the **fixest** package does not change our estimates:
```{r, eval = FALSE}
head(fixest_example$isatpanel.result$mean.results)
```
Compared to the default estimator:
```{r, eval = FALSE}
head(is_lm$isatpanel.result$mean.results)
```
However, changing the `cluster` specification of course does. **The Standard Error correction with it's current implementation is not valid, so allows for many more indicators than true - clustering is therefore currently not recommended.**
```{r, eval = FALSE}
fixest_example_cluster <- isatpanel(data = EUCO2residential,
formula = lagg.directem_pc ~ lgdp + I(lgdp^2) + pop,
index = c("country","year"),
effect = "twoways",
fesis = TRUE,
engine = "fixest",
cluster = "individual")
```
```{r, eval = FALSE}
plot(fixest_example_cluster)
```