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.
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}\]
# 2, 3, 4 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.
# A is an animal diversity and P is a plant diversity
# μ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.
# F is the amount of funding and S is the size of laboratory.
# μi = α + βFFi + βSSi
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).
library(rethinking)
N <- 100 #number of cases
salary <- rnorm(n = 100, mean = 0, sd = 1)
energy <- rnorm(n = N, mean = salary, sd = 2)
education <- rnorm(n = N, mean = salary, sd = 1)
d <- data.frame(education, energy, salary)
# bind all together in data frame
pairs(d)
# Association between number of theater energy seen and education
m <- map(
alist(
education ~ dnorm(mu, sigma),
mu <- a + bo * energy,
a ~ dnorm(0, 5),
bo ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = d
)
precis(m)
## mean sd 5.5% 94.5%
## a -0.0883578 0.13279465 -0.30058930 0.1238737
## bo 0.1877438 0.05868320 0.09395669 0.2815309
## sigma 1.3265795 0.09380318 1.17666389 1.4764951
#association vanishes when person's salary is added to the model
m <- map(
alist(
education ~ dnorm(mu, sigma),
mu <- a + bo * energy + bi * salary,
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.00206620 0.09543109 -0.15458352 0.15045112
## bo 0.01874227 0.04541306 -0.05383657 0.09132112
## bi 1.02620116 0.10509989 0.85823123 1.19417109
## sigma 0.94906544 0.06710883 0.84181257 1.05631832
# We can tell from the results above that when the variable ‘energy’ is added to the model, the correlation of ‘education’ and ‘salary’ largely vanished.
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
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)
# With the masking relationship, the bivariate associations should be weak/widely variable.
m <- 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(m)
## mean sd 5.5% 94.5%
## a -0.05473483 0.1155844 -0.2394610 0.1299913
## ba 0.31629486 0.1204175 0.1238444 0.5087453
## sigma 1.15422749 0.0816145 1.0237918 1.2846632
m <- 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(m)
## mean sd 5.5% 94.5%
## a -0.03641269 0.11432444 -0.2191252 0.1462999
## bi -0.35100691 0.11758423 -0.5389292 -0.1630846
## sigma 1.14353988 0.08086022 1.0143096 1.2727701
m <- 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(m)
## mean sd 5.5% 94.5%
## a -0.0824455 0.09446387 -0.2334170 0.06852601
## ba 0.8537757 0.12429764 0.6551241 1.05242736
## bi -0.8658036 0.12250644 -1.0615925 -0.67001461
## sigma 0.9424236 0.06663919 0.8359213 1.04892585
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 high divorce rate will increase the number of single people which will probably increase the marriage rate. We can bring in the remarriage rate as one predictor of the multiple regression: marriage rate ~ divorce rate + remarriage rate. If the impact of divorce rate largely vanish after bringing in the remarriage rate, then the assumption is proved.
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.
library(dplyr)
data("WaffleDivorce")
d <- WaffleDivorce
# Data retrieved from Wikipedia page "The Church of Jesus Christ of Latter-day Saints membership history" statistics, except for Nevada - https://en.wikipedia.org/wiki/The_Church_of_Jesus_Christ_of_Latter-day_Saints_membership_history#:~:text=The%20membership%20of%20The%20Church,since%20the%200.93%25%20in%201937 and standardized:
d$pct_LDS <- c(0.77, 4.58, 6.00, 1.07, 1.91, 2.61, 0.45, 0.58, 0.45,
0.75, 0.82, 5.30, 25.86, 0.45, 0.68, 0.90, 1.32, 0.80, 0.64, 0.82, 0.72, 0.41, 0.45, 0.59, 0.73, 1.18, 4.73, 1.30, 0.65, 0.38, 3.31, 0.43, 0.85, 1.52, 0.54, 1.24, 3.64, 0.41, 0.40, 0.80, 1.20, 0.77, 1.25, 66.32, 0.74, 1.13, 3.80, 0.96, 0.47, 11.70 )
m <- quap(alist(Divorce ~ dnorm(mu,sigma),
mu <- a + br*Marriage + ba*MedianAgeMarriage + bp*pct_LDS,
a ~ dnorm(0,100), c(br,ba,bp) ~ dnorm(0,10),
sigma ~ dunif(0,10)), data=d)
precis(m)
## mean sd 5.5% 94.5%
## a 38.457262426 6.92724384 27.38618884 49.52833602
## br 0.004678079 0.07573113 -0.11635490 0.12571105
## ba -1.099611582 0.22472378 -1.45876359 -0.74045957
## bp -0.063235016 0.02284595 -0.09974726 -0.02672277
## sigma 1.339008461 0.13390287 1.12500581 1.55301111
# The median age at marriage or percent LDS population are negatively associated with divorce rates.
#From the model, states with lower median age at marriage or lower percentage of LDS have higher divorce rate.
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 = α +β_g*G_i + β_e*E_i + β_r*R_i
# G_i stands for the price of gasoline, E_i stands for the amount of exercise, and R_i stands for the consumption of restaurant meals.
# μ_i represents lower obesity rate. The more exercise you get, the less obese you will be. The less you eat in restaurants, the less obese you will be.