Multiple regression is no oracle, but only a golem. It is logical, but the relationships it describes are conditional associations, not causal influences. Therefore additional information, from outside the model, is needed to make sense of it. This chapter presented introductory examples of some common frustrations: multi-collinearity, post-treatment bias, and collider bias. Solutions to these frustrations can be organized under a coherent framework in which hypothetical causal relations among variables are analyzed to cope with confounding. In all cases, causal models exist outside the statistical model and can be difficult to test. However, it is possible to reach valid causal inferences in the absence of experiments. This is good news, because we often cannot perform experiments, both for practical and ethical reasons.
Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them.
Finally, upon completion, name your final output .html
file as: YourName_ANLY505-Year-Semester.html and publish
the assignment to your R Pubs account and submit the link to Canvas.
Each question is worth 5 points.
6-1. Use the Waffle House data, data(WaffleDivorce), to find the total causal influence of number of Waffle Houses on divorce rate. Justify your model or models with a causal graph (draw a DAG).
data("WaffleDivorce")
dat_waffle = WaffleDivorce %>%
as_tibble() %>%
select(divorce = Divorce, age = MedianAgeMarriage,
m_rate = Marriage, waffle = WaffleHouses,
south = South) %>%
mutate(across(-south, standardize),
south = south + 1)
# S="south",A="medium age at marriage",M="marriage rate",W="waffle",D="divorce rate"
tribble(
~ name, ~ x, ~ y,
"S", 0, 2,
"A", 1, 2,
"M", 2, 1,
"W", 0, 0,
"D", 1, 0
) %>%
dagify(
D ~ A + W,
W ~ S,
A ~ S,
M ~ A,
coords = .) %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_node(colour = "black", shape = 1, size = 20) +
geom_dag_text(aes(label = name), color = "black", size = 8) +
geom_dag_edges(edge_color = "green") +
labs(caption = "The total causal influence of number of Waffle Houses on divorce rate.") +
theme_void()
6-2. Build a series of models to test the implied conditional independencies of the causal graph you used in the previous problem. If any of the tests fail, how do you think the graph needs to be amended? Does the graph need more or fewer arrows? Feel free to nominate variables that aren’t in the data.
dagitty("dag{
waffle -> divorce <- age <- south -> waffle
age -> m_rate}") %>%
impliedConditionalIndependencies()
## age _||_ wffl | soth
## dvrc _||_ m_rt | age
## dvrc _||_ soth | age, wffl
## m_rt _||_ soth | age
## m_rt _||_ wffl | soth
## m_rt _||_ wffl | age
test_independence = function(model.input, coeff.input, depth.output = 1){
suppressWarnings(
precis(model.input, depth = depth.output) %>%
as_tibble(rownames = "estimate") %>%
filter(estimate %in% {{coeff.input}}) %>%
mutate(across(where(is.numeric), round, digits = 2)) %>%
knitr::kable()
)
}
alist(
waffle ~ dnorm(mu, sigma),
mu <- a[south] + Bage*age,
a[south] ~ dnorm(0, 0.5),
c(Bage) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_waffle) %>%
test_independence(coeff.input = "Bage")
| estimate | mean | sd | 5.5% | 94.5% |
|---|---|---|---|---|
| Bage | 0.03 | 0.1 | -0.13 | 0.2 |
alist(
m_rate ~ dnorm(mu, sigma),
mu <- a + Bdivorce*divorce + Bage*age,
a ~ dnorm(0, 0.2),
c(Bdivorce, Bage) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_waffle) %>%
test_independence(coeff.input = "Bdivorce")
| estimate | mean | sd | 5.5% | 94.5% |
|---|---|---|---|---|
| Bdivorce | -0.06 | 0.12 | -0.25 | 0.13 |
alist(
divorce ~ dnorm(mu, sigma),
mu <- a[south] + Bage*age + Bwaffle*waffle,
a[south] ~ dnorm(0, 0.5),
c(Bage, Bwaffle) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_waffle) %>%
test_independence(depth.output = 2, coeff.input = c("a[1]", "a[2]"))
| estimate | mean | sd | 5.5% | 94.5% |
|---|---|---|---|---|
| a[1] | -0.08 | 0.14 | -0.30 | 0.13 |
| a[2] | 0.20 | 0.23 | -0.17 | 0.57 |
alist(
waffle ~ dnorm(mu, sigma),
mu <- a + Bm_rate*m_rate + Bage*age,
a ~ dnorm(0, 0.2),
c(Bm_rate, Bage) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_waffle) %>%
test_independence(coeff.input = "Bm_rate")
| estimate | mean | sd | 5.5% | 94.5% |
|---|---|---|---|---|
| Bm_rate | -0.09 | 0.18 | -0.38 | 0.2 |
6-3. For the DAG you made in the previous problem, can you write a data generating simulation for it? Can you design one or more statistical models to produce causal estimates? If so, try to calculate interesting counterfactuals. If not, use the simulation to estimate the size of the bias you might expect. Under what conditions would you, for example, infer the opposite of a true causal effect?
tribble(
~ name, ~ x, ~ y,
"area", 1, 2,
"avgfood", 0, 1,
"groupsize",2, 1,
"weight", 1, 0
) %>%
dagify(
avgfood ~ area,
groupsize ~ avgfood,
weight ~ avgfood + groupsize,
coords = .) %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_node(internal_colour = "white", size = 20,
alpha = 0.8, colour = "black") +
geom_dag_text(aes(label = abbreviate(name)), color = "white", size = 5) +
geom_dag_edges(edge_color = "green") +
labs(caption = "Directed acyclic graph for the foses data.") +
theme_void()
All three problems below are based on the same data. The data in data(foxes) are 116 foxes from 30 different urban groups in England. These foxes are like street gangs. Group size varies from 2 to 8 individuals. Each group maintains its own urban territory. Some territories are larger than others. The area variable encodes this information. Some territories also have more avgfood than others. We want to model the weight of each fox. For the problems below, assume the following DAG:
6-4. Use a model to infer the total causal influence of area on weight. Would increasing the area available to each fox make it heavier (healthier)? You might want to standardize the variables. Regardless, use prior predictive simulation to show that your model’s prior predictions stay within the possible outcome range.
data(foxes)
foxes$avgfood = (foxes$avgfood-mean(foxes$avgfood))/sd(foxes$avgfood)
foxes$groupsize = (foxes$groupsize-mean(foxes$groupsize))/sd(foxes$groupsize)
foxes$area = (foxes$area -mean(foxes$area ))/sd(foxes$area)
foxes$weight = (foxes$weight-mean(foxes$weight))/sd(foxes$weight)
N = 100
a = rnorm(N, 0, 0.3)
b = rnorm(N, 0, 0.5)
plot(NULL, xlim=range(foxes$area),ylim=c(-4,4))
xbar=mean(foxes$area)
for(i in 1:N) curve(a[i] + b[i]*(x- xbar),
from=min(foxes$area), to = max(foxes$area), add = TRUE,
col = col.alpha("blue",0.3)
)
model1 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_area*area,
a ~ dnorm(0,0.3),
b_area ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model1)
## mean sd 5.5% 94.5%
## a 1.036672e-06 0.08798903 -0.1406224 0.1406245
## b_area 1.883443e-02 0.09089575 -0.1264345 0.1641034
## sigma 9.912653e-01 0.06466635 0.8879159 1.0946146
6-5. Now infer the causal impact of adding food to a territory. Would this make foxes heavier? Which covariates do you need to adjust for to estimate the total causal influence of food?
model2 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model2)
## mean sd 5.5% 94.5%
## a -1.512120e-07 0.08797913 -0.1406078 0.1406075
## b_avgfood -2.421171e-02 0.09088495 -0.1694634 0.1210400
## sigma 9.911432e-01 0.06465846 0.8878065 1.0944799
6-6. Now infer the causal impact of group size. Which covariates do you need to adjust for? Looking at the posterior distribution of the resulting model, what do you think explains these data? That is, can you explain the estimates for all three problems? How do they go together?
model3 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood + b_group *groupsize,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
b_group ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model3)
## mean sd 5.5% 94.5%
## a -1.895617e-06 0.08397562 -0.1342112 0.1342074
## b_avgfood 4.772261e-01 0.17913284 0.1909372 0.7635150
## b_group -5.735219e-01 0.17915107 -0.8598399 -0.2872039
## sigma 9.421071e-01 0.06176290 0.8433981 1.0408161
model4 = quap(
alist(
groupsize ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model4)
## mean sd 5.5% 94.5%
## a 5.832897e-05 0.03959950 -0.06322931 0.06334597
## b_avgfood 8.957865e-01 0.04000068 0.83185772 0.95971535
## sigma 4.302644e-01 0.02818190 0.38522433 0.47530457
6-7. Sometimes, in order to avoid multicollinearity, people inspect pairwise correlations among predictors before including them in a model. This is a bad procedure, because what matters is the conditional association, not the association before the variables are included in the model. To highlight this, consider the DAG X → Z → Y. Simulate data from this DAG so that the correlation between X and Z is very large. Then include both in a model prediction Y. Do you observe any multicollinearity? Why or why not? What is different from the legs example in the chapter?
N = 100
X = rnorm(N, mean = 0, sd = 1)
Z = rnorm(N, mean = X, sd = 0.5)
Y = rnorm(N, mean = Z, sd = 1)
cor(X,Z)
## [1] 0.8649936
legs = quap(
alist(
Y ~ dnorm(mu,sigma),
mu<-a+bX*X+bZ*Z,
c(a,bX,bZ)~dnorm(0,1),
sigma~dexp(1)
), data=list(X=X,Y=Y,Z=Z)
)
precis(legs)
## mean sd 5.5% 94.5%
## a -0.1585994 0.09874409 -0.3164115 -0.0007872602
## bX -0.1207175 0.21563498 -0.4653438 0.2239088457
## bZ 1.1094661 0.18860376 0.8080409 1.4108913551
## sigma 0.9854146 0.06920610 0.8748099 1.0960192913
plot(precis(legs))