Chapter 5 - Many Variables and Spurious Waffles

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.

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.

Questions

5-1. 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}\]

# Model 1 has only one independent variable, so its not multiple linear regression model.
# Models 2, 3,and 4 have multiple independent variables so they are multiple linear regression models

5-2. 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 = α + βfi + βli
# where as
# f is funding 
# l is size of laboratory
# ui time to Phd
#As the time to PHD can't be negative,slope parameters should be positive

5-3. 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). Plot the correlation before analysis, designate and run the ‘quap’ model, show the ‘precis’ table, plot the results and explain.

n <- 100
a <- rnorm(n)
b <- rnorm(n, a, 2)
c <- rnorm(n, a, 1)

df <- data.frame(a, b, y=c)
pairs(df)

model1 <- lm(c ~ a, data=df)
 precis(model1)
##                     mean        sd       5.5%     94.5%
## (Intercept) -0.005890265 0.1008412 -0.1670540 0.1552735
## a            0.926325843 0.1033042  0.7612258 1.0914259
plot( precis(model1) )

model2 <- lm(c ~ a+b, data=df)
 precis(model2)
##                     mean         sd       5.5%      94.5%
## (Intercept)  0.006136132 0.10128609 -0.1557386 0.16801087
## a            0.964800046 0.10876249  0.7909766 1.13862351
## b           -0.052566495 0.04702524 -0.1277219 0.02258892
plot( precis(model2) )

#In this model, b predicts C but a do not predict c, which means the association between a and c  is spurious.

5-4. 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. Plot the correlation before analysis, designate and run the ‘quap’ model, show the ‘precis’ table, plot the results and explain.

n <- 100
rho <- 0.7                         
p <- rnorm( n )                  
q <- rnorm( n, rho*p, sqrt(1-rho^2) )
r <- rnorm( n, p - q)        
df <- data.frame(p = p, q=q, r=r)
pairs(df)

#The q and p are positive correlated while r and p has negatively correlated, the bi variate relationships of q and r to p are masked

5-5. 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. Make sure to include the ‘quap’ model, the ‘precis’ table and your explanation of the results.

data("WaffleDivorce")
set.seed(5)

data <- WaffleDivorce
data$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)
 
model <- map(
  alist(
    Divorce ~ dnorm(mu, sigma),
    mu <- a + bm * Marriage + ba * MedianAgeMarriage + bl * LDS,
    a ~ dnorm(10, 20),
    bm ~ dnorm(0, 10),
    ba ~ dnorm(0, 10),
    bl ~ dnorm(0, 10),
    sigma ~ dunif(0, 5)
  ),
  data = data
)

plot(precis(model))

precis(model)
##              mean         sd       5.5%      94.5%
## a     35.55357420 6.59932574 25.0065771 46.1005713
## bm     0.02447209 0.07356019 -0.0930913  0.1420355
## ba    -1.00410929 0.21435015 -1.3466822 -0.6615363
## bl    -5.82711748 2.19739462 -9.3389785 -2.3152565
## sigma  1.34267528 0.13484888  1.1271607  1.5581898
#  LDS has a high negative correlation with the divorce rate.The findings demonstrate that the slopes for both median age at marriage and percentage of LDS population are negative, implying that divorce rates are adversely related. The divorce rate is not well predicted by the marriage rate, and states with a higher median age of marriage had lower divorce rates.

5-6. In the divorce example, suppose the DAG is: M → A → D. What are the implied conditional independencies of the graph? Are the data consistent with it? (Hint: use the dagitty package)

library(dagitty)
data(WaffleDivorce)
d <- WaffleDivorce
d$D <-standardize(d$Divorce)
d$M <-standardize(d$Marriage)
d$A  <-standardize(d$MedianAgeMarriage)
D = d$D
M = d$M
A = d$A

maddag <- dagitty( "dag{M -> A -> D}")
impliedConditionalIndependencies(maddag)
## D _||_ M | A
equivalentDAGs(maddag)
## [[1]]
## dag {
## A
## D
## M
## A -> D
## M -> A
## }
## 
## [[2]]
## dag {
## A
## D
## M
## A -> D
## A -> M
## }
## 
## [[3]]
## dag {
## A
## D
## M
## A -> M
## D -> A
## }
#DAG suggests that divorce rate (D) is independent of marriage rate (M) conditional on median age of marriage, as shown by the equivalentDAGs finding.