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. 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 = β_xx_i + β_zz_i \tag{3} \\ μ_i = α + β(x_i − z_i) \tag{4} \\ μ_i = α + β_xx_i + β_zz_i \tag{5} \\ \end{align}\]

# 2, 3, 4 and 5 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.

#μi=α+βAAi+βPPi  

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.

#μi=α+βFFi+βLLi

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}\]

# 1, 3, 4 and 5.

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).

N <- 150 
set.seed(111)
cases <- rnorm(N, 30000, 500)
calls <- rnorm(N, 0.95 * cases, 150)
sales <- rnorm(N, 0.7 * cases, 200)


df_5m1 <- data.frame(cases, calls, sales)
pairs(df_5m1)

# calls ~ sales
m1 <- lm(calls ~ sales, df_5m1)
precis(m1)
##                    mean           sd         5.5%     94.5%
## (Intercept) 6165.796683 1.095749e+03 4414.5776733 7917.0157
## sales          1.064433 5.216302e-02    0.9810665    1.1478
# calls ~ sales + cases
m2 <- lm(calls ~ sales + cases, df_5m1)
precis(m2)
##                     mean           sd          5.5%        94.5%
## (Intercept) 131.28569734 621.60267526 -862.15543401 1124.7268287
## sales         0.02767693   0.05601384   -0.06184401    0.1171979
## cases         0.92645916   0.04423169    0.85576838    0.9971499

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.

N <- 500
set.seed(12345)
junkFood <- rnorm(n = N, mean = 0, sd = 1)
weightGained <- rnorm(n = N, mean = .7 * junkFood, sd = sqrt(1 - .7 ^2))
tempHappiness <- rnorm(n = N, mean = junkFood - weightGained, sd = 1)
df_5m2 <- data.frame(junkFood, weightGained, tempHappiness)
pairs(df_5m2)

# tempHappiness ~ junkFood
m3 <- lm(tempHappiness ~ junkFood, df_5m2)
precis(m3)
##                   mean         sd       5.5%      94.5%
## (Intercept) -0.0568037 0.05575329 -0.1459082 0.03230083
## junkFood     0.3750502 0.05616954  0.2852804 0.46481994
# tempHappiness ~ junkFood + weightGained
m4 <- lm(tempHappiness ~ junkFood + weightGained, df_5m2)
precis(m4)
##                     mean         sd       5.5%       94.5%
## (Intercept)  -0.04930516 0.04514815 -0.1214606  0.02285031
## junkFood      1.08170898 0.06301534  0.9809983  1.18241967
## weightGained -1.01469091 0.06262637 -1.1147799 -0.91460188

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?

#We can use the multiple regression to study the relationship between them, by taking both divorce rate and re-marriage rate. And see how's the impact of divorce rate.

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.

# import the 'WaffleDivorce' data
data(WaffleDivorce)
df_5m4 <- WaffleDivorce
# insert the LDS data which is found at:  https://en.wikipedia.org/wiki/The_Church_of_Jesus_Christ_of_Latter-day_Saints_membership_statistics_(United_States)

df_5m4$LDS <- c(0.0077,0.0458,0.0600,0.0107,0.0191,0.0261,0.0045,0.0058,0.0045,0.0075,0.0082,0.0530,0.2586,0.0045,0.0068,0.0090,0.0132,0.0080,0.0064,0.0082,0.0072,0.0041,0.0045,0.0059,0.0073,0.0118,0.0473,0.0130,0.0065,0.0038,0.0331,0.0043,0.0085,0.0152,0.0054,0.0124,0.0364,0.0041,0.0040,0.0080,0.0120,0.0077,0.0125,0.6632,0.0074,0.0113,0.0380,0.0096,0.0047,0.1170)

# check the structure of the dataframe
str(df_5m4)
## 'data.frame':    50 obs. of  14 variables:
##  $ Location         : Factor w/ 50 levels "Alabama","Alaska",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ Loc              : Factor w/ 50 levels "AK","AL","AR",..: 2 1 4 3 5 6 7 9 8 10 ...
##  $ Population       : num  4.78 0.71 6.33 2.92 37.25 ...
##  $ MedianAgeMarriage: num  25.3 25.2 25.8 24.3 26.8 25.7 27.6 26.6 29.7 26.4 ...
##  $ Marriage         : num  20.2 26 20.3 26.4 19.1 23.5 17.1 23.1 17.7 17 ...
##  $ Marriage.SE      : num  1.27 2.93 0.98 1.7 0.39 1.24 1.06 2.89 2.53 0.58 ...
##  $ Divorce          : num  12.7 12.5 10.8 13.5 8 11.6 6.7 8.9 6.3 8.5 ...
##  $ Divorce.SE       : num  0.79 2.05 0.74 1.22 0.24 0.94 0.77 1.39 1.89 0.32 ...
##  $ WaffleHouses     : int  128 0 18 41 0 11 0 3 0 133 ...
##  $ South            : int  1 0 0 1 0 0 0 0 0 1 ...
##  $ Slaves1860       : int  435080 0 0 111115 0 0 0 1798 0 61745 ...
##  $ Population1860   : int  964201 0 0 435450 379994 34277 460147 112216 75080 140424 ...
##  $ PropSlaves1860   : num  0.45 0 0 0.26 0 0 0 0.016 0 0.44 ...
##  $ LDS              : num  0.0077 0.0458 0.06 0.0107 0.0191 0.0261 0.0045 0.0058 0.0045 0.0075 ...
# standardize the variables for the model
df_5m4$Marriage.s <- (df_5m4$Marriage - mean(df_5m4$Marriage)) / sd(df_5m4$Marriage)
df_5m4$MedianAgeMarriage.s <- (df_5m4$MedianAgeMarriage - mean(df_5m4$MedianAgeMarriage)) / sd(df_5m4$MedianAgeMarriage)
df_5m4$LDS.s <- (df_5m4$LDS - mean(df_5m4$LDS)) / sd(df_5m4$LDS)

# build the model
# build a model, inspect results
m5 <- map(
  alist(
    Divorce ~ dnorm(mean = mu, sd = sigma),
    mu <- a + b.m * Marriage.s + b.md * MedianAgeMarriage.s + b.lds * LDS.s,
    a ~ dnorm(mean = 0, sd = 100),
    c(b.m, b.md, b.lds) ~ dnorm(mean = 0, sd = 10),
    sigma ~ dunif(min = 0, 10)
  ),
  data = df_5m4
)
precis(m5)
##              mean        sd       5.5%      94.5%
## a      9.68794425 0.1893654  9.3853018  9.9905867
## b.m    0.01214311 0.2878760 -0.4479383  0.4722245
## b.md  -1.37440128 0.2800408 -1.8219605 -0.9268421
## b.lds -0.62692521 0.2263719 -0.9887113 -0.2651392
## sigma  1.33901787 0.1339047  1.1250123  1.5530234

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.

#μi=α+βPPi+βEEi+βRRi