-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path05-heteroskedasticity.Rmd
1267 lines (966 loc) · 36 KB
/
05-heteroskedasticity.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
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: "Heteroskedasticity, Part 2"
subtitle: "EC 421, Set 5"
author: "Edward Rubin"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
xaringan::moon_reader:
css: ['default', 'metropolis', 'metropolis-fonts', 'my-css.css']
# self_contained: true
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
class: inverse, middle
```{r setup, include = F}
options(htmltools.dir.version = FALSE)
library(pacman)
p_load(
broom,
latex2exp, ggplot2, ggthemes, viridis, extrafont,
kableExtra,
dplyr, magrittr, knitr, parallel
)
# Define pink color
red_pink <- "#e64173"
grey_light <- "grey70"
grey_mid <- "grey50"
grey_dark <- "grey20"
# Dark slate grey: #314f4f
# Notes directory
dir_slides <- "~/Dropbox/UO/Teaching/EC421W19/LectureNotes/05Heteroskedasticity/"
# Knitr options
opts_chunk$set(
comment = "#>",
fig.align = "center",
fig.height = 7,
fig.width = 10.5,
warning = F,
message = F
)
opts_chunk$set(dev = "svg")
options(device = function(file, width, height) {
svg(tempfile(), width = width, height = height)
})
# A blank theme for ggplot
theme_empty <- theme_bw() + theme(
line = element_blank(),
rect = element_blank(),
strip.text = element_blank(),
axis.text = element_blank(),
plot.title = element_blank(),
axis.title = element_blank(),
plot.margin = structure(c(0, 0, -0.5, -1), unit = "lines", valid.unit = 3L, class = "unit"),
legend.position = "none"
)
theme_simple <- theme_bw() + theme(
line = element_blank(),
panel.grid = element_blank(),
rect = element_blank(),
strip.text = element_blank(),
axis.text.x = element_text(size = 18, family = "STIXGeneral"),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_blank(),
axis.title = element_blank(),
# plot.margin = structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit"),
legend.position = "none"
)
theme_axes_math <- theme_void() + theme(
text = element_text(family = "MathJax_Math"),
axis.title = element_text(size = 22),
axis.title.x = element_text(hjust = .95, margin = margin(0.15, 0, 0, 0, unit = "lines")),
axis.title.y = element_text(vjust = .95, margin = margin(0, 0.15, 0, 0, unit = "lines")),
axis.line = element_line(
color = "grey70",
size = 0.25,
arrow = arrow(angle = 30, length = unit(0.15, "inches")
)),
plot.margin = structure(c(1, 0, 1, 0), unit = "lines", valid.unit = 3L, class = "unit"),
legend.position = "none"
)
theme_axes_serif <- theme_void() + theme(
text = element_text(family = "MathJax_Main"),
axis.title = element_text(size = 22),
axis.title.x = element_text(hjust = .95, margin = margin(0.15, 0, 0, 0, unit = "lines")),
axis.title.y = element_text(vjust = .95, margin = margin(0, 0.15, 0, 0, unit = "lines")),
axis.line = element_line(
color = "grey70",
size = 0.25,
arrow = arrow(angle = 30, length = unit(0.15, "inches")
)),
plot.margin = structure(c(1, 0, 1, 0), unit = "lines", valid.unit = 3L, class = "unit"),
legend.position = "none"
)
theme_axes <- theme_void() + theme(
text = element_text(family = "Fira Sans Book"),
axis.title = element_text(size = 18),
axis.title.x = element_text(hjust = .95, margin = margin(0.15, 0, 0, 0, unit = "lines")),
axis.title.y = element_text(vjust = .95, margin = margin(0, 0.15, 0, 0, unit = "lines")),
axis.line = element_line(
color = grey_light,
size = 0.25,
arrow = arrow(angle = 30, length = unit(0.15, "inches")
)),
plot.margin = structure(c(1, 0, 1, 0), unit = "lines", valid.unit = 3L, class = "unit"),
legend.position = "none"
)
```
# Prologue
---
# Schedule
## Last Time
Heteroskedasticity: Issues and tests
## Today
- First assignment was due last night.
- Living with heteroskedasticity
## Upcoming
- Second assignment once we finish this lecture
- Midterm <2 weeks
---
# EC 421
## Goals
- Develop .hi[intuition] for econometrics.
- Learn how to .hi[*apply*] econometrics—strengths, weaknessed, *etc.*
- Learn .hi[.mono[R]].
--
.mono[R] does the calculations and has already memorized the formulas.
I want you to know what the formulas mean, when/why we use them, and when they fail/work.
--
This course has the potential to be one of the most useful/valuable/applicable/marketable classes that you take at UO.
---
layout: true
# Heteroskedasticity
## Review
---
class: inverse, middle
---
Three review questions
**Question 1:** What is the difference between $u_i$ and $e_i$?
**Question 2:** We spend *a lot* of time discussing $u_i^2$. Why?
**Question 3:** We also spend *a lot* of time discussing $e_i^2$. Why?
---
**Question 1:** What is the difference between $u_i$ and $e_i$?
**Answer 1:**
--
<br> $\color{#e64173}{u_i}$ gives the .hi[population disturbance] for the $i$.super[th] observation.
--
$u_i$ measures how far the $i$.super[th] observation is from the .hi[population] line, _i.e._,
$$ u\_i = y\_i - \color{#e64173}{\underbrace{\left(\beta\_0 + \beta\_1 x\_i\right)}\_{\text{Population line}}} $$
--
$\color{#6A5ACD}{e_i}$ gives the .hi-purple[regression residual (error)] for the $i$.super[th] observation.
--
$e_i$ measures how far the $i$.super[th] observation is from the .hi-purple[sample regression] line, _i.e._,
$$ e\_i = y\_i - \color{#6A5ACD}{\underbrace{\left(\hat{\beta}\_0 + \hat{\beta}\_1 x\_i\right)}\_{\text{Sample reg. line} = \hat{y}}} = y\_i - \color{#6A5ACD}{\hat{y}_i} $$
---
**Question 2:** We spend *a lot* of time discussing $u_i^2$. Why?
**Answer 2:**
One of major assumptions is that our disturbances (the $u_i$'s) are homoskedastic (they have constant variance), _i.e._, $\mathop{\text{Var}} \left( u_i \middle| x_i \right) = \sigma^2$.
We also assume that the mean of these disturbances is zero, $\color{#e64173}{\mathop{\boldsymbol{E}}\left[ u_i \middle| x_i \right] = 0}$.
By definition, $\mathop{\text{Var}} \left( u_i \middle| x_i \right) = \mathop{\boldsymbol{E}}\Big[ u_i^2 - \underbrace{\color{#e64173}{\mathop{\boldsymbol{E}}\left[ u_i \middle| x_i \right]}^2}_{\color{#e64173}{=0}} \Big| x_i \Big] = \mathop{\boldsymbol{E}}\left[ u_i^2 \middle| x_i \right]$
Thus, if we want to learn about the variance of $u_i$, we can focus on $u_i^2$.
---
**Question 3:** We also spend *a lot* of time discussing $e_i^2$. Why?
**Answer 3:**
We cannot observe $u_i$ (or $u_i^2$).
But $u_i^2$ tells us about the variance of $u_i$.
We use $e_i^2$ to learn about $u_i^2$ and, consequently, $\sigma_i^2$.
---
layout: false
# Heteroskedasticity
## Review: Current assumptions
--
1. Our sample (the $x_k$'s and $y_i$) was .hi[randomly drawn] from the population.
--
2. $y$ is a .hi[linear function] of the $\beta_k$'s and $u_i$.
--
3. There is no perfect .hi[multicollinearity] in our sample.
--
4. The explanatory variables are .hi[exogenous]: $\mathop{\boldsymbol{E}}\left[ u \middle| X \right] = 0 \left(\implies \mathop{\boldsymbol{E}}\left[ u \right] = 0\right)$.
--
5. The disurbances have .hi[constant variance] $\sigma^2$ and .hi[zero covariance], _i.e._,
- $\mathop{\boldsymbol{E}}\left[ u_i^2 \middle| X_i \right] = \mathop{\text{Var}} \left( u_i \middle| X_i \right) = \sigma^2 \implies \mathop{\text{Var}} \left( u_i \right) = \sigma^2$
- $\mathop{\text{Cov}} \left( u_i, \, u_j \middle| X_i,\,X_j \right) = \mathop{\boldsymbol{E}}\left[ u_i u_j \middle| X_i,\,X_j \right] = 0$ for $i\neq j$
--
6. The disturbances come from a .hi[Normal] distribution, _i.e._, $u_i \overset{\text{iid}}{\sim} \mathop{\text{N}}\left( 0, \sigma^2 \right)$.
---
layout: true
# Heteroskedasticity
## Review
---
Today we're focusing on assumption \#5:
> 5\. The disurbances have .hi[constant variance] $\sigma^2$ and .hi[zero covariance], _i.e._,
> - $\mathop{\boldsymbol{E}}\left[ u_i^2 \middle| X_i \right] = \mathop{\text{Var}} \left( u_i \middle| X \right) = \sigma^2 \implies \mathop{\text{Var}} \left( u_i \right) = \sigma^2$
> - $\mathop{\text{Cov}} \left( u_i, \, u_j \middle| X_i,\,X_j \right) = \mathop{\boldsymbol{E}}\left[ u_i u_j \middle| X_i,\,X_j \right] = 0$ for $i\neq j$
--
Specifically, we will focus on the assumption of .hi[constant variance] (also known as *homoskedasticity*).
--
**Violation of this assumption:** Our disturbances have different variances.
--
.hi[Heteroskedasticity:] $\mathop{\text{Var}} \left( u_i \right) = \sigma^2_i$ and $\sigma^2_i \neq \sigma^2_j$ for some $i\neq j$.
---
Classic example of heteroskedasticity: The funnel
Variance of $u$ increases with $x$
```{R, het ex1, dev = "svg", echo = F, fig.height = 4.5}
set.seed(12345)
ggplot(data = tibble(
x = runif(1e3, -3, 3),
e = rnorm(1e3, 0, sd = 4 + 1.5 * x)
), aes(x = x, y = e)) +
geom_point(color = "darkslategrey", size = 2.75, alpha = 0.5) +
labs(x = "x", y = "u") +
theme_axes_math
```
---
Another example of heteroskedasticity: (double funnel?)
Variance of $u$ increasing at the extremes of $x$
```{R, het ex2 , dev = "svg", echo = F, fig.height = 4.5}
set.seed(12345)
ggplot(data = tibble(
x = runif(1e3, -3, 3),
e = rnorm(1e3, 0, sd = 2 + x^2)
), aes(x = x, y = e)) +
geom_point(color = "darkslategrey", size = 2.75, alpha = 0.5) +
labs(x = "x", y = "u") +
theme_axes_math
```
---
Another example of heteroskedasticity:
Differing variances of $u$ by group
```{R, het ex3 , dev = "svg", echo = F, fig.height = 4.5}
set.seed(12345)
ggplot(data = tibble(
g = sample(c(F,T), 1e3, replace = T),
x = runif(1e3, -3, 3),
e = rnorm(1e3, 0, sd = 0.5 + 2 * g)
), aes(x = x, y = e, color = g, shape = g, alpha = g)) +
geom_point(size = 2.75) +
scale_color_manual(values = c("darkslategrey", red_pink)) +
scale_shape_manual(values = c(16, 1)) +
scale_alpha_manual(values = c(0.5, 0.8)) +
labs(x = "x", y = "u") +
theme_axes_math
```
---
.hi[Heteroskedasticity] is present when the variance of $u$ changes with any combination of our explanatory variables $x_1$ through $x_k$.
---
layout: false
# Testing for heteroskedasticity
We have some tests that may help us detect heteroskedasticity.
- Goldfeld-Quandt
- Breusch-Pagan
- White
--
What do we do if we detect it?
---
layout: true
# Living with heteroskedasticity
---
class: inverse, middle, true
---
In the presence of heteroskedasticity, OLS is
- still .hi[unbiased]
- .hi[no longer the most efficient] unbiased linear estimator
On average, we get the right answer but with more noise (less precision).
<br> *Also:* Our standard errors are biased.
--
**Options:**
1. Check regression .hi[specification].
2. Find a new, more efficient .hi[unbiased estimator] for $\beta_j$'s.
3. Live with OLS's inefficiency; find a .hi[new variance estimator].
- Standard errors
- Confidence intervals
- Hypothesis tests
---
layout: true
# Living with heteroskedasticity
## Misspecification
---
As we've discussed, the specification.pink[<sup>†</sup>] of your regression model matters a lot for the unbiasedness and efficiency of your estimator.
**Response \#1:** Ensure your specification doesn't cause heteroskedasticity.
.footnote[.pink[†] *Specification:* Functional form and included variables.]
---
*Example:* Let the population relationship be $$ y_i = \beta_0 + \beta_1 x_i + \beta_2 x_i^2 + u_i $$
with $\mathop{\boldsymbol{E}}\left[ u_i \middle| x_i \right] = 0$ and $\mathop{\text{Var}} \left( u_i \middle| x_i \right) = \sigma^2$.
However, we omit $x^2$ and estimate $$ y_i = \gamma_0 + \gamma_1 x_i + w_i $$
Then $$ w_i = u_i + \beta_2 x_i^2 \implies \mathop{\text{Var}} \left( w_i \right) = f(x_i) $$
_I.e._, the variance of $w_i$ changes systematically with $x_i$ (heteroskedasticity).
---
```{R, spec data, include = F}
# Set the seed
set.seed(1234)
# Generate data
spec_df <- tibble(x = runif(1e3, 0, 3), y = exp(0.5 + 0.6 * x + rnorm(1e3, sd = 0.3)))
# Add residuals ('w': wrong specification; 'c': correct specification)
spec_df %<>% mutate(
e_w = lm(y ~ x, data = spec_df) %>% residuals(),
e_c = lm(log(y) ~ x, data = spec_df) %>% residuals()
)
```
.pink[Truth:] $\color{#e64173}{\log\left(y_i\right) = \beta_0 + \beta_1 x_i + u_i}$ .slate[**Misspecification:**] $\color{#314f4f}{y_i = \beta_0 + \beta_1 x_i + v_i}$
```{R, spec plot1, echo = F, dev = "svg", fig.height = 5}
ggplot(data = spec_df, aes(x = x)) +
geom_point(aes(y = e_w), color = "darkslategrey", size = 2.75, alpha = 0.5, shape = 16) +
geom_point(aes(y = e_c), color = red_pink, size = 2.5, alpha = 0, shape = 19) +
labs(x = "x", y = "e") +
theme_axes_math
```
---
.pink[**Truth:**] $\color{#e64173}{\log\left(y_i\right) = \beta_0 + \beta_1 x_i + u_i}$ .slate[Misspecification:] $\color{#314f4f}{y_i = \beta_0 + \beta_1 x_i + v_i}$
```{R, spec plot2, echo = F, dev = "svg", fig.height = 5}
ggplot(data = spec_df, aes(x = x)) +
geom_point(aes(y = e_w), color = "darkslategrey", size = 2.75, alpha = 0.25, shape = 1) +
geom_point(aes(y = e_c), color = red_pink, size = 2.5, alpha = 0.5, shape = 19) +
labs(x = "x", y = "e") +
theme_axes_math
```
---
More generally:
**Misspecification problem:** Incorrect specification of the regression model can cause heteroskedasticity (among other problems).
--
**Solution:** 💡 Get it right (_e.g._, don't omit $x^2$).
--
**New problems:**
- We often don't know the *right* specification.
- We'd like a more formal process for addressing heteroskedasticity.
--
**Conclusion:** Specification often will not "solve" heteroskedasticity.
<br> However, correctly specifying your model is still really important.
---
layout: true
# Living with heteroskedasticity
## Weighted least squares
---
Weighted least squares (WLS) presents another approach.
**Response \#2:** Increase efficiency by weighting our observations.
--
Let the true population relationship be
$$
\begin{align}
y_i = \beta_0 + \beta_1 x_{i} + u_i \tag{1}
\end{align}
$$
with $u_i \sim \mathop{N} \left( 0,\, \sigma_i^2 \right)$.
--
Now transform $(1)$ by dividing each observation's data by $\sigma_i$, _i.e._,
$$
\begin{align}
\dfrac{y_i}{\sigma_i} &= \beta_0 \dfrac{1}{\sigma_i} + \beta_1 \dfrac{x_{i}}{\sigma_i} + \dfrac{u_i}{\sigma_i} \tag{2}
\end{align}
$$
---
$$
\begin{align}
y_i &= \beta_0 + \beta_1 x_{i} + u_i \tag{1} \\[1em]
\dfrac{y_i}{\sigma_i} &= \beta_0 \dfrac{1}{\sigma_i} + \beta_1 \dfrac{x_{i}}{\sigma_i} + \dfrac{u_i}{\sigma_i} \tag{2}
\end{align}
$$
Whereas $(1)$ is heteroskedastic,
--
$\color{#e64173}{(2)}$ .hi[is homoskedastic].
∴ OLS is efficient and unbiased for estimating the $\beta_k$ in $(2)$!
--
Why is $(2)$ homoskedastic?
--
$\mathop{\text{Var}} \left( \dfrac{u_i}{\sigma_i} \middle| x_i \right) =$
--
$\dfrac{1}{\sigma_i^2} \mathop{\text{Var}} \left( u_i \middle| x_i \right) =$
--
$\dfrac{1}{\sigma_i^2} \sigma_i^2 =$
--
$1$
---
WLS is great, but we need to know $\sigma_i^2$, which is generally unlikely.
We can *slightly* relax this requirement—instead requiring
1. $\mathop{\text{Var}} \left( u_i | x_i \right) = \sigma_i^2 = \sigma^2 h(x_i)$
2. We know $h(x)$.
--
As before, we transform our heteroskedastic model into a homoskedastic model. This time we divide each observation's data<sup>.pink[†]</sup> by $\sqrt{h(x_i)}$.
.footnote[
.pink[†] Divide *all* of the data by $\sqrt{h(x_i)}$, including the intercept.
]
---
$$
\begin{align}
y_i &= \beta_0 + \beta_1 x_{i} + u_i \tag{1} \\[1em]
\dfrac{y_i}{\sqrt{h(x_i)}} &= \beta_0 \dfrac{1}{\sqrt{h(x_i)}} + \beta_1 \dfrac{x_{i}}{\sqrt{h(x_i)}} + \dfrac{u_i}{\sqrt{h(x_i)}} \tag{2}
\end{align}
$$
with $\mathop{\text{Var}} \left( u_i | x_i \right) = \sigma^2 h(x_i)$.
--
Now let's check that $(2)$ is indeed homoskedastic.
$\mathop{\text{Var}} \left( \dfrac{u_i}{\sqrt{h(x_i)}} \middle| x_i \right) =$
--
$\dfrac{1}{h(x_i)} \mathop{\text{Var}} \left( u_i \middle| x_i \right) =$
--
$\dfrac{1}{h(x_i)} \sigma^2 h(x_i) =$
--
$\color{#e64173}{\sigma^2}$
.hi[Homoskedasticity!]
---
.hi[Weighted least squares] (WLS) estimators are a special class of .hi[generalized least squares] (GLS) estimators focused on heteroskedasticity.
--
$$
y\_i = \beta\_0 + \beta\_1 x\_{1i} + u\_i \quad \color{#e64173}{\text{ vs. }} \quad
\dfrac{y\_i}{\sigma\_i} = \beta\_0 \dfrac{1}{\sigma\_i} + \beta\_1 \dfrac{x\_{1i}}{\sigma\_i} + \dfrac{u\_i}{\sigma\_i}
$$
*Notes:*
1. WLS **transforms** a heteroskedastic model into a homoskedastic model.
2. **Weighting:** WLS downweights observations with higher variance $u_i$'s.
3. **Big requirement:** WLS requires that we *know* $\sigma_i^2$ for each observation.
4. WLS is generally **infeasible**. *Feasible* GLS (FGLS) offers a solution.
5. Under its assumptions: WLS is the **best linear unbiased estimator**.
---
layout: true
# Living with heteroskedasticity
## Heteroskedasticity-robust standard errors
---
**Response \#3:**
- Ignore OLS's inefficiency (in the presence of heteroskedasticity).
- Focus on .hi[unbiased estimates for our standard errors].
- In the process: Correct inference.
--
**Q:** What is a standard error?
--
<br>**A:** The .hi[standard deviation of an estimator's distribution].
--
Estimators (like $\hat{\beta}_1$) are random variables, so they have distributions.
Standard errors give us a sense of how much variability is in our estimator.
---
*Recall*: We can write the OLS estimator for $\beta_1$ as
$$ \hat{\beta}\_1 = \beta\_1 + \dfrac{\sum_i \left( x\_i - \overline{x} \right) u\_i}{\sum\_i \left( x\_i - \overline{x} \right)^2} = \beta\_1 + \dfrac{\sum_i \left( x\_i - \overline{x} \right) u\_i}{\text{SST}\_x} \tag{3} $$
--
Let $\mathop{\text{Var}} \left( u_i \middle| x_i \right) = \sigma_i^2$.
--
We can use $(3)$ to write the variance of $\hat{\beta}_1$, _i.e._,
$$ \mathop{\text{Var}} \left( \hat{\beta}_1 \middle| x_i \right) = \dfrac{\sum_i \left( x_i - \overline{x} \right)^2 \sigma_i^2}{\text{SST}_x^2} \tag{4} $$
---
If we want unbiased estimates for our standard errors, we need an unbiased estimate for
$$ \dfrac{\sum_i \left( x_i - \overline{x} \right)^2 \sigma_i^2}{\text{SST}_x^2} $$
Our old friend Hal White provided such an estimator:.pink[<sup>†</sup>]
$$ \widehat{\mathop{\text{Var}}} \left( \hat{\beta}_1 \right) = \dfrac{\sum_i \left( x_i - \overline{x} \right)^2 e_i^2}{\text{SST}_x^2} $$
where the $e_i$ comes from the OLS regression of interest.
.footnote[
.pink[†] This specific equation is for simple linear regression.
]
---
Our heteroskedasticity-robust estimators for the standard error of $\beta_j$.
.hi[Case 1] Simple linear regression, $y_i = \beta_0 + \beta_1 x_i + u_i$
$$ \widehat{\mathop{\text{Var}}} \left( \hat{\beta}_1 \right) = \dfrac{\sum_i \left( x_i - \overline{x} \right)^2 e_i^2}{\text{SST}_x^2} $$
.hi[Case 2] Multiple (linear) regression, $y_i = \beta_0 + \beta_1 x_{1i} + \cdots + \beta_k x_{ki} + u_i$
$$ \widehat{\mathop{\text{Var}}} \left( \hat{\beta}\_j \right) = \dfrac{\sum\_i \hat{r}\_{ij}^2 e\_i^2}{\text{SST}\_{x\_j^2}} $$
where $\hat{r}_{ij}$ denotes the i.super[th] residual from regressing $x_j$ on all other explanatory variables.
---
With these standard errors, we can return to correct statistical inferencel
_E.g._, we can update our previous $t$ statistic formula with our new heteroskedasticity-robust standard erros.
$$ t = \dfrac{\text{Estimate} - \text{Hypothesized value}}{\text{Standard error}} $$
---
**Notes**
- We are still using .hi[OLS estimates for] $\color{#e64173}{\beta_j}$
- Our het.-robust standard errors use a .hi[different estimator].
- Homoskedasticity
- Plain OLS variance estimator is more efficient.
- Het.-robust is still unbiased.
- Heteroskedasticity
- Plain OLS variance estimator is biased.
- Het.-robust variance estimator is unbiased.
---
These standard errors go by many names
- Heteroskedasticity-robust standard errors
- Het.-robust standard errors
- White standard errors
- Eicker-White standard errors
- Huber standard errors
- Eicker-Huber-White standards errors
- (some other combination of Eicker, Huber, and White)
**Do not say:** "Robust standard errors". The problem: "robust" to what?
---
layout: false
class: inverse, middle
# Living with heteroskedasticity
## Examples
---
layout: true
# Living with heteroskedasticity
---
## Examples
Back to our test-scores dataset…
```{R, ex test data}
# Load packages
library(pacman)
p_load(tidyverse, Ecdat)
# Select and rename desired variables; assign to new dataset; format as tibble
test_df <- Caschool %>% select(
test_score = testscr, ratio = str, income = avginc, enrollment = enrltot
) %>% as_tibble()
# View first 2 rows of the dataset
head(test_df, 2)
```
---
layout: true
# Living with heteroskedasticity
## Example: Model specification
---
We found significant evidence of heteroskedasticity.
Let's check if it was due to misspecifying our model.
---
Model.sub[1]: $\text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i$
<br>`lm(test_score ~ ratio + income, data = test_df)`
```{R, ex spec1, echo = F, dev = "svg", fig.height = 4.75}
# Model 1: test ~ ratio + income
test_df %<>% mutate(e1 = lm(test_score ~ ratio + income, data = test_df) %>% residuals())
# Plot
ggplot(data = test_df, aes(x = income, y = e1)) +
geom_point(size = 3, alpha = 0.5, color = red_pink) +
labs(x = "Income", y = TeX("\\textit{e}")) +
theme_axes_serif
```
---
Model.sub[2]: $\log\left(\text{Score}_i\right) = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i$
<br>`lm(log(test_score) ~ ratio + income, data = test_df)`
```{R, ex spec2, echo = F, dev = "svg", fig.height = 4.75}
# Model 1: test ~ ratio + income
test_df %<>% mutate(e2 = lm(log(test_score) ~ ratio + income, data = test_df) %>% residuals())
# Plot
ggplot(data = test_df, aes(x = income)) +
geom_point(aes(y = e2), size = 3, alpha = 0.5, color = red_pink) +
labs(x = "Income", y = TeX("\\textit{e}")) +
theme_axes_serif
```
---
Model.sub[3]: $\log\left(\text{Score}_i\right) = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \log\left(\text{Income}_i\right) + u_i$
<br>`lm(log(test_score) ~ ratio + log(income), data = test_df)`
```{R, ex spec3, echo = F, dev = "svg", fig.height = 4.75}
# Model 1: test ~ ratio + income
test_df %<>% mutate(e3 = lm(log(test_score) ~ ratio + log(income), data = test_df) %>% residuals())
# Plot
ggplot(data = test_df, aes(x = log(income))) +
geom_point(aes(y = e3), size = 3, alpha = 0.5, color = red_pink) +
labs(x = "Log(Income)", y = TeX("\\textit{e}")) +
theme_axes_serif
```
---
Let's test this new specification with the White test for heteroskedasticity.
.center[
Model.sub[3]: $\log\left(\text{Score}_i\right) = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \log\left(\text{Income}_i\right) + u_i$
]
```{R, ex spec test, include = F}
white_r2_spec <- lm(e3^2 ~
ratio * log(income) + I(ratio^2) + I(log(income)^2),
data = test_df
) %>% summary() %$% r.squared
white_stat_spec <- white_r2_spec %>% multiply_by(420)
```
--
The regression for the White test
--
$$
\begin{align}
e_i^2 = &\alpha_0 + \alpha_1 \text{Ratio}_i + \alpha_2 \log\left(\text{Income}_i\right) + \alpha_3 \text{Ratio}_i^2 + \alpha_4 \left(\log\left(\text{Income}_i\right)\right)^2 \\
&+ \alpha_5 \left(\text{Ratio}_i\times\log\left(\text{Income}_i\right)\right) + v_i
\end{align}
$$
--
yields $R_e^2\approx`r round(white_r2_spec, 3)`$
--
and test statistic of
--
$\widehat{\text{LM}} = n\times R_e^2 \approx `r round(white_stat_spec, 1)`$.
--
Under H.sub[0], $\text{LM}$ is distributed as
--
$\chi_5^2$
--
$\implies$ *p*-value $\approx$ `r pchisq(white_stat_spec, 5, lower.tail = F) %>% round(3)`.
--
∴
--
.hi[Reject H.sub[0].]
--
.hi[Conclusion:]
--
There is statistically significant evidence of heteroskedasticity at the five-percent level.
---
Okay, we tried adjusting our specification, but there is still evidence of heteroskedasticity.
**Next:** In general, you will turn to heteroskedasticity-robust standard errors.
- OLS is still unbiased for the .hi[coefficients] (the $\beta_j$'s)
- Heteroskedasticity-robust standard errors are unbiased for the .hi[standard errors] of the $\hat{\beta}_j$'s, _i.e._, $\sqrt{\mathop{\text{Var}} \left( \hat{\beta}_j \right)}$.
---
layout: true
# Living with heteroskedasticity
## Example: Het.-robust standard errors
---
Let's return to our model
$$ \text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i $$
We can use the `lfe` package in .mono[R] to calculate standard errors.
---
$$ \text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i $$
1\. Run the regression with `felm()` (instead of `lm()`)
```{R, lfe1}
# Load 'lfe' package
p_load(lfe)
# Regress log score on ratio and log income
test_reg <- felm(test_score ~ ratio + income, data = test_df)
```
--
*Notice* that `felm()` uses the same syntax as `lm()` for this regression.
---
$$ \text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i $$
2\. Estimate het.-robust standard errors with `robust = T` option in `summary()`
```{R, lfe2a, eval = F}
# Het-robust standard errors with 'robust = T'
summary(test_reg, robust = T)
```
```{R, lfe2b, echo = F}
test_het_out <- summary(test_reg, robust = T) %>% capture.output()
test_het_out[10:13] %>% paste0("\n") %>% cat()
```
---
Ceofficients and **heteroskedasticity-robust standard errors**:
```{R, lfe3, eval = F}
summary(test_reg, robust = T)
```
```{R, lfe4, echo = F}
test_het_out <- summary(test_reg, robust = T) %>% capture.output()
test_het_out[10:13] %>% paste0("\n") %>% cat()
```
Ceofficients and **plain OLS standard errors** (assumes homoskedasticity):
```{R, lfe5, eval = F}
summary(test_reg, robust = F)
```
```{R, lfe6, echo = F}
test_hom_out <- summary(test_reg, robust = F) %>% capture.output()
test_hom_out[10:13] %>% paste0("\n") %>% cat()
```
---
layout: true
# Living with heteroskedasticity
## Example: WLS
---
We mentioned that WLS is often not possible—we need to know the functional form of the heteroskedasticity—either
**A**\. $\sigma_i^2$
or
**B**\. $h(x_i)$, where $\sigma_i^2 = \sigma^2 h(x_i)$
--
There *are* occasions in which we can know $h(x_i)$.
---
Imagine individuals in a population have homoskedastic disturbances.
However, instead of observing individuals' data, we observe (in data) groups' averages (_e.g._, cities, counties, school districts).
If these groups have different sizes, then our dataset will be heteroskedastic—in a predictable fashion.
**Recall:** The variance of the sample mean depends upon the sample size,
$$ \mathop{\text{Var}} \left( \overline{x} \right) = \dfrac{\sigma_x^2}{n} $$
--
**Example:** Our school testing data is averaged at the school level.
---
*Example:* Our school testing data is averaged at the school level.
Even if individual students have homoskedastic disturbances, the schools would have heteroskedastic disturbances, _i.e._,
**Individual-level model:** $\quad \text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i$
**School-level model:** $\quad \overline{\text{Score}}_s = \beta_0 + \beta_1 \overline{\text{Ratio}}_s + \beta_2 \overline{\text{Income}}_s + \overline{u}_s$
where the $s$ subscript denotes an individual school (just as $i$ indexes an individual person).
$$ \mathop{\text{Var}} \left( \overline{u}_s \right) = \dfrac{\sigma^2}{n_s} $$
---
For WLS, we're looking for a function $h(x_s)$ such that $\mathop{\text{Var}} \left( \overline{u}_s | x_s \right) = \sigma^2 h(x_s)$.
--
We just showed<sup>.pink[†]</sup> that $\mathop{\text{Var}} \left( \overline{u}_s |x_s \right) = \dfrac{\sigma^2}{n_s}$.
.footnote[
.pink[†] Assuming the individuals' disturbances are homoskedastic.
]
--
Thus, $h(x_s) = 1/n_s$, where $n_s$ is the number of students in school $s$.
--
To implement WLS, we divide each observation's data by $1/\sqrt{h(x_s)}$, meaning we need to multiply each school's data by $\sqrt{n_s}$.
--
The variable .mono[enrollment] in the .mono[test_df] dataset is our $n_s$.
---
Using WLS to estimate $\text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i$
**Step 1:** Multiply each variable by $1/\sqrt{h(x_i)} = \sqrt{\text{Enrollment}_i}$
```{R, wls1}
# Create WLS transformed variables, multiplying by sqrt of 'pop'
test_df <- mutate(test_df,
test_score_wls = test_score * sqrt(enrollment),
ratio_wls = ratio * sqrt(enrollment),
income_wls = income * sqrt(enrollment),
intercept_wls = 1 * sqrt(enrollment)
)
```
Notice that we are creating a transformed intercept.
---
Using WLS to estimate $\text{Score}_i = \beta_0 + \beta_1 \text{Ratio}_i + \beta_2 \text{Income}_i + u_i$
**Step 2:** Run our WLS (transformed) regression
```{R, wls2}
# WLS regression
wls_reg <- lm(
test_score_wls ~ -1 + intercept_wls + ratio_wls + income_wls,
data = test_df
)
```
--
*Note:* The `-1` in our regression tells .mono[R] not to add an intercept, since we are adding a transformed intercept (`intercept_wls`).
---
The .hi[WLS estimates and standard errors:]
```{R, wls3, echo = F}
# Grab the summary
test_wls_out <- summary(wls_reg) %>% capture.output()
# Print the coefficients
test_wls_out[11:14] %>% paste0("\n") %>% cat()
```
--
<br>
The .hi[OLS estimates] and .hi[het.-robust standard errors:]
```{R, wls4, echo = F}
# Print the coefficients
test_het_out[10:13] %>% paste0("\n") %>% cat()
```
---
Alternative to doing your own weighting: feed `lm()` some `weights`.
```{R, wls5, eval = F}
lm(test_score ~ ratio + income, data = test_df, weights = enrollment)
```
---
layout: false
# Living with heteroskedasticity
In this example
- .hi[Heteroskedasticity-robust standard errors] did not change our standard errors very much (relative to plain OLS standard errors).
- .hi[WLS] changed our answers a bit—coefficients and standard errors.
--
These examples highlighted a few things:
1. Using the correct estimator for your standard errors really matters.<sup>.pink[†]</sup>
2. Econometrics doesn't always offer an obviously *correct* route.
.footnote[
.pink[†] Sit in on an economics seminar, and you will see what I mean.
]
--
To see \#1, let's run a simulation.
---
layout: true