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?

dag_6.1 <- dagitty("dag { U [unobserved]
                          V [unobserved]
                          X -> Y
                          X <- U <- A -> C -> Y
                          U -> B <- C
                          C <- V -> Y }")
coordinates(dag_6.1) = list(
  x = c(X=0, U=0, A=1, B=1, C=2, Y=2, V=2.5),
  y = c(X=0, U=1, A=1.5, B=0.5, C=1, Y=0, V=0.5)
)
drawdag(dag_6.1)

adjustmentSets( dag_6.1 , exposure="X" , outcome="Y" )
## { A }
# There are 4 paths from X to Y:
# X <- U <- A -> C -> Y
# X <- U -> B <- C -> Y
# X <- U <- A -> C <- V -> Y
# X <- U -> B <- C <- V -> Y


#Since B is a collider paths 2 and 4 are already closed. 
# Path 1 and 3 are accessible since A is a fork.
# We might be able to close A and C in the original Dag but C would lead us to a new path through V.Hence We just need to condition on A to make the conclusions about X's casual effect on Y. 

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.7

set.seed(909)
x<- rnorm(n)
z<- rnorm(n,x*b_xz)
y<- rnorm(n,z*b_zy)

d <- data.frame(x,y,z)
cor(d)
##           x         y         z
## x 1.0000000 0.5811009 0.8077636
## y 0.5811009 1.0000000 0.7388561
## z 0.8077636 0.7388561 1.0000000
#Yes, there is multicollinearity  between X & Z since they both are highly correlated. 


m6.1<- quap( alist( 
  y ~ dnorm( mu , sigma ), 
  mu <- a + b_xz*x + b_zy*z,
  a ~ dnorm( 10 , 100 ), 
  c(b_xz,b_zy) ~ dnorm( 2 , 10 ), 
  sigma ~ dexp( 1 ) ), 
  data=d ) 

precis(m6.1)
##              mean        sd       5.5%      94.5%
## a     -0.10915474 0.0902031 -0.2533167 0.03500725
## b_xz  -0.05747396 0.1450075 -0.2892239 0.17427598
## b_zy   0.73251426 0.1074185  0.5608387 0.90418979
## sigma  0.90125831 0.0633088  0.8000786 1.00243799
plot(precis(m6.1))

post <- extract.samples(m6.1)
plot( b_xz ~ b_zy , post , col=col.alpha(rangi2,0.1) , pch=16 )

sum_b_xzb_zy <- post$b_xz + post$b_zy
dens( sum_b_xzb_zy , col=rangi2 , lwd=2 , xlab="sum of b_xz and b_zy" )

# The difference is that using Z will provide extra information about Y However in the legs example, the length of two legs are not providing extra information but the same info.

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(foxes)

library(dplyr)
fox_data <- foxes %>%
  as_tibble() %>%
  select(area, avgfood, weight, groupsize) %>%
  mutate(across(everything(), standardize))

fox_data
## # A tibble: 116 x 4
##      area avgfood    weight groupsize
##     <dbl>   <dbl>     <dbl>     <dbl>
##  1 -2.24  -1.92    0.414       -1.52 
##  2 -2.24  -1.92   -1.43        -1.52 
##  3 -1.21  -1.12    0.676       -1.52 
##  4 -1.21  -1.12    1.30        -1.52 
##  5 -1.13  -1.32    1.12        -1.52 
##  6 -1.13  -1.32   -1.08        -1.52 
##  7 -2.02  -1.52    0.000291    -1.52 
##  8 -2.02  -1.52   -0.371       -1.52 
##  9  0.658 -0.0591  1.35        -0.874
## 10  0.658 -0.0591  0.896       -0.874
## # ... with 106 more rows
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=fox_data )

precis(m1)
##               mean         sd       5.5%     94.5%
## a     4.271738e-06 0.08360861 -0.1336184 0.1336270
## bA    1.890355e-02 0.09089573 -0.1263654 0.1641725
## sigma 9.912652e-01 0.06466634  0.8879159 1.0946145
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) +
  labs(x = "Area (std)", y = "Weight (std)", 
       caption = "Figure 9: Prior predictive simulation for standardised area on weight.") +
  theme_minimal()

plot(precis(m1))

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?

#Here there are two ways but we go for second one since single predictor is enough.
#avgfood -> groupsize -> weight
#Avgfood-> weight
m2 = quap(
    alist(
        weight ~ dnorm( mu , sigma ) ,
        mu <- a + bF*avgfood,
        a ~ dnorm( 0 , 0.2 ) ,
        bF ~ dnorm( 0 , 0.5 ) ,
        sigma ~ dexp( 1 )
) , fox_data )

precis(m2)
##                mean         sd       5.5%     94.5%
## a     -4.044035e-10 0.08360017 -0.1336092 0.1336092
## bF    -2.421163e-02 0.09088502 -0.1694634 0.1210402
## sigma  9.911440e-01 0.06465859  0.8878071 1.0944809

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?

#we can do this by simply adjusting the avg food. 
m3 = quap(
     alist(weight ~ dnorm(mu, sigma),  
                  mu <- a + Bavgf*avgfood + Bgrps*groupsize, 
                  a ~ dnorm(0, 0.1), 
                  c(Bavgf, Bgrps) ~ dnorm(0, 0.5), 
                  sigma ~ dexp(1)),
     data =fox_data) 

precis(m3)
##                mean         sd       5.5%      94.5%
## a     -1.740805e-05 0.06583684 -0.1052374  0.1052026
## Bavgf  4.771789e-01 0.17912624  0.1909006  0.7634572
## Bgrps -5.734505e-01 0.17914476 -0.8597585 -0.2871426
## sigma  9.420598e-01 0.06175516  0.8433631  1.0407565
plot(precis(m3))