Chapter 5 - Many Variables and Spurious Waffles

This chapter introduced multiple regression, a way of constructing descriptive models for how the mean of a measurement is associated with more than one predictor variable. The defining question of multiple regression is: What is the value of knowing each predictor, once we already know the other predictors? The answer to this question does not by itself provide any causal information. Causal inference requires additional assumptions. Simple directed acyclic graph (DAG) models of causation are one way to represent those assumptions.

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. Problems are labeled Easy (E), Medium (M), and Hard(H).

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

5E1. Which of the linear models below are multiple linear regressions? \[\begin{align} {μ_i = α + βx_i} \tag{1}\\ μ_i = β_xx_i + β_zz_i \tag{2} \\ μ_i = α + β(x_i − z_i) \tag{3} \\ μ_i = α + β_xx_i + β_zz_i \tag{4} \\ \end{align}\]

# μ_i = β_xx_i + β_zz_i \tag{2} \\
# μ_i = α + β(x_i − z_i) \tag{3} \\
# μ_i = α + β_xx_i + β_zz_i \tag{4} \\
# They are multiple linear regressions. 

5E2. Write down a multiple regression to evaluate the claim: Animal diversity is linearly related to latitude, but only after controlling for plant diversity. You just need to write down the model definition.

\[\begin{align} μ_i = α + β_xx_i + p_yy_i \end{align}\]

# Model Definition: x is the animal diversity, y is plant diversity. 

5E3. Write down a multiple regression to evaluate the claim: Neither amount of funding nor size of laboratory is by itself a good predictor of time to PhD degree; but together these variables are both positively associated with time to degree. Write down the model definition and indicate which side of zero each slope parameter should be on.

\[\begin{align} μ_i = α + β_xx_i + β_yy_i \end{align}\]

# Model Definition: x is the amount of funding, y is the size of laboratory. 

5E4. Suppose you have a single categorical predictor with 4 levels (unique values), labeled A, B, C and D. Let Ai be an indicator variable that is 1 where case i is in category A. Also suppose Bi, Ci, and Di for the other categories. Now which of the following linear models are inferentially equivalent ways to include the categorical variable in a regression? Models are inferentially equivalent when it’s possible to compute one posterior distribution from the posterior distribution of another model. \[\begin{align} μ_i = α + β_AA_i + β_BB_i + β_DD_i \tag{1} \\ μ_i = α + β_AA_i + β_BB_i + β_CC_i + β_DD_i \tag{2} \\ μ_i = α + β_BB_i + β_CC_i + β_DD_i \tag{3} \\ μ_i = α_AA_i + α_BB_i + α_CC_i + α_DD_i \tag{4} \\ μ_i = α_A(1 − B_i − C_i − D_i) + α_BB_i + α_CC_i + α_DD_i \tag{5} \\ \end{align}\]

# Tag 1,3,4,5 are inferentially equivalent

5M1. Invent your own example of a spurious correlation. An outcome variable should be correlated with both predictor variables. But when both predictors are entered in the same model, the correlation between the outcome and one of the predictors should mostly vanish (or at least be greatly reduced).

# Example for correlation between height results in sleep well when eating healthy is included.
n = 1000
height = rnorm(n, 0, 1)
sleep = rnorm(n, height, 2)
eat = rnorm(n, height, 1)

df = data.frame(height, sleep, eat)
pairs(df)

