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: multicollinearity, 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. Modify the DAG on page 186 to include the variable V, an unobserved cause of C and Y: C ← V → Y. Reanalyze the DAG. Draw the DAG. How many paths connect X to Y? Which must be closed? Which variables should you condition on now?
dag6_1<- dagitty("dag{
U [unobserved]
V [unobserved]
X -> Y
X <- U -> B <- C -> Y
U <- A -> C
C <- V -> Y
}")
coordinates(dag6_1)<-list(
x=c(X=0,Y=2,U=0,A=1,B=1,C=2,V=2.5),
y=c(X=2,Y=2,U=1,A=0.2,B=1.5,C=1,V=1.5)
)
# map the dag
dag6_1 %>% drawdag
# there are 4 distinct paths between X and Y as below:
# 1- X <- U <- A -> C -> Y
# 2- X <- U <- A -> C <- V -> Y
# 3- X <- U -> B <- C -> Y
# 4- X <- U -> B <- C <- V -> Y
# Now, two paths with B (3 and 4) are closed and paths 1 and 2 are open. Thus in the old DAG we could close either A or C. Nevertheless, we see C is a collider in the new dag so we can condition only on A
# Using dagitty to confirm which paths should be closed
dag6_1 %>% adjustmentSets(exposure = "X", outcome = "Y") %>% print
## { A }
6-2. 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?
sim_dat <- tibble(x = rnorm(1e4),
z = rnorm(1e4, x, 0.5),
y = rnorm(1e4, z)) %>%
mutate(across(everything(), standardize))
sim_dat %>%
summarise(correlation = cor(x, z))
## # A tibble: 1 x 1
## correlation
## <dbl>
## 1 0.895
# correlation between X and Z is large
alist(y ~ dnorm(mu, sigma),
mu <- a + bx*x + bz*z,
a ~ dnorm(0, 0.2),
c(bx, bz) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = sim_dat) %>%
precis() %>%
as_tibble(rownames = "estimate") %>%
filter(estimate %in% c("bx", "bz")) %>%
rename(lower_pi = `5.5%`, upper_pi = `94.5%`) %>%
ggplot() +
geom_linerange(aes(xmin = lower_pi, xmax = upper_pi, y = estimate),
size = 1.5, colour = "red") +
geom_point(aes(x = mean, y = estimate),
shape = 21, colour = "grey20", stroke = 1,
size = 5, fill = "purple") +
labs(y = NULL, x = "Estimate", caption = "Coefficient Plot for Simulated Data with
Collinearity") +
theme_minimal()
# # the effect of X is completely hidden as we condition on Z in a pipe.
# We don't see anything new about X once the model knows Z. At the same time, to know that Z is only a mediatory variable, we would need a causal model expressed in a DAG.
# Now, the key difference to the legs example here is that X and Z are causally related as X causes Z.
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-3. 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.
# there could be two paths
#1- area -> avgfood -> weight
#2- area -> avgfood -> groupsize -> weight
##data load
data("foxes")
dat_foxes <- foxes %>%
as_tibble() %>%
mutate(across(-group, standardize))
##model
m_foxes <- alist(weight ~ dnorm(mu, sigma),
mu <- a + Barea*area,
a ~ dnorm(0, 0.2),
Barea ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_foxes)
#let's if our priors are falling outside a realistic range, Using prior predictive simulation
m_foxes %>%
extract.prior() %>%
link(m_foxes, post = .,
data = list(area = seq(-2, 2, length.out = 20))) %>%
as_tibble() %>%
pivot_longer(cols = everything(), values_to = "weight") %>%
add_column(
area = rep(seq(-2, 2, length.out = 20), 1000),
type = rep(as.character(1:1000), each = 20)) %>%
ggplot() +
geom_line(aes(x = area, y = weight, group = type),
alpha = 0.1, colour = 'red') +
labs(x = "Area (std)", y = "Weight (std)",
caption = "Prior Predictive Simulation for Standardised Area on Weight") +
theme_minimal()
precis(m_foxes)
## mean sd 5.5% 94.5%
## a 2.438196e-07 0.08360868 -0.1336226 0.1336231
## Barea 1.883473e-02 0.09089583 -0.1264344 0.1641038
## sigma 9.912662e-01 0.06466650 0.8879166 1.0946157
test <- chisq.test(table(dat_foxes$avgfood, dat_foxes$weight))
test
##
## Pearson's Chi-squared test
##
## data: table(dat_foxes$avgfood, dat_foxes$weight)
## X-squared = 2508.8, df = 2500, p-value = 0.4469
##there is an association between avgfood and weight
6-4. 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?
# two paths again
#1- groupsize -> weight
#2- groupsize -> avgfood -> weight
alist(weight ~ dnorm(mu, sigma),
mu <- a + Bavgf*avgfood + Bgrps*groupsize,
a ~ dnorm(0, 0.2),
c(Bavgf, Bgrps) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_foxes) %>% precis()
## mean sd 5.5% 94.5%
## a 1.311782e-07 0.08013799 -0.1280759 0.1280761
## Bavgf 4.772700e-01 0.17912292 0.1909970 0.7635430
## Bgrps -5.735531e-01 0.17914129 -0.8598554 -0.2872507
## sigma 9.420428e-01 0.06175237 0.8433506 1.0407350
# We observe that groupsize is negatively related to weight, and when groupsize is in the model the effect of avgfood on weight is positive.
6-5. 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?
# two paths again
#1- groupsize -> weight
#2- groupsize -> avgfood -> weight
alist(weight ~ dnorm(mu, sigma),
mu <- a + Bavgf*avgfood + Bgrps*groupsize,
a ~ dnorm(0, 0.2),
c(Bavgf, Bgrps) ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = dat_foxes) %>% precis()
## mean sd 5.5% 94.5%
## a -2.053648e-07 0.08013492 -0.1280713 0.1280709
## Bavgf 4.771578e-01 0.17911765 0.1908932 0.7634223
## Bgrps -5.734867e-01 0.17913549 -0.8597798 -0.2871936
## sigma 9.419998e-01 0.06174534 0.8433189 1.0406808
# We can see that groupsize is negatively related to weight. And when groupsize is in the model the effect of avgfood on weight is positive.