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 = β_xx_i + β_zz_i \tag{3} \\ μ_i = α + β(x_i − z_i) \tag{4} \\ μ_i = α + β_xx_i + β_zz_i \tag{5} \\ \end{align}\]
#The model 2, 3, 4, and 5 are multiple linear regressions, because they all have two predictors xi and zi.
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
#Here 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.
#μ_i = α + β_FF_i + β_SS_i
#Here F is amount of funding and S is size of laboratory.
#Both these slope parameters β_F and β_S 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, and 5 are inferentially equivalent, because it’s possible to compute their posterior distribution from the posterior distribution of each other's model, and their category’s intercept and difference can from each other's category.
#However, the model 2 is non-identifiable because it have the slope for all categories.
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).
#I invent an example of a spurious correlation between the numbers of speeding tickets received and car accidents that vanishes when miles driven is included.
N <- 100
miles_driven <- rnorm(n = N, mean = 0, sd = 1)
tickets <- rnorm(n = N, mean = miles_driven, sd = 2)
accidents <- rnorm(n = N, mean = miles_driven, sd = 1)
d <- data.frame(accidents, tickets, miles_driven)
pairs(d)
#spurious association between the numbers of speeding tickets received and car accidents
m <- map(
alist(
accidents ~ dnorm(mu, sigma),
mu <- a + bo * tickets,
a ~ dnorm(0, 5),
bo ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = d
)
precis(m)
## mean sd 5.5% 94.5%
## a 0.07060623 0.13082400 -0.13847578 0.2796882
## bo 0.13846643 0.06367263 0.03670527 0.2402276
## sigma 1.30731481 0.09244090 1.15957640 1.4550532
#this association vanishes when miles driven is added to the model
m <- map(
alist(
accidents ~ dnorm(mu, sigma),
mu <- a + bo * tickets + bi * miles_driven,
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.002284196 0.10871052 -0.1714562 0.17602460
## bo -0.010762032 0.05708226 -0.1019905 0.08046645
## bi 0.919183909 0.13542007 0.7027565 1.13561133
## sigma 1.081550641 0.07647693 0.9593257 1.20377554
##In this model, miles driven predicts car accidents when the numbers of speeding tickets received is already known, but the numbers of speeding tickets received does not predict car accidents when miles driven is already known. Thus, the bivariate association between car accidents and the numbers of speeding tickets received is spurious.
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.
#I invent an example of a masked relationship involving the prediction of weight loss from the amount of time one spends working out and the amount of food consumed.
N <- 100
rho <- 0.6
work_out <- rnorm(n = N, mean = 0, sd = 1)
food_consumed <- rnorm(n = N, mean = rho * work_out, sd = sqrt(1 - rho^2))
weight_loss <- rnorm(n = N, mean = work_out - food_consumed, sd = 1)
d <- data.frame(weight_loss, work_out, food_consumed)
pairs(d)
#The bivariate associations are weak because of the masking relationship.
m <- map(
alist(
weight_loss ~ dnorm(mu, sigma),
mu <- a + ba * work_out,
a ~ dnorm(0, 5),
ba ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = d
)
precis(m)
## mean sd 5.5% 94.5%
## a -0.1687508 0.12361269 -0.3663077 0.02880618
## ba 0.6444972 0.13001772 0.4367038 0.85229066
## sigma 1.2319960 0.08711603 1.0927678 1.37122425
m <- map(
alist(
weight_loss ~ dnorm(mu, sigma),
mu <- a + bi * food_consumed,
a ~ dnorm(0, 5),
bi ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = d
)
precis(m)
## mean sd 5.5% 94.5%
## a -0.06211833 0.13629851 -0.2799497 0.15571300
## bi -0.30274012 0.13488476 -0.5183120 -0.08716822
## sigma 1.34171479 0.09487334 1.1900889 1.49334070
#Now in the multivariate regression, the masking relationship is resolved.
m <- map(
alist(
weight_loss ~ dnorm(mu, sigma),
mu <- a + ba * work_out + bi * food_consumed,
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.05582939 0.10234906 -0.2194030 0.1077442
## ba 1.08490758 0.12336440 0.8877474 1.2820677
## bi -0.82703553 0.11752262 -1.0148594 -0.6392117
## sigma 1.00732538 0.07122843 0.8934886 1.1211622
##Because work out increases weight loss and food consumed, and food consumed decrease weight loss, the bivariate relationships of work out and food consumed to weight loss are masked. However, this is resolved in the multivariate model - the slopes for work out and food consumed are larger.
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?
#A high divorce rate causes a higher marriage rate by bringing more unmarried/single individuals and they can get re-married. This could be evaluated using multiple regression to predict marriage rate by both divorce rate and re-marriage rate. If at this time divorce rate does not predict marriage rate when the re-marriage rate is known, we can say the high divorce rate causes a higher marriage rate by bringing more unmarried/single individuals.
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)
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 multiple 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.41331589 6.77724537 24.58196883 46.2446630
## bm 0.05337478 0.08263839 -0.07869733 0.1854469
## ba -1.02867626 0.22476183 -1.38788908 -0.6694634
## bl -0.60799248 0.29064636 -1.07250149 -0.1434835
## sigma 1.37908584 0.13848079 1.15776679 1.6004049
##In this model, the slope of marriage was wide with an interval includes zero, but the slopes of both median age at marriage and percentage of LDS population were negative with intervals did not include zero. Thus, states with older median age at marriage or higher percentages of LDS population had lower divorce rates.
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.
#Multiple regression model:
#μ_i=α+β_GG_i+β_EE_i+β_RR_i
#G is the price of gasoline, E is an exercise-associated variable, and R is a restaurant-associated variable. We can use self-reported frequencies of exercise and eating out, or we can even measure calories burned through exercise and calories ingested from restaurants.