Skip to content

Commit f06f102

Browse files
author
Lauren Talluto
committed
Update lectuers for 3 dec
1 parent 7e29623 commit f06f102

File tree

128 files changed

+705
-603
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+705
-603
lines changed

index.md

+4-4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-263 KB
Binary file not shown.
-526 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-98.2 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

lec/5_inference_ii.Rmd lec/6_inference_ii.Rmd

+38-34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ output:
99
css: rmd_style.css
1010
self_contained: false
1111
lib_dir: lib
12+
mathjax: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js
1213
beamer_presentation: default
1314
---
1415

@@ -243,10 +244,9 @@ grpl
243244

244245
## 2. Sample from the joint posterior
245246

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+
247249

248-
::: {.columns}
249-
:::: {.column}
250250

251251
```{r eval = FALSE}
252252
library(rstan)
@@ -270,8 +270,10 @@ X_scaled = scale(X)
270270
271271
```
272272

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.
275277

276278
```{r, cache = TRUE}
277279
@@ -283,7 +285,8 @@ standat1 = list(
283285
X = X_scaled[which_forest, "NDVI", drop=FALSE],
284286
mu_a = 0,
285287
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!
287290
sigma_a = 10,
288291
sigma_b = 5
289292
)
@@ -294,8 +297,7 @@ fit1 = sampling(bird_glm, data = standat1, iter=5000,
294297
295298
```
296299

297-
::::
298-
:::
300+
299301

300302
## 2. Sample from the joint posterior
301303

@@ -315,9 +317,6 @@ print(fit1, par = c("a", "B"))
315317

316318
Next, we choose two variables, and try them using birds of different habitat types.
317319

318-
::: {.columns}
319-
:::: {.column}
320-
321320

322321
```{r, message=FALSE, cache = TRUE}
323322
# Second, looking at how two variables influence birds of different types
@@ -339,8 +338,9 @@ X_2 = cbind(X_2,
339338
head(X_2)
340339
```
341340

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.
344344

345345

346346
```{r, message=FALSE, cache = TRUE}
@@ -360,8 +360,7 @@ standat2 = list(
360360
fit2 = sampling(bird_glm, data = standat2, iter=5000,
361361
chains = 4, refresh = 0)
362362
```
363-
::::
364-
:::
363+
365364

366365
## 2. Sample from the joint posterior
367366

@@ -608,15 +607,16 @@ quantile(phi, c(0.05, 0.95))
608607

609608
* How does richness respond to the individual variables, holding other variables constant?
610609

611-
```{r, message=FALSE, echo = FALSE, fig.width=10}
610+
```{r, message=FALSE, echo = FALSE, fig.width=14}
612611
## generate a dataset to predict a line for all combinations
613612
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))
620620
predict_dat = cbind(intercept=1, predict_dat)
621621
622622
## this computes a posterior distribution for E(y) | predict_dat
@@ -631,21 +631,25 @@ predict_dat$y_lower = apply(y, 2, quantile, 0.05)
631631
632632
## Create some nice labels for ggplot
633633
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") +
646640
labs(fill="Type of bird", colour="Type of bird") +
647641
xlim(-1.8, 1.8)
648642
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+
649653
```
650654

651655

0 commit comments

Comments
 (0)