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?
#There are two paths from X to Y
# 1) X<-U<-A->C<-V->Y: This path has a fork, a collider and a pipe. Because C<-V->Y is not having a relation, this path can be closed by conditioning on A.
# 2) X<-U->B<-C<-V->Y: This path has two colliders. This path can be closed if there is no condition on B or V
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(processx)
library(rstan)
library(coda)
library(mvtnorm)
library(devtools)
library(loo)
N = 1000
set.seed(100)
RT1 = 0.9
RT2 = 0.6
X = rnorm(N)
Z = rnorm(N,RT1*X)
Y = rnorm(N,RT2*Z)
data_1 = data.frame(X, Y, Z)
cor(data_1)
## X Y Z
## X 1.0000000 0.4117249 0.6924074
## Y 0.4117249 1.0000000 0.5692564
## Z 0.6924074 0.5692564 1.0000000
pairs(data_1)
model_1 <- quap( alist(
Y ~ dnorm( mu , sigma ) ,
mu <- a + bX*X + bZ*Z ,
a ~ dnorm( 0, 100 ) ,
bX ~ dnorm( 0 , 100 ) ,
bZ ~ dnorm( 0 , 100 ) ,
sigma ~ dexp( 1 )
), data=data_1)
precis(model_1)
## mean sd 5.5% 94.5%
## a -0.01389909 0.03305798 -0.06673213 0.03893395
## bX 0.04169084 0.04447406 -0.02938730 0.11276898
## bZ 0.51121920 0.03371324 0.45733893 0.56509947
## sigma 1.04523777 0.02335383 1.00791383 1.08256170
#From the result shown it is evident that multicolinearity is present.The incorporation of z provides additional information about Y.
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.
# There could be two paths as there is no direct path from area to weight
#1- area -> avgfood -> weight
#2- area -> avgfood -> groupsize -> weight
library (tidyverse)
##We load the data as
data("foxes")
data_foxes <- foxes %>%
as_tibble() %>%
mutate(across(-group, standardize))
#We build the model as
m_foxes <- alist(weight ~ dnorm(mu, sigma),
mu <- a + bA*area,
a ~ dnorm(0, 0.2),
bA ~ dnorm(0, 0.5),
sigma ~ dexp(1)) %>%
quap(data = data_foxes)
#We now check if our priors are falling outside a realistic range using the code below and incorporating prior predictive simulation
m_foxes %>%
extract.prior() %>%
link(m_foxes, 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 = 'blue') +
labs(x = "Area (std)", y = "Weight (std)",
caption = "Prior Predictive Simulation for Standardised Area on Weight") +
theme_minimal()
precis(m_foxes)
## mean sd 5.5% 94.5%
## a -1.271709e-07 0.08360868 -0.1336229 0.1336227
## bA 1.883367e-02 0.09089583 -0.1264354 0.1641028
## sigma 9.912662e-01 0.06466651 0.8879167 1.0946158
test <- chisq.test(table(data_foxes$avgfood, data_foxes$weight))
test
##
## Pearson's Chi-squared test
##
## data: table(data_foxes$avgfood, data_foxes$weight)
## X-squared = 2508.8, df = 2500, p-value = 0.4469
# From the results we observe that there is a relation between avgfood and 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?
m2 = quap(
alist(
weight ~ dnorm( mu , sigma ) ,
mu <- a + bF*avgfood,
a ~ dnorm( 0 , 0.2 ) ,
bF ~ dnorm( 0 , 0.5 ) ,
sigma ~ dexp( 1 )
) , data=data_foxes )
precis(m2)
## mean sd 5.5% 94.5%
## a -5.195459e-05 0.08357634 -0.1336231 0.1335192
## bF -2.424605e-02 0.09085466 -0.1694494 0.1209572
## sigma 9.908017e-01 0.06460292 0.8875537 1.0940496
library(dplyr)
library(tibble)
library(tidyr)
m2 %>%
extract.prior() %>%
link(m2, post = .,
data = list(avgfood = seq(-2, 2, length.out = 20))) %>%
as_tibble() %>%
pivot_longer(cols = everything(), values_to = "weight") %>%
add_column(
avgfood = rep(seq(-2, 2, length.out = 20), 1000),
type = rep(as.character(1:1000), each = 20)) %>%
ggplot() +
geom_line(aes(x = avgfood, y = weight, group = type),
alpha = 0.1, colour = "blue") +
labs(x = "Food (std)", y = "Weight (std)",
caption = "Prior Predictive Simulation for Standardised Food on Weight.") +
theme_minimal()
# From the results we infer that average food is positively related to 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?
m3 <- quap(
alist(
weight ~ dnorm( mu , sigma ) ,
mu <- a + bF*avgfood + bG*groupsize,
a ~ dnorm( 0 , 0.2 ) ,
bF ~ dnorm( 0 , 0.5 ) ,
bG ~ dnorm( 0 , 0.5 ) ,
sigma ~ dexp( 1 )
) , data=data_foxes )
precis(m3)
## mean sd 5.5% 94.5%
## a -4.848571e-07 0.08013806 -0.1280766 0.1280756
## bF 4.772533e-01 0.17912321 0.1909798 0.7635268
## bG -5.735262e-01 0.17914170 -0.8598293 -0.2872232
## sigma 9.420438e-01 0.06175254 0.8433513 1.0407363
# From the results we infer that groupsize is negatively related to weight. Also, when groupsize is included in the model, the effect of average food is poitive on weight.