-
Notifications
You must be signed in to change notification settings - Fork 11
/
logistic-regression.qmd
3002 lines (2172 loc) · 61.9 KB
/
logistic-regression.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
---
subtitle: "Logistic regression and variations"
---
# Models for Binary Outcomes {#sec-Bernoulli-models}
---
{{< include shared-config.qmd >}}
### Acknowledgements {.unnumbered}
This content is adapted from:
- @dobson4e, Chapter 7
- @vittinghoff2e, Chapter 5
- [David Rocke](https://dmrocke.ucdavis.edu/)'s materials from the [2021 edition of Epi 204](https://dmrocke.ucdavis.edu/Class/EPI204-Spring-2021/EPI204-Spring-2021.html)
- @NahhasIRMPHR [Chapter 6](https://www.bookdown.org/rwnahhas/RMPH/blr.html)
## Introduction
### What is logistic regression?
**Logistic regression** is a framework for modeling [binary](probability.qmd#def-binary) outcomes, conditional on one or more *predictors* (a.k.a. *covariates*).
---
::: {#exr-binary-examples}
#### Examples of binary outcomes
What are some examples of binary outcomes in the health sciences?
:::
----
:::: {.solution}
{{< include binary-outcome-examples.qmd >}}
::::
---
Logistic regression uses the [Bernoulli](probability.qmd#def-bernoulli) distribution to model the outcome variable, conditional on one or more covariates.
---
::: {#exr-def-bernoulli}
Write down a mathematical definition of the Bernoulli distribution.
:::
---
::::{.solution}
{{< include def-bernoulli.qmd >}}
::::
---
### Logistic regression versus linear regression
Logistic regression differs from linear regression, which uses the Gaussian ("normal") distribution to model the outcome variable, conditional on the covariates.
---
:::: {#exr-linear}
Recall: what kinds of outcomes is linear regression used for?
::::
---
::: {.solution}
Linear regression is typically used for numerical outcomes that aren't event counts or waiting times for an event. Examples of outcomes that are often analyzed using linear regression include include weight, height, and income.
:::
## Risk Estimation and Prediction
::: notes
In Epi 203, you have already seen methods for modeling binary outcomes using one covariate that is also binary (such as exposure/non-exposure).
In this section, we review one-covariate analyses, with a special focus on risk ratios and odds ratios, which are important concepts for interpreting logistic regression.
:::
---
:::::{#exm-oc-mi}
### Oral Contraceptive Use and Heart Attack
* Research question: how does oral contraceptive (OC) use affect the risk of myocardial infarction (MI; a.k.a. heart attack)?
:::{.notes}
This was an issue when oral contraceptives were first developed, because the original formulations used higher concentrations of hormones. Modern OCs don't have this issue.
@tbl-oc-mi contains simulated data for an imaginary follow-up (a.k.a. *prospective*) study in which two groups are identified, one using OCs and another not using OCs, and both groups are tracked for three years to determine how many in each groups have MIs.
:::
```{r}
#| message: false
#| code-fold: true
#| label: tbl-oc-mi
#| tbl-cap: Simulated data from study of oral contraceptive use and heart attack risk
library(dplyr)
oc_mi =
tribble(
~OC, ~MI, ~Total,
"OC use", 13, 5000,
"No OC use", 7, 10000
) |>
mutate(`No MI` = Total - MI) |>
relocate(`No MI`, .after = MI)
totals =
oc_mi |>
summarize(across(c(MI, `No MI`, Total), sum)) |>
mutate(OC = "Total")
tbl_oc_mi = bind_rows(oc_mi, totals)
tbl_oc_mi
```
:::::
---
::::{#exr-probs}
Review: estimate the probabilities of MI for OC users and non-OC users in @exm-oc-mi.
::::
----
:::{.solution}
```{r, include = FALSE}
#| label: calc-prs
p_MI_OC = 13/5000
p_MI_nOC = 7/10000
```
$$\ph(MI|OC) = \frac{13}{5000} = `r p_MI_OC`$$
$$\ph(MI|\neg OC) = \frac{7}{10000} = `r p_MI_nOC`$$
:::
---
#### Controls
::::{.callout-note}
##### Two meanings of "controls"
Depending on context, "controls" can mean either individuals who don't experience an *exposure* of interest, or individuals who don't experience an *outcome* of interest.
::::
---
:::{#def-cases-retrospective}
##### cases and controls in retrospective studies
In *retrospective studies*, participants who experience the outcome of interest are called **cases**, while participants who don't experience that outcome are called **controls**.
:::
---
:::{#def-cases-prospective}
##### treatment groups and control groups in prospective studies
In *prospective studies*, the group of participants who experience the treatment or exposure of interest is called the **treatment group**, while the participants who receive the baseline or comparison treatment (for example, clinical trial participants who receive a placebo or a standard-of-care treatment rather than an experimental treatment) are called **controls**.
:::
## Comparing Probabilities
### Risk differences
::: notes
The simplest comparison of two probabilities, $\pi_1$, and $\pi_2$, is the difference of their values:
:::
:::{#def-RD}
#### Risk difference
The **risk difference** of two probabilities, $\pi_1$, and $\pi_2$, is the difference of their values:
$$\delta(\pi_1,\pi_2) \eqdef \pi_1 - \pi_2$$
:::
---
:::{#exm-RD}
#### Difference in MI risk
In @exm-oc-mi, the maximum likelihood estimate of the difference in MI risk between OC users and OC non-users is:
```{r, include = FALSE}
#| label: compute-risk-diff
rd_OC = p_MI_OC - p_MI_nOC
```
$$
\begin{aligned}
\hat\delta(\pi(OC), \pi(\neg OC))
&= \delta(\hat\pi(OC), \hat\pi(\neg OC))\\
&= \hat\pi(OC) - \hat\pi(\neg OC)\\
&= `r p_MI_OC` - `r p_MI_nOC`\\
&= `r rd_OC`
\end{aligned}
$$
:::
---
### Risk ratios
:::{#def-RR}
### Relative risk ratios
:::: notes
The **relative risk** of probability $\pi_1$ compared to another probability $\pi_2$, also called the **risk ratio**, **relative risk ratio**, **probability ratio**, or **rate ratio**, is the ratio of those probabilities:
::::
$$\rho(\pi_1,\pi_2) = \frac{\pi_1}{\pi_2}$$
:::
---
:::{#exm-RR}
:::: notes
Above, we estimated that:
::::
$$\ph(MI|OC) = `r 13/5000`$$
$$\ph(MI|\neg OC) = `r 7/10000`$$
:::: notes
So we might estimate that the *relative risk* of MI for OC versus non-OC is:
::::
```{r}
#| label: compute-risk-ratio
rr = (13/5000)/(7/10000)
```
$$
\begin{aligned}
\hat\rho(OC, \neg OC)
&=\frac{\ph(MI|OC)}{\ph(MI|\neg OC)}\\
&= \frac{`r 13/5000`}{`r 7/10000`}\\
&= `r rr`
\end{aligned}
$$
:::: notes
We might summarize this result by saying that "the estimated probability of MI among OC users was `r rr` as high as the estimated probability among OC non-users.
::::
:::
---
### Relative risk differences
:::{#def-RRD}
#### Relative risk difference
:::: notes
Sometimes, we divide the risk difference by the comparison probability; the result is called the **relative risk difference**:
::::
$$\xi(\pi_1,\pi_2) \eqdef \frac{\delta(\pi_1,\pi_2)}{\pi_2}$$
:::
---
:::{#thm-rrd-vs-rr}
#### Relative risk difference equals risk ratio minus 1
$$\xi(\pi_1,\pi_2) = \rho(\pi_1,\pi_2) - 1$$
:::
---
::: proof
$$
\begin{aligned}
\xi(\pi_1,\pi_2)
&\eqdef \frac{\delta(\pi_1,\pi_2)}{\pi_2}
\\&= \frac{\pi_1-\pi_2}{\pi_2}
\\&= \frac{\pi_1}{\pi_2} - 1
\\&= \rho(\pi_1,\pi_2) - 1
\end{aligned}
$$
:::
---
### Changing reference groups in risk comparisons
:::: notes
Risk differences, risk ratios, and relative risk differences are defined by two probabilities, plus a choice of which probability is the **baseline** or **reference** probability (i.e., which probability is the subtrahend of the risk difference or the denominator of the risk ratio).
:::
$$\delta(\pi_2,\pi_1) = -\delta(\pi_1, \pi_2)$$
$$\rho(\pi_2,\pi_1) = \inv{\rho(\pi_1,\pi_2)}$$
$$\xi(\pi_2,\pi_1) = \inv{\xi(\pi_2,\pi_1) + 1} - 1$$
:::{#exr-change-ref-group}
Prove the relationships above.
:::
---
:::{#exm-ref}
#### Switching the reference group in a risk ratio
Above, we estimated that the risk ratio of OC versus non-OC is:
$$
\begin{aligned}
\rho(OC, \neg OC)
&= `r (13/5000)/(7/10000)`
\end{aligned}
$$
In comparison, the risk ratio for non-OC versus OC is:
$$
\begin{aligned}
\rho(\neg OC, OC)
&=\frac{\ph(MI|\neg OC)}{\ph(MI|OC)}\\
&= \frac{`r 7/10000`}{`r 13/5000`}\\
&= `r (7/10000)/(13/5000)`\\
&= \frac{1}{\rho(OC, \neg OC)}
\end{aligned}
$$
:::
## Odds and Odds Ratios
### Odds and probabilities
::: notes
In logistic regression, we will make use of a mathematically-convenient transformation of probability, called *odds*.
:::
:::{#def-odds}
#### Odds
The **odds** of an outcome $A$, which we will represent using $\odds$ ("omega"), is the probability that the outcome occurs, divided by the probability that it doesn't occur:
$$\odds(A) \eqdef \frac{\Pr(A)}{\Pr(\neg A)}$$
:::
---
:::{#thm-prob-to-odds}
If the probability of an outcome $A$ is $\Pr(A)=\pi$,
then the corresponding odds of $A$ is:
$$\oddsf{\pi} = \frac{\pi}{1-\pi}$$ {#eq-odds-probs}
:::
---
:::{.proof}
$$
\ba
\Pr(\neg A) &= 1 - \Pr(A)
\\ &= 1 - \pi
\ea
$$
$$
\ba
\tf \odds(A) &\eqdef \frac{\Pr(A)}{\Pr(\neg A)}
\\ &= \frac{\pi}{1-\pi}
\ea
$$
:::
---
::: notes
[Function @eq-odds-probs], which transforms probabilities into odds, can be called the **odds function**. @fig-odds-probs graphs the shape of this function.
:::
```{r}
#| code-fold: true
#| label: fig-odds-probs
#| fig-cap: "Odds versus probability"
odds = function(pi) pi / (1 - pi)
library(ggplot2)
ggplot() +
geom_function(
fun = odds,
arrow = arrow(ends = "last"),
mapping = aes(col = "odds function")
) +
xlim(0, .99) +
xlab("Probability") +
ylab("Odds") +
geom_abline(aes(
intercept = 0,
slope = 1,
col = "y=x"
)) +
theme_bw() +
labs(colour = "") +
theme(legend.position = "bottom")
```
---
:::{#exm-odds}
#### Computing odds from probabilities
In @exr-probs, we estimated that the probability of MI, given OC use, is $\pi(OC) \eqdef \Pr(MI|OC) = `r 13/5000`$. If this estimate is correct, then the odds of MI, given OC use, is:
```{r}
#| include: false
#| label: compute-odds-from-prob
pi_OC = 13/5000
odds_OC = odds(pi_OC)
```
$$
\begin{aligned}
\odds(OC)
&\eqdef \frac{\Pr(MI|OC)}{\Pr(\neg MI|OC)}\\
&=\frac{\Pr(MI|OC)}{1-\Pr(MI|OC)}\\
&=\frac{\pi(OC)}{1-\pi(OC)}\\
&=\frac{`r 13/5000`}{1-`r 13/5000`}\\
&\approx `r odds_OC |> round(digits = 6) |> as.character()`
\end{aligned}
$$
:::
---
:::{#exr-odds}
#### Computing odds from probabilities
Estimate the odds of MI, for non-OC users.
::::{.solution}
```{r, include = FALSE}
pi_nOC = 7 / 10000
odds_nOC = pi_nOC / (1 - pi_nOC)
```
$$
\odds(\neg OC) = `r odds_nOC`
$$
::::
:::
---
:::{#thm-est-odds}
#### One-sample MLE for odds
Let $X_1,...X_n$ be a set of $n$ $\iid$ Bernoulli trials, and let $X = \sumin X_i$ be their sum.
Then the maximum likelihood estimate of the odds of $X=1$, $\odds$, is:
$$
\hat{\odds}= \frac{x}{n-x}
$$
:::
---
::: proof
$$
\begin{aligned}
1-\hat\pi
&= 1-\frac{x}{n}\\
&= \frac{n}{n} - \frac{x}{n}\\
&= \frac{n - x}{n}
\end{aligned}
$$
Thus, the estimated odds is:
$$
\begin{aligned}
\frac{\hat\pi}{1-\hat\pi}
&= \frac{\left(\frac{x}{n}\right)}{\left(\frac{n-x}{n}\right)}\\
&= \frac{x}{n-x}
\end{aligned}
$$
:::: notes
That is, odds can be calculated directly as "# events" divided by "# nonevents" (without needing to calculate $\hat\pi$ and $1-\hat\pi$ first).
::::
:::
---
::::{#exm-odds-shortcut}
#### calculating odds using the shortcut
In @exm-odds, we calculated
$$
\begin{aligned}
\odds(OC)
&=`r (13/5000)/(1- (13/5000))`
\end{aligned}
$$
Let's recalculate this result using our shortcut.
::::
---
::::{#sol-odds-shortcut}
$$
\begin{aligned}
\odds(OC)
&=\frac{13}{5000-13}\\
&=`r (13)/(5000- 13)`
\end{aligned}
$$
Same answer as in @exm-odds!
::::
---
:::{#thm-odds-simplified}
#### Simplified expression for odds function
::: notes
An equivalent expression for the odds function is
:::
$$
\oddsf{\pi} = \invf{\invf{\pi}-1)}
$$ {#eq-odds-reduced}
:::
---
:::{#exr-odds2}
Prove that @eq-odds-reduced is equivalent to @def-odds.
:::
---
:::{#thm-deriv-odds}
#### Derivative of odds function
$$\doddsf{\pi} = \frac{1}{\sqf{1-\pi}}$$
:::
---
::: proof
$$
\ba
\doddsf{\pi}
&= \deriv{\pi}\paren{\frac{\pi}{1-\pi}}
\\ &= \frac {\deriv{\pi}\pi} {1-\pi} -
\paren{\frac{\pi}{\sqf{1-\pi}} \cd \deriv{\pi}\paren{1-\pi}}
\\ &= \frac{1}{1-\pi} - \frac{\pi}{\sqf{1-\pi}} \cd (-1)
\\ &= \frac{1}{1-\pi} + \frac{\pi}{\sqf{1-\pi}}
\\ &= \frac{1-\pi}{\sqf{1-\pi}} + \frac{\pi}{\sqf{1-\pi}}
\\ &= \frac{1-\pi + \pi}{\sqf{1-\pi}}
\\ &= \frac{1}{\sqf{1-\pi}}
\ea
$$
:::
---
#### Odds of rare events
For rare events (small $\pi$), odds and probabilities are nearly equal, because $1-\pi \approx 1$ (see @fig-odds-probs).
For example, in @exm-odds, the probability and odds differ by $`r abs(pi_OC - odds_OC)`$.
---
:::{#exr-odds-probs}
What odds value corresponds to the probability $\pi = 0.2$, and what is the numerical difference between these two values?
:::
---
::::{.solution}
$$
\odds = \frac{\pi}{1-\pi}
=\frac{.2}{.8}
= .25
$$
::::
---
:::{#thm-odds-minus-probs}
Let $\odds = \frac{\pi}{1-\pi}$. Then:
$$\odds - \pi = \odds \cd \pi$$
:::
---
:::{#exr-odds-minus-probs}
Prove @thm-odds-minus-probs.
:::
---
::: solution
$$
\ba
\odds - \pi
&= \frac{\pi}{1-\pi} - \pi
\\ &= \frac{\pi}{1-\pi} - \frac{\pi(1-\pi)}{1-\pi}
\\ &= \frac{\pi}{1-\pi} - \frac{\pi - \pi^2}{1-\pi}
\\ &= \frac{\pi - (\pi - \pi^2)}{1-\pi}
\\ &= \frac{\pi - \pi + \pi^2}{1-\pi}
\\ &= \frac{\pi^2}{1-\pi}
\\ &= \frac{\pi}{1-\pi} \pi
\\ &= \odds \pi
\ea
$$
:::
---
:::{#lem-odds-neg}
#### Odds of a non-event
If $\pi$ is the odds of event $A$
and $\odds$ is the corresponding odds of $A$,
then the odds of $\neg A$ are:
$$
\odds(\neg A) = \frac{1-\pi}{\pi}
$$
:::
::: proof
Left to the reader.
:::
---
### The inverse odds function
:::{#def-inv-odds}
##### inverse odds function
The **inverse odds function**,
$$\invoddsf{\odds} \eqdef \frac{\odds}{1 + \odds}$$
converts odds into their corresponding probabilities (@fig-inv-odds).
:::
::: notes
The inverse-odds function takes an odds as input and produces a probability as output. Its domain of inputs is $[0,\infty)$ and its range of outputs is $[0,1]$.
I haven't seen anyone give the inverse-odds function a concise name; maybe $\text{prob}()$?
:::
---
```{r}
#| label: fig-inv-odds
#| fig-cap: The inverse odds function, $\invoddsf{\odds}$
odds_inv = function(omega) (1 + omega^-1)^-1
ggplot() +
geom_function(fun = odds_inv, aes(col = "inverse-odds")) +
xlab("Odds") +
ylab("Probability") +
xlim(0,5) +
ylim(0,1) +
geom_abline(aes(intercept = 0, slope = 1, col = "x=y"))
```
---
:::{#exr-odds-probs}
What probability corresponds to an odds of $\odds = 1$, and what is the numerical difference between these two values?
:::
---
::::{.solution}
$$
\pi(1) = \frac{1}{1+1}
=\frac{1}{2}
= .5
$$
$$
1 - \pi(1) = 1 - .5 = .5
$$
::::
---
:::{#lem-invodds-simplified}
##### Simplified expression for inverse odds function
::: notes
An equivalent expression for the inverse odds function is
:::
$$
\pi(\odds) = (1+\odds^{-1})^{-1}
$$ {#eq-inv-odds-reduced}
:::
---
:::{#exr-inv-odds2}
Prove that @eq-inv-odds-reduced is equivalent to @def-inv-odds.
:::
---
:::{#lem-one-minus-odds-inv}
#### One minus inverse-odds
$$1 - \oddsinvf{\odds} = \frac{1}{1+\odds}$$
:::
---
::: {.proof}
$$
\ba
1 - \oddsinvf{\odds} &= 1 - \frac{\odds}{1 + \odds}
\\ &= \frac{\red{1+\odds}}{1 + \odds} - \frac{\blue{\odds}}{1 + \odds}
\\ &= \frac{\red{(1+\odds)} - \blue{\odds}}{1 + \odds}
\\ &= \frac{1 + \odds - \odds}{1 + \odds}
\\ &= \frac{1}{1 + \odds}
\ea
$$
:::
---
:::{#thm-inverse-odds-nonevent}
If $\odds$ is the odds of event $A$,
then the probability that $A$ does not occur is:
$$\Pr(\neg A) = \frac{1}{1+\odds}$$
:::
---
::: proof
:::: notes
Use @lem-one-minus-odds-inv:
::::
$$
\ba
\Pr(\neg A)
&= 1 - \Pr(A)
\\ &= 1 - \oddsinvf{\odds}
\\ &= \frac{1}{1 + \odds}
\ea
$$
:::
---
:::{#thm-deriv-invodds}
##### Derivative of inverse odds function
$$\doddsinvf{\odds} = \frac{1}{\sqf{1+\odds}}$$
:::
---
::: proof
:::: notes
Use the quotient rule:
::::
$$
\ba
{\oddsinv}'(\odds)
&= \deriv{\odds} \oddsinvf{\odds}
\\ &= \deriv{\odds} \frac{\odds}{1+\odds}
\\ &= \frac{\deriv{\odds}\odds}{1+\odds} - \frac{\odds}{\sqf{1+\odds}} \cd \deriv{\odds}(1+\odds)
\\ &= \frac{1}{1+\odds} - \frac{\odds}{\sqf{1+\odds}} \cd 1
\\ &= \frac{1}{1+\odds} - \frac{\odds}{\sqf{1+\odds}}
\\ &= \frac{1+\odds}{\sqf{1+\odds}} - \frac{\odds}{\sqf{1+\odds}}
\\ &= \frac{1+\odds - \odds}{\sqf{1+\odds}}
\\ &= \frac{1}{\sqf{1+\odds}}
\ea
$$
:::
---
:::{#cor-deriv-invodds}
$$\doddsinvf{\odds} = \sqf{1 - \oddsinvf{\odds}}$$
:::
---
### Odds ratios
::: notes
Now that we have defined odds, we can introduce another way of comparing event probabilities: odds ratios.
:::
:::{#def-OR}
##### Odds ratio
The **odds ratio** for two odds $\odds_1$, $\odds_2$ is their ratio:
$$\theta(\odds_1, \odds_2) = \frac{\odds_1}{\odds_2}$$
:::
---
:::{#exm-OR}
#### Calculating odds ratios
In @exm-oc-mi, the odds ratio for OC users versus OC-non-users is:
$$
\begin{aligned}
\theta(\odds(OC), \odds(\neg OC))
&= \frac{\odds(OC)}{\odds(\neg OC)}\\
&= \frac{`r pi_OC`}{`r pi_nOC`}\\
&= `r pi_OC / pi_nOC`\\
\end{aligned}
$$
:::
---
#### A shortcut for calculating odds ratio estimates {.smaller}
::: notes
The general form of a two-by-two table is shown in @tbl-2x2-generic.
| | Event | Non-Event | Total
|-------------- | ------ | ---------- | -----
|Exposed | a | b | a+b
|Non-exposed | c | d | c+d
|Total | a+c | b+d | a+b+c+d
: A generic 2x2 table {#tbl-2x2-generic}
:::
::: notes
From this table, we have:
:::
* $\hat\pi(Event|Exposed) = a/(a+b)$
* $\hat\pi(\neg Event|Exposed) = b/(a+b)$
* $\hat\odds(Event|Exposed) = \frac{\left(\frac{a}{a+b}\right)}{\left(\frac{b}{a+b}\right)}=\frac{a}{b}$
* $\hat\odds(Event|\neg Exposed) = \frac{c}{d}$
(see @exr-odds-generic)
* $\theta(Exposed,\neg Exposed) = \frac{\frac{a}{b}}{\frac{c}{d}} = \frac{ad}{bc}$
---
:::{#exr-odds-generic}
Given @tbl-2x2-generic, show that $\hat\odds(Event|\neg Exposed) = \frac{c}{d}$.
:::
---
#### Properties of odds ratios {#sec-OR-props}
:::: notes
Odds ratios have a special property: we can swap a covariate with the outcome, and the odds ratio remains the same.
::::
:::{#thm-or-swap}
##### Odds ratios are reversible
For any two events $A$, $B$:
$$\theta(A|B) = \theta(B|A)$$
:::
---
::: {.proof}
{{< include prf-OR-reversible.qmd >}}
:::
---
:::{#exm-or-inv-MI}
In @exm-oc-mi, we have:
$$
\begin{aligned}
\theta(MI; OC)
&\eqdef
\frac{\odds(MI|OC)}{\odds(MI|\neg OC)}\\
&\eqdef \frac
{\left(\frac{\Pr(MI|OC)}{\Pr(\neg MI|OC)}\right)}
{\left(\frac{\Pr(MI|\neg OC)}{\Pr(\neg MI|\neg OC)}\right)}\\
&= \frac
{\left(\frac{\Pr(MI,OC)}{\Pr(\neg MI,OC)}\right)}
{\left(\frac{\Pr(MI,\neg OC)}{\Pr(\neg MI,\neg OC)}\right)}\\
&= \left(\frac{\Pr(MI,OC)}{\Pr(\neg MI,OC)}\right)
\left(\frac{\Pr(\neg MI,\neg OC)}{\Pr(MI,\neg OC)}\right)\\
&= \left(\frac{\Pr(MI,OC)}{\Pr(MI,\neg OC)}\right)
\left(\frac{\Pr(\neg MI,\neg OC)}{\Pr(\neg MI,OC)}\right)\\
&= \left(\frac{\Pr(OC,MI)}{\Pr(\neg OC,MI)}\right)
\left(\frac{\Pr(\neg OC,\neg MI)}{\Pr(OC,\neg MI)}\right)\\
&= \left(\frac{\Pr(OC|MI)}{\Pr(\neg OC|MI)}\right)
\left(\frac{\Pr(\neg OC|\neg MI)}{\Pr(OC|\neg MI)}\right)\\
&= \frac{\left(\frac{\Pr(OC|MI)}{\Pr(\neg OC|MI)}\right)}
{\left(\frac{\Pr(OC|\neg MI)}{\Pr(\neg OC|\neg MI)}\right)}\\
&\eqdef \frac{\odds(OC|MI)}
{\odds(OC|\neg MI)}\\
&\eqdef \theta(OC; MI)
\end{aligned}
$$
:::
---
:::{#exr-2x2-generic}
For @tbl-2x2-generic, show that $\hat\theta(Exposed, Unexposed) = \hat\theta(Event, \neg Event)$.
:::