-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides-mlm1.qmd
591 lines (382 loc) · 13.2 KB
/
slides-mlm1.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
---
title: "Multilevel Modelling Part I - Introduction"
subtitle: "HDAT9700 Statistical Modelling II"
author: Mark Hanly
execute:
echo: false
format:
revealjs:
chalkboard: true
preview-links: auto
logo: images/Landscape__1.Col_Pos_CBDRH.png
footer: "© UNSW 2022"
slide-number: c/t
theme: ["theme-hdat9700.scss"]
title-slide-attributes:
data-background-image: images/galaxy.jpeg
data-background-size: contain
---
## Outline
::: incremental
- **Part 1** Motivating examples
- **Part 2** Multilevel data structures
- **Part 3** Multilevel models
- **Part 4** Illustration with Shiny app
:::
## **Part 1.** Motivating examples
::: section-break
![](https://media3.giphy.com/media/3oxRmsoHngzymwDl2E/giphy.gif?cid=790b7611f2d9999351ebb3de3038b66f36d4553021440699&rid=giphy.gif&ct=g)
:::
## {background-image="images/slides/mlm/cartwright-2012.png" background-size="contain"}
## Pre-reading {.smaller}
### Key points
::: incremental
- In 1999, UK solicitor Sally Clarke was **wrongly convicted** of killing her two infant sons (who died 13 months apart)
- She ultimately served over **three years in prison** before her conviction was overturned
- The defence argued that the probability of two deaths was 1 in 73 million
- Based on an estimated probability of Sudden Infant Death for each child of around 1 in 8500 (1/8500 X 1/8500 \~= 1/73000000)
:::
## Pre-reading
> The most fundamental error in Meadow's testimony was that he treated the two deaths as occurring independently
. . .
**Question** Why might the two deaths be dependent?
## Pre-reading
### Example: Health anxiety and GP visits
::: columns
::: {.column width="60%"}
![](images/slides/mlm/cartwright1a-2012.png){width="80%"}
:::
::: {.column .fragment width="40%"}
Assumes 60 independent observations
:::
:::
## Pre-reading
### Example: Health anxiety and GP visits
::: columns
::: {.column width="60%"}
![](images/slides/mlm/cartwright1b-2012.png){width="80%"}
:::
::: {.column .fragment width="40%"}
15 participants contribute data at 4 time points
:::
:::
## An example closer to home
::: columns
::: {.column width="50%"}
![](images/slides/mlm/lujic-2012.png){width="100%"}
::: {style="font-size: 0.5em; color: grey;"}
Lujic et al (2014). [Variation in the recording of common health conditions in routine hospital data](http://dx.doi.org/10.1136/bmjopen-2014-005768). BMJ Open.
:::
:::
::: {.column .incremental width="50%"}
#### Three research questions
1. Does admin data under-report morbidities?
2. Does this vary by hospital?
3. What patient and hospital factors predict this?
:::
:::
## Data structure {.smaller}
| Hospital ID | Person ID | Diabetes (self-report) | Diabetes (admin) | Agree | Education | Hospital type |
|-----------|-----------|-----------|-----------|-----------|-----------|-----------|
| A | 101 | 1 | 1 | yes | Yr 10 | Public |
| A | 102 | 0 | 0 | yes | Yr 12 | Public |
| A | 103 | 1 | 0 | no | Yr 12 | Public |
| A | 201 | 1 | 1 | yes | Degree | Private |
| A | 202 | 0 | 1 | no | Degree | Private |
| A | 203 | 1 | 0 | no | Yr 12 | Private |
: Admitted Patients Data Collection linked to 45 and Up survey data
## Challenges in this analysis
::: incremental
1. Usual GLM ignores that patients attending the same hospital have similar characteristics
2. Can't add 313 dummy variables for each hospital in NSW (RQ2)
3. Or if we did, we couldn't also control for hospital-level variables, e.g. hospital type, hospital size.
:::
## The solution?
::: {.fragment .fade-down}
Multilevel models!
:::
::: incremental
- Appropriately reflect dependence in the data
- Address more interesting research questions
- Does quality of administrative data vary by hospital?
- What patient and hospital characteristics predict an outcome?
- Do patient and hospital characteristics interact to influence an outcome?
:::
## An example closer to home
::: columns
::: {.column width="30%"}
![](images/slides/mlm/lujic-2012.png){width="100%"}
::: {style="font-size: 0.5em; color: grey;"}
Lujic et al (2014). [Variation in the recording of common health conditions in routine hospital data](http://dx.doi.org/10.1136/bmjopen-2014-005768). BMJ Open.
:::
:::
::: {.column .incremental width="70%"}
#### Results
- Significant **between-hospital variation** was found (ranging from 8% of unexplained variation for diabetes to 22% for heart disease)
- Implications for comparisons involving heart disease based solely on hospital data
:::
:::
## **Part 2.** Multilevel data structures
::: section-break
![](https://media3.giphy.com/media/3oxRmsoHngzymwDl2E/giphy.gif?cid=790b7611f2d9999351ebb3de3038b66f36d4553021440699&rid=giphy.gif&ct=g)
:::
## Multilevel contexts
#### A two-level hierarchy with patient at level 1 and hospital at level 2
![](images/slides/mlm/context1.png)
## Multilevel contexts
#### A two-level hierarchy with observations at level 1 clustered within patients at level 2
![](images/slides/mlm/context2.png)
## Multilevel contexts
#### A three-level hierarchy with patients at level 1
![](images/slides/mlm/context3.png)
## Multilevel contexts
#### Multiple membership (Patients attend multiple hospitals)
![](images/slides/mlm/context4.png)
## Multilevel contexts
#### Cross-classification
![](images/slides/mlm/context5.png)
## **Part 3.** Multilevel models
::: section-break
![](https://media3.giphy.com/media/3oxRmsoHngzymwDl2E/giphy.gif?cid=790b7611f2d9999351ebb3de3038b66f36d4553021440699&rid=giphy.gif&ct=g)
:::
## {background-iframe="http://mfviz.com/hierarchical-models/"}
::: {style="font-size: 0.4em; color: grey; position: absolute; right: 0; bottom: 0;"}
[http://mfviz.com/hierarchical-models/](http://mfviz.com/hierarchical-models/){target="_blank"}
:::
## How is a multilvel model operationalised?
::: {.fragment .fade-down}
It's all about the **residuals**
:::
## Single level residuals {auto-animate="true"}
![](images/slides/mlm/residuals1.png)
## Single level residuals {auto-animate="true"}
![](images/slides/mlm/residuals1.png){width="50%"}
The residual is the difference between the observed value and the predicted value
. . .
$$
e_3 = y_3 - \hat{y_3}
$$
## Multilevel residuals {auto-animate="true"}
![](images/slides/mlm/residuals2.png)
## Multilevel residuals {auto-animate="true"}
![](images/slides/mlm/residuals2.png){width="50%"}
Decompose into **group-level** residuals & **within-group** residuals
$$
e_3 = u_2 + e_{12}
$$
## Multilevel residuals {.smaller}
*Both sets of residuals are assumed to come from a normal distribution*
#### Level 1 residuals
$$
e_{ij} \sim \text{N}(0,\sigma^2_{e})
$$
::: fragment
- Mean 0
- Fixed variance $\sigma^2_{e}$
:::
<br>
#### Level 2 residuals
$$
u_{0j} \sim \text{N}(0,\sigma^2_{u0})
$$
::: fragment
- Mean 0
- Fixed variance $\sigma^2_{u0}$
:::
## Aside
::: columns
:::{.column width=20%}
![](https://upload.wikimedia.org/wikipedia/commons/b/b1/Greek_lc_sigma1.png){fig-align='center'}
:::
:::{.column width=80%}
:::{.callout-tip}
## Mathematical notation
This little bad boy is a lower case sigma, the 18th letter of the Greek alphabet.
In statistics, $\sigma$ is conventionally used to represent the standard deviation. we will be talking a lot about the variance, which is the square of the standard deviation $\sigma^2$.
You may have also met $\sigma$'s big brother $\Sigma$, which looks different but is pronounced the same. $\Sigma$ is conventionally used to indicate the sum of a series of numbers.
:::
:::
:::
## Variance components model
*An empty two-level multilevel model*
$$
\begin{align*}
y_{ij} &= \beta_0 + u0_j + e_{ij} \\[10pt]
u_{0j} &\sim \text{N}(0,\sigma^2_{u0}) \\[10pt]
e_{ij} &\sim \text{N}(0,\sigma^2_{e})
\end{align*}
$$
## Variance components model
*These are the parameters we estimate*
$$
\begin{align*}
y_{ij} &= {\color{red}{\beta_0}} + u0_j + e_{ij} \\[10pt]
u_{0j} &\sim \text{N}(0,{\color{red}{\sigma^2_{u0}}}) \\[10pt]
e_{ij} &\sim \text{N}(0,{\color{red}{\sigma^2_{e}}})
\end{align*}
$$
## The Variance Partition Coefficient (VPC)
*The proportion of variance at level 2*
$$
VPC = \frac{\sigma^2_{u0}}{\sigma^2_{u0} + \sigma^2_{e}}
$$
. . .
> *"20% of variation in patient's outcome was attributable to the hospital they were admitted to"*
## Interpreting the VPC {auto-animate=true}
::: columns
::: {.column width="50%"}
#### A
![](images/slides/mlm/mlm8.png){width='80%'}
:::
::: {.column width="50%"}
#### B
![](images/slides/mlm/mlm7.png){width='80%'}
:::
:::
Which plot shows higher VPC, A or B?
## Interpreting the VPC {auto-animate=true}
::: columns
::: {.column width="50%"}
#### A
![](images/slides/mlm/mlm8.png){width='50%'}
:::
::: {.column width="50%"}
#### B
![](images/slides/mlm/mlm7.png){width='100%'}
:::
:::
The answer is Plot B - most of the variation in the data is attributable to the higher level, e.g. the groups.
## Random intercept model {auto-animate=true}
::: columns
::: {.column width="80%"}
![](images/slides/mlm/mlm10.png){width='80%'}
:::
::: {.column width="20%"}
:::
:::
## Random intercept model {auto-animate=true}
::: columns
::: {.column width="40%"}
![](images/slides/mlm/mlm10.png)
:::
::: {.column width="60%"}
$$
\begin{align*}
y_{ij} &= \beta_0 + \beta X_{ij}+ u0_j + e_{ij} \\[10pt]
u_{0j} &\sim \text{N}(0,\sigma^2_{u0}) \\[10pt]
e_{ij} &\sim \text{N}(0,\sigma^2_{e})
\end{align*}
$$
:::
:::
## Random intercept model {auto-animate=true}
::: columns
::: {.column width="40%"}
![](images/slides/mlm/mlm10.png)
:::
::: {.column width="60%"}
$$
\begin{align*}
y_{ij} &= {\color{red}{\beta_0}} + {\color{red}{\beta}}X_{ij} + u0_j + e_{ij} \\[10pt]
u_{0j} &\sim \text{N}(0,{\color{red}{\sigma^2_{u0}}}) \\[10pt]
e_{ij} &\sim \text{N}(0,{\color{red}{\sigma^2_{e}}})
\end{align*}
$$
:::
:::
::: {.incremental .smaller}
- One or more X covariates
- Covariates can be at level 1, level 2 or even an interaction between level 1 and level 2
- Group level lines are parallel
:::
## Random intercept - random slope model {auto-animate=true}
::: columns
::: {.column width="80%"}
![](images/slides/mlm/mlm11.png){width='80%'}
:::
::: {.column width="20%"}
:::
:::
## Random intercept - random slope model {auto-animate=true}
::: columns
::: {.column width="40%"}
![](images/slides/mlm/mlm11.png)
:::
::: {.column width="60%"}
$$
\begin{align*}
y_{ij} &= \beta_0 + \beta X_{ij} + u0_j + u1_j + e_{ij} \\[10pt]
\begin{pmatrix} u0_j \\ u1_j \end{pmatrix} &\sim
\text{MVN}\begin{pmatrix} \sigma^2_{u0} & \sigma_{01} \\ \sigma_{01} & \sigma^2_{u1} \end{pmatrix} \\[10pt]
e_{ij} &\sim \text{N}(0,\sigma^2_{e})
\end{align*}
$$
:::
:::
## Random intercept - random slope model {auto-animate=true}
::: columns
::: {.column width="40%"}
![](images/slides/mlm/mlm11.png)
:::
::: {.column width="60%"}
$$
\begin{align*}
y_{ij} &= {\color{red}{\beta_0}} + {\color{red}{\beta}} X_{ij} + u0_j + u1_j + e_{ij} \\[10pt] \begin{pmatrix} u0_j \\ u1_j \end{pmatrix} &\sim \text{MVN}\begin{pmatrix} {\color{red}{\sigma^2_{u0}}} & {\color{red}{\sigma_{01}}} \\ \sigma_{01} & {\color{red}{\sigma^2_{u1}}} \end{pmatrix} \\[10pt]
e_{ij} &\sim \text{N}(0,{\color{red}{\sigma^2_{e}}})
\end{align*}
$$
:::
:::
::: {.incremental style="font-size:0.7em;"}
- The level 2 residuals are assumed to be drawn from a multivariate normal distribution, introducing a new covariance parameter $\sigma_{01}$
:::
## Interpreting the covariance parameter {auto-animate=true}
::: columns
::: {.column width="50%"}
#### A
![](images/slides/mlm/mlm14.png){width='80%'}
:::
::: {.column width="50%"}
#### B
![](images/slides/mlm/mlm13.png){width='80%'}
:::
:::
Which plot shows positive covariance $\sigma_{01}$, A or B?
## Interpreting the covariance parameter {auto-animate=true}
::: columns
::: {.column width="50%"}
#### A
![](images/slides/mlm/mlm14.png){width='50%'}
:::
::: {.column width="50%"}
#### B
![](images/slides/mlm/mlm13.png){width='100%'}
:::
:::
The answer is Plot B - groups with an above average random intercept have an above average random slope
## Summary {.smaller}
:::{.incremental}
#### Motivation
- Appropriate standard errors
- Answer questions related to different levels
#### Multilevel hierarchies
- Patients nested within hospitals
- Observations nested within patients
#### Residuals
- Decomposed into (i) group-level residuals and (ii) within-group residuals
#### Basic models
- Random intercept model
- Random slope model
:::
## **Part 4.** Illustration with Shiny app
::: section-break
![](https://media3.giphy.com/media/3oxRmsoHngzymwDl2E/giphy.gif?cid=790b7611f2d9999351ebb3de3038b66f36d4553021440699&rid=giphy.gif&ct=g)
:::
## {background-iframe="https://cbdrh.shinyapps.io/mlm1/ "}
::: {style="font-size: 0.4em; color: grey; position: absolute; right: 0; bottom: 0;"}
[cbdrh.shinyapps.io/mlm1](https://cbdrh.shinyapps.io/mlm1/){target="_blank"}
```{r, echo=TRUE, eval=FALSE}
# Run in RStudio
hdat9700tutorials::run('mlm-1')
```
:::