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,5 are all 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

#The A in the model represents animal diversity and P represents 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.

#μi=α+βFFi+βLLi

#In this model, F stands for funding amount and L stands for size of laboratory, both have a positive direction as they are positively associated with time to degree.

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 are inferentially equivalent models.

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

#Enter a model to see the correlation between education level and score on social status largely reduced when entering family income.
N <- 200
income <- rnorm(n = 200, mean = 0, sd = 1)
edu <- rnorm(n = N, mean = income, sd = 2)
sostus <- rnorm(n = N, mean = income, sd = 1)
d <- data.frame(sostus, edu, income)
pairs(d)

m1 <- map(
  alist(
    sostus ~ dnorm(mu, sigma),
    mu <- a + bo * edu,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m1)
##             mean         sd        5.5%         94.5%
## a     -0.1460930 0.09140945 -0.29218291 -3.014890e-06
## bo     0.1363793 0.04302012  0.06762484  2.051338e-01
## sigma  1.2917681 0.06458825  1.18854364  1.394993e+00
m2 <- map(
  alist(
    sostus ~ dnorm(mu, sigma),
    mu <- a + bo * edu + bi * income,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    bi ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m2)
##              mean         sd       5.5%        94.5%
## a     -0.01747663 0.06414441 -0.1199918  0.085038527
## bo    -0.05352201 0.03259797 -0.1056199 -0.001424163
## bi     1.00653849 0.06881002  0.8965668  1.116510200
## sigma  0.89782977 0.04489151  0.8260845  0.969575068

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.

#Enter a model to see the correlation between health status and sleep deprivation on social status largely reduced when entering sick days.
N <- 200
rho <- 0.6
nonsleep <- rnorm(n = N, mean = 0, sd = 1)
sick <- rnorm(n = N, mean = rho * nonsleep, sd = sqrt(1 - rho^2))
health <- rnorm(n = N, mean = nonsleep - sick, sd = 1)
d <- data.frame(health, nonsleep, sick)
pairs(d)

m3 <- map(
  alist(
    health ~ dnorm(mu, sigma),
    mu <- a + ba * nonsleep,
    a ~ dnorm(0, 5),
    ba ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m3)
##              mean         sd       5.5%     94.5%
## a     -0.03178395 0.09292833 -0.1803014 0.1167335
## ba     0.56366398 0.08562612  0.4268169 0.7005111
## sigma  1.31387816 0.06569181  1.2088900 1.4188663
m4 <- map(
  alist(
    health ~ dnorm(mu, sigma),
    mu <- a + bi * sick,
    a ~ dnorm(0, 5),
    bi ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m4)
##              mean         sd       5.5%      94.5%
## a     -0.01969182 0.10066795 -0.1805786  0.1411950
## bi    -0.27049121 0.10029577 -0.4307832 -0.1101992
## sigma  1.42363832 0.07118176  1.3098761  1.5374005
m5 <- map(
  alist(
    health ~ dnorm(mu, sigma),
    mu <- a + ba * nonsleep + bi * sick,
    a ~ dnorm(0, 5),
    ba ~ dnorm(0, 5),
    bi ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m5)
##              mean         sd       5.5%      94.5%
## a     -0.06967862 0.07408098 -0.1880743  0.0487171
## ba     1.10981169 0.08504610  0.9738916  1.2457318
## bi    -0.98767082 0.09193970 -1.1346082 -0.8407334
## sigma  1.04614869 0.05230726  0.9625516  1.1297458

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?

#The probable reason that higher divoice rate leades to higher marriage rate is that divorces create single individuals to enter the dating candidates pool. To test the hypothesis, we can enter a multiple regression model: marriage rate ~ remarriage rate + divoice rate. If the correlation between divorce rate and marriage rate is greatly reduced after remarriage rate is entered, then the hypothesis is true. 

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 waffledivorce data from wikipedia

data("WaffleDivorce")
set.seed(5)

d <- WaffleDivorce
d$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)

#adjust for the skewness
d$logLDS <- log(d$LDS)
d$logLDS.s <- (d$logLDS - mean(d$logLDS)) / sd(d$logLDS)

simplehist(d$LDS)

simplehist(d$logLDS)

simplehist(d$logLDS.s)

#fit a model to check results
m6 <- map(
  alist(
    Divorce ~ dnorm(mu, sigma),
    mu <- a + bm * Marriage + ba * MedianAgeMarriage + bl * logLDS.s,
    a ~ dnorm(10, 20),
    bm ~ dnorm(0, 10),
    ba ~ dnorm(0, 10),
    bl ~ dnorm(0, 10),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m6)
##             mean         sd        5.5%      94.5%
## a     35.4464738 6.77473049 24.61914599 46.2738016
## bm     0.0534216 0.08261297 -0.07860988  0.1854531
## ba    -1.0299865 0.22467479 -1.38906026 -0.6709128
## bl    -0.6077923 0.29055028 -1.07214777 -0.1434368
## sigma  1.3786268 0.13836381  1.15749466  1.5997589

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.

#We can have a multiple linear regression model as: μi=α+βPPi+βEEi+βRRi
#P represents the gas price, E represents excercising time and R stands for time eating in resturants for weeks.