generated from waldronbios2/templatesession
/
session_lecture.Rmd
503 lines (407 loc) · 12.1 KB
/
session_lecture.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
---
title: 'Session 5: loglinear regression part 2'
author: "Levi Waldron"
clean: false
output:
beamer_presentation:
colortheme: dove
df_print: paged
fonttheme: structurebold
slide_level: 2
theme: Hannover
slidy_presentation: default
md_document:
preserve_yaml: false
always_allow_html: true
institute: CUNY SPH Biostatistics 2
---
# Learning objectives and outline
## Learning objectives
1. Define and identify over-dispersion in count data
2. Define the negative binomial (NB) distribution and identify applications for it
3. Define zero-inflated count models
4. Fit and interpret Poisson and NB, with and without zero inflation
## Outline
1. Review of log-linear Poisson glm
2. Review of diagnostics and interpretation of coefficients
3. Over-dispersion
+ Negative Binomial distribution
4. Zero-inflated models
Resources:
* Vittinghoff section 8.1-8.3
* Short tutorials on regression in R (and Stata, SAS, SPSS, Mplus)
+ https://stats.idre.ucla.edu/other/dae/
# Review
## Components of GLM
* **Random component** specifies the conditional distribution for the response variable - it doesn’t have to be normal but can be any distribution that belongs to the “exponential” family of distributions
* **Systematic component** specifies linear function of predictors (linear predictor)
* **Link** [denoted by g(.)] specifies the relationship between the expected value of the random component and the systematic component, can be linear or nonlinear
## Motivating example: Choice of Distribution
* Count data are often modeled as Poisson distributed:
+ mean $\lambda$ is greater than 0
+ variance is also $\lambda$
+ Probability density $P(k, \lambda) = \frac{\lambda^k}{k!} e^{-\lambda}$
```{r, echo=FALSE, fig.height=6}
##par(cex=2) #increase size of type and axes
plot(x=0:10, y=dpois(0:10, lambda=1),
type="b", lwd=2,
xlab="Counts (k)", ylab="Probability density")
lines(x=0:10, y=dpois(0:10, lambda=2),
type="b", lwd=2, lty=2, pch=2)
lines(x=0:10, dpois(0:10, lambda=4),
type="b", lwd=2, lty=3, pch=3)
legend("topright", lwd=2, lty=1:3, pch=1:3,
legend=c(expression(paste(lambda, "=1")),
expression(paste(lambda, "=2")),
expression(paste(lambda, "=4"))))
```
## Poisson model: the GLM
The **systematic part** of the GLM is:
$$
log(\lambda_i) = \beta_0 + \beta_1 \textrm{RACE}_i + \beta_2 \textrm{TRT}_i + \beta_3 \textrm{ALCH}_i + \beta_4 \textrm{DRUG}_i
$$
Or alternatively:
$$
\lambda_i = exp \left( \beta_0 + \beta_1 \textrm{RACE}_i + \beta_2 \textrm{TRT}_i + \beta_3 \textrm{ALCH}_i + \beta_4 \textrm{DRUG}_i \right)
$$
The **random part** is (Recall the $\lambda_i$ is both the mean and variance of a Poisson distribution):
$$
y_i \sim Poisson(\lambda_i)
$$
## Example: Risky Drug Use Behavior
* Outcome is # times the drug user shared a syringe in the past month (`shared_syr`)
* Predictors: `sex`, `ethn`, `homeless`
+ filtered to `sex` "M" or "F", `ethn` "White", "AA", "Hispanic"
\small
```{r loadandfilter, echo=FALSE, message=FALSE}
suppressPackageStartupMessages({
library(readr)
library(dplyr)
})
needledat <- readr::read_csv("needle_sharing.csv")
needledat2 <- needledat %>%
dplyr::filter(sex %in% c("M", "F") &
ethn %in% c("White", "AA", "Hispanic") &
!is.na(homeless)) %>%
mutate(
homeless = recode(homeless, "0" = "no", "1" = "yes"),
hiv = recode(
hivstat,
"0" = "negative",
"1" = "positive",
"2" = "yes"
)
)
```
```{r}
summary(needledat2$shared_syr)
var(needledat2$shared_syr, na.rm = TRUE)
```
## Example: Risky Drug Use Behavior
Exploratory plots
```{r, echo=FALSE}
par(mfrow = c(1, 2))
##par(cex=2)
hist(needledat2$shared_syr, main = "")
plot(
sort(needledat2$shared_syr),
pch = ".",
xlab = "count",
ylab = "# participants"
)
```
* There are a _lot_ of zeros and variance is much greater than mean
+ Poisson model is probably not a good fit
## Fitting a Poisson model
```{r, results='hide'}
fit.pois <- glm(shared_syr ~ sex + ethn + homeless,
data = needledat2,
family = poisson(link = "log"))
```
## Residuals plots
```{r, echo=FALSE, warning=FALSE}
par(mfrow = c(2, 2))
plot(fit.pois)
```
* Poisson model is definitely not a good fit.
# Over-dispersion
## When the Poisson model doesn't fit
1. Variance > mean (over-dispersion)
+ Negative binomial distribution
2. Excess zeros (zero inflation)
+ Can introduce zero-inflation
## Negative binomial distribution
* The binomial distribution is the number of successes in n trials:
+ Roll a die ten times, how many times do you see a 6?
* The negative binomial distribution is the number of successes it takes to observe r failures:
+ How many times do you have to roll the die to see a 6 ten times?
+ Note that the number of rolls is no longer fixed.
+ In this example, p=5/6 and a 6 is a "failure"
## Negative binomial GLM
*One way* to parametrize a NB model is with a **systematic part** equivalent to the Poisson model:
$$
log(\lambda_i) = \beta_0 + \beta_1 \textrm{RACE}_i + \beta_2 \textrm{TRT}_i + \beta_3 \textrm{ALCH}_i + \beta_4 \textrm{DRUG}_i
$$
Or:
$$
\lambda_i = exp \left( \beta_0 + \beta_1 \textrm{RACE}_i + \beta_2 \textrm{TRT}_i + \beta_3 \textrm{ALCH}_i + \beta_4 \textrm{DRUG}_i \right)
$$
And a **random part**:
$$
y_i \sim NB(\lambda_i, \theta)
$$
* $\theta$ is a **dispersion parameter** that is estimated
* When $\theta = 0$ it is equivalent to Poisson model
* `MASS::glm.nb()` uses this parametrization, `dnbinom()` does not
* The Poisson model can be considered **nested** within the Negative Binomial model
## Negative Binomial Random Distribution
```{r, echo=FALSE}
plot(
x = 0:40,
y = dnbinom(0:40, size = 10, prob = 0.5),
type = "b",
lwd = 2,
ylim = c(0, 0.2),
xlab = "Counts (k)",
ylab = "Probability density"
)
lines(
x = 0:40,
y = dnbinom(0:40, size = 20, prob = 0.5),
type = "b",
lwd = 2,
lty = 2,
pch = 2
)
lines(
x = 0:40,
y = dnbinom(0:40, size = 10, prob = 0.3),
type = "b",
lwd = 2,
lty = 3,
pch = 3
)
legend(
"topright",
lwd = 2,
lty = 1:3,
pch = 1:3,
legend = c("n=10, p=0.5", "n=20, p=0.5", "n=10, p=0.3")
)
```
## Compare Poisson vs. Negative Binomial
Negative Binomial Distribution has two parameters: # of trials n, and probability of success p
```{r, echo=FALSE}
plot(
x = 0:40,
y = dnbinom(0:40, size = 10, prob = 0.5),
type = "b",
lwd = 2,
ylim = c(0, 0.15),
xlab = "Counts (k)",
ylab = "Probability density"
)
lines(
x = 0:40,
y = dnbinom(0:40, size = 20, prob = 0.5),
type = "b",
lwd = 2,
lty = 2,
pch = 2
)
lines(
x = 0:40,
y = dnbinom(0:40, size = 10, prob = 0.3),
type = "b",
lwd = 2,
lty = 3,
pch = 3
)
lines(x = 0:40,
y = dpois(0:40, lambda = 9),
col = "red")
lines(x = 0:40,
y = dpois(0:40, lambda = 20),
col = "red")
legend(
"topright",
lwd = c(2, 2, 2, 1),
lty = c(1:3, 1),
pch = c(1:3, -1),
col = c(rep("black", 3), "red"),
legend = c("n=10, p=0.5", "n=20, p=0.5", "n=10, p=0.3", "Poisson")
)
```
## Negative Binomial Regression
```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(MASS)
fit.negbin <- MASS::glm.nb(shared_syr ~ sex +
ethn + homeless,
data = needledat2)
```
##
\tiny
```{r}
summary(fit.negbin)
```
## Likelihood ratio test
Basis: Under $H_0$: no improvement in fit by more complex model, difference in model residual deviances is $\chi^2$-distributed.
Deviance: $\Delta (\textrm{D}) = -2 * \Delta (\textrm{log likelihood})$
```{r}
(ll.negbin <- logLik(fit.negbin))
(ll.pois <- logLik(fit.pois))
pchisq(2 * (ll.negbin - ll.pois), df=1,
lower.tail=FALSE)
```
## NB regression residuals plots
```{r, echo=FALSE, warning=FALSE}
par(mfrow = c(2, 2))
plot(fit.negbin)
```
# Zero Inflation
## Zero inflated "two-step" models
**Step 1**: logistic model to determine whether count is zero or Poisson/NB
**Step 2**: Poisson or NB regression distribution for $y_i$ not set to zero by *1.*
## Poisson Distribution with Zero Inflation
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(gamlss)
##par(cex=2) #increase size of type and axes
plot(
x = 0:10,
y = dpois(0:10, lambda = 2),
type = "b",
lwd = 2,
ylim = c(0, 0.5),
xlab = "Counts (k)",
ylab = "Probability density"
)
lines(
x = 0:10,
y = dZIP(0:10, mu = 2, sigma = 0.2),
type = "b",
lwd = 2,
lty = 2,
pch = 2
)
lines(
x = 0:10,
y = dZIP(0:10, mu = 2, sigma = 0.4),
type = "b",
lwd = 2,
lty = 3,
pch = 3
)
legend(
"topright",
lwd = 2,
lty = 1:3,
pch = 1:3,
legend = c(expression(paste(lambda, "=2")),
expression(
paste("ZIP: ", lambda, "=2, ", Sigma, "=0.2")
),
expression(
paste("ZIP: ", lambda, "=2, ", Sigma, "=0.4")
))
)
```
## Zero-inflated Poisson regression
```{r, echo=TRUE, results='hide', message=FALSE, warning=FALSE}
library(pscl)
fit.ZIpois <-
pscl::zeroinfl(shared_syr~sex+ethn+homeless,
dist = "poisson",
data = needledat2)
```
##
\tiny
```{r}
summary(fit.ZIpois)
```
## Zero-inflated Negative Binomial regression
```{r, echo=TRUE, results='hide', message=FALSE}
fit.ZInegbin <-
pscl::zeroinfl(shared_syr~sex+ethn+homeless,
dist = "negbin",
data = needledat2)
```
* *NOTE*: zero-inflation model can include any of your variables as predictors
* *WARNING* Default in `zerinfl()` function is to use _all_ variables as predictors in logistic model
##
\tiny
```{r}
summary(fit.ZInegbin)
```
## Zero-inflated NB - simplified
* Model is much more interpretable if the exposure of interest is _not_ included in the zero-inflation model.
* E.g. with HIV status as the only predictor in zero-inflation model:
```{r, echo=TRUE, results='hide', message=FALSE}
fit.ZInb2 <- pscl::zeroinfl(shared_syr ~ sex + ethn +
homeless + hiv | hiv,
dist = "negbin",
data = needledat2)
```
##
\tiny
```{r}
summary(fit.ZInb2)
```
## Intercept-only ZI model
```{r}
fit.ZInb3 <-
pscl::zeroinfl(shared_syr~sex+ethn+homeless|1,
dist = "negbin",
data = needledat2)
```
##
\tiny
```{r}
summary(fit.ZInb3)
```
## Confidence intervals
Use the `confint()` function for all these models (don't try to specify which package confint comes from). E.g.:
```{r}
confint(fit.ZInb3)
```
## Residuals vs. fitted values
I invisibly define functions `plotpanel1` and `plotpanel2` that will work for all types of models (see lab). These use Pearson residuals.
```{r, echo=FALSE}
plotpanel1 <- function(fit, ...) {
plot(
x = predict(fit),
y = residuals(fit, type = "pearson"),
xlab = "Predicted Values",
ylab = "Pearson Residuals",
...
)
abline(h = 0, lty = 3)
lines(lowess(x = predict(fit), y = resid(fit, type = "pearson")),
col = "red")
}
plotpanel2 <- function(fit, ...) {
resids <- scale(residuals(fit, type = "pearson"))
qqnorm(resids, ylab = "Std Pearson resid.", ...)
qqline(resids)
}
```
```{r, echo=FALSE, warning=FALSE}
par(mfrow = c(2, 2))
plotpanel1(fit.pois, main = "Residuals vs. Fitted\n Poisson")
plotpanel1(fit.negbin, main = "Residuals vs. Fitted\n Negative Binomial")
plotpanel1(fit.ZIpois, main = "Residuals vs. Fitted\n Zero-inflated Poisson")
plotpanel1(fit.ZInegbin, main = "Residuals vs. Fitted\n Zero-inflated Negative Binomial")
```
## Quantile-quantile plots for residuals
```{r, echo=FALSE, warning=FALSE}
par(mfrow = c(2, 2))
plotpanel2(fit.pois, main = "Normal Q-Q Plot\n Poisson")
plotpanel2(fit.negbin, main = "Normal Q-Q Plot\n Negative Binomial")
plotpanel2(fit.ZIpois, main = "Normal Q-Q Plot\n Zero-inflated Poisson")
plotpanel2(fit.ZInegbin, main = "Normal Q-Q Plot\n Zero-inflated Negative Binomial")
```
_still_ over-dispersed - ideas?
## Summary / Conclusions
* These are multiplicative models
* Fitting zero-inflated models can be problematic (convergence, over-complicated default models), especially for small samples
* Use QQ and residuals plots to assess model fit
* Can use LRT to compare nested models