Week 5 - Elemental Confounds

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

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.

library(rethinking)

data(WaffleDivorce)
data <- WaffleDivorce
View(data)

data$LDS_percnt <- c(0.75, 4.53, 6.18, 1, 2.01, 2.82, 0.43, 0.55, 0.38,
0.75, 0.82, 5.18, 26.35, 0.44, 0.66, 0.87, 1.25, 0.77, 0.64, 0.81,
0.72, 0.39, 0.44, 0.58, 0.72, 1.14, 4.78, 1.29, 0.61, 0.37, 3.34,
0.41, 0.82, 1.48, 0.52, 1.2, 3.85, 0.4, 0.37, 0.83, 1.27, 0.75,
1.21, 67.97, 0.74, 1.13, 3.99, 0.92, 0.44, 11.5 )

m_5_1 <- quap(
alist(
Divorce ~ dnorm(mu,sigma),
mu <- a + bm*Marriage + ba*MedianAgeMarriage + bl*LDS_percnt,
a ~ dnorm(0,100),
c(bm,ba,bl) ~ dnorm(0,10),
sigma ~ dunif(0,10)
),
data=data )
precis( m_5_1 )
##               mean         sd        5.5%       94.5%
## a     38.478966044 6.93380670 27.39740374 49.56052835
## bm     0.003444499 0.07568782 -0.11751926  0.12440825
## ba    -1.099650693 0.22494427 -1.45915508 -0.74014631
## bl    -0.061298914 0.02227601 -0.09690028 -0.02569755
## sigma  1.340038445 0.13400705  1.12586930  1.55420759
# The results show that both median marriage age and LDS percent are inversely related to the divorce rate.

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?

N <- 200
set.seed(771)

rh1 <- 0.9
rh2 <- 0.5
x <- rnorm(N,10,2) 
z <- rh1 * x + sqrt(1 - rh1^2) * rnorm( N , 0 , 1)  
y <- rh2 * z + sqrt(1 - rh2^2) * rnorm( N , 0 , 1) 
dsim <- data.frame(x, y, z)
cor(dsim)
##           x         y         z
## x 1.0000000 0.6950396 0.9729591
## y 0.6950396 1.0000000 0.7083499
## z 0.9729591 0.7083499 1.0000000
m <- quap( alist(
y ~ dnorm( mu , sigma ) ,
mu <- a + bl*x + br*z , 
a ~ dnorm( 0, 30 ) ,
bl ~ dnorm( 10 , 100 ) ,
br ~ dnorm( 10 , 100 ) ,
sigma ~ dexp( 1 )
), data=dsim) 
precis(m)
##              mean       sd       5.5%     94.5%
## a       -6.249604 29.92932  -54.08243  41.58322
## bl     -13.238155 63.58939 -114.86628  88.38997
## br      16.164918 70.21268  -96.04851 128.37835
## sigma 1301.897467      NaN        NaN       NaN
# Yes, there is multicollinearity because there is a big difference between bl and br. It is different from the legs example in the chapter because the variables x and z are correlated but are not very close in values as the left and right 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.

library(dplyr)
library(tibble)
library(tidyr)
library(ggplot2)

data("foxes")
data <- foxes %>% 
  as_tibble() %>% 
  mutate(across(-group, standardize))

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=data) 


precis(m1)
##               mean         sd       5.5%     94.5%
## a     2.744603e-06 0.08360880 -0.1336203 0.1336258
## Ba    1.883437e-02 0.09089598 -0.1264350 0.1641037
## sigma 9.912679e-01 0.06466678  0.8879179 1.0946179
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, colour = "blue") +
  labs(x = "Area (std)", y = "Weight (std)", 
       caption = "Simulation for standardised area on weight.") +
  theme_classic()

#Increasing the area available to each fox would not make it heavier (healthier)

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?

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) 

precis(m2)
##                mean         sd       5.5%     94.5%
## a     -1.457442e-06 0.08360170 -0.1336131 0.1336102
## BF    -2.425487e-02 0.09088696 -0.1695098 0.1210001
## sigma  9.911660e-01 0.06466216  0.8878233 1.0945086
# Adding food to the territory may not make foxes heavier. We need to adjust the covariate between average food and weight.

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?

m3 <- quap(alist(weight ~ dnorm(mu, sigma),  
                  mu <- a + BF*avgfood + BG*groupsize, 
                  a ~ dnorm(0, 0.2), 
                  c(BF, BG) ~ dnorm(0, 0.5), 
                  sigma ~ dexp(1)),data=data) 

precis(m3)
##                mean         sd       5.5%      94.5%
## a      4.596992e-06 0.08013802 -0.1280714  0.1280806
## BF     4.772507e-01 0.17912314  0.1909773  0.7635241
## BG    -5.735248e-01 0.17914162 -0.8598277 -0.2872219
## sigma  9.420432e-01 0.06175244  0.8433509  1.0407355
#