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.
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, 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.
#μ_i = β_aA_i + β_pP_i
# A inidicates animal diversity; 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.
#μ_i = β_fF_i + β_sS_i
#F is amount of funding; S is 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}\]
#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).
n <- 100
x1 <- rnorm(n)
x2 <- rnorm(n, x1, 2)
y <- rnorm(n, x1, 1)
df <- data.frame(x1, x2, y=y)
pairs(df)
library(rethinking)
mod1 <- lm(y ~ ., data=df)
plot( precis(mod1) )
mod2 <- lm(y ~ x2, data=df)
plot( precis(mod2) )
mod2 <- lm(y ~ x2, data=df)
plot( precis(mod2) )
# This is an example of spurious because as we can see when both predictors are entered in the same model, the correlation between the y and x2 significantly reduced.
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 <- 200
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.05557648 0.08342445 -0.1889049 0.07775191
## junkFood 0.32543163 0.07771353 0.2012304 0.44963286
# tempHappiness ~ junkFood + weightGained
m4 <- lm(tempHappiness ~ junkFood + weightGained, df_5m2)
precis(m4)
## mean sd 5.5% 94.5%
## (Intercept) -0.004121453 0.07113351 -0.1178065 0.1095636
## junkFood 0.980432707 0.09959392 0.8212624 1.1396030
## weightGained -0.904655815 0.10296574 -1.0692150 -0.7400967
# Here we can see that junk food increases both weight gained and temporary happiness score, while the weight gaining could decrease the happiness level, therefore the relationship of junk food consumption and happiness are masked.
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?
# Let us hypothesize that a high divorce rate causes a higher marriage rate.
# This can be evaluated by multiple regression with regressing marriage rate on both divorce rate and re-marriage rate.
# Our hypothesis will be supported if divorce rate no longer predicts marriage rate even when the re-marriage rate is known.
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.
# Use protion of LDS data by state from wiki and add a new column to the waffleDivorce data.
data(WaffleDivorce)
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)
simplehist(d$LDS)
simplehist(d$logLDS)
simplehist(d$logLDS.s)
# Build the regression model
m <- 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(m)
## mean sd 5.5% 94.5%
## a 35.43744547 6.77527326 24.60925022 46.2656407
## bm 0.05340808 0.08261803 -0.07863149 0.1854477
## ba -1.02963512 0.22469365 -1.38873897 -0.6705313
## bl -0.60808006 0.29056914 -1.07246566 -0.1436945
## sigma 1.37871932 0.13838749 1.15754938 1.5998893
# In this model, the slope of marriage is close to 0, while the slope of median age at marriage and LDS are negative. We can conclude that 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.
# We can have a multiple regression model as follows, where P is the gasoline price, E is the average time spent on exercise every week, R is the average times of eating in restaurants per week:
#obesity=α+β_Gaspx+β_Excercise+β_DineOut