forked from behrman/ros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesquite_tv.Rmd
More file actions
437 lines (325 loc) · 8.56 KB
/
mesquite_tv.Rmd
File metadata and controls
437 lines (325 loc) · 8.56 KB
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
---
title: "Regression and Other Stories: Mesquite"
author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
---
Tidyverse version by Bill Behrman.
Predicting the yields of mesquite bushes. See Chapter 12 in
Regression and Other Stories.
-------------
```{r, message=FALSE}
# Packages
library(tidyverse)
library(bayesplot)
library(rstanarm)
# Parameters
# Seed
SEED <- 4587
# Mesquite biomass production data
file_mesquite <- here::here("Mesquite/data/mesquite.dat")
# Common code
file_common <- here::here("_common.R")
# Functions
# LOO R^2
loo_r2 <- function(fit, digits = 2) {
round(median(loo_R2(fit)), digits = digits)
}
# Bayesian R^2
bayes_r2 <- function(fit, digits = 2) {
round(median(bayes_R2(fit)), digits = digits)
}
# Plot kernel density of data and sample replicates
plot_density_overlay <- function(y, y_rep) {
ggplot(mapping = aes(y)) +
stat_density(
aes(group = rep, color = "y_rep"),
data =
seq_len(nrow(y_rep)) %>% map_dfr(~ tibble(rep = ., y = y_rep[., ])),
geom = "line",
position = "identity",
alpha = 0.5,
size = 0.25
) +
stat_density(aes(color = "y"), data = tibble(y), geom = "line", size = 1) +
scale_y_continuous(breaks = 0) +
scale_color_discrete(
breaks = c("y", "y_rep"),
labels = c("y", expression(y[rep]))
) +
theme(legend.text.align = 0) +
labs(
x = NULL,
y = NULL,
color = NULL
)
}
#===============================================================================
# Run common code
source(file_common)
```
# 12 Transformation and regression
## 12.6 Building and comparing regression models for prediction
### Example predicting the yields of mesquite bushes
Data
```{r, message=FALSE}
mesquite <- read_table(file_mesquite)
mesquite
```
The outcome variable is `weight`, the total weight in grams of photosynthetic material derived from harvesting the bush. The other variables are:
* `group`: Two separate sets of measurements were taken, one on a group of 26 bushes and the other on a different group 20 bushes measured at a different time of year
* `diam1`: Diameter of the canopy (the leafy area of the bush) in meters, measured along the longer axis of the bush
* `diam2`: Canopy diameter measured along the shorter diameter
* `total_height`: Total height of bush
* `canopy_height`: Height of canopy
* `density`: Plant unit density (number of primary stems per plant unit)
Model weight on all the predictors.
```{r}
fit_1 <-
stan_glm(
weight ~ diam1 + diam2 + canopy_height + total_height + density + group,
data = mesquite,
seed = SEED,
refresh = 0
)
fit_1
```
LOO log score
```{r}
loo_1 <- loo(fit_1)
```
We get warnings about high Pareto k values, so we follow the advice and call `loo()` again with the suggested argument.
```{r, message=FALSE}
loo_1 <- loo(fit_1, k_threshold = 0.7)
loo_1
```
LOO $R^2$
```{r, warning=FALSE}
loo_r2(fit_1)
```
Bayesian $R^2$
```{r}
bayes_r2(fit_1)
```
Model log(weight) on log transformed predictors.
```{r}
fit_2 <-
stan_glm(
log(weight) ~
log(diam1) + log(diam2) + log(canopy_height) + log(total_height) +
log(density) + group,
data = mesquite,
seed = SEED,
refresh = 0
)
fit_2
```
LOO log score
```{r}
loo_2 <- loo(fit_2)
```
We get a warning about a high Pareto k value, so we again follow the advice and call `loo()` again with the suggested argument.
```{r, message=FALSE}
loo_2 <- loo(fit_2, k_threshold = 0.7)
loo_2
```
LOO $R^2$
```{r, warning=FALSE}
loo_r2(fit_2)
```
Bayesian $R^2$
```{r}
bayes_r2(fit_2)
```
### Using the Jacobian to adjust the predictive comparison after a tranformation
Since model 1 uses `weight` as its outcome variable and model 2 uses `log(weight)`, a Jacobian correction is needed in order to compare the models.
```{r}
loo_2_with_jacobian <- loo_2
loo_2_with_jacobian$pointwise[, "elpd_loo"] <-
loo_2_with_jacobian$pointwise[, "elpd_loo"] - log(mesquite$weight)
loo_2_with_jacobian_elpd <- sum(loo_2_with_jacobian$pointwise[, "elpd_loo"])
loo_2_with_jacobian_elpd
```
With the Jacobian correction, we can now compare the log scores of models 1 and 2. There will be a warning about the outcome variables being different, but this is OK because we have made the correction.
```{r}
loo_compare(loo_1, loo_2_with_jacobian)
```
Model 2 has the better log score.
#### Posterior predictive checking for non-log model
Replicates from non-log model.
```{r}
set.seed(700)
y_rep_1 <- posterior_predict(fit_1)
n_sims <- nrow(y_rep_1)
n_rep <- 100
sims_sample <- sample(n_sims, n_rep)
```
Kernel density of data and `r n_rep` sample replicates from non-log model.
```{r}
plot_density_overlay(y = mesquite$weight, y_rep = y_rep_1[sims_sample, ]) +
labs(
title =
str_glue(
"Kernel density of data and {n_rep} sample replicates from non-log model"
),
x = "Weight"
)
```
Kernel density of data and `r n_rep` sample replicates from non-log model using bayesplot.
```{r}
ppc_dens_overlay(y = mesquite$weight, yrep = y_rep_1[sims_sample, ]) +
theme(
axis.line.y = element_blank(),
text = element_text(family = "sans"),
) +
labs(title = "Model for weight")
```
#### Posterior predictive checking for model in log scale
Replicates from log model.
```{r}
set.seed(700)
y_rep_2 <- posterior_predict(fit_2)
```
Kernel density of data and `r n_rep` sample replicates from log model.
```{r}
plot_density_overlay(y = log(mesquite$weight), y_rep = y_rep_2[sims_sample, ]) +
labs(
title =
str_glue(
"Kernel density of data and {n_rep} sample replicates from log model"
),
x = "Log weight"
)
```
Kernel density of data and `r n_rep` sample replicates from log model using bayesplot.
```{r}
ppc_dens_overlay(y = log(mesquite$weight), yrep = y_rep_2[sims_sample, ]) +
theme(
axis.line.y = element_blank(),
text = element_text(family = "sans"),
) +
labs(title = "Model for log(weight)")
```
Marginal posteriors for log model using bayesplot.
```{r}
mcmc_areas(as.matrix(fit_2), regex_pars = "^(log|group)") +
theme(text = element_text(family = "sans"))
```
Posterior coefficients of `log(canopy_height)` and `log(total_height)`.
```{r}
sims_2 <- as_tibble(fit_2)
sims_2 %>%
ggplot(aes(`log(canopy_height)`, `log(total_height)`)) +
geom_hline(yintercept = 0) +
geom_vline(xintercept = 0) +
geom_point(alpha = 0.5, size = 0.5) +
labs(
title =
"Posterior coefficients of log(canopy_height) and log(total_height)",
x = "Coefficient of log(canopy_height)",
y = "Coefficient of log(total_height)"
)
```
We can see that although the univariate marginal densities overlap with zero, the joint distribution is clearly separated from zero.
### Constructing a simpler model
Additional transformed variables.
```{r}
mesquite <-
mesquite %>%
mutate(
canopy_volume = diam1 * diam2 * canopy_height,
canopy_area = diam1 * diam2,
canopy_shape = diam1 / diam2
)
```
#### A model with canopy volume variable
```{r}
fit_3 <-
stan_glm(
log(weight) ~ log(canopy_volume),
data = mesquite,
seed = SEED,
refresh = 0
)
fit_3
```
LOO log score
```{r}
loo_3 <- loo(fit_3)
loo_3
```
LOO $R^2$
```{r}
loo_r2(fit_3)
```
Bayesian $R^2$
```{r}
bayes_r2(fit_3)
```
Compare log scores. Models 2 and 3 both use `log(weight)` as the outcome variable, so they can be compared directly.
```{r}
loo_compare(loo_2, loo_3)
```
Model 2 has the better log score.
#### Add canopy area and shape to model
```{r}
fit_4 <-
stan_glm(
log(weight) ~
log(canopy_volume) + log(canopy_area) + log(canopy_shape) +
log(total_height) + log(density) + group,
data = mesquite,
seed = SEED,
refresh = 0
)
fit_4
```
LOO log score
```{r}
loo_4 <- loo(fit_4)
loo_4
```
LOO $R^2$
```{r, warning=FALSE}
loo_r2(fit_4)
```
Bayesian $R^2$
```{r}
bayes_r2(fit_4)
```
Compare log scores.
```{r}
loo_compare(loo_2, loo_4)
```
The predictor variables in model 4 and just a linear transformation of those in model 2, so the log scores for the two models are virtually the same.
#### A model with just canopy volume and canopy shape
```{r}
fit_5 <-
stan_glm(
log(weight) ~ log(canopy_volume) + log(canopy_shape) + group,
data = mesquite,
seed = SEED,
refresh = 0
)
fit_5
```
LOO log score
```{r}
loo_5 <- loo(fit_5)
loo_5
```
LOO $R^2$
```{r, warning=FALSE}
loo_r2(fit_5)
```
Bayesian $R^2$
```{r}
bayes_r2(fit_5)
```
Compare log scores.
```{r}
loo_compare(loo_4, loo_5)
```
The simpler model 5, with a subset of the predictor variables of model 4, has the better log score.