-
Notifications
You must be signed in to change notification settings - Fork 11
/
count-regression.qmd
300 lines (209 loc) · 6.54 KB
/
count-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
---
subtitle: "Poisson regression and variations"
---
# Models for Count Outcomes
---
## Acknowledgements {.unnumbered}
This content is adapted from:
- @dobson4e, Chapter 9
- @vittinghoff2e, Chapter 8
---
{{< include shared-config.qmd >}}
## Introduction
::: notes
This chapter presents models for
[count data](probability.qmd#sec-count-vars) outcomes.
With covariates,
the event rate $\lambda$
becomes a function of the covariates
$\vX = (X_1, \dots,X_n)$.
Typically, count data models use
a $\log{}$ link function,
and thus an $\exp{}$ inverse-link function.
That is:
:::
$$
\begin{aligned}
\Expp[Y | \vX = \vx, T = t] &= \mu(\vx,t)
\\ \mu(\vx,t) &= \lambda(\vx)\cdot t
\\ \lambda(\vx) &= \exp{\eta(\vx)}
\\ \eta(\vx) &= \vx'\tilde \beta = \beta_0 + \beta_1 x_1 + \dots + \beta_p x_p
\end{aligned}
$$
Therefore,
$$
\begin{aligned}
\logf{\Expp[Y | \vX = \vx,T=t]}
&= \logf{\mu(\vx)}\\
&=\logf{\lambda(\vx) \cdot t}\\
&=\logf{\lambda(\vx)} + \log{t}\\
&=\logf{\exp{\eta(\vx)}} + \log{t}\\
&=\eta(\vx) + \log{t}\\
&=\vx'\tilde\beta + \log{t}\\
&=(\beta_0 +\beta_1 x_1+\dots + \beta_p x_p) + \log{t}\\
\end{aligned}
$$
In contrast with the other covariates (represented by $\vX$), $T$ enters this expression with a $\log{}$ transformation and without a corresponding $\beta$ coefficient.
:::{.callout-note}
Terms that enter the linear component of a model without a coefficient, such as $\log{t}$ here, are called **offsets**.
:::
### Rate ratios {.smaller}
::: notes
Differences on the log-rate scale become ratios on the rate scale,
because
:::
$$\exp{a-b} = \frac{\exp{a}}{\exp{b}}$$
(recall from [Algebra 2](math-prereqs.qmd#cor-exp-sum))
Therefore, according to this model,
**differences of $\delta$ in covariate $x_j$ correspond to rate ratios of $\exp{\beta_j \cdot \delta}$**.
That is, letting $\vX_{-j}$ denote vector $\vX$ with element $j$ removed:
$$
\begin{aligned}
&{
\left\{
\log{\Expp[Y |{\color{red}{X_j = a}}, \vX_{-j}=\vx_{-j},T=t]}
\atop
{-\log{\Expp[Y |{\color{red}{X_j = b}}, \vX_{-j}=\vx_{-j},T=t]}}
\right\}
}\\
&=
{\left\{
\log{t} + \beta_0 + \beta_1 x_1 + ... + {\color{red}{\beta_j (a)}} + ...+\beta_p x_p
\atop
{-\log{t} + \beta_0 + \beta_1 x_1 + ... + {\color{red}{\beta_j (b)}} + ...+\beta_p x_p}
\right\}}\\
&= \color{red}{\beta_j(a-b)}
\end{aligned}
$$
And accordingly,
$$
\begin{aligned}
\frac
{\mathbb{E}[Y |{\color{red}{X_j = a}}, \vX_{-j} = \vx_{-j}, T = t]
}
{
\Expp[Y |{\color{red}{X_j = b}}, \vX_{-j}=\vx_{-j},T=t]
}
=
\exp{{\color{red}{\beta_j(a-b)}}}
\end{aligned}
$$
## Inference for count regression models
### Confidence intervals for regression coefficients and rate ratios
As usual:
$$
\beta \in \left[\ci \right]
$$
Rate ratios: exponentiate CI endpoints
$$
\exp{\beta} \in \left[\exp{\ci} \right]
$$
### Hypothesis tests for regression coefficients
$$
t = \frac{\hat \beta - \beta_0}{\hse{\hb}}
$$
Compare $t$ or $|t|$ to the tails of the standard Gaussian distribution, according to the null hypothesis.
### Comparing nested models
log(likelihood ratio) tests, as usual.
## Prediction
$$
\begin{aligned}
\hat y
&\eqdef \hat{\Expp}[Y|\vX= \vx,T=t]\\
&=\hat\mu(\vx, t)\\
&=\hat\lambda(\vx) \cdot t\\
&=\exp{\hat\eta(\vx)} \cdot t\\
&=\exp{\vx'\hat{\boldsymbol{\beta}}} \cdot t
\end{aligned}
$$
## Diagnostics
### Residuals
#### Observation residuals
$$e \eqdef y - \hat y$$
#### Pearson residuals
$$r = \frac{e}{\hse{e}} \approx \frac{e}{\sqrt{\hat y}}$$
#### Standardized Pearson residuals
$$r_p = \frac{r}{\sqrt{1-h}}$$
where $h$ is the "leverage" (which we will continue to leave undefined).
#### Deviance residuals
$$
d_k = \text{sign}(y - \hat y)\left\{\sqrt{2[\ell_{\text{full}}(y) - \ell(\hat\beta; y)]}\right\}
$$
:::{.callout-note}
$$\text{sign}(x) \eqdef \frac{x}{|x|}$$
In other words:
* $\text{sign}(x) = -1$ if $x < 0$
* $\text{sign}(x) = 0$ if $x = 0$
* $\text{sign}(x) = 1$ if $x > 0$
::::{.content-hidden}
```{r}
plot(sign,xlim = c(-1,1), xlab = "x", ylab = "sign(x)")
```
::::
:::
## Zero-inflation
### Models for zero-inflated counts
We assume a latent (unobserved) binary variable, $Z$, which we model using logistic regression:
$$P(Z=1|X=x) = \pi(x) = \expit(\gamma_0 + \gamma_1 x_1 +...)$$
According to this model, if $Z=1$, then $Y$ will always be zero, regardless of $X$ and $T$:
$$P(Y=0|Z=1,X=x,T=t) = 1$$
Otherwise (if $Z=0$), $Y$ will have a Poisson distribution, conditional on $X$ and $T$, as above.
Even though we never observe $Z$, we can estimate the parameters $\gamma_0$-$\gamma_p$, via maximum likelihood:
$$
\begin{aligned}
P(Y=y|X=x,T=t) &= P(Y=y,Z=1|...) + P(Y=y,Z=0|...)
\end{aligned}
$$
(by the Law of Total Probability)
where
$$
\begin{aligned}
P(Y=y,Z=z|...)
&= P(Y=y|Z=z,...)P(Z=z|...)
\end{aligned}
$$
---
::: {#exr-zinf-pmf}
Expand $P(Y=0|X=x,T=t)$, $P(Y=1|X=x,T=t)$ and $P(Y=y|X=x,T=t)$ into expressions involving $P(Z=1|X=x,T=t)$ and $P(Y=y|Z=0,X=x,T=t)$.
:::
---
::: {#exr-zinf-moments}
Derive the expected value and variance of $Y$, conditional on $X$ and $T$, as functions of $P(Z=1|X=x,T=t)$ and $\Expp[Y|Z=0,X=x,T=t]$.
:::
## Over-dispersion
---
::: notes
The Poisson distribution model **forces** the variance to equal the mean.
In practice, many count distributions will have a variance substantially larger than the mean (or occasionally, smaller).
:::
:::: {#def-overdispersion}
#### Overdispersion
A random variable $X$ is **overdispersed**
relative to a model $\p(X=x)$ if
if its empirical variance in a dataset is larger than
the value is predicted by the fitted model $\hat{\p}(X=x)$.
::::
::: notes
c.f.
@dobson4e §3.2.1, 7.7, 9.8;
@vittinghoff2e §8.1.5;
and <https://en.wikipedia.org/wiki/Overdispersion>.
When we encounter overdispersion,
we can try to reduce the residual variance
by adding more covariates.
:::
### Negative binomial models
There are alternatives to the Poisson model.
Most notably,
the [negative binomial model](probability.qmd#sec-nb-dist).
We can still model $\mu$ as a function of $X$ and $T$ as before,
and we can combine this model with zero-inflation
(as the conditional distribution for the non-zero component).
### Quasipoisson
An alternative to Negative binomial is the "quasipoisson" distribution. I've never used it, but it seems to be a method-of-moments type approach rather than maximum likelihood. It models the variance as $\Var{Y} = \mu\theta$, and estimates $\theta$ accordingly.
See `?quasipoisson` in R for more.
## Example: needle-sharing
(adapted from @vittinghoff2e, §8)
{{< include exr-needle-sharing.qmd >}}
## More on count regression
- <https://bookdown.org/roback/bookdown-BeyondMLR/ch-poissonreg.html>