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

#Models 2, 3 & 5 are the 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 = α + βAAi + βPP \end{align}\]

#A is animal diversity and P 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 = α + βFFi + βSSi \end{align}\]

#Both beta Bf and Bs should be positive.

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

#Models 1, 3, 4 & 5 are inferentially equivalent because each of them allows the computation of each other’s posterior distribution.

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

Spurious correlation: Executives who say please and thank you more often enjoy better share performance.

N <- 100
executive <- rnorm(n = N, mean = 0, sd = 1)
polite <- rnorm(n = N, mean = executive, sd = 2)
performance <- rnorm(n = N, mean = executive, sd = 1)
d <- data.frame(executive, polite, performance)
pairs(d)

m <- quap(
  alist(
    performance ~ dnorm(mu, sigma),
    mu <- a + bo * polite,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m)
##              mean         sd        5.5%     94.5%
## a     -0.03769411 0.13218720 -0.24895479 0.1735666
## bo     0.12176967 0.06298334  0.02111012 0.2224292
## sigma  1.32122843 0.09342475  1.17191763 1.4705392
m <- quap(
  alist(
    performance ~ dnorm(mu, sigma),
    mu <- a + bo * polite + bi * executive,
    a ~ dnorm(0, 5),
    bo ~ dnorm(0, 5),
    bi ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m)
##              mean         sd       5.5%      94.5%
## a     -0.05244658 0.10412818 -0.2188635 0.11397036
## bo    -0.05175053 0.05433466 -0.1385878 0.03508676
## bi     1.11129012 0.14205222  0.8842632 1.33831701
## sigma  1.04046614 0.07357184  0.9228841 1.15804815

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 <- 100
rho <- 0.6
income <- rnorm(n = N, mean = 0, sd = 1)
spending <- rnorm(n = N, mean = rho * income, sd = sqrt(1 - rho^2))
happiness <- rnorm(n = N, mean = spending - income, sd = 1)
d <- data.frame(income, spending, happiness)
pairs(d)

m <- quap(
  alist(
    happiness ~ dnorm(mu, sigma),
      mu <- a + bp * income,
    a ~ dnorm(0, 5),
    bp ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m)
##              mean         sd       5.5%      94.5%
## a     -0.04436491 0.12583554 -0.2454744  0.1567446
## bp    -0.30386675 0.12364410 -0.5014739 -0.1062596
## sigma  1.25270074 0.08857869  1.1111349  1.3942666
m <- quap(
  alist(
    happiness ~ dnorm(mu, sigma),
    mu <- a + bs * spending,
    a ~ dnorm(0, 5),
    bs ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m)
##             mean         sd        5.5%     94.5%
## a     0.00901256 0.12668725 -0.19345813 0.2114833
## bs    0.26811672 0.12777107  0.06391387 0.4723196
## sigma 1.26248716 0.08927103  1.11981481 1.4051595
m <- quap(
  alist(
    happiness ~ dnorm(mu, sigma),
    mu <- a + bp * income + bs * spending,
    a ~ dnorm(0, 5),
    bs ~ dnorm(0, 5),
    bp ~ dnorm(0, 5),
    sigma ~ dunif(0, 5)
  ),
  data = d
)

precis(m)
##              mean         sd       5.5%      94.5%
## a     -0.02607028 0.10882805 -0.1999985  0.1478580
## bs     0.85328386 0.14681734  0.6186414  1.0879263
## bp    -0.85761530 0.14318383 -1.0864507 -0.6287799
## sigma  1.08284872 0.07656888  0.9604769  1.2052206

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?

#There might be some other predictors that influenced both divorce rate and marriage rate towards the same direction. 

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.

data("WaffleDivorce")
data("foxes")
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)
d$logLDS <- log(d$LDS)
d$logLDS.s <- (d$logLDS - mean(d$logLDS)) / sd(d$logLDS)
m <- map(
  alist(
    Divorce ~ dnorm(mu, sigma),
    mu <- a + bm * Marriage + ba * MedianAgeMarriage + bl * logLDS.s,
    a ~ dnorm(0, 10),
    bm ~ dnorm(0, 10),
    ba ~ dnorm(0, 10),
    bl ~ dnorm(0, 10),
    sigma ~ dunif(0, 5)
  ),
  data = d
)
precis(m)
##             mean         sd         5.5%       94.5%
## a     24.9819512 6.23826759 15.011994709 34.95190766
## bm     0.1347515 0.08174196  0.004112042  0.26539093
## ba    -0.6914287 0.20819295 -1.024161284 -0.35869621
## bl    -0.5572051 0.30001515 -1.036687228 -0.07772293
## sigma  1.4254730 0.14928528  1.186886317  1.66405973

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} Oi=α+βCCi+βDDi+βPPi \end{align}\]

#We can use the calories burnt (C), dine-out calories (D) and gasoline price (P) to predict the obesity rate (O)