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}\]
# Linear models 2, 3 and 4 are multiple linear regression.
# μ_i = β_xx_i + β_zz_i
# μ_i = α + β(x_i − z_i)
# μ_i = α + β_xx_i + β_zz_i
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' 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
# 'f' is amount of funding and '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}\]
# Models 1, 3, 4 and 5 are inferentially equivalent ways to include the categorical variable in a regression
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).
In this model, nightout predicts exam scores on standardized achievement tests when the number of coffees consumed is already known but the number of coffees consumed does not predict exam scores on standardized achievement tests when number of nightouts is already known, which means the association between exam scores and number of coffees consumed is spurious.
There is a (spurious) association between number of coffees and exams.
# I create a fictitious correlation using the method described on page 135 to simulate data.
n = 100
exam <- rnorm(n, mean = 0, sd = 1)
coffee <- rnorm(n, mean = exam, sd = 2)
nightout <- rnorm(n, mean = exam, sd = 1)
df <- data.frame(exam, coffee, nightout)
pairs(df)
m <- map(
alist(
exam ~ dnorm(mu, sigma),
mu <- a + bo * coffee,
a ~ dnorm(0, 5),
bo ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = df
)
precis(m)
## mean sd 5.5% 94.5%
## a 0.01213962 0.08587785 -0.12510977 0.1493890
## bo 0.16598247 0.04242731 0.09817544 0.2337895
## sigma 0.85336557 0.06034167 0.75692792 0.9498032
m <- map(
alist(
exam ~ dnorm(mu, sigma),
mu <- a + bo * coffee + bi * nightout,
a ~ dnorm(0, 5),
bo ~ dnorm(0, 5),
bi ~ dnorm(0, 5),
sigma ~ dunif(0, 5)
),
data = df
)
precis(m)
## mean sd 5.5% 94.5%
## a 0.01522278 0.06604690 -0.09033293 0.1207785
## bo 0.08765235 0.03396154 0.03337525 0.1419295
## bi 0.42796088 0.05149017 0.34566964 0.5102521
## sigma 0.65625583 0.04640457 0.58209236 0.7304193
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.
The slopes for cigarette consumption and illness in the multivariate model grew substantially bigger in amplitude, as can be seen. The bivariate associations of cigarette consumption and feelings of disease to happiness are concealed because cigarette consumption boosts happiness while sentiments of illness decreases happiness.
# I created a fictitious correlation using the method described on page 141 to simulate data.
# The bivariate relationships should be weak due to the masking relationship.
N <- 100
rho <- 0.6
cigarette <- rnorm(n = N, mean = 0, sd = 1)
illness <- rnorm(n = N, mean = rho * cigarette, sd = sqrt(1 - rho^2))
happiness <- rnorm(n = N, mean = cigarette - illness, sd = 1)
d <- data.frame(happiness, cigarette, illness)
pairs(d)
m5.1 <- quap(
alist(
happiness ~ dnorm( mu , sigma ) ,
mu <- a + bN*cigarette ,
a ~ dnorm( 0 , 0.2 ) ,
bN ~ dnorm( 0 , 0.5 ) ,
sigma ~ dexp( 1 )
) ,
data=d
)
precis(m5.1)
## mean sd 5.5% 94.5%
## a 0.1767131 0.10629636 0.006830965 0.3465952
## bN 0.3166882 0.11357447 0.135174226 0.4982021
## sigma 1.2507626 0.08782812 1.110396268 1.3911289
m5.2 <- quap(
alist(
happiness ~ dnorm( mu , sigma ) ,
mu <- a + bM*illness,
a ~ dnorm( 0 , 0.2 ) ,
bM ~ dnorm( 0 , 0.5 ) ,
sigma ~ dexp( 1 )
) ,
data=d
)
precis(m5.2)
## mean sd 5.5% 94.5%
## a 0.1802463 0.10245263 0.01650719 0.3439854
## bM -0.4762016 0.10962786 -0.65140808 -0.3009951
## sigma 1.1893666 0.08356788 1.05580904 1.3229243
m5.3 <- quap(
alist(
happiness ~ dnorm( mu , sigma ) ,
mu <- a + bN*cigarette + bM*illness,
a ~ dnorm( 0 , 0.2 ) ,
bN ~ dnorm( 0 , 0.5 ) ,
bM ~ dnorm( 0 , 0.5 ) ,
sigma ~ dexp( 1 )
) ,
data=d
)
precis(m5.3)
## mean sd 5.5% 94.5%
## a 0.1756149 0.08232101 0.04404999 0.3071797
## bN 0.8240481 0.09871548 0.66628168 0.9818145
## bM -0.9366775 0.09998210 -1.09646820 -0.7768868
## sigma 0.9010343 0.06359410 0.79939867 1.0026700
# By incorporating both predictor variables in the regression, the posterior association of both with the outcome has increased.
plot( coeftab( m5.1 , m5.2 , m5.3 ) , pars=c("bM","bN") )
# Visually comparing this posterior to those of the previous two models helps to see the pattern of change:
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, similar to the firefighter example, may result in a bigger number of unmarried men/women who are more likely to marry again, resulting in a higher marriage rate because divorced persons may marry numerous times throughout their lives. To forecast the overall marriage rate, we may utilize the divorce and remarriage rates and test the r square for multiple regression.
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.
As a result, the slopes of median age at marriage and percentage of LDS population were negative, indicating that higher percentages of LDS or older median age at marriage resulted in lower divorce rates.
data(WaffleDivorce)
d <- WaffleDivorce
# WaffleDivorce$Location
# Nevada data is missing
# data source https://en.wikipedia.org/wiki/The_Church_of_Jesus_Christ_of_Latter-day_Saints_membership_statistics_(United_States)
d$LDS <- c(0.77, 4.58,6,1.07,1.91,2.61,0.45,0.58,0.45,0.75,0.82,5.3,25.86,0.45,0.68,0.9,1.32,0.8,0.64,0.82,0.72,0.41,0.45,0.59,0.73,1.18,4.73,1.3,0.65,0.38,3.31,0.43,0.85,1.52,0.54,1.24,3.64,0.41,0.4,0.8,1.2,0.77,1.25,66.32,0.74,1.13,3.8,0.96,0.47,11.7)
simplehist(d$LDS)
# Because these percentages have a positive skew, I'll log-transform them before standardizing them.
d$pct_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$log_LDS <- log(d$pct_LDS)
simplehist(d$log_LDS)
d$log_LDS.s <- (d$log_LDS - mean(d$log_LDS)) / sd(d$log_LDS)
simplehist(d$log_LDS.s)
m <- quap(
alist(
Divorce ~ dnorm(mu,sigma),
mu <- a + bm*Marriage + ba*MedianAgeMarriage + bp*log_LDS.s,
a ~ dnorm(0,100),
bm ~ dnorm( 0 , 10 ) ,
ba ~ dnorm( 0 , 10 ) ,
bp ~ dnorm(0,10),
sigma ~ dunif(0,10)
),
data=d )
precis( m )
## mean sd 5.5% 94.5%
## a 38.48864087 7.1390522 27.0790566 49.8982252
## bm 0.02940646 0.0843614 -0.1054194 0.1642323
## ba -1.12811262 0.2361656 -1.5055509 -0.7506743
## bp -0.62240372 0.2902323 -1.0862510 -0.1585564
## sigma 1.37610957 0.1376548 1.1561106 1.5961085
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.
# Model 1:
# μ_i = β_gasA_i + β_drivingB_i
# 'A' is gas price and 'B' is hours driving per year per capita.
# Model 2:
# μ_i = β_gasA_i + β_restaurantB_i
# 'A' is gas price and 'B' is restaurant trips per year per capita.