-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstanhour3.Rmd
573 lines (375 loc) · 11.1 KB
/
stanhour3.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
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
---
title: "Stan @ ISEC 2020"
subtitle: ""
author: "The Crew"
date: "20 June 2020"
output:
xaringan::moon_reader:
css: xaringan-themer.css
lib_dir: libs
nature:
highlightStyle: solarized-light
highlightLines: true
countIncrementalSlides: false
---
class: inverse, center, middle
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(ggplot2)
```
```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_solarized_light()
```
# Building a Logistic Regression Model
---
## Cabo Pulmo, Baja California Sur, México
.center[]
---
## Pelagios Kakunjá -- Bull Shark Data
Frida Lara -- http://migramar.org/hi/en/lara/
James Ketchum -- https://www.pelagioskakunja.org/james-ketchum
.center[]
https://www.pelagioskakunja.org/
---
## Presence/Absence Data from an Acoustic Array
.pull-left[
$Y$ can takes on the value of `1` when the shark is detected, and `0` when it is not.
Observation Process:
$$
Y \sim Bernoulli(p)
$$
Prior:
$$
p \sim unif(0, 1)
$$
$$
p \sim beta(1, 1)
$$
]
.pull-right[
Photo by Miguel Grau Gómez:
```{r, echo=FALSE, out.width ="70%"}
knitr::include_graphics("photos/bullshark_acousticarray.jpg")
```
]
---
## Stan Code
```{stan, output.var = "ex1", eval=F}
data{
int<lower=1> TT;
int y[TT];
}
parameters{
real<lower=0, upper=1> p;
}
model{
p ~ uniform(0, 1);
//p ~ beta(1,1);
y ~ bernoulli(p);
}
```
.footnote[Order doesn't matter in the model code.]
---
class: center
### Priors over the interval (0,1):
Combinations:
( $\nu_1 = 1, \nu_2=1$ ), ( $\nu_1=2, \nu_2=1$ ) , ( $\nu_1=1, \nu_2=2$ )
( $\nu_1=10, \nu_2=1$ ), ( $\nu_1=1, \nu_2=10$ ), ( $\nu_1=10, \nu_2=10$ )
```{r, echo=FALSE, fig.height=5}
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
alpha = c(1, 2, 1, 10, 1, 10)
beta = c(1, 1, 2, 1, 10, 10)
x01 <- data.frame(x=c(0, 1))
betaplot <- ggplot(x01, aes(x))
for(g in 1:6)
betaplot <- betaplot +
stat_function(fun=dbeta, args=list(shape1=alpha[g], shape2=beta[g]), color=cbPalette[g], size=1)
betaplot + theme_minimal() + ylab("f(x)")
```
---
### Is the probability constant over time?
$Y$ can takes on the value of `1` when the shark is detected, and `0` when it is not.
$$
Y_t \sim Bernoulli(p_t)
$$
$$
logit(p_t) = f(t)
$$
Commonly, $f(t)$ is expressed as a linear function of parameters and covariate values.
$$
f(t) = \beta_0 + \beta_1 x_t
$$
Priors:
$$\beta_0 \sim N(m_{\beta_0}, sd_{\beta_0}) \qquad \beta_1 \sim N(0, sd_{\beta_1})$$
.footnote[ $f(t)$ can also be a smooth non-linear function! -- `brms`, `rstanarm`]
---
### Stan Code
Changes: allowing for time-varying covariates
```{stan, output.var = "ex2", eval=F}
data{
int<lower=1> TT;
int y[TT];
int<lower=1> ncov;
matrix[TT, ncov + 1] x;
}
parameters{
vector[ncov + 1] beta;
}
model{
beta ~ normal(0, 0.5);
y ~ bernoulli_logit(x*beta);
}
```
---
## Accounting for individual size/sex differences
We may expect and want to account for differences across size and sex.
$$f(t) = \beta_0 (sex, size) + \beta_1 * x_t$$
$$\beta_0 (sex, size) = \alpha_0 + \alpha_1 *male + \alpha_2 *(size - baselinesize)$$
Where we have:
$$
male = 1 \quad \text{if shark is male (has claspers); zero o.w.}
$$
$$
baselinesize: \text{ e.g. size at which the sharks reach maturity, here approx. 220 cm }
$$
---
## Stan Code
Changes: removing intercept term $\beta_0$ and allowing it to reflect baseline differences across sharks. Change `x` to no longer contain a column of ones.
.pull-left[
```{stan, output.var = "ex2", eval=F}
data{
...
int<lower=1> ncov;
matrix[TT, ncov] x;
vector[TT] bsize;
int sex[TT];
}
parameters{
vector[ncov] beta;
vector[3] alpha;
}
```
]
.pull-right[
```{stan, output.var="ex21", eval=F}
model{
alpha[1] ~ normal(0, 0.5);
alpha[2] ~ normal(0, 0.5);
//values range from -40 to 40
alpha[3] ~ normal(0, 0.1);
beta ~ normal(0, 0.5);
y ~ bernoulli_logit(alpha[1] +
alpha[2]*sex +
alpha[3]*bsize +
x*beta);
}
```
]
---
class: inverse, middle
# Missing Discrete-Valued Covariates
## (or categorical covariates)
---
class: middle, center
### Marginalizing over discrete-valued covariates/categorical covariates
`Law of Total Probability`
$$P(Y) = \sum_{n=1}^N P(Y|X = x_n)P(X=x_n)$$
---
### Marginalizing in the shark example
Sex $\in$ {female, male} $\rightarrow$ {0, 1}
We go from this:
$$
Y_t \sim Bernoulli(p_t)
$$
To:
$$
Y_t \sim \pi Bernoulli(p_t(female)) + (1-\pi)Bernoulli(p_t(male))
$$
$\pi \in (0, 1)$
$$p_t(female) = \beta_0(female, size) + \beta_1x_t$$
$$p_t(male) = \beta_0(male, size) + \beta_1x_t$$
Where $\pi$ represents the probability that the shark is female.
---
## Stan Code
Changes: need to identify the observations from the shark that is missing a value for sex.
```{stan, output.var="ex3", eval=F}
data{
int<lower=1> TT;
int y[TT];
//vector with 1 if sex is missing
int sexmissing[TT];
int<lower=1> ncov;
matrix[TT, ncov] x;
vector bsize[TT];
vector sex[TT];
}
parameters{
vector[ncov] beta;
vector[3] alpha;
real<lower=0, upper=1> pi;
}
```
---
## Stan Code
Changes: need to identify the observations from the shark that is missing a value for sex.
```{stan, output.var="ex31", eval=F}
model{
...
for(t in 1:TT){
if(sexmissing[t] == 1){
target += log_mix(pi,
bernoulli_logit_lpmf(y[t] | alpha[1] +
alpha[3]*bsize +
x[t]*beta),
bernoulli_logit_lpmf(y[t] | alpha[1] +
alpha[2] +
alpha[3]*bsize[t] +
x[t]*beta));
} else {
y[t] ~ bernoulli_logit(alpha[1] +
alpha[2]*sex[t] +
alpha[3]*bsize[t] +
x[t]*beta);
}
}
}
```
---
class: inverse
## A very not profound overview of a hierarchical model but definitely an example of one
---
## More differences across individuals
A simple hierarchical model:
$$Y_{jt} \sim Bernoulli(p_{jt})$$
$$logit(p_{jt}) = \beta_0 (sex, size) + \beta_{j1} * x_t$$
$$\beta_{j1} \sim N(\mu, \sigma)$$
$$\mu \sim N(m_{\mu}, s_{\mu}) \quad \sigma \sim N^+(m_{\sigma}, s_{\sigma})$$
Why might this be important to include? Aside from the differences across individuals related to their sex and size (related to maturity), the sharks may not all have the same temporal patterns. Some may arrive before others, leave a bit later, and understanding individual variation plays an important role in their conservation.
---
## Stan Code
Changes: have an index for individual shark and $\beta_1$ is now a hierarchical term in the model
```{stan, output.var = "ex4", eval=F}
data{
...
int nsharks;
vector[TT] sharkid;
}
parameters{
...
vector[nsharks] beta;
real betamu;
real<lower=0> betasig;
}
```
---
## Stan Code
Changes: have an index for individual shark and $\beta_1$ is now a hierarchical term in the model
```{stan, output.var="ex41", eval=F}
model{
alpha[1] ~ normal(0, 0.5);
alpha[2] ~ normal(0, 0.5);
//values range from -40 to 40
alpha[3] ~ normal(0, 0.1);
betamu ~ normal(0, 0.1);
betasig ~ normal(0, 0.1);
beta ~ normal(betamu, betasig);
for(t in 1:TT){
y[t] ~ bernoulli_logit(alpha[1] + alpha[2]*sex[t] +
alpha[3]*bsize[t] + x[t]*beta[sharkid[t]]);
}
}
```
---
class: inverse
# Model Assessment:
# To Describe, Explain or Predict?
---
### Describe(/Explain):
- Use full data set to fit model
- Evaluate posterior predictive checks
- For logistic regression, `binned residuals for discrete data`
Bayesian Data Analysis: http://www.stat.columbia.edu/~gelman/book/ (pg 157-158)
Galit Shmueli's: To Explain or Predict? https://www.stat.berkeley.edu/~aldous/157/Papers/shmueli.pdf
---
class: inverse
## Predict: `projpred`
### Projection predictive variable selection
```{r, eval=F}
library(rstanarm)
library(projpred)
library(ggplot2)
library(bayesplot)
theme_set(theme_classic())
```
https://mc-stan.org/projpred/articles/quickstart.html
---
### projpred
Back to the fixed effects for a minute:
```{r, eval=F}
n <- 7300
D <- 5
p0 <- 2 # prior guess for the number of relevant variables
tau0 <- p0/(D-p0) * 1/sqrt(n)
#prior_coeff <- hs(global_scale = tau0, slab_scale = 1)
logregcov.horseshoe <- stan_glm(formula = Presence~.,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%
mutate(BSize = Size - 220),
prior = hs(global_scale = 0.007802743, slab_scale = 1))
logregcov.defaultpriors <- stan_glm(formula = Presence~.,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%
mutate(BSize = Size - 220))
logregcov.n01 <- stan_glm(formula = Presence~.,
family = binomial(link="logit"),
data = sharkfun%>%%>%dplyr::select(-Shark)
mutate(BSize = Size - 220), prior = normal(0,1))
```
---
### projpred (cont.)
K-fold Cross-Validation:
```{r, eval=F}
cvs.horseshoe <- cv_varsel(logregcov.horseshoe,
method='forward',
cv_method='kfold', K=5)
cvplot.horseshoe <- varsel_plot(cvs.horseshoe,
stats=c('elpd, acc')) +
theme_minimal() + theme(text=element_text(size=15),
legend.position = "none") +
ggtitle("Classification Accuracy (5-fold CV)")
```
---
### What you can get from projpred:
-- Out-of-sample and In-sample accuracies
```{r, eval=F}
## out of sample predictive accuracies
cvplot.horseshoe
```
-- Suggested size of the models
```{r, eval=F}
## suggested size
suggest_size(cvs)
```
-- Order of the variables
```{r, eval=F}
vs <- varsel(logregcov.horseshoe, method='forward')
vs$vind
varsel_plot(vs, stats=c('elpd', 'acc'), deltas=F)
#----------
cvs.horseshoe$vind
```
---
### Other: Occupancy Models
Observation process:
$$Y \sim Bernoulli(Zp)$$
Latent process:
$$Z \sim Bernoulli(\psi)$$
$Z$ -- True occupancy
$p$ -- Detection probability
Not logistic regression as usually thought of, but similar structure and can easily put into Stan as well. Can extend to Binomial/Poisson structures.
---
class: inverse, center
### Last Slide for Now
Check out: https://stanecology.github.io
