-
Notifications
You must be signed in to change notification settings - Fork 1
/
Regression_modelling.html
1116 lines (897 loc) · 35.1 KB
/
Regression_modelling.html
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Regression Modelling in R</title>
<meta charset="utf-8" />
<meta name="author" content="Chris Mainey - [email protected]" />
<script src="libs/header-attrs-2.25/header-attrs.js"></script>
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/panelset-0.2.6/panelset.css" rel="stylesheet" />
<script src="libs/panelset-0.2.6/panelset.js"></script>
<link rel="stylesheet" href="nhsr-theme/css/nhsr.css" type="text/css" />
<link rel="stylesheet" href="nhsr-theme/css/nhsr-fonts.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: title-slide, left, bottom
<div>
<style type="text/css">.xaringan-extra-logo {
width: 110px;
height: 128px;
z-index: 0;
background-image: url(nhsr-theme/img/logo-nhs-blue.png);
background-size: contain;
background-repeat: no-repeat;
position: absolute;
top:1em;right:1em;
}
</style>
<script>(function () {
let tries = 0
function addLogo () {
if (typeof slideshow === 'undefined') {
tries += 1
if (tries < 10) {
setTimeout(addLogo, 100)
}
} else {
document.querySelectorAll('.remark-slide-content:not(.title-slide):not(.inverse):not(.hide-logo)')
.forEach(function (slide) {
const logo = document.createElement('div')
logo.classList = 'xaringan-extra-logo'
logo.href = null
slide.appendChild(logo)
})
}
}
document.addEventListener('DOMContentLoaded', addLogo)
})()</script>
</div>
# Regression Modelling in `R`
----
## **A bit of theory and application**
### Chris Mainey - [email protected]
### <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:#005EB8;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg>@chrismainey
#### 24/11/2022
---
# Workshop Overview
- Correlation
- Linear Regression
- Specifying a model with `lm`
- Interpreting the model output
- Assessing model fit
- Multiple Regression
- Prediction
- Generalized Linear Models using `glm`
- Logistic Regression
<br>
___Mixture of theory, examples and practical exercises___
---
# Relationships between variables
If two variables are related, we usually describe them as 'correlated'.
<br>
Usually interested in "strength" and "direction" of association
--
<br><br>
Two analysis techniques commonly used to investigate:
+ ___Correlation:___ shows direction, and strength of association
+ ___Regression:___ estimate how one (or more) variable(s) change in relation to each other. Usually:
+ `\(y\)` (the variable we're interested in) is the "dependent variable" or "outcome"
+ `\(x\)` (or more than one, `\(x_{i}\)`) as the "independent variables" or "predictors"
--
<br>
Sometimes the effects of other variables interact/mask this (___"confounding"___)
---
## Example:
<img src="Regression_modelling_files/figure-html/lm1-1.png" style="display: block; margin: auto;" />
---
# Correlation
+ Measured with a correlation coefficient ('Pearson' is the most common)
+ Range:
+ __-1 to 1:__ Perfect negative to Perfect positive Correlation
+ __0:__ No Correlation
--
<center>
<img src="https://upload.wikimedia.org/wikipedia/commons/d/d4/Correlation_examples2.svg" height="300" class="center">
</center>
.footnote[
Graphic from:
Wikipedia: [Correlation and dependence:](https://en.wikipedia.org/wiki/Correlation_and_dependence)
By DenisBoigelot, https://commons.wikimedia.org/w/index.php?curid=15165296 [Accessed 24 Sept 2019]
]
---
# Correlation in R
Lets check the correlation in our generated data:
```r
cor(x, y)
## [1] 0.8650106
cor.test(x,y)
##
## Pearson's product-moment correlation
##
## data: x and y
## t = 11.944, df = 48, p-value = 5.53e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.7727115 0.9214882
## sample estimates:
## cor
## 0.8650106
```
+ `cor.test` is a correlation and a t-test.
+ Different types of correlation coefficient, default is 'Pearson'
+ Doesn't work for different distributions, data types or more variables
---
### Regression models (1)
Regression gives us more options than correlation:
<img src="Regression_modelling_files/figure-html/lm3-1.png" width="1770" style="display: block; margin: auto;" />
`$$y= \alpha + \beta x + \epsilon$$`
---
### Regression models (2)
Zooming in...
<img src="Regression_modelling_files/figure-html/lm35-1.png" width="1770" style="display: block; margin: auto;" />
---
## Regression equation
`$$\large{y= \alpha + \beta_{i} x_{i} + \epsilon}$$`
<br>
.pull-left[
+ `\(y\)` - is our 'outcome', or 'dependent' variable
+ `\(\alpha\)` - is the 'intercept', the point where our line crosses y-axis
+ `\(\beta\)` - is a coefficient (weight) applied to `\(x\)`
+ `\(x\)` - is our 'predictor', or 'independent' variable
+ `\(i\)` - is our index, we can have `\(i\)` predictor variables, each with a coefficient
+ `\(\epsilon\)` - is the remaining ('residual') error
]
--
.pull-right[
We are making some assumptions:
+ Linear relations
+ Data points are independent (not correlated)
+ Normally distributed error
+ Homoskedastic (error doesn't vary across the range)
]
---
### Ordinary Least Squares 'OLS'
+ 'Residual' distance between prediction and data point ( `\(\epsilon\)` ).
--
+ Sum would be zero, so we 'square' (^2) it, and minimise the _'sum of the squares'_
--
<img src="Regression_modelling_files/figure-html/lm2-1.png" style="display: block; margin: auto;" />
---
## Regression models (3)
So now let's create a linear regression model. I prefer to create them as objects so I can use them again later. Let's call this one _model1_. That's fairly bad naming, but oh well....
<br><br><br>
Let's say we have a data.frame called _mydata_ and columns called _Y_ (that we are predicting) using a column called _X_
```r
model1 <- lm(Y ~ X, data = mydata)
```
<br><br>
We can then use other methods on this object, like `print()`, `summary()`, `plot()` and `predict()`.
<br><br>
The next two slides show the output of the summary function and plot.
---
## `lm` summary
```
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.9575 -2.2614 0.4444 2.4475 4.1663
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.4776 1.1386 4.811 1.53e-05 ***
## x 1.2507 0.1047 11.944 5.53e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.77 on 48 degrees of freedom
## Multiple R-squared: 0.7482, Adjusted R-squared: 0.743
## F-statistic: 142.7 on 1 and 48 DF, p-value: 5.53e-16
```
--
+ We can test fit using f-tests, prediction error, or the R<sup>2</sup> (the proportion of variation in `\(y\)`, explained by `\(x\)`).
---
## Interpretation (1)
So how do we interpret the output?
+ The intercept `\((\alpha)\)` = 5.48
+ The coefficient `\((\beta)\)` for `\(x\)` = 1.25
___"For each increase of 1 in `\(x\)`, `\(y\)` increases by 1.25, starting at 5.48."___
--
<br><br>
A common addition is to __"mean-centre and scale"__ our variables. So `\(x\)` becomes:
$$ \frac{(x - \bar{x})}{\sigma_x} $$
```r
model1_scaled <- lm(Y ~ scale(X), data = mydata)
```
---
## Interpretation (2)
.panelset[
.panel[.panel-name[Original]
```
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.9575 -2.2614 0.4444 2.4475 4.1663
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.4776 1.1386 4.811 1.53e-05 ***
## x 1.2507 0.1047 11.944 5.53e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.77 on 48 degrees of freedom
## Multiple R-squared: 0.7482, Adjusted R-squared: 0.743
## F-statistic: 142.7 on 1 and 48 DF, p-value: 5.53e-16
```
]
.panel[.panel-name[Scaled]
```
##
## Call:
## lm(formula = y ~ scale(x))
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.9575 -2.2614 0.4444 2.4475 4.1663
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 18.2469 0.3917 46.59 < 2e-16 ***
## scale(x) 4.7256 0.3956 11.94 5.53e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.77 on 48 degrees of freedom
## Multiple R-squared: 0.7482, Adjusted R-squared: 0.743
## F-statistic: 142.7 on 1 and 48 DF, p-value: 5.53e-16
```
]
.panel[.panel-name[Interpretation]
<br><br>
This converts our interpretation:
+ The intercept becomes the average `\(y\)` value
+ `\(\beta\)` becomes the change in `\(y\)` for one standard deviation increase in `\(x\)`
<br>
So...
+ Average `\(x\)` is 18.24.69
+ Each increase of 1 standard deviation in `\(x\)` increase `\(y\)` by 4.7256
]
]
---
## Regression diagnostics (1)
A common check is to plot residuals:
![](Regression_modelling_files/figure-html/rplotsetup-1.png)<!-- -->
---
## Regression diagnostics (2)
A common check is to plot residuals:
![](Regression_modelling_files/figure-html/rplotsetup2-1.png)<!-- -->
---
class: center, middle
## Exercise 1: Linear regression with a single predictor
---
# More than one predictor?
Our plots in earlier slides make sense in 2 dimensions, but regression is not limited to this.
--
<br><br>
If we add more predictors, our interpretation of each coefficient becomes: <br>
+ ___"The change in `\(y\)` whilst holding all others parameters constant"___
<br><br>
We can add more predictors with the `+`:
```r
lm(y ~ x1 + x2 + x3 + xi)
```
---
## Categorical variables
How do we enter categorical variables into a model?
+ Models won't understand text, and numbers are numeric, so we use `factor` variables?
--
<br>
`Factors` are 'dummy coded:'
+ _'pivotted' to binary columns_
+ Contain a reference level: with categories: "A", "B" & "C", we get:
```
## (Intercept) CategoryB CategoryC
## 1 1 0 0
## 2 1 1 0
## 3 1 0 1
## 4 1 0 1
## 5 1 0 0
## 6 1 1 0
## attr(,"assign")
## [1] 0 1 1
## attr(,"contrasts")
## attr(,"contrasts")$Category
## [1] "contr.treatment"
```
---
class: middle
## Exercise 2: Linear regression with multiple predictors
---
class: middle center
<div class="figure">
<img src="https://imgs.xkcd.com/comics/linear_regression.png" alt="https://xkcd.com/1725/" width="70%" />
<p class="caption">https://xkcd.com/1725/</p>
</div>
---
## What about non-linear data?
- Data are not necessarily linear. Death is binary, LOS is a count etc.
- We can use the __Generalized Linear Model (GLM)__:
`$$\large{g(\mu)= \alpha + \beta x}$$`
<br>
Where `\(\mu\)` is the _expectation_ of `\(Y\)`, and `\(g\)` is the link function
--
+ We assume a distribution from the [Exponential family](https://en.wikipedia.org/wiki/Exponential_family):
+ Binomial for binary, TRUE/FALSE, PASS/FAIL
+ Poisson for counts
--
+ The link function transforms the data before fitting a model
--
+ Can't use OLS for this, so we use 'maximum-likelihood' estimation, which is not exact.
--
+ Many of the methods, and `R` function, for `lm` are common to `glm`, but we can't use R<sup>2</sup>. Other measures include AUC ('C-statistic'), and AIC or likelihood ratio tests.
---
## Generalized Linear Models
Let's model the probably of death in a data set from US Medicaid.
+ The data are in the `COUNT` package, and are called `medpar`
+ Load the library and use the `data()` function to load it.
--
+ We'll use a `glm`, with a `binomial` distribution.
+ The `binomial` family automatically uses the `logit` link function: the log-odds of the event.
```r
library(COUNT)
data(medpar)
glm_binomial <- glm(died ~ factor(age80) + los + factor(type), data=medpar, family="binomial")
ModelMetrics::auc(glm_binomial)
## [1] 0.6372224
```
---
```r
summary(glm_binomial)
##
## Call:
## glm(formula = died ~ factor(age80) + los + factor(type), family = "binomial",
## data = medpar)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.590949 0.097351 -6.070 1.28e-09 ***
## factor(age80)1 0.656493 0.129180 5.082 3.73e-07 ***
## los -0.037483 0.007871 -4.762 1.92e-06 ***
## factor(type)2 0.418704 0.144611 2.895 0.00379 **
## factor(type)3 0.961028 0.230489 4.170 3.05e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1922.9 on 1494 degrees of freedom
## Residual deviance: 1857.8 on 1490 degrees of freedom
## AIC: 1867.8
##
## Number of Fisher Scoring iterations: 4
```
---
## Interactions
+ 'Interactions' are where predictor variables affect each other.
+ Allows us to separate effects into
+ Can add using `*` or `:` (check help for which to use)
--
```r
glm_binomial2 <- glm(died ~ factor(age80) * los + factor(type), data=medpar, family="binomial")
ModelMetrics::auc(glm_binomial2)
## [1] 0.6376572
```
---
```r
summary(glm_binomial2)
##
## Call:
## glm(formula = died ~ factor(age80) * los + factor(type), family = "binomial",
## data = medpar)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.561604 0.104479 -5.375 7.65e-08 ***
## factor(age80)1 0.525379 0.207818 2.528 0.01147 *
## los -0.040738 0.008995 -4.529 5.93e-06 ***
## factor(type)2 0.417439 0.144681 2.885 0.00391 **
## factor(type)3 0.964771 0.231118 4.174 2.99e-05 ***
## factor(age80)1:los 0.014507 0.017954 0.808 0.41909
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1922.9 on 1494 degrees of freedom
## Residual deviance: 1857.2 on 1489 degrees of freedom
## AIC: 1869.2
##
## Number of Fisher Scoring iterations: 4
```
---
# Interpretation
+ Our model coefficients in `lm` were straight-forward multipliers
+ `glm` is similar, but it is on the scale of the ___link-function___.
+ log scale for `poisson` models, or logit (log odds) scale for `binomial`
+ It is common to transform outputs back to the original ('response') scale.
+ This gives Incident Rate Ratios for `poisson`, or Odds Ratios for `binomial`.
```r
cbind(Link=coef(glm_binomial2), Response=exp(coef(glm_binomial2)))
```
```
## Link Response
## (Intercept) -0.56160376 0.5702937
## factor(age80)1 0.52537865 1.6910991
## los -0.04073769 0.9600809
## factor(type)2 0.41743940 1.5180694
## factor(type)3 0.96477095 2.6241865
## factor(age80)1:los 0.01450661 1.0146123
```
---
# Odds-what-now?
Odds is a concept commonly used in statistics, but often misunderstood. Lets' consider a '2 x 2 table':
<img src="./man/figures/2_by_2.png" width="60%" style="display: block; margin: auto;" />
.pull-left[
__Relative risk:__
+ _a / (a + b)_
+ _d / (c + d)_
]
.pull-right[
__Odds:__
+ _a / b_
+ _c / d_
]
---
# Odds Ratio
.pull-left[
<img src="./man/figures/2_by_2_pt2.png" width="100%" style="display: block; margin: auto;" />
]
.pull-right[
__Odds Ratio:__
+ _<span style="color: red;">(a / b)</span> / <span style="color: blue;">( c / d)</span>_
+ _= <span style="color: red;">a</span><span style="color: blue;">d</span> / <span style="color: blue;">c</span><span style="color: red;">b</span>_
<br><br>
+ If odds ratio = 1, chance of outcome the same in each group
+ If odds ratio >1 - greater chance of outcome in exposure group
+ If odds ratio <1 - lesser chance of outcome in exposure group
]
<br><br>
Great explainer: https://www.youtube.com/watch?v=ixKhS0Silb4
---
class: middle
## Exercise 3: Generalized Linear Model (GLM)
---
## Prediction (1)
- We can then use our model to predict our expected `\(Y\)`:
- Need to decide what scale to predict on: `link` or `response`
```r
library(dplyr)
medpar$preds <- predict(glm_binomial2, type="response")
top_n(medpar,5) %>% knitr::kable(format = "html")
```
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:right;"> los </th>
<th style="text-align:right;"> hmo </th>
<th style="text-align:right;"> white </th>
<th style="text-align:right;"> died </th>
<th style="text-align:right;"> age80 </th>
<th style="text-align:right;"> type </th>
<th style="text-align:right;"> type1 </th>
<th style="text-align:right;"> type2 </th>
<th style="text-align:right;"> type3 </th>
<th style="text-align:left;"> provnum </th>
<th style="text-align:right;"> preds </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> 558 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030017 </td>
<td style="text-align:right;"> 0.7114250 </td>
</tr>
<tr>
<td style="text-align:left;"> 919 </td>
<td style="text-align:right;"> 5 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030061 </td>
<td style="text-align:right;"> 0.6894160 </td>
</tr>
<tr>
<td style="text-align:left;"> 1464 </td>
<td style="text-align:right;"> 2 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032000 </td>
<td style="text-align:right;"> 0.7060100 </td>
</tr>
<tr>
<td style="text-align:left;"> 1486 </td>
<td style="text-align:right;"> 5 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032002 </td>
<td style="text-align:right;"> 0.6894160 </td>
</tr>
<tr>
<td style="text-align:left;"> 1488 </td>
<td style="text-align:right;"> 4 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032002 </td>
<td style="text-align:right;"> 0.6950045 </td>
</tr>
</tbody>
</table>
---
## Prediction (2)
- Lets see the 10 cases with the highest predicted risk of death:
```r
medpar %>% arrange(desc(preds)) %>% top_n(10) %>% knitr::kable(format = "html")
```
<table>
<thead>
<tr>
<th style="text-align:left;"> </th>
<th style="text-align:right;"> los </th>
<th style="text-align:right;"> hmo </th>
<th style="text-align:right;"> white </th>
<th style="text-align:right;"> died </th>
<th style="text-align:right;"> age80 </th>
<th style="text-align:right;"> type </th>
<th style="text-align:right;"> type1 </th>
<th style="text-align:right;"> type2 </th>
<th style="text-align:right;"> type3 </th>
<th style="text-align:left;"> provnum </th>
<th style="text-align:right;"> preds </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> 558 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030017 </td>
<td style="text-align:right;"> 0.7114250 </td>
</tr>
<tr>
<td style="text-align:left;"> 1464 </td>
<td style="text-align:right;"> 2 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032000 </td>
<td style="text-align:right;"> 0.7060100 </td>
</tr>
<tr>
<td style="text-align:left;"> 1488 </td>
<td style="text-align:right;"> 4 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032002 </td>
<td style="text-align:right;"> 0.6950045 </td>
</tr>
<tr>
<td style="text-align:left;"> 919 </td>
<td style="text-align:right;"> 5 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030061 </td>
<td style="text-align:right;"> 0.6894160 </td>
</tr>
<tr>
<td style="text-align:left;"> 1486 </td>
<td style="text-align:right;"> 5 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032002 </td>
<td style="text-align:right;"> 0.6894160 </td>
</tr>
<tr>
<td style="text-align:left;"> 955 </td>
<td style="text-align:right;"> 6 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030061 </td>
<td style="text-align:right;"> 0.6837716 </td>
</tr>
<tr>
<td style="text-align:left;"> 896 </td>
<td style="text-align:right;"> 9 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030061 </td>
<td style="text-align:right;"> 0.6665153 </td>
</tr>
<tr>
<td style="text-align:left;"> 941 </td>
<td style="text-align:right;"> 9 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030061 </td>
<td style="text-align:right;"> 0.6665153 </td>
</tr>
<tr>
<td style="text-align:left;"> 1084 </td>
<td style="text-align:right;"> 10 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 030069 </td>
<td style="text-align:right;"> 0.6606596 </td>
</tr>
<tr>
<td style="text-align:left;"> 1482 </td>
<td style="text-align:right;"> 11 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:right;"> 3 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 0 </td>
<td style="text-align:right;"> 1 </td>
<td style="text-align:left;"> 032000 </td>
<td style="text-align:right;"> 0.6547543 </td>
</tr>
</tbody>
</table>
---
class: middle
# Exercise 4: Predicting from models
---
# Summary
- Correlation shows the direction and strength of association
- Regression allows us to quantify the relationships
--
- We can use a single, or multiple, predictors
- Regression coefficients explain how much a change in `\(x\)` affects `\(y\)`
--
- R<sup>2</sup> is a common measure of in linear models, C-statistic/AUC/ROC in logistic models
--
- Generalized Linear Model (`glm`) allow linear models on a transformed scale, e.g. logistic regression for binary variables
- Interactions terms allow us to examine confounded predictors
--
- Consider back-transforming GLM coefficients for interpretability (e.g. odds ratios)
--
- We can predict from our model objects, but must remember the link-function scale in `glm`
---
class: middle
# Exercise 5: Predicting 10-year CHD risk in Framingham data
---
class: middle center
<div class="figure">
<img src="https://imgs.xkcd.com/comics/correlation.png" alt="https://xkcd.com/552/" width="70%" />
<p class="caption">https://xkcd.com/552/</p>
</div>
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"ratio": "16:9",
"highlightStyle": "github",
"highlightLines": true,
"highlightLanguage": "r",
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");