Skip to content

Commit 18cc546

Browse files
add to ch8
1 parent 16a49d6 commit 18cc546

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

ch8_conditional.qmd

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ library(dagitty)
2323
library(ggdag)
2424
library(ggrepel)
2525
library(ggthemes)
26+
library(rcartocolor)
2627
2728
library(rethinking)
2829
data(rugged)
@@ -228,13 +229,13 @@ The first thing to realize is that just including an indicator variable for Afri
228229

229230
To build a model that alls nations inside nd outside Africa to have different intercepts, we need to modify the model for $\mu_i$ so that the mean is conditional on continent. The conventional way to do this would be to just add another term to the linear model:
230231

231-
$$ \mu_i = \alpha + \beta_!(\text{rugged}_i - \overline{rugged}) - \beta_2 \mathbf{I} (\text{Africa}_i)$$
232+
$$ \mu_i = \alpha + \beta_1(\text{rugged}_i - \overline{rugged}) - \beta_2 \mathbf{I} (\text{Africa}_i)$$
232233

233234
Where $\text{Africa}_i$ is a 0/1 indicator variable for Africa or not Africa. But this model assumes that african countries have more uncertainty inherently built into their $mu$ estimate, which makes no sense.
234235

235236
Our simple solution is to create separate intercepts for the different categories, like so:
236237

237-
$$ \mu_i = \alpha_\text{Africa [i]} + \beta_!(\text{rugged}_i - \overline{rugged})$$
238+
$$ \mu_i = \alpha_\text{Africa [i]} + \beta_1(\text{rugged}_i - \overline{rugged})$$
238239
Where $\text{Africa [i]}$ is an index variable which takes the value 1 for African nations and 2 for all other nations.
239240

240241
```{r}
@@ -287,4 +288,42 @@ as_tibble(b8.1) %>%
287288
axis.ticks.y = element_blank(),
288289
panel.grid = element_blank())
289290
290-
```
291+
```
292+
293+
294+
```{r}
295+
#| fig-width: 10
296+
#| fig-height: 2
297+
298+
b8.0 <- add_criterion(b8.0, criterion = "waic")
299+
b8.1 <- add_criterion(b8.1, criterion = "waic")
300+
b8.1$criteria$waic
301+
302+
w <- loo_compare(b8.0, b8.1, criterion = "waic")
303+
304+
w[, 7:8] %>%
305+
data.frame() %>%
306+
rownames_to_column("model_name") %>%
307+
mutate(model_name = fct_reorder(model_name, waic, .desc = T)) %>%
308+
309+
ggplot(aes(x = waic, y = model_name,
310+
xmin = waic - se_waic,
311+
xmax = waic + se_waic)) +
312+
geom_pointrange(color = carto_pal(7, "BurgYl")[7],
313+
fill = carto_pal(7, "BurgYl")[5], shape = 21) +
314+
labs(title = "My custom WAIC plot",
315+
x = NULL, y = NULL) +
316+
theme(axis.ticks.y = element_blank())+
317+
theme_minimal()
318+
319+
b8.0 <- add_criterion(b8.0, criterion = "loo")
320+
b8.1 <- add_criterion(b8.1, criterion = "loo")
321+
322+
loo_compare(b8.0, b8.1, criterion = "loo") %>%
323+
print(simplify = F)
324+
```
325+
326+
$$\eta_n = \sum_{i = 1}^K b_i x_{ni}$$
327+
328+
$$\eta_n = b_1 \exp(b_2 x_n)$$
329+
$$cum_{AY, dev} \sim N(\mu_{AY, dev}, \sigma) \mu_{AY, dev} = ult_{AY} \left(1 - \exp\left(- \left( \frac{dev}{\theta} \right)^\omega \right) \right)$$

0 commit comments

Comments
 (0)