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?
library(tibble)
library(ggplot2)
library(ggdag)
library(dagitty)
dag_coords <- tibble(name = c("X", "U", "A", "B", "C", "Y", "V"),
x = c(1, 1, 2, 2, 3, 3, 3.5),
y = c(1, 2, 2.5, 1.5, 2, 1, 1.5))
dagify(Y ~ X + C + V,
X ~ U,
U ~ A,
B ~ U + C,
C ~ A + V,
coords = dag_coords) %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(data = . %>% filter(name %in% c("U", "V")),
shape = 1, stroke = 2, color = "black") +
geom_dag_text(color = "black", size = 10) +
geom_dag_edges(edge_color = "black", edge_width = 2,
arrow_directed = grid::arrow(length = grid::unit(15, "pt"),
type = "closed")) +
theme_void()
# There are now 4 paths from X to Y.
new_dag <- dagitty("dag { U [unobserved]
V [unobserved]
X -> Y
X <- U <- A -> C -> Y
U -> B <- C
C <- V -> Y }")
adjustmentSets(new_dag, exposure = "X", outcome = "Y")
## { A }
# Because B is a collider, so the two paths which include B are closed. If we want to make causal inferences of X on Y, now we can only condition on 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?
library(tidyverse)
set.seed(1984)
library(brms)
library(here)
library(ggdist)
n <- 100
dat <- tibble(x = rnorm(n)) %>%
mutate(z = rnorm(n, mean = x, sd = 0.4),
y = rnorm(n, mean = z))
sim_cor <- cor(dat$x, dat$z)
sim_cor
## [1] 0.932523
#In our simulated data, the correlation between x and z is above 0.9. Next we will build a model with x and z as covariates:
b6m2 <- brm(y ~ 1 + x + z, data = dat, family = gaussian,
prior = c(prior(normal(0, 0.2), class = Intercept),
prior(normal(0, 0.5), class = b),
prior(exponential(1), class = sigma)),
iter = 5000, warmup = 2000, chains = 4, cores = 4, seed = 54365)
posterior_samples(b6m2) %>%
as_tibble() %>%
select(-lp__) %>%
pivot_longer(everything()) %>%
ggplot(aes(x = value, y = name)) +
stat_halfeye(.width = c(0.67, 0.89, 0.97))
# In the example of legs, the posterior distributions had significant overlap.
# However, it does not appear in this case though we observe multicollinearity.
# This is due to only z predicts the outcome in this case.
# But in example of legs, both the legs predict the outcome.
# Thus, there is no multicollinearity in this DAG example, and we can see that the posterior for x is centered on zero.
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.
library(magrittr)
library(dplyr)
data(foxes)
fox_dat <- foxes[,-1] %>%
as_tibble() %>%
mutate(across(everything(), standardize))
set.seed(1234)
N<-100
a <- rnorm(N, 0, 0.3)
b <- rnorm(N, 0, 0.5)
plot(NULL, xlim=range(fox_dat$area),ylim=c(-4,4))
xbar<-mean(fox_dat$area)
for(i in 1:N) curve(a[i] + b[i]*(x- xbar),
from=min(fox_dat$area), to = max(fox_dat$area), add = TRUE,
col = col.alpha("black",0.2)
)
m6.3 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_area*area,
a ~ dnorm(0,3),
b_area ~ dnorm(0,5),
sigma ~ dexp(1)
), data = fox_dat
)
precis(m6.3)
## mean sd 5.5% 94.5%
## a -2.446178e-08 0.09199323 -0.1470230 0.1470229
## b_area 1.947062e-02 0.09242001 -0.1282344 0.1671757
## sigma 9.912636e-01 0.06466607 0.8879147 1.0946125
plot(precis(m6.3))
# There is no significant causal effect on the 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?
m6.4 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood,
a ~ dnorm(0,3),
b_avgfood ~ dnorm(0,5),
sigma ~ dexp(1)
), data = fox_dat
)
precis(m6.4)
## mean sd 5.5% 94.5%
## a -2.308364e-06 0.09198029 -0.1470046 0.1470000
## b_avgfood -2.503161e-02 0.09240701 -0.1727159 0.1226526
## sigma 9.911240e-01 0.06465533 0.8877923 1.0944557
plot(precis(m6.4))
# No adjustment is needed.
# Avgfood food does not have a significant causal impact on weight.
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?
m6.5 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood + b_groupsize*groupsize,
a ~ dnorm(0,3),
b_avgfood ~ dnorm(0,5),
b_groupsize ~ dnorm(0,5),
sigma ~ dexp(1)
), data = fox_dat
)
precis(m6.5)
## mean sd 5.5% 94.5%
## a -6.327216e-07 0.08717126 -0.1393171 0.1393159
## b_avgfood 6.389574e-01 0.20206519 0.3160182 0.9618966
## b_groupsize -7.367768e-01 0.20206519 -1.0597160 -0.4138376
## sigma 9.392599e-01 0.06129382 0.8413005 1.0372192
plot(precis(m6.5))
# This positive causal effect of avgfood is cancelled out because avgfood leads to increase in groupsize, which has a decreasing effect on weight.