m1 <- quap(
  alist(
    height ~ dnorm(mu, sigma),
    mu <- a + bo * sleep,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = df
)
precis(m1)
##             mean         sd        5.5%      94.5%
## a     0.02317201 0.02904235 -0.02324328 0.06958729
## bo    0.19735797 0.01303510  0.17652536 0.21819059
## sigma 0.91621714 0.02048715  0.88347472 0.94895956
m2 <- quap(
  alist(
    height ~ dnorm(mu, sigma),
    mu <- a + bo * sleep + bi * eat,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    bi ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = df
)
precis(m2)
##             mean          sd         5.5%      94.5%
## a     0.02751358 0.020571364 -0.005363436 0.06039059
## bo    0.11032620 0.009636714  0.094924874 0.12572754
## bi    0.47195947 0.014975889  0.448025111 0.49589384
## sigma 0.64895761 0.014509427  0.625768745 0.67214648

5M2. Invent your own example of a masked relationship. An outcome variable should be correlated with both predictor variables, but in opposite directions. And the two predictor variables should be correlated with one another.

# Example of masked relationship of weight from eating healthy and stress. 
N <- 100
rho <- 0.6
eating <- rnorm(n = N, mean = 0, sd = 1)
stress <- rnorm(n = N, mean = rho * eating, sd = sqrt(1 - rho^2))
weight <- rnorm(n = N, mean = stress - eating, sd = 1)
d <- data.frame(weight, eating, stress)
pairs(d)

m3 <- quap(
  alist(
    weight ~ dnorm(mu, sigma),
      mu <- a + bp * eating,
    a ~ dnorm(0, 5),
    bp ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m3)
##              mean        sd       5.5%      94.5%
## a      0.06821838 0.1439175 -0.1617895  0.2982263
## bp    -0.39175786 0.1295058 -0.5987332 -0.1847825
## sigma  1.43682830 0.1015989  1.2744536  1.5992030
m4 <- quap(
  alist(
    weight ~ dnorm(mu, sigma),
    mu <- a + bs * stress,
    a ~ dnorm(0, 5),
    bs ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m4)
##            mean        sd       5.5%     94.5%
## a     0.1149685 0.1440357 -0.1152283 0.3451654
## bs    0.3772968 0.1276351  0.1733113 0.5812824
## sigma 1.4395258 0.1017874  1.2768499 1.6022017
# Masking Relationship
m5 <- quap(
  alist(
    weight ~ dnorm(mu, sigma),
    mu <- a + bp * eating + bs * stress,
    a ~ dnorm(0, 5),
    bs ~ dnorm(0, 5),
    bp ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m5)
##              mean        sd       5.5%      94.5%
## a      0.07183022 0.1095003 -0.1031723  0.2468328
## bs     1.08772081 0.1275584  0.8838578  1.2915838
## bp    -1.11071236 0.1296715 -1.3179524 -0.9034723
## sigma  1.09301778 0.0772874  0.9694976  1.2165380

5M3. It is sometimes observed that the best predictor of fire risk is the presence of firefighters— States and localities with many firefighters also have more fires. Presumably firefighters do not cause fires. Nevertheless, this is not a spurious correlation. Instead fires cause firefighters. Consider the same reversal of causal inference in the context of the divorce and marriage data. How might a high divorce rate cause a higher marriage rate? Can you think of a way to evaluate this relationship, using multiple regression?

# By increasing the size of sample data, we can hypothesize that higher divorce rate would cause higher marriage rate. We add a sample group of unmarried/single people then use multiple regression on divorce rate on marriage rate and rate of marriages following divorce. 

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

options(repos=structure(c(CRAN="http://cran.r-project.org")))
install.packages(c('devtools','coda','mvtnorm'))
## 
## The downloaded binary packages are in
##  /var/folders/m5/rkn4nv217j9fq8q9lj6w575r0000gn/T//RtmpOoo0Sd/downloaded_packages
library(devtools)
library(rethinking)

data("WaffleDivorce")
wd = WaffleDivorce
head(wd)
##     Location Loc Population MedianAgeMarriage Marriage Marriage.SE Divorce
## 1    Alabama  AL       4.78              25.3     20.2        1.27    12.7
## 2     Alaska  AK       0.71              25.2     26.0        2.93    12.5
## 3    Arizona  AZ       6.33              25.8     20.3        0.98    10.8
## 4   Arkansas  AR       2.92              24.3     26.4        1.70    13.5
## 5 California  CA      37.25              26.8     19.1        0.39     8.0
## 6   Colorado  CO       5.03              25.7     23.5        1.24    11.6
##   Divorce.SE WaffleHouses South Slaves1860 Population1860 PropSlaves1860
## 1       0.79          128     1     435080         964201           0.45
## 2       2.05            0     0          0              0           0.00
## 3       0.74           18     0          0              0           0.00
## 4       1.22           41     1     111115         435450           0.26
## 5       0.24            0     0          0         379994           0.00
## 6       0.94           11     0          0          34277           0.00
lds <- c(0.0077, 0.0453, 0.0610, 0.0104, 0.0194, 0.0270, 0.0044, 0.0057, 0.0041, 0.0075, 0.0082, 0.0520, 0.2623, 0.0045, 0.0067, 0.0090, 0.0130, 0.0079, 0.0064, 0.0082, 0.0072, 0.0040, 0.0045, 0.0059, 0.0073, 0.0116, 0.0480, 0.0130, 0.0065, 0.0037, 0.0333, 0.0041, 0.0084, 0.0149, 0.0053, 0.0122, 0.0372, 0.0040, 0.0039, 0.0081, 0.0122, 0.0076, 0.0125, 0.6739, 0.0074, 0.0113, 0.0390, 0.0093, 0.0046, 0.1161)

m_std <- (wd$Marriage - mean(wd$Marriage)) / sd(wd$Marriage)
m_med <- (wd$MedianAgeMarriage - mean(wd$MedianAgeMarriage)) / sd(wd$MedianAgeMarriage)
per_lds <- (lds - mean(lds)) / sd(lds)

m6 <- map(
  alist(
    Divorce ~ dnorm(mu, sigma),
    mu<- a + b_mr * m_std + b_ma * m_med + b_lds * per_lds,
    a ~ dnorm(0, 120),
    c(b_mr, b_ma, b_lds) ~ dnorm(0, 20),
    sigma ~ dunif(0,20)
  ), data=wd)

precis(m6)
##               mean        sd       5.5%      94.5%
## a      9.687975594 0.1894172  9.3852504  9.9907008
## b_mr   0.008580802 0.2878078 -0.4513917  0.4685533
## b_ma  -1.376130743 0.2803028 -1.8241088 -0.9281527
## b_lds -0.625235133 0.2261722 -0.9867020 -0.2637683
## sigma  1.339383182 0.1339381  1.1253242  1.5534422

5M5. One way to reason through multiple causation hypotheses is to imagine detailed mechanisms through which predictor variables may influence outcomes. For example, it is sometimes argued that the price of gasoline (predictor variable) is positively associated with lower obesity rates (outcome variable). However, there are at least two important mechanisms by which the price of gas could reduce obesity. First, it could lead to less driving and therefore more exercise. Second, it could lead to less driving, which leads to less eating out, which leads to less consumption of huge restaurant meals. Can you outline one or more multiple regressions that address these two mechanisms? Assume you can have any predictor data you need. \[\begin{align} μ_i = α + β_GG_i + β_DD_i + β_EE_i \end{align}\]

# G is for the cost of gas, E is for more exercise and D is for less driving.