-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathweek7.qmd
2420 lines (1089 loc) · 60 KB
/
week7.qmd
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Applied microeconometrics"
subtitle: "Weeks 7 and 8 - Instrumental variables"
author: "Josh Merfeld"
institute: "KDI School"
date: "2024-11-04"
date-format: long
format:
revealjs:
self-contained: true
slide-number: false
progress: false
theme: [serif, custom.scss]
width: 1500
height: 1500*(9/16)
code-copy: true
code-fold: show
code-overflow: wrap
highlight-style: github
execute:
echo: true
warnings: false
message: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, dev = "png") # NOTE: switched to png instead of pdf to decrease size of the resulting pdf
def.chunk.hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- def.chunk.hook(x, options)
#ifelse(options$size != "a", paste0("\n \\", "tiny","\n\n", x, "\n\n \\normalsize"), x)
ifelse(options$size != "normalsize", paste0("\n \\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
library(tidyverse)
library(kableExtra)
library(fixest)
library(ggpubr)
library(RColorBrewer)
library(haven)
library(fwildclusterboot)
library(modelsummary)
library(terra)
library(tidyterra)
library(cowplot)
```
## What are we doing today?
- Introduction to IVs
- Requirements/assumptions
- IVs and RCTs
- In a world of LATE
- Weak instruments
## Instrumental variables
- Instrumental variables (IVs) are a way to estimate causal effects when we have endogeneity
- The endogeneity can take many forms: omitted variables, measurement error, simultaneity, etc.
- Consider my paper: effects of pollution on agricultural productivity
- What's the problem with simply regression productivity on pollution?
## Endogeneity in the pollution example
![](week7assets/pollution1.png){fig-align="center"}
## Endogeneity in the pollution example
![](week7assets/pollution2.png){fig-align="center"}
## Putting structure on this
- What we really want to estimate is this:
\begin{gather} \label{eq:iv1} productivity_{it} = \beta_0 + \beta_1 pollution_{it} + \epsilon_{it} \end{gather}
where $\beta_1$ is the causal effect of pollution on productivity.
- Endogeneity is defined as $cov(pollution_{it}, \epsilon_{it})\neq0$
- That is, the error term is correlated with the endogenous variable
- A common example is omitted variables
## Putting structure on this
\begin{gather} \tag{1} productivity_{it} = \beta_0^* + \beta_1^* pollution_{it} + \epsilon_{it}^* \end{gather}
- When we estimate this, due to the way OLS works, the residuals and pollution will be orthogonal
- That is, $cov(pollution_{it}, \epsilon_{it}^*)=0$
- This is a property of OLS
- However, the issue is that under endogeneity, $\beta^*_1\neq\beta_1$
- That is, the OLS estimate of $\beta_1$ is biased *for the true structural parameter*
## Putting structure on this
- Another way to think about it is that what we want to estimate is this:
\begin{gather} productivity_{it} = \beta_0 + \beta_1 pollution_{it} + \beta_2 X + \epsilon_{it} \end{gather}
- But if we don't properly control for everything -- in this case $X$ -- we are really estimating this:
\begin{gather} \label{eq:iv2} productivity_{it} = \tilde{\beta_0} + \tilde{\beta_1} pollution_{it} + \eta_{it}, \end{gather}
where $\eta_{it} = \beta_2 X_{it} + \epsilon_{it}$.
## Differences in differences?
- One solution is to use a differences-in-differences (DiD) approach
- This requires the assumption of parallel trends
- That is, the trends in the outcome variable would have been the same in the absence of the treatment
- But what if changing economic growth is leading to changes in both pollution and productivity?
- Then the parallel trends assumption is violated since areas with more pollution are also experiencing faster economic growth
## Control for growth?
- If you're willing to make assumptions about what the omitted variables are, maybe you could control for theme
- But this is a strong assumption
- No matter what we do, we'll have to make assumptions, though
## Enter: instruments
- Let's take a different approach
- We'll use an instrument
- A variable that is correlated with the endogenous variable (pollution) but is not correlated with the error term
## Instrument in the pollution example
![](week7assets/pollution3.png){fig-align="center"}
## Requirements of an instrument
- I very purposefully created the example so that the instrument is correlated with pollution
- But it's not *directly* correlated with productivity
- And it's not correlated with the omitted variable (the error term... will show you this in a second)
- Let's look at these more formally
## Back to our problem
\begin{gather} \tag{3} productivity_{it} = \tilde{\beta_0} + \tilde{\beta_1} pollution_{it} + \eta_{it} \end{gather}
- Can we estimate a version of this equation -- that is, without controlling for $X_{it}$ -- and still get causal effects?
- Maybe, if we can find a valid instrument.
- So what makes an instrument valid?
## What else can instruments help with?
- It turns out IVs can also help with measurement error
- If we have a variable that is measured with error, we can use an instrument to correct for this
- From Hansen, consider the model:
\begin{gather} X = Q + u, \end{gather}
where $X$ is the variable we observe, $Q$ is the variable we want to measure, and $u$ is measurement error.
- Assume that $cov(u, Q)=0$, so that the measurement error is *random*, i.e. uncorrelated with the true value of $Q$.
- This is known as classical measurement error
## Classical measurement error and attenuation bias
- We want to estimate:
\begin{gather} Y = \beta_0 + \beta_1 Q + \epsilon, \end{gather}
but what we really estimate is:
\begin{gather} Y = \tilde{\beta}_0 + \tilde{\beta}_1 X + \tilde{\epsilon} = \tilde{\beta}_0 + \tilde{\beta}_1 (Q + u) + \tilde{\epsilon} \end{gather}
## Classical measurement error and attenuation bias
- This is what we get:
\begin{gather} \tilde{\beta}_1 = \beta_1\left(1-\frac{\mathbb{E}(u^2)}{\mathbb{E}(X^2)}\right) \end{gather}
- By definition, $\mathbb{E}(X^2)>\mathbb{E}(u^2)$, so $\tilde{\beta}_1<\beta_1$.
- Why is this true?
- That is, the OLS estimate of $\beta_1$ is biased *towards zero*
- This is called attenutation bias, but is only guaranteed with the measurement error is classical (random)
## Requirements for an instrument
\begin{gather} \tag{3} productivity_{it} = \tilde{\beta_0} + \tilde{\beta_1} pollution_{it} + \eta_{it} \end{gather}
1. The instrument must be correlated with the endogenous variable (pollution)
2. The instrument must not be correlated with the error term ($\eta_{it}$)
- Note that this implies two things:
- The instrument must not be correlated with any omitted variable (here $X_{it}$)
- The instrument must not directly affect the outcome ($productivity_{it}$)
## Using an instrument
- If we can find a valid instrument, we can use it to estimate the causal effect of pollution on productivity
- The simplest example uses two stages:
1. $pollution_{it} = \pi_0 + \pi_1 instrument_{it} + \nu_{it}$
2. $productivity_{it} = \phi_0 + \phi_1 pollution_{it} + \zeta_{it}$
- We can then estimate $\phi_1$ using OLS
- Note that only under certain circumstances will $\phi_1=\beta_1$
- More on this later
## The intuition with venn diagrams
![](week7assets/iv1.png){fig-align="center"}
## The IV only affects productivity through pollution
![](week7assets/iv2.png){fig-align="center"}
## This doesn't work. Direct effects on productivity!
![](week7assets/iv3.png){fig-align="center"}
## This doesn't work. Correlated with growth!
![](week7assets/iv4.png){fig-align="center"}
## Back to our "two stages", redefining names
$$\text{Stage}\;1:\;T_{it} = \pi_0 + \pi_1 Z_{it} + \nu_{it}$$
$$\text{Stage}\;2:\;Y_{it} = \phi_0 + \phi_1 T_{it} + \zeta_{it}$$
- Requirements:
- $cov(Z_{it}, T_{it}) \neq 0$
- $cov(Z_{it}, \zeta_{it}) = 0$
- We first regress T on the instrument to get $\hat{T}_{it}$
- Then, we use the predicted values of T to estimate the effects on Y
- If the IV is valid, these predicted values *are unrelated to the omitted variables!*
## Some comments
$$\text{Stage}\;1:\;T_{it} = \pi_0 + \pi_1 Z_{it} + \nu_{it}$$
\begin{gather}cov(Z_{it}, T_{it}) \neq 0\end{gather}
- This is the first requirement
- We can test this!
- F-test of all *excluded instruments* in the first stages
- I say all excluded instruments because you can technically have more than one
## Some comments
$$\text{Stage}\;1:\;T_{it} = \pi_0 + \pi_1 Z_{it} + \nu_{it}$$
$$\text{Stage}\;2:\;Y_{it} = \phi_0 + \phi_1 T_{it} + \zeta_{it}$$
\begin{gather}cov(Z_{it}, \zeta_{it}) = 0\end{gather}
- This is the second requirement
- We cannot explicitly test this
- This is an identifying *assumption*
- We need this to be true to attribute causality to the second stage
## Some comments
$$\text{Stage}\;1:\;T_{it} = \pi_0 + \pi_1 Z_{it} + \nu_{it}$$
$$\text{Stage}\;2:\;Y_{it} = \phi_0 + \phi_1 T_{it} + \zeta_{it}$$
\begin{gather}cov(Z_{it}, \zeta_{it}) = 0\end{gather}
- Note that we will use $Z_{it}$ to predict $T_{it}$.
- We cannot actually observe $cov(Z_{it}, \zeta_{it})$
- So if $cov(Z_{it}, \zeta_{it})\neq0$...
- Then this correlation will be contained in the predicted values, $\hat{T}_{it}$
- i.e. the predicted values will still be endogenous
## IVs in supply and demand
- Economists have long been interested in supply and demand
- Obviously...
- How does a change in supply affect prices?
- Not a straightforward question to answer, because prices are determined jointly by supply and demand
- We can't determine what is changing when we observe market prices
- One option: an instrument that moves only one side of the market
- Small note: this is how IVs originally came about in economics
## Favara and Imbs, 2015 (*American Economic Review*)
- How does the availability of credit affect house prices?
- They use a change in deregulation of banks in the US
- This deregulation led to an increase in credit supply
- But it did not affect credit demand, since it was a supply-side change
- Idea: show the change in credit availability for banks affected by the change
- And no change for banks not affected by the change
## Deregulation index across states and years
![](week7assets/deregulation1.png){fig-align="center"}
## Two stages: predict credit supply, then predict house prices
\begin{align} &\text{Stage 1: } credit_{ct} = \delta_0 + \delta_1 deregulation_{ct} + \delta_2 X_{ct} + \alpha_c + \gamma_t + \nu_{ct} \\
&\text{Stage 2: } price_{ct} = \beta_0 + \beta_1 credit_{ct} + \beta_2 X_{ct} + \phi_c + \eta_t + \zeta_{ct} \end{align}
- They instrument for $credit$ using $deregulation$
- $deregulation$ is correlated with $credit$ but not with $\zeta_{ct}$, according to the authors
- (Let's ignore whether this is true for now since it's so contextual)
- They control for $X_{ct}$, which is a vector of controls
- This is also a two-way fixed effects specification:
- $\alpha_c$ and $\gamma_t$ ($\phi_c$ and $\eta_t$ in stage 2) are county and year fixed effects
## Replication data: `week7files/hmda_merged.dta`
```{r rep1, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
library(haven)
df <- read_dta("week7files/hmda_merged.dta")
head(df)
# key controls: LDl_hpi Dl_inc LDl_inc Dl_pop LDl_pop Dl_her_v LDl_her_v
# instrument: Linter_bra
# endogenous variables: Dl_nloans_b Dl_vloans_b Dl_lir_b
# weights: w1
# restriction: border counties only (border==1)
# county and year FE
# cluster on state
```
## Reduced form
- It is common to estimate the reduced form of the first stage
- This is a regression of the outcome of interest on the instrument
- In this case, this equals
\begin{gather} price_{ct} = B_0 + B_1 deregulation_{ct} + B_2 X_{ct} + \cdots \end{gather}
## Reduced form
```{r rep1b, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
bordercounties <- df |> filter(border==1)
summary(feols(Dl_hpi ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n"))
```
## First stage
```{r rep2, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
bordercounties <- df |> filter(border==1)
reg1 <- feols(Dl_nloans_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg2 <- feols(Dl_vloans_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg3 <- feols(Dl_lir_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
```
## First stage
```{r rep3, echo = FALSE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
bordercounties <- df |> filter(border==1)
reg1 <- feols(Dl_nloans_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg2 <- feols(Dl_vloans_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg3 <- feols(Dl_lir_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
table <- etable(reg1, reg2, reg3,
digits = 3, fitstat = c("n"), se.below = TRUE, depvar = FALSE,
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
colnames(table) <- c("", "Loans", "Loan volume", "Loan-to-inc. ratio")
table[c(1,3,5,7,9,11,13,15),1] <- c("IV", "House price (lag)", "Inc. p.c.", "Inc. p.c. (lag)", "Population", "Population (lag)", "Herf. index", "Herf. index (lag)")
table <- table[-c(17:21),]
tabletemp <- etable(reg1, reg2, reg3,
digits = 10, fitstat = c("n"), se.below = TRUE, depvar = FALSE,
coefstat = "tstat",
# change significance codes to the norm
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1))
# extract t statistics for first coeffcicient only
tstats <- as_vector(tabletemp[2,2:4])
# remove parentheses
tstats <- gsub("\\(", "", tstats)
tstats <- gsub("\\)", "", tstats)
tstats <- as.numeric(tstats)
# square for F-test
tstats <- tstats^2
# round to three digits
tstats <- round(tstats, 3)
# add to bottom of table
table[18,] <- c("F-test for instrument", tstats)
kable(table,
align = "lccc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) |>
row_spec(16, hline_after = TRUE) |>
column_spec(1,width = "7cm") |>
column_spec(c(2:4),width = "3.5cm") |>
kable_styling() |>
footnote("Note: F-test differs from results in paper due to differences in how xtreg calculates standard errors.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>
footnote("Standard errors clustered on state in parentheses.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>
kable_styling(font_size = 18)
```
## First stage predictions vs. actual values... what do you notice?
```{r rep4, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
bordercounties <- df |> filter(border==1)
reg1 <- feols(Dl_nloans_b ~ Linter_bra + LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v | county + year,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
# predict out of first stage
bordercounties$credit <- NA
bordercounties$credit[reg1$obs_selection$obsRemoved] <- predict(reg1)
bordercounties$credit2 <- NA
bordercounties$credit2[reg2$obs_selection$obsRemoved] <- predict(reg2)
bordercounties$credit3 <- NA
bordercounties$credit3[reg3$obs_selection$obsRemoved] <- predict(reg3)
# create plot with predicted vs. actual
ggplot(bordercounties, aes(x = Dl_nloans_b, y = credit)) +
geom_point(alpha = 0.5) +
labs(x = "Actual change", y = "Predicted change") +
# same limits on both axes
coord_cartesian(xlim = c(min(bordercounties$Dl_nloans_b, na.rm = T), max(bordercounties$Dl_nloans_b, na.rm = T)),
ylim = c(min(bordercounties$Dl_nloans_b, na.rm = T), max(bordercounties$Dl_nloans_b, na.rm = T))) +
theme_minimal()
```
## First stage predictions vs. actual values... what do you notice?
```{r rep5, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE}
sums <- bordercounties |>
dplyr::select(Dl_nloans_b, credit) |>
na.omit() |>
summarize(across(everything(), list(min = min, max = max, sd = sd)))
sums <- rbind(sums, sums)
sums[2,1:3] <- sums[2,4:6]
sums <- sums[,1:3]
sums <- cbind(sums[,1], sums)
sums[,1] <- c("Actual", "Predicted")
colnames(sums) <- c("", "min", "max", "SD")
sums[,2:4] <- round(sums[,2:4], 3)
kable(sums,
align = "lccc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE
) |>
kable_styling() |>
kable_styling(font_size = 18)
```
- Note how much less variance there is in the predicted values than the actual values
- This is the point of using an instrument!
- We are able to isolate the variation in the endogenous variable that is not correlated with the error term
- This is of course only a subset of the total variation in the endogenous variable
- This will be important later
## We cannot simply use the predicted values in the second stage... standard errors will be wrong!
```{r rep6, echo = TRUE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# create a macro for the main regression controls (to avoid repetition and save space)
setFixest_fml(..controls = ~ LDl_hpi + Dl_inc + LDl_inc + Dl_pop + LDl_pop + Dl_her_v + LDl_her_v)
# Let's use feols to estimate the two stages
reg1 <- feols(Dl_hpi ~ ..controls | county + year | Dl_nloans_b ~ Linter_bra,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg2 <- feols(Dl_hpi ~ ..controls | county + year | Dl_vloans_b ~ Linter_bra,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
reg3 <- feols(Dl_hpi ~ ..controls | county + year | Dl_lir_b ~ Linter_bra,
data = bordercounties, weights = bordercounties$w1,
cluster = "state_n")
```
## `fixest` will give us the correct standard errors, however (first stage)
```{r rep7, echo = TRUE, eval = FALSE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# first stage:
etable(
reg1, reg2, reg3,
stage = 1,
se.below = TRUE,
depvar = FALSE,
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
digits = "r3",
digits.stats = "r0",
fitstat = c("ivwald", "n"), # make sure to use ivwald for first-stage F-test
coefstat = "se",
group = list(controls = "LDl_hpi"),
keep = "Linter_bra"
)
```
## `fixest` will give us the correct standard errors, however (first stage)
```{r rep8, echo = FALSE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# first stage:
table <- etable(
reg1, reg2, reg3,
stage = 1,
se.below = TRUE,
depvar = FALSE,
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
digits = "r3",
digits.stats = "r3",
fitstat = c("ivwald", "n"), # make sure to use ivwald for first-stage F-test
coefstat = "se",
group = list(controls = "LDl_hpi"),
keep = "Linter_bra"
)
table[1,1] <- "IV (deregulation index)"
colnames(table) <- c("", "Loans", "Loan volume", "Loan-to-inc. ratio")
table[4,2:4] <- ""
table <- table[-c(7:8),]
kable(table,
align = "lccc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) |>
row_spec(6, hline_after = TRUE) |>
column_spec(1,width = "3cm") |>
column_spec(c(2:4),width = "2.5cm") |>
kable_styling() |>
footnote("Note: The Wald (similar to F-test) values do not equal the values in the paper due to differences in how xtreg calculates standard errors.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>
footnote("Standard errors clustered on state in parentheses.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>
kable_styling(font_size = 18)
```
## `fixest` will give us the correct standard errors, however (second stage)
```{r rep9, echo = TRUE, eval = FALSE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# second stage:
etable(
reg1, reg2, reg3,
stage = 2,
se.below = TRUE,
depvar = FALSE,
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
digits = "r3",
digits.stats = "r3",
fitstat = c("ivwald", "n"), # make sure to use ivwald for first-stage F-test
coefstat = "se",
group = list(controls = "LDl_hpi"),
keep = c("Dl_nloans_b", "Dl_vloans_b", "Dl_lir_b")
)
```
## `fixest` will give us the correct standard errors, however (second stage)
```{r rep10, echo = FALSE, eval = TRUE, message = FALSE, warning = TRUE, size = "tiny", out.width = "55%", fig.align = "center"}
# first stage:
table <- etable(
reg1, reg2, reg3,
stage = 2,
se.below = TRUE,
depvar = FALSE,
signif.code = c("***" = 0.01, "**" = 0.05, "*" = 0.1),
digits = "r3",
digits.stats = "r3",
fitstat = c("ivwald", "n"),
coefstat = "se",
group = list(controls = "LDl_hpi"),
keep = c("Dl_nloans_b", "Dl_vloans_b", "Dl_lir_b")
)
table[c(1, 3, 5),1] <- c("Loans", "Loan volume", "Loan-to-inc. ratio")
colnames(table) <- c("", "(1)", "(2)", "(3)")
table[8,2:4] <- ""
table <- table[-c(11:12),]
table[11,3] <- table[12,3]
table[11,4] <- table[13,4]
table <- table[-c(12:13),]
table[11,1] <- "Wald (1st stage)"
kable(table,
align = "lccc", booktabs = TRUE, linesep = "", escape = FALSE, row.names = FALSE) |>
row_spec(10, hline_after = TRUE) |>
column_spec(1,width = "3cm") |>
column_spec(c(2:4),width = "2.5cm") |>
kable_styling() |>
footnote("Note: The Wald (similar to F-test) values do not equal the values in the paper due to differences in how xtreg calculates standard errors.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>
footnote("Standard errors clustered on state in parentheses.", general_title = "",
footnote_as_chunk = TRUE,
escape = FALSE
) |>