Chapter 6 - The Haunted DAG & The Causal Terror

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.

Questions

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(rethinking)
library(dagitty)
library(tidygraph)
m1= dagitty("dag{
U [unobserved]
V [unobserved]
X -> Y
X <- U -> B <- C -> Y
U <- A -> C
C <- V -> Y
}")
coordinates(m1)=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)
)


m1 %>% drawdag

# The paths between X and Y are:(1) X -> Y (2) X <- U <- A -> C -> Y (3) X <- U <- A -> C <- V -> Y(4) X <- U -> B <- C -> Y (5) X <- U -> B <- C <- V -> Y

m1 %>% adjustmentSets(exposure="X",outcome="Y") %>% print
## { A }
#A should be closed

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?

n=100 
b_xz=0.9
b_zy=0.1

set.seed(900)
x=rnorm(n)
z=rnorm(n, x*b_xz)
y=rnorm (n, z*b_zy)

data= data.frame(x,y,z)
cor(data)
##            x          y         z
## x 1.00000000 0.01420212 0.6636059
## y 0.01420212 1.00000000 0.1640509
## z 0.66360587 0.16405095 1.0000000
library(rethinking)


model= quap( alist( 
  y ~ dnorm( mu , sigma ), 
  mu <- a + b_xz*x + b_zy*z,
  a ~ dnorm( 0 , 100 ), 
  c(b_xz,b_zy) ~ dnorm( 0 , 100 ), 
  sigma ~ dexp( 1 ) ), 
  data=data )


precis(model)
##             mean         sd        5.5%       94.5%
## a     -0.2468408 0.09958666 -0.40599950 -0.08768207
## b_xz  -0.1716382 0.13205608 -0.38268930  0.03941295
## b_zy   0.2160299 0.10175477  0.05340614  0.37865368
## sigma  0.9712900 0.06818546  0.86231649  1.08026357
plot(precis(model))

post = extract.samples(model) 
plot( z ~ x , post , col=col.alpha(rangi2,0.1) , pch=16 )

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.

data$area = scale( data$area )
data$food = scale( data$avgfood )
data$groupsize = scale( data$groupsize )
data$weight = scale( data$weight)

m1 = quap(
    alist(
        weight ~ dnorm( mu , sigma ) ,
        mu <- a + bA*area ,
        a ~ dnorm( 0 , 0.2 ) ,
        bA ~ dnorm( 0 , 0.5 ) ,
        sigma ~ dexp( 1 )
) , data=data )

precis(m1)
##                mean         sd       5.5%     94.5%
## a     -2.574194e-06 0.08360862 -0.1336253 0.1336202
## bA     1.883444e-02 0.09089576 -0.1264345 0.1641034
## sigma  9.912654e-01 0.06466637  0.8879160 1.0946147
library(dplyr)
library(tibble)
library(tidyr)
m1 %>% 
  extract.prior() %>% 
  link(m1, 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 = "Figure 9: Prior predictive simulation for standardised area on weight.") +
  theme_minimal()

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?

m2 = quap(
    alist(
        weight ~ dnorm( mu , sigma ) ,
        mu <- a + bF*food,
        a ~ dnorm( 0 , 0.2 ) ,
        bF ~ dnorm( 0 , 0.5 ) ,
        sigma ~ dexp( 1 )
) , data=data )

precis(m2)
##                mean         sd       5.5%     94.5%
## a      0.0002124658 0.08360409 -0.1334030 0.1338279
## bF    -0.0242665516 0.09088999 -0.1695263 0.1209932
## sigma  0.9912001829 0.06466773  0.8878487 1.0945517
library(dplyr)
library(tibble)
library(tidyr)
m2 %>% 
  extract.prior() %>% 
  link(m2, post = .,
    data = list(food = seq(-2, 2, length.out = 20))) %>% 
  as_tibble() %>%
  pivot_longer(cols = everything(), values_to = "weight") %>%
  add_column(
    food = rep(seq(-2, 2, length.out = 20), 1000),
    type = rep(as.character(1:1000), each = 20)) %>% 
  ggplot() +
  geom_line(aes(x = food, y = weight, group = type), 
            alpha = 0.1, colour = "red") +
  labs(x = "Food (std)", y = "Weight (std)", 
       caption = "Figure 9: Prior predictive simulation for standardised food on weight.") +
  theme_minimal()

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?

# groupsize on weight
#need to include avgfood
m3 <- quap(
    alist(
        weight ~ dnorm( mu , sigma ) ,
        mu <- a + bF*food + bG*groupsize,
        a ~ dnorm( 0 , 0.2 ) ,
        bF ~ dnorm( 0 , 0.5 ) ,
        bG ~ dnorm( 0 , 0.5 ) ,
        sigma ~ dexp( 1 )
) , data=data )

precis(m3)
##                mean         sd       5.5%      94.5%
## a     -3.260706e-07 0.08013799 -0.1280763  0.1280757
## bF     4.772541e-01 0.17912305  0.1909809  0.7635273
## bG    -5.735265e-01 0.17914155 -0.8598293 -0.2872237
## sigma  9.420428e-01 0.06175238  0.8433506  1.0407351