9
9
css : rmd_style.css
10
10
self_contained : false
11
11
lib_dir : lib
12
+ mathjax : https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js
12
13
beamer_presentation : default
13
14
---
14
15
@@ -243,10 +244,9 @@ grpl
243
244
244
245
## 2. Sample from the joint posterior
245
246
246
- Let's try two models. First, we will see how NDVI affects diversity.
247
+ First we compile the model and prepare the data.
248
+
247
249
248
- ::: {.columns}
249
- :::: {.column}
250
250
251
251
``` {r eval = FALSE}
252
252
library(rstan)
@@ -270,8 +270,10 @@ X_scaled = scale(X)
270
270
271
271
```
272
272
273
- ::::
274
- :::: {.column}
273
+
274
+ ## 2. Sample from the joint posterior
275
+
276
+ Next we fit an initial model using only NVDI as a predictor.
275
277
276
278
``` {r, cache = TRUE}
277
279
@@ -283,7 +285,8 @@ standat1 = list(
283
285
X = X_scaled[which_forest, "NDVI", drop=FALSE],
284
286
mu_a = 0,
285
287
mu_b = 0,
286
- # these prior scales are still SUPER vague (exp(20) is a possible intercept under this prior!)
288
+ # these prior scales are still SUPER vague
289
+ # exp(20) is a possible intercept under this prior!
287
290
sigma_a = 10,
288
291
sigma_b = 5
289
292
)
@@ -294,8 +297,7 @@ fit1 = sampling(bird_glm, data = standat1, iter=5000,
294
297
295
298
```
296
299
297
- ::::
298
- :::
300
+
299
301
300
302
## 2. Sample from the joint posterior
301
303
@@ -315,9 +317,6 @@ print(fit1, par = c("a", "B"))
315
317
316
318
Next, we choose two variables, and try them using birds of different habitat types.
317
319
318
- ::: {.columns}
319
- :::: {.column}
320
-
321
320
322
321
``` {r, message=FALSE, cache = TRUE}
323
322
# Second, looking at how two variables influence birds of different types
@@ -339,8 +338,9 @@ X_2 = cbind(X_2,
339
338
head(X_2)
340
339
```
341
340
342
- ::::
343
- :::: {.column}
341
+ ## 2. Sample from the joint posterior
342
+
343
+ Next, we choose two variables, and try them using birds of different habitat types.
344
344
345
345
346
346
``` {r, message=FALSE, cache = TRUE}
@@ -360,8 +360,7 @@ standat2 = list(
360
360
fit2 = sampling(bird_glm, data = standat2, iter=5000,
361
361
chains = 4, refresh = 0)
362
362
```
363
- ::::
364
- :::
363
+
365
364
366
365
## 2. Sample from the joint posterior
367
366
@@ -608,15 +607,16 @@ quantile(phi, c(0.05, 0.95))
608
607
609
608
* How does richness respond to the individual variables, holding other variables constant?
610
609
611
- ``` {r, message=FALSE, echo = FALSE, fig.width=10 }
610
+ ``` {r, message=FALSE, echo = FALSE, fig.width=14 }
612
611
## generate a dataset to predict a line for all combinations
613
612
xx = seq(-1.8,1.8, length.out = 200)
614
- predict_dat = rbind(data.table(forcover = xx, ndvi = 0, open=0, gen = 0, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
615
- data.table(forcover = xx, ndvi = 0, open=1, gen = 0, op_fc = xx, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
616
- data.table(forcover = xx, ndvi = 0, open=0, gen = 1, op_fc = 0, op_nd = 0, ge_fc = xx, ge_ndvi = 0),
617
- data.table(forcover = 0, ndvi = xx, open=0, gen = 0, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
618
- data.table(forcover = 0, ndvi = xx, open=1, gen = 0, op_fc = 0, op_nd = 1, ge_fc = 0, ge_ndvi = 0),
619
- data.table(forcover = 0, ndvi = xx, open=0, gen = 1, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 1))
613
+ predict_dat = rbind(
614
+ data.table(forcover = xx, ndvi = 0, open=0, gen = 0, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
615
+ data.table(forcover = xx, ndvi = 0, open=1, gen = 0, op_fc = xx, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
616
+ data.table(forcover = xx, ndvi = 0, open=0, gen = 1, op_fc = 0, op_nd = 0, ge_fc = xx, ge_ndvi = 0),
617
+ data.table(forcover = 0, ndvi = xx, open=0, gen = 0, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 0),
618
+ data.table(forcover = 0, ndvi = xx, open=1, gen = 0, op_fc = 0, op_nd = 1, ge_fc = 0, ge_ndvi = 0),
619
+ data.table(forcover = 0, ndvi = xx, open=0, gen = 1, op_fc = 0, op_nd = 0, ge_fc = 0, ge_ndvi = 1))
620
620
predict_dat = cbind(intercept=1, predict_dat)
621
621
622
622
## this computes a posterior distribution for E(y) | predict_dat
@@ -631,21 +631,25 @@ predict_dat$y_lower = apply(y, 2, quantile, 0.05)
631
631
632
632
## Create some nice labels for ggplot
633
633
predict_dat$bird = with(predict_dat, ifelse(open == 1, "open", ifelse(gen == 1, "generalist", "forest")))
634
- predict_dat$panel = "Forest Cover | NDVI=0"
635
- predict_dat$panel[601:1200] = "NDVI | Forest Cover=0"
636
-
637
- ## Create a combined x-variable for ggplot; this works because forcover
638
- ## and ndvi are transformed to mean = 0, and the predictions are conditional;
639
- ## if one variable is nonzero, the other must be zero
640
- predict_dat$x = predict_dat$forcover + predict_dat$ndvi
641
-
642
- ggplot(predict_dat, aes(x=x, y=y_med, col=bird)) +
643
- geom_ribbon(aes(x=x, ymin=y_lower, ymax=y_upper, fill=bird), alpha=0.5) +
644
- geom_line() + facet_grid(.~panel) +
645
- theme_minimal() + ylab("Species Richness") + xlab(expression(sigma)) +
634
+
635
+
636
+ p1 = ggplot(predict_dat[1:600,], aes(x=forcover, y=y_med, col=bird)) +
637
+ geom_ribbon(aes(x=forcover, ymin=y_lower, ymax=y_upper, fill=bird), alpha=0.5) +
638
+ geom_line() + xlab("Forest Cover | NDVI=0 (sd)") +
639
+ theme_minimal() + ylab("Species Richness") +
646
640
labs(fill="Type of bird", colour="Type of bird") +
647
641
xlim(-1.8, 1.8)
648
642
643
+ p2 = ggplot(predict_dat[601:1200,], aes(x=ndvi, y=y_med, col=bird)) +
644
+ geom_ribbon(aes(x=ndvi, ymin=y_lower, ymax=y_upper, fill=bird), alpha=0.5) +
645
+ geom_line() + xlab("NDVI | Forest Cover=0 (sd)") +
646
+ theme_minimal() + ylab("Species Richness") +
647
+ labs(fill="Type of bird", colour="Type of bird") +
648
+ xlim(-1.8, 1.8)
649
+
650
+ grid.arrange(p1, p2, nrow = 1)
651
+
652
+
649
653
```
650
654
651
655
0 commit comments