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.
5-1. In the divorce data, States with high numbers of members of the Church of Jesus Christ of Latter-day Saints (LDS) have much lower divorce rates than the regression models expected. Find a list of LDS population by State and use those numbers as a predictor variable, predicting divorce rate using marriage rate, median age at marriage, and percent LDS population (possibly standardized). You may want to consider transformations of the raw percent LDS variable. Make sure to include the ‘quap’ model, the ‘precis’ table and your explanation of the results.
data(WaffleDivorce)
d_data <- WaffleDivorce
d_data$LDS <- c(0.77, 4.58,6,1.07,1.91,2.61,0.45,0.58,0.45,0.75,0.82,5.3,25.86,0.45,0.68,0.9,1.32,0.8,0.64,0.82,0.72,0.41,0.45,0.59,0.73,1.18,4.73,1.3,0.65,0.38,3.31,0.43,0.85,1.52,0.54,1.24,3.64,0.41,0.4,0.8,1.2,0.77,1.25,66.32,0.74,1.13,3.8,0.96,0.47,11.7)
##variable standardization
d_data$Marriage.standardized <- (d_data$Marriage - mean(d_data$Marriage)) / sd(d_data$Marriage)
d_data$MedianAgeMarriage.standardized <- (d_data$MedianAgeMarriage - mean(d_data$MedianAgeMarriage)) / sd(d_data$MedianAgeMarriage)
d_data$LDS.standardized <- (d_data$LDS - mean(d_data$LDS)) / sd(d_data$LDS)
##model
modela <- map(
alist(
Divorce ~ dnorm(mean = mu, sd = sigma),
mu <- alpha + beta.marriage.rate * Marriage.standardized + beta.median.age.at.marriage * MedianAgeMarriage.standardized + beta.lds * LDS.standardized,
alpha ~ dnorm(mean = 0, sd = 100),
c(beta.marriage.rate, beta.median.age.at.marriage, beta.lds) ~ dnorm(mean = 0, sd = 10),
sigma ~ dunif(min = 0, 10)
),
data = d_data
)
precis(modela)
## mean sd 5.5% 94.5%
## alpha 9.68797028 0.1893620 9.3853332 9.9906074
## beta.marriage.rate 0.01212554 0.2878709 -0.4479477 0.4721988
## beta.median.age.at.marriage -1.37444709 0.2800358 -1.8219984 -0.9268958
## beta.lds -0.62693717 0.2263679 -0.9887168 -0.2651575
## sigma 1.33899428 0.1338988 1.1249982 1.5529904
5-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(1000)
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.7263822
##High positive correlation of 0.72 b/w X & Z.
##Legs Example
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.05291385 0.09639023 -0.2069641 0.1011364
## bX 0.02640418 0.13804371 -0.1942163 0.2470247
## bZ 0.94902707 0.10610182 0.7794559 1.1185983
## sigma 0.96228274 0.06755547 0.8543161 1.0702494
plot(precis(legs))
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:
5-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)
data_fox = foxes
data_fox$area = scale( data_fox$area )
data_fox$food = scale( data_fox$avgfood )
data_fox$groupsize = scale( data_fox$groupsize )
data_fox$weight = scale( data_fox$weight)
model1 = 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_fox )
precis(model1)
## mean sd 5.5% 94.5%
## a -4.702008e-08 0.08360638 -0.1336192 0.1336191
## bA 1.883317e-02 0.09089290 -0.1264312 0.1640976
## sigma 9.912332e-01 0.06466113 0.8878922 1.0945742
library(dplyr)
library(tibble)
library(tidyr)
model1 %>%
extract.prior() %>%
link(model1, 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: Area ") +
theme_minimal()
5-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?
modelb = 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_fox )
precis(modelb)
## mean sd 5.5% 94.5%
## a -6.688077e-07 0.08360019 -0.1336099 0.1336086
## bF -2.421009e-02 0.09088504 -0.1694619 0.1210418
## sigma 9.911442e-01 0.06465863 0.8878073 1.0944812
library(dplyr)
library(tibble)
library(tidyr)
modelb %>%
extract.prior() %>%
link(modelb, 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 = "blue") +
labs(x = "Food (std)", y = "Weight (std)",
caption = "Prior Predictive Simulation: Food") +
theme_minimal()
5-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?
model3 <- 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_fox )
precis(model3)
## mean sd 5.5% 94.5%
## a -3.941216e-07 0.08013798 -0.1280764 0.1280756
## bF 4.772520e-01 0.17912305 0.1909788 0.7635253
## bG -5.735240e-01 0.17914155 -0.8598268 -0.2872212
## sigma 9.420427e-01 0.06175235 0.8433505 1.0407349