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?
modified_dag = dagitty("dag{
U [unobserved]
V [unobserved]
X -> Y
X <- U -> B <- C -> Y
U <- A -> C
C <- V -> Y
}")
drawdag(modified_dag)
#Pathways that connect X to Y :
#1. X <- U <- A -> C -> Y
#2. X <- U -> B <- C <- V -> Y
#3. X <- U <- A -> C <- V -> Y
#4. X <- U -> B <- C -> Y
# And there is a direct path X -> Y
# Compared to the original question , now C is also a collider, if we want to close this backdoor, then we don't need to stratify C
# Since A is a folk , we need to stratify A to close this path.
adjustmentSets( modified_dag , exposure="X" , outcome="Y" )
## { A }
# In conclusion, we need to condition on variable 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?
set.seed(5)
N = 100
X = rnorm(N, mean = 0, sd = 1)
Z = rnorm(N, mean = X, sd = 1)
Y = rnorm(N, mean = Z, sd = 1)
cor(X,Z)
## [1] 0.7179199
# Comment: in this case, the correlation between X and Z is positive and strong (0.72), if we use both X and Z to predict Y, we could observe the muticollinearity
legs = quap(
alist(
Y ~ dnorm(mu,sigma),
mu<-a+bX*X+bZ*Z,
c(a,bX,bZ)~dnorm(0,1),
sigma~dexp(1)
), data=list(X=X,Y=Y,Z=Z)
)
precis(legs)
## mean sd 5.5% 94.5%
## a -0.003807053 0.09437836 -0.1546419 0.1470278
## bX 0.010578731 0.14291409 -0.2178256 0.2389830
## bZ 1.043368512 0.09082528 0.8982122 1.1885249
## sigma 0.947415009 0.06652147 0.8411009 1.0537292
# in the legs example. X is not causal to Z, however in this question, X is causal to z. Since now Z is a pipe to connect between X and Y, if we include Z in the model, then it is going to block the path, which makes the coefficient for X approaching to zero, and then we observed Y is independent of X
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.
set.seed(5)
# Comment in this model, since both the avgfood and groupsize are pipes in between, we don't want to stratify them to block the path between area and weight
data(foxes)
foxes$avgfood <- (foxes$avgfood-mean(foxes$avgfood))/sd(foxes$avgfood)
foxes$groupsize <- (foxes$groupsize-mean(foxes$groupsize))/sd(foxes$groupsize)
foxes$area <- (foxes$area -mean(foxes$area ))/sd(foxes$area)
foxes$weight <- (foxes$weight-mean(foxes$weight))/sd(foxes$weight)
N <- 100
a <- rnorm(N, 0, 0.3)
b <- rnorm(N, 0, 0.5)
plot(NULL, xlim=range(foxes$area),ylim=c(-4,4))
xbar<-mean(foxes$area)
for(i in 1:N) curve(a[i] + b[i]*(x- xbar),
from=min(foxes$area), to = max(foxes$area), add = TRUE,
col = col.alpha("black",0.3)
)
# Construct the model
model1 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_area*area,
a ~ dnorm(0,0.3),
b_area ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model1)
## mean sd 5.5% 94.5%
## a -4.078230e-08 0.08798911 -0.1406236 0.1406235
## b_area 1.883348e-02 0.09089583 -0.1264356 0.1641026
## sigma 9.912662e-01 0.06466650 0.8879166 1.0946158
# Since the coefficient for area is close to zero, we could conclude there's no causal impact for the area to the weight of foxes
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?
# Comment: in this model, we only measure weight versus the avg food, since we don't want to block the path, so don't involve the groupsize as a variable, avgfood is the only factor in this case
model2 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model2)
## mean sd 5.5% 94.5%
## a 4.606536e-07 0.08798001 -0.1406086 0.1406095
## b_avgfood -2.421097e-02 0.09088592 -0.1694642 0.1210423
## sigma 9.911541e-01 0.06466023 0.8878146 1.0944936
# Comment:Since the coefficient for avg food is close to zero, we could conclude there's no causal impact for the avg food to the weight of foxes
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?
# Comment: Since the avg food is a folk in this case, in order to close this backdoor path, we will have to stratify by both avg food and group size to analyze the effect
model3 = quap(
alist(
weight ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood + b_group *groupsize,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
b_group ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model3)
## mean sd 5.5% 94.5%
## a -1.345378e-07 0.08397042 -0.1342011 0.1342008
## b_avgfood 4.772525e-01 0.17912320 0.1909790 0.7635260
## b_group -5.735251e-01 0.17914170 -0.8598282 -0.2872221
## sigma 9.420438e-01 0.06175253 0.8433513 1.0407362
model4 = quap(
alist(
groupsize ~ dnorm(mu, sigma),
mu <- a + b_avgfood*avgfood,
a ~ dnorm(0,0.3),
b_avgfood ~ dnorm(0,0.5),
sigma ~ dexp(1)
), data = foxes
)
precis(model4)
## mean sd 5.5% 94.5%
## a -1.388340e-06 0.03959316 -0.06327891 0.06327613
## b_avgfood 8.957154e-01 0.03999439 0.83179665 0.95963416
## sigma 4.301944e-01 0.02817048 0.38517257 0.47521630
# Comment: In this case the avg food have a significant positive relationship to the weight, and group size have a significant negative relationship to the weight, since avg food has a strong positive relationship to the group size, Which makes the model has no significant causal effect between only weight and avg food