Skip to content

Commit 002ab72

Browse files
2nd vignette started ch8
1 parent f910848 commit 002ab72

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

ch8_conditional.qmd

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ library(rcartocolor)
2727
2828
library(rethinking)
2929
data(rugged)
30+
data(tulips)
3031
3132
detach(package:rethinking, unload = T)
3233
@@ -50,7 +51,7 @@ To model deeper conditionality—where the importance of one predictor depends u
5051
More generally, interactions are central to most statistical models beyond the cozy world of Gaussian outcomes and linear models of the mean. In generalized linear models (GLMs), even when one does not explicitly define variables as interacting, they will always interact to some degree. Multilevel models induce similar effects. Common sorts of multilevel models are essentially massive interaction models, in which estimates (intercepts and slopes) are conditional on clusters (person, genus, village, city, galaxy) in the data. Multilevel interaction effects are complex. They’re not just allowing the impact of a predictor variable to change depending upon some other variable, but they are also estimat- ing aspects of the distribution of those changes. This may sound like genius, or madness, or both. Regardless, you can’t have the power of multilevel modeling without it.
5152

5253

53-
## Continuous with Discrete Interactions
54+
## Discrete ~Continuous Interactions
5455

5556

5657
### Ruggedness vs GDP in Africa 🌍
@@ -574,3 +575,78 @@ w[, 7:8] %>%
574575
theme_minimal()
575576
```
576577

578+
Out interaction model performs best yet, it receives more than 95% of the model weight. Though the remaining weight percentage placed on our 2nd model, does mean there is some reason to think that our model is a bit overfit on its slopes.
579+
580+
#####
581+
The interaction there has two equally valid phrasings.
582+
(1) How much does the association between ruggedness and log GDP depend upon whether the nation is in Africa?
583+
(2) How much does the association of Africa with log GDP depend upon ruggedness?
584+
585+
While these two possibilities sound different to most humans, our statistical engine thinks they are identical.
586+
587+
```{r}
588+
fitted(b8.2,
589+
newdata = simCov,
590+
summary = F) %>%
591+
data.frame() %>%
592+
pivot_longer(everything()) %>%
593+
bind_cols(expand_grid(draws = 1:4000, simCov)) %>%
594+
select(-name) %>%
595+
pivot_wider(names_from = cont, values_from = value) %>%
596+
mutate(delta = `African` - `Not African`) %>%
597+
ggplot(aes(x = rugged_std, y = delta)) +
598+
stat_lineribbon(.width = .95, fill = palette_pander(n = 8)[8], alpha = 3/4) +
599+
geom_hline(yintercept = 0, linetype = 2) +
600+
annotate(geom = "text",
601+
x = .2, y = 0,
602+
label = "Africa higher GDP\nAfrica lower GDP",
603+
family = "Times") +
604+
labs(x = "ruggedness (standardized)",
605+
y = "expected difference log GDP") +
606+
coord_cartesian(xlim = c(0, 1),
607+
ylim = c(-0.3, 0.2))+
608+
theme_minimal()
609+
610+
```
611+
612+
This plot is counter-factual. There is no raw data here. Instead we are seeing through the model’s eyes and imagining comparisons between iden- tical nations inside and outside Africa, as if we could independently manipulate continent and also terrain ruggedness. Below the horizontal dashed line, African nations have lower expected GDP. This is the case for most terrain ruggedness values. But at the highest rugged- ness values, a nation is possibly better off inside Africa than outside it. Really it is hard to find any reliable difference inside and outside Africa, at high ruggedness values. It is only in smooth nations that being in Africa is a liability for the economy.
613+
614+
Simple interactions are symmetric. Within the model, there’s no basis to prefer one interpretation over the other, because in fact they are the same interpretation. But when we reason causally about models, our minds tend to prefer one interpretation over the other, because it’s usually easier to imagine manipulating one of the predictor variables instead of the other. In this case, it’s hard to imagine manipulating which continent a nation is on. But it’s easy to imagine manipulating terrain ruggedness, by flattening hills or blasting tunnels through mountains. Africa’s unusually positive relationship with terrain ruggedness is due to historical causes, not contemporary terrain, then tunnels might improve economies in the present. At the same time, continent is not really a cause of economic activity. Rather there are historical and political factors associated with continents, and we use the continent variable as a proxy for those factors. It is manipulation of those other factors that would matter.
615+
616+
617+
### Continuous - Continuous Interactions
618+
619+
Richard McElreath writes:
620+
621+
>I want to convince the reader that interaction effects are difficult to interpret. They are nearly impossible to interpret, using only posterior means and standard deviations. Once interactions exist, multiple parameters are always in play at the same time. It is hard enough with the simple, categorical interactions from the terrain ruggedness example. Once we start modeling interactions among more than one continuous variables, it gets much harder. It’s one thing to make a slope conditional upon a category. In such a context, the model reduces to estimating a different slope for each category. But it’s quite a lot harder to understand that a slope varies in a continuous fashion with a continuous variable. Interpretation is much harder in this case, even though the mathematics of the model are essentially the same as in the categorical case.
622+
623+
#### Tulips Example
624+
625+
The data in this example are sizes of blooms from beds of tulips grown in greenhouses, under different soil and light conditions. The blooms column will be our outcome—what we wish to predict. The water and shade columns will be our predictor variables.
626+
627+
Since both light and water help plants grow and produce blooms, it stands to reason that the independent effect of each will be to produce bigger blooms. But we’ll also be interested in the interaction between these two variables. In the absence of light, for example, it’s hard to see how water will help a plant—photosynthesis depends upon both light and water. Like- wise, in the absence of water, sunlight does a plant little good. One way to model such an interdependency is to use an interaction effect. In the absence of a good mechanistic model of the interaction, one that uses a theory about the plant’s physiology to hypothesize the functional relationship between light and water, then a simple linear two-way interaction is a good start. But ultimately it’s not close to the best that we could do.
628+
629+
The causal DAG for this model is quite simple we have $\text{Water} \rightarrow \text{Bloom} \leftarrow \text{Shade}$. As before the DAG doesn't tell us the function through which Water and Shade jointly include Bloom. In principle, every unique combination of Water and Shade could have a different mean Bloom. But we'll start simpler.
630+
631+
##### Model with No Interaction
632+
633+
634+
635+
$$ \text{Bloom}_i \sim Normal(\mu_\text{Bloom}_i, \sigma)$$
636+
637+
$$ \mu_{\text{Bloom}_i} = \alpha + \beta_W(\text{Water}_i - \overline{Water}) + \beta_S(\text{Shade}_i - \overline{Shade})$$
638+
```{r}
639+
640+
tulips_clean <- tulips %>%
641+
mutate(water_z = (water - mean(water)),
642+
shade_z = (shade - mean(shade)),
643+
blooms_s = blooms / max(blooms))
644+
645+
ggplot(data = tulips_clean)+
646+
geom_jitter(aes(x = water_z, y = blooms_s, color = shade_z), height = 0, width = .03)
647+
648+
```
649+
650+
651+
652+
##### Model with Interaction

index.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Machine learning techniques are far more powerful than traditional statistics. B
2020
</details>
2121

2222
<details>
23-
<summary>**Businesses say they want data processing, but what they're really saying is they want system automation (e.g. invoices need to be categorized and sent to accounting)?**</summary>
23+
<summary>**Businesses say they want data processing, but what they're really saying is they want system automation (e.g. invoices need to be categorized and sent to accounting).**</summary>
2424

2525

2626
Yes, businesses need lots of automation, but they also need help making decisions. Who to hire, who to let go, what product to sell, at what price to sell, to name a few. These decisions are in a weird gray zone, they require some human judgement but would benefit from computer aid. Statistics are the best set of tools for many of these problems. They allow both humans and computers to add their judgement's to an analysis, getting a better result than either would on their own.

0 commit comments

Comments
 (0)