-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathweek3.qmd
1420 lines (679 loc) · 39.5 KB
/
week3.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: "Week 3 - Discrete Choice"
author: "Josh Merfeld"
institute: "KDI School"
date: "2024-09-30"
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)
})
library(tidyverse)
library(kableExtra)
library(fixest)
library(ggpubr)
library(RColorBrewer)
library(haven)
library(mfx)
library(nnet)
library(survival)
library(survminer)
df <- read_dta("week3files/data.dta")
data(iris)
multinomresults <- multinom(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iris)
```
# Introduction
## What are we doing today?
- This week we will review methods for limited dependent variables
- We will also discuss how to interpret the results
- Some of this will be review, but there will be some new stuff, too
- For example, we will discuss poisson regression, which you might not have seen before
## What are we doing today?
- Much of the mathematical notation and theory comes from *Econometrics* by Bruce Hansen
- I already discussed the older version of the textbook that is available for free online
- A small note:
- You can use OLS for binary outcomes! This is actually pretty common in economics.
- I'll discuss this more in a bit.
- Other disciplines (e.g. public health) really like some of the new methods we will discuss today, so they are worth knowing.
# Binary Choice
## Binary choice
- Let's start with the simplest possibility: binary choice
- You can think of this as a No/Yes or False/True question, but we will generally refer to it as 0/1 choice
- In programming, always remember that $FALSE=0$ and $TRUE=1$
- Focusing on the binary choice case will allow us to build intuition for the more general case of discrete choice
- We will also be able to use the same data as we move to the more general case
## Dichotomous variables
- We will be thinking about this $Y\in\{0,1\}$
- In other words, $Y$ is a dichotomous (dummy) variable that can only be equal to 0 or 1
- We are going to discuss methods to output *conditional probabilities*:
$$\begin{gather} \mathbb{P}\left[Y=1|X\right] \end{gather}$$
## The error term
- Consider the following model:
$$\begin{gather} Y = \beta X + \epsilon, \end{gather}$$
where $X$ can have any number of columns (variables), $k$.
- We already know that $\epsilon$ does not need to be normally distributed
- In fact, if $Y\in\{0,1\}$, then $\epsilon$ *will never be normally distributed*
- Why?
## The error term
- $\epsilon$ has a two-point conditional distribution:
$$\begin{gather} \epsilon = \Biggl\{ \begin{array}{ll} 1 - P(X), & \text{with probability } P(X) \\ P(X), & \text{with probability } 1 - P(X) \end{array} \end{gather}$$
- $\epsilon$ is *heteroskedastic*:
$$\begin{gather} \text{Var}(\epsilon|X) = P(X)(1-P(X)) \end{gather}$$
- In fact, the variance of any dummy variable is $P(1-P)$, where $P$ is the probability of the dummy variable being equal to 1
## Scatterplots are pretty worthless!
:::: {.columns}
::: {.column width="50%"}
```{r scatter, echo = TRUE, eval = FALSE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
# for reading in Stata data.
# Install this using your console
library(haven)
# read in data for the week:
df <- read_dta("data.dta")
# scatter of in_poverty on h_age:
ggplot(data = df) +
geom_point(aes(x = h_age, y = in_poverty)) +
labs(x = "Head age", y = "In poverty this month") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
::: {.column width="50%"}
```{r scatter2, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "100%", fig.align = "center"}
ggplot(data = df) +
geom_point(aes(x = h_age, y = in_poverty)) +
labs(x = "Head age", y = "In poverty this month") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
::::
## Let's plot out the conditional probabilities (means)
:::: {.columns}
::: {.column width="50%"}
```{r scatter3, echo = TRUE, eval = FALSE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
# means by age
povmeans <- df %>%
group_by(h_age) %>%
summarize(mean = mean(in_poverty)) %>%
ungroup
# scatter of in_poverty on h_age:
ggplot(data = povmeans) +
geom_point(aes(x = h_age, y = mean)) +
labs(x = "Head age", y = "P(poverty)") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
::: {.column width="50%"}
```{r scatter4, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "100%", fig.align = "center"}
# means by age
povmeans <- df %>%
group_by(h_age) %>%
summarize(mean = mean(in_poverty)) %>%
ungroup
# scatter of in_poverty on h_age:
ggplot(data = povmeans) +
geom_point(aes(x = h_age, y = mean)) +
labs(x = "Head age", y = "P(poverty)", title = "Mean poverty by age of head") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
:::
::::
## Linear probability model (LPM)
- We can estimate this using OLS:
$$\begin{gather} Y = \beta X + \epsilon \end{gather}$$
```{r lpm, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "100%", fig.align = "center"}
f1 <- feols(in_poverty ~ h_male + h_age, data = df)
f2 <- feols(in_poverty ~ h_male + h_age, data = df, vcov = "HC1")
etable(f1, f2)
```
## Linear probability model (LPM)
```{r lpm2, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "100%", fig.align = "center"}
summary(feols(in_poverty ~ h_male + h_age, data = df, vcov = "HC1"))
```
- The interpretation of these coefficients is pretty straightforward:
- It is the change in the probability of being in poverty for a one-unit change in the variable of interest
- Male-headed households are 6.1 *percentage points* less likely to be poor than female-headed households, controlling for age.
- Each addition year of age *increases* the probability of being in poverty by 0.005 *percentage points*, controlling for gender of the head.
## Linear probability model (LPM)
- Sometimes people motivate other estimation methods based on heteroskedasticity
- But we can easily correct for this using robust standard errors (HC1 in `feols`)
- There are two other problems with LPM, though:
. . .
- The predicted values can be outside of the 0-1 range
- Is this a problem? Maybe. Maybe not. It depends on what you're doing. \pause
- Constant effects throughout the probability distribution
. . .
- Is this realistic? If we think someone has a 95 percent probability of being poor, do we think the percentage point change would be the same for changing a variable relative to someone with a 50 percent probability of being poor?
## Another option
- We can think of this as a *latent variable* model:
$$\begin{gather} Y^* = \beta X + \epsilon \\
\epsilon\sim G(\epsilon) \\
Y = \mathbbm{1}(Y^*>0) = \Biggl\{ \begin{array}{ll} 1, & \text{if } Y^*>0 \\ 0, & \text{otherwise} \end{array} \end{gather}$$
where $Y^*$ is the latent variable, $G(\cdot)$ is the distribution of $\epsilon$, and $\mathbbm{1}(\cdot)$ is the indicator function.
- One way to think about this is that $y^*$ is utility, but we only observe whether the choice increases utility ($y=1$) or doesn't ($y=0$).
## Let's give this a bit more structure
- Note that $Y=1$ iff $Y^*>0$, which is the same as saying $\beta X + \epsilon > 0$
- The response probability is then given by the CDF of $\epsilon$ evaluated at $-\beta X$:
$$\begin{gather} \mathbb{P}\left[Y=1|X\right] = \mathbb{P}\left[\epsilon > -\beta X\right] = 1 - G(-\beta X) = G(\beta X) \end{gather}$$
- Note that CDFs (cumulative distribution functions) give us probabilities of being *less than or equal to* a given value
- The last equality holds because $G(\cdot)$ will always be *symmetric around 0* here
- That value here is $\beta X$
## CDF examples of the logistic (sigmoid) function - Wikipedia
![](week3files/cdfwiki)
## The link function
- The function $G(\cdot)$ is called the *link function* and plays an important role here
- Two common link functions are the *logit* and *probit* link functions:
- They are defined as follows:
- Logit: $G(\epsilon) = \frac{e^{\epsilon}}{1+e^{\epsilon}} = \frac{1}{1+e^{-\epsilon}}$
- Probit: $G(\epsilon) = \Phi(\epsilon)$, where $\Phi(\cdot)$ is the CDF of the standard normal distribution
- We will discuss these in more detail in a bit
## Likelihood
- Likelihood: the joint probability of the data evaluated with the sample, as a function of the parameters
- What?
- Let's start with probit, which uses a normal distribution. Here is the conditional density of $Y$ given $X$ under this assumption:
$$\begin{gather} f(Y|X) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{1}{2\sigma^2}(Y-\beta X)^2\right) \end{gather}$$
## Likelihood
$$\begin{gather} f(Y|X) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{1}{2\sigma^2}(y-\beta X)^2\right) \end{gather}$$
- In this case, what is the probability we *observe our sample given the values of $\beta$ and $\sigma$*?
$$\begin{align} f(y_1,\ldots,y_n | x_1,\ldots, x_n) &= \prod_{i=1}^n f(y_i | x_i) \\
&= \prod_{i=1}^n \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left(-\frac{1}{2\sigma^2}(y_i-\beta x_i)^2\right) \\
&= \frac{1}{(2\pi\sigma^2)^{n/2}}\exp\left(-\frac{1}{2\sigma^2}\prod_{i=1}^n(y_i-\beta x_i)^2\right) \\
&= L_n(\beta, \sigma^2) \end{align}$$
## Likelihood
$$\begin{gather} f(y_1,\ldots,y_n | x_1,\ldots, x_n) = L_n(\beta, \sigma^2) \end{gather}$$
- This is the *likelihood function*
- Note that it is a function of the parameters, $\beta$ and $\sigma^2$
- The properties of logs make this easier to work with:
$$\begin{gather} \ell_n(\beta, \sigma^2) = \log(L_n(\beta, \sigma^2)) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^n(y_i-\beta x_i)^2 \end{gather}$$
- This is the *log likelihood function*
- It is of course also a function of the parameters, $\beta$ and $\sigma^2$
## Maximum likelihood estimation
$$\begin{gather} \ell_n(\beta, \sigma^2) = \log(L_n(\beta, \sigma^2)) = -\frac{n}{2}\log(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_{i=1}^n(y_i-\beta x_i)^2 \end{gather}$$
- We have our log likelihood function, which is a function of the parameters, $\beta$ and $\sigma^2$
## Maximum likelihood estimation
- What we want to do is find the values of $\beta$ and $\sigma^2$ that *maximize* this function
- In other words, the values that make our sample the most likely to have been observed, or the biggest probability of observing our sample
- This is called *maximum likelihood estimation*
$$\begin{gather} \left(\hat{\beta}, \hat{\sigma}^2\right) = \underset{\beta\in\mathbb{R}^k,\sigma^2>0}{\text{argmax}}\;\ell_n(\beta, \sigma^2) \end{gather}$$
where $k$ is the number of variables (coefficients), including the intercept.
## Maximum likelihood estimation
- A simple example is a coin flip
- Let's say we flip a coin. If it's a fair coin, what is the probability of obtaining heads?
. . .
- 50% or 0.5 (we generally work with the proportion 0.5, and not the percent)
. . .
- What is the probability of obtaining heads twice in a row?
. . .
- 0.5 * 0.5 = 0.25
- Three times in a row?
. . .
- 0.5 * 0.5 * 0.5 = 0.125
## Maximum likelihood estimation
- Say we flip the coin a bunch of times
- For argument's sake, let's say we flip it 100 times and obtain 60 heads
- If we know nothing about whether the coin is actually fair, what is the *most likely distribution* that would give us 60 heads and 40 tails? \pause
- It's a distribution in which the probability of heads is 0.6! \pause
- This is like maximum likelihood estimation. We are trying to find the parameters that makes our sample the most likely to have been observed.
- In this case, the parameter would be the true mean of the distribution of the coin (where heads = 1 and tails = 0).
- We could then of course test whether this is significantly different from 0.5, which might be our null hypothesis.
## Generalizing maximum likelihood estimation
$$\begin{gather} \left(\hat{\beta}, \hat{\sigma}^2\right) = \underset{\beta\in\mathbb{R}^k,\sigma^2>0}{\text{argmax}}\;\ell_n(\beta, \sigma^2) \end{gather}$$
- MLE is generally always estimated using numerical optimization
- We will not discuss the details of this here
- The basic reason is that most likelihood functions are not easy to maximize analytically (i.e. they have no closed-form solution)
- In the case of the normal regression model, however, there is a closed-form solution
- And this is the same closed-form solution as OLS!
## Logit for binary choice
- Let's return to our binary choice model.
- Regardless of how you estimate it, the probability mass function for $Y$ is:
$$\begin{gather} \pi(y) = p^y(1-p)^{1-y}, \end{gather}$$
where $p$ is the probability of $Y=1$, or the mean. Remember that $Y\in\{0,1\}$; i.e., it can only equal 0 or 1.
- Let's bring our link function back into it. The *conditional* probability is:
$$\begin{gather} \pi(Y|X)=G(\beta X)^Y(1-G(\beta X))^{1-Y}=G(\beta X)^Y(G(-\beta X))^{1-Y}=G(\beta Z), \end{gather}$$
where $Z = \Biggl\{ \begin{array}{ll} X, & \text{if } Y=1 \\ -X, & \text{if } Y=0 \end{array}$
## Logit for binary choice
$$\begin{gather} \pi(Y|X)=G(\beta Z), \end{gather}$$
- Taking logs (because they're easy to work with), we get the log likelihood function:
$$\begin{gather} \ell_n(\beta) = \sum_{i=1}^n \log G(\beta Z) \end{gather}$$
- This is the same as the log likelihood function for probit, except that the link function is different.
## Logit for binary choice
- Again, we want to find the values of $\beta$ (and $\sigma$, which will show up in the link function) that maximize this function.
$$\begin{gather} \left(\hat{\beta}, \hat{\sigma}^2\right) = \underset{\beta\in\mathbb{R}^k,\sigma^2>0}{\text{argmax}}\;\ell_n(\beta, \sigma^2) \end{gather}$$
- Something interesting is that in practice, we don't numerically maximize...
- Instead, we *minimize* the *negative* of the log likelihood function!
$$\begin{gather} \left(\hat{\beta}, \hat{\sigma}^2\right) = \underset{\beta\in\mathbb{R}^k,\sigma^2>0}{\text{argmin}}\;-\ell_n(\beta, \sigma^2) \end{gather}$$
## An example in R - Household variables and poverty using glm()
```{r mle1, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "logit")))
```
## Interpreting logit output
```{r mle2, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "logit")))$coefficients
```
- How do we interpret these coefficients?
- The coefficients are "log odds"
- For male household heads, the log odds of being in poverty is 0.280 *lower* than that for female household heads
- What?
## Log odds
- What are log odds?
- Let's start with odds:
$$\begin{gather} \text{odds} = \frac{p}{1-p}, \end{gather}$$
where $p$ is the probability of $y = 1$ (being poor in this case).
- Log odds?
$$\begin{gather} \text{log odds} = \log\left(\frac{p}{1-p}\right) \end{gather}$$
## Log odds and logit
- Logit regression is basically estimating:
$$\begin{gather} \log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1 X_1 + \ldots + \beta_k X_k \end{gather}$$
## The intercept is the log odds of being in poverty for female households
```{r mle3, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "logit")))$coefficients
```
$$\begin{gather} \log\left(\frac{p}{1-p}\right) = -0.6196447 \\
\left(\frac{p}{1-p}\right) = exp(-0.6196447) \\
\left(\frac{p}{1-p}\right) \approx 0.538 \\
p = 0.538-0.538p \\
1.538p = 0.538 \\
p\approx 0.350 \end{gather}$$
What is the actual mean for female headed households? `r format(mean(df$in_poverty[df$h_male==0]), digits = 3)`
## The coefficient?
$$\begin{gather} \log\left(\frac{p_m}{1-p_m}\right) - \log\left(\frac{p_f}{1-p_f}\right) = -0.2795628 \\
\log\left(\frac{\frac{p_m}{1-p_m}}{\frac{p_f}{1-p_f}}\right) = -0.2795628 \\
\left(\frac{\frac{p_m}{1-p_m}}{\frac{p_f}{1-p_f}}\right) = exp(-0.2795628) \\
\left(\frac{\frac{p_m}{1-p_m}}{\frac{p_f}{1-p_f}}\right) = 0.7561142 \end{gather}$$
- In the last line, this is referred to as an *odds ratio*.
- It's less than one, which means male-headed households are *less likely* to be in poverty.
- Their *odds* of being in poverty are around 24% lower.
## The coefficient?
- Mean for female-headed households: `r format(mean(df$in_poverty[df$h_male==0]), digits = 3)`
- odds ($\frac{p_f}{1-p_f}$): `r format(mean(df$in_poverty[df$h_male==0])/(1-mean(df$in_poverty[df$h_male==0])), digits = 3)`
- Mean for male-headed households: `r format(mean(df$in_poverty[df$h_male==1]), digits = 3)`
- odds $\left(\frac{p_m}{1-p_m}\right)$: `r format(mean(df$in_poverty[df$h_male==1])/(1-mean(df$in_poverty[df$h_male==1])), digits = 3)`
- Odds ratio ($\frac{\frac{p_m}{1-p_m}}{\frac{p_f}{1-p_f}}$): `r format((mean(df$in_poverty[df$h_male==1])/(1-mean(df$in_poverty[df$h_male==1])))/(mean(df$in_poverty[df$h_male==0])/(1-mean(df$in_poverty[df$h_male==0]))), digits = 3)`
- Exponentiating shows us the odds ratio!
## Marginal effects
- We can also calculate marginal effects
- These are the change in the probability of being in poverty for a one-unit change in the variable of interest
- An important note is that this *depends on where you are located in the distribution*
- We just calculated the means, so with only the binary independent variable, we know that the marginal effect is:
- `r format(mean(df$in_poverty[df$h_male==1]) - mean(df$in_poverty[df$h_male==0]), digits = 3)`
- We will use the "mfx" package for this, so please install it in the console.
## Marginal effects
```{r mle4, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
logitmfx(in_poverty ~ h_male, data = df)
# for binary outcomes, it shows the change from 0 to 1!
```
## logit with more coefficients
```{r mle5, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
logitmfx(in_poverty ~ h_male + h_age, data = df)
# for binary outcomes, it shows the change from 0 to 1!
# for continuous variables, it's the derivative (i.e. instantaneous change)!
# By default, it calculates these by holding variables AT THEIR MEANS
```
## Probit
```{r probit1, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "probit")))$coefficients
```
- What about probit coefficients?
- These relate to the *CDF of the standard normal distribution*
- The intercept is the mean for female-headed households
## Standard normal CDF
- The intercept is -0.3856924
- The mean poverty for female-headed households is `r format(mean(df$in_poverty[df$h_male==0]), digits = 3)`
- Here's the CDF for the standard normal distribution with the intercept:
```{r probit2, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
ggplot() +
stat_function(data = data.frame(x = c(-3, 3)), aes(x = x), fun = pnorm) +
theme_bw() +
labs(x = "x", y = "CDF(x)") +
geom_vline(xintercept = -0.3856924, linetype = "dotted", color = "blue") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Standard normal CDF
- The intercept is -0.3856924
- The mean poverty for female-headed households is `r format(mean(df$in_poverty[df$h_male==0]), digits = 3)`
- Here's the CDF for the standard normal distribution with BOTH:
```{r probit3, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
ggplot() +
stat_function(data = data.frame(x = c(-3, 3)), aes(x = x), fun = pnorm) +
theme_bw() +
labs(x = "x", y = "CDF(x)") +
geom_vline(xintercept = -0.3856924, linetype = "dotted", color = "blue") +
geom_hline(yintercept = mean(df$in_poverty[df$h_male==0]), linetype = "dotted", color = "blue") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Now let's look at the coefficient on h_male
```{r probit4, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "65%", fig.align = "center"}
summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "probit")))$coefficients
```
## Standard normal CDF
- The intercept is -0.3038412 and the coefficient is -0.1699918
- Here's the CDF for the standard normal distribution with BOTH:
```{r probit5, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
results <- summary(glm(in_poverty ~ h_male, data = df, family = binomial(link = "probit")))$coefficients
ggplot() +
stat_function(data = data.frame(x = c(-3, 3)), aes(x = x), fun = pnorm) +
theme_bw() +
labs(x = "x", y = "CDF(x)") +
geom_vline(xintercept = results[1,1], linetype = "dotted", color = "blue") +
geom_vline(xintercept = results[1,1] + results[2,1], linetype = "dotted", color = "orange") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Standard normal CDF
- The intercept is -0.3856924 and the coefficient is -0.1699918
- What's the change in PROBABILITY? $\text{mean(male)} - \text{mean(female)}$ or `r format(mean(df$in_poverty[df$h_male==1]) - mean(df$in_poverty[df$h_male==0]), digits = 3)`
```{r probit6, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
ggplot() +
stat_function(data = data.frame(x = c(-3, 3)), aes(x = x), fun = pnorm) +
theme_bw() +
labs(x = "x", y = "CDF(x)") +
geom_vline(xintercept = results[1,1], linetype = "dotted", color = "blue") +
geom_vline(xintercept = results[1,1] + results[2,1], linetype = "dotted", color = "orange") +
geom_hline(yintercept = mean(df$in_poverty[df$h_male==0]), linetype = "dotted", color = "blue") +
geom_hline(yintercept = mean(df$in_poverty[df$h_male==1]), linetype = "dotted", color = "orange") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Marginal effects
- The intercept is -0.3856924 and the coefficient is -0.1699918
- What's the change in PROBABILITY? $\text{mean(male)} - \text{mean(female)}$ or `r format(mean(df$in_poverty[df$h_male==1]) - mean(df$in_poverty[df$h_male==0]), digits = 3)`
```{r probit7, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
probitmfx(in_poverty ~ h_male, data = df)
```
## Probit and marginal effects
```{r probit8, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# change in z-scores
summary(glm(in_poverty ~ h_male + log(h_age), data = df, family = binomial(link = "probit")))$coefficients
```
```{r probit9, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# change in probability, holding other variables at their means
probitmfx(in_poverty ~ h_male + log(h_age), data = df)
```
# Multiple discrete choice
## What if the outcome has more than two categories?
- Many outcomes are not 0/1.
- We can think of outcomes with discrete categories, but more than two:
- Religion
- Political party
- Opinion on a likert scale (e.g. strongly agree, agree, neutral, disagree, strongly disagree)
- Months in poverty
- There's a key difference between the first two and the last two:
- The first two are *unordered*
- The last two are *ordered*
## Months in poverty - distribution
```{r ologit1, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
ggplot() +
geom_histogram(data = df, aes(x = months_in_poverty), binwidth = 1) +
theme_bw() +
labs(x = "Months in poverty", y = "Count") +
scale_x_continuous(breaks = seq(0, 12, 1)) +
geom_vline(xintercept = mean(df$months_in_poverty), linetype = "dotted", color = "blue") +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
```
## Ordered logistic regression with the polr function
```{r ologit2, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# note that the outcome must be a FACTOR variable for polr
summary(polr(as_factor(months_in_poverty) ~ h_male + h_age, data = df, Hess = TRUE))
```
## Ordered data: ordinal logit/probit
- When we have ordered discrete variable, we can use an ordered logit or probit model
- These are also called *ordinal* logit/probit models
- The idea is that we have a latent variable, $Y^*$, that is continuous
- We observe $Y$ as a discrete variable, but it is *ordered*
- We can think of $Y$ as being *binned* into categories
$$\begin{gather} Y = \Biggl\{ \begin{array}{ll} 1, & \text{if } Y^*\in(-\infty,\theta_1] \\ 2, & \text{if } Y^*\in(\theta_1,\theta_2] \\ \vdots & \vdots \\ J, & \text{if } Y^*\in(\theta_{J-1},\infty) \end{array} \end{gather}$$
## The interpretation?
```{r ologit3, echo = FALSE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# note that the outcome must be a FACTOR variable for polr
summary(polr(as_factor(months_in_poverty) ~ h_male + h_age, data = df, Hess = TRUE))
```
- The interpretation is similar to logit: a change in the log-odds of being in a higher level of months in poverty!
## Understanding fit with MLE
- There is no r-squared in MLE
- It is not a true r-squared *because there is no sense of "mean" with discrete data, especially unordered data*
- We can use the log likelihood function to compare models
- The log likelihood function is a function of the parameters, $\beta$ and $\sigma^2$
- The higher the log likelihood, the better the fit
- We can also use the *Akaike Information Criterion* (AIC) and *Bayesian Information Criterion* (BIC)
- These are functions of the log likelihood function and the number of parameters
- The lower the AIC/BIC, the better the fit
- These are best thought of as useful for comparing across models
- Difficult to interpret them on their own
## AIC
- AIC is defined as follows:
- $k$ is the number of parameters in the model
- $L$ is the log likelihood function
- AIC: $2k - 2\log(L)$
```{r aic, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, size = "tiny", out.width = "55%", fig.align = "center"}
# save our model
results <- glm(in_poverty ~ h_male + h_age, data = df, family = binomial(link = "probit"))