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?

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

drawdag(dagy)

adjustmentSets(dagy, exposure = "X", outcome = "Y")
## { 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?

N = 200
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.9025096
df <- data.frame(Y,X,Z)

model1 = quap(
  alist(
      Y ~ dnorm(mu, sigma),
      mu <- a + bX*X + bZ*Z,
      a ~ dnorm(0,0.2),
      bX ~ dnorm(0,0.5),
      bZ ~ dnorm(0,0.5),
     sigma ~ dexp(1)
  ), data = df
)

precis(model1)
##             mean         sd        5.5%     94.5%
## a     0.09133156 0.06998987 -0.02052577 0.2031889
## bX    0.02288200 0.15831074 -0.23012914 0.2758931
## bZ    0.88854915 0.13645511  0.67046753 1.1066308
## sigma 1.05138323 0.05242174  0.96760316 1.1351633
plot(precis(model1))

# there is no multicolinarity 

# Why 
#The coefficient bX and bZ display narrow posterior distributions, and bZ is clearly right to zero whilte bx is clearly around the center. 

#What is different from the legs example in the chapter?
#Legs example having both left and right leg length could be used to forecast height. But in current example Y is directly correlated only with Z, thus X does not have a strong forecasting power for Y conditional on 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.

library(magrittr)
library(dplyr)

data(foxes)

fox_data <- foxes[,-1] %>%
           as_tibble() %>%
           mutate(across(everything(), standardize))

set.seed(1234)
N<-150
a <- rnorm(N, 0, 0.3)
b <- rnorm(N, 0, 0.5)
plot(NULL, xlim=range(fox_data$area),ylim=c(-4,4))
xbar<-mean(fox_data$area)
for(i in 1:N) curve(a[i] + b[i]*(x- xbar),
                   from=min(fox_data$area), to = max(fox_data$area), add = TRUE,
                   col = col.alpha("black",0.2)
                   )

model2= 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_data
)
precis(model2)
##                mean         sd       5.5%     94.5%
## a      1.803442e-07 0.09199330 -0.1470229 0.1470232
## b_area 1.947078e-02 0.09242009 -0.1282344 0.1671759
## sigma  9.912644e-01 0.06466620  0.8879153 1.0946134
plot(precis(model2))

# we can observe that, Area and weight have 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?

model3 = 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_data
)

precis(model3)
##                    mean         sd       5.5%     94.5%
## a          1.412613e-06 0.09198243 -0.1470043 0.1470071
## b_avgfood -2.503159e-02 0.09240916 -0.1727193 0.1226561
## sigma      9.911471e-01 0.06465909  0.8878094 1.0944848
plot(precis(model3))

# There is not necessary of adjustment needed, since total casual effect of avgfood is our interest.
# As per the precision plot,avgfood and impact of weight have no significant impact

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?

model4 = 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_data
)

precis(model4)
##                      mean         sd       5.5%      94.5%
## a            1.494137e-05 0.08717176 -0.1393024  0.1393322
## b_avgfood    6.389421e-01 0.20206634  0.3160010  0.9618831
## b_groupsize -7.367740e-01 0.20206634 -1.0597150 -0.4138329
## sigma        9.392652e-01 0.06129469  0.8413044  1.0372259
plot(precis(model4))

# Which is a positive causal effect of avgfood is cancelled out it is because the avgfood leads to increase in groupsize, since it has a decreasing effect on weight.