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. 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.

Questions

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}\]

# {2},{3},{4} and {5} 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.

# Here is the model definition, where A_i is for Animal diversity, P_i is for plant diversity, ui is for latitude: 
# μi = a + b*A_i + c*P_i

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.

# Here is the model definition, where F_i is for amount of funding, L_i is for size of funding, ui is for time to PhD degree: 
#μ_i=a + b * (F_i+L_i)

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
# 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).

N <- 200 
set.seed(123)
complaints <- rnorm(N, 30000, 500)
Turn_Down <- rnorm(N, complaints,200)
income <- rnorm(N, complaints,100)


table <- data.frame(complaints, Turn_Down, income)
pairs(table)

m1 <- lm(complaints ~ Turn_Down, table)
precis(m1)
##                     mean           sd         5.5%        94.5%
## (Intercept) 4322.0494919 779.58187027 3076.1270949 5567.9718890
## Turn_Down      0.8556708   0.02597879    0.8141517    0.8971899
m2 <- lm(complaints ~ Turn_Down + income, table)
precis(m2)
##                    mean           sd        5.5%        94.5%
## (Intercept) 738.6231004 378.32351874 133.9890481 1343.2571527
## Turn_Down     0.1964563   0.02672650   0.1537422    0.2391704
## income        0.7787818   0.02830886   0.7335387    0.8240248
# In this example we can see that the credit card application conplaints is related to the number of turn down and the applicants' income. Turn down has been a strong predictor for compalints, but by adding income variable, the predictive power of turn down has reduced significantly.

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 <- 500
set.seed(123)
KnowledgeGained <- rnorm(n = N, mean = 0, sd = 1)
Study <- rnorm(n = N, mean = .7 * KnowledgeGained, sd = sqrt(1 - .7 ^2))
pressure <- rnorm(n = N, mean = Study - KnowledgeGained, sd = 1)
table <- data.frame(Study, KnowledgeGained, pressure)
pairs(table)

m3 <- lm(pressure ~ Study, table)
precis(m3)
##                   mean         sd        5.5%      94.5%
## (Intercept) 0.00488409 0.05475141 -0.08261924 0.09238742
## Study       0.41125740 0.05670034  0.32063931 0.50187549
m4 <- lm(pressure ~ Study + KnowledgeGained, table)
precis(m4)
##                        mean         sd        5.5%       94.5%
## (Intercept)      0.02415452 0.04437708 -0.04676862  0.09507766
## Study            1.07441685 0.06157583  0.97600677  1.17282692
## KnowledgeGained -0.98934885 0.06116830 -1.08710761 -0.89159009
# in this example we can see that, Study hour can increase the pressure that a student has,and also increase the knowledge gained. While the pressure can also reduce the knowledge gained from studying.  

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 indicates a high single rate because people become single after they
# got divorced, then they have the propability to get married again. If the multiple 
# regression model can be written like: marrage rate ~ divorce rate + re-marriage rate + others, and by bringing in re-marriage rate, the impact from divorce rate reduces a lot, then this idea could be 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.

data(WaffleDivorce)
library(readxl)
#Downloded LDS data from following website and saved it as Excel file: https://en.wikipedia.org/wiki/The_Church_of_Jesus_Christ_of_Latter-day_Saints_membership_statistics_(United_States)

LDS<-read_excel("C:/Users/keren/Desktop/LDS.xlsx")
class(LDS$State)
## [1] "character"
class(WaffleDivorce$Location)
## [1] "factor"
WaffleDivorce$Location<-as.character(WaffleDivorce$Location)

LDS_State<-LDS[, c(1,4)]
names(LDS_State)<-c("Location","LDS")

Divorce_LDS<-merge(WaffleDivorce,LDS_State, by.x = "Location")

Divorce_LDS$Marriage.s <- (Divorce_LDS$Marriage - mean(Divorce_LDS$Marriage)) / sd(Divorce_LDS$Marriage)

Divorce_LDS$MedianAgeMarriage.s <- (Divorce_LDS$MedianAgeMarriage - mean(Divorce_LDS$MedianAgeMarriage)) / sd(Divorce_LDS$MedianAgeMarriage)

Divorce_LDS$LDS.s <- (Divorce_LDS$LDS - mean(Divorce_LDS$LDS)) / sd(Divorce_LDS$LDS)

divorce_model <- map(
  alist(
    Divorce ~ dnorm(mean = mu, sd = sigma),
    mu <- a + b * Marriage.s + c * MedianAgeMarriage.s + d * LDS.s,
    a ~ dnorm(mean = 0, sd = 100),
    c(b, c, d) ~ dnorm(mean = 0, sd = 10),
    sigma ~ dunif(min = 0, 10)
  ),
  data = Divorce_LDS
)
precis(divorce_model)
##              mean        sd       5.5%      94.5%
## a      9.68796650 0.1893624  9.3853287  9.9906043
## b      0.01211122 0.2878715 -0.4479630  0.4721855
## c     -1.37444321 0.2800364 -1.8219955 -0.9268909
## d     -0.62692007 0.2263684 -0.9887005 -0.2651397
## sigma  1.33899707 0.1338995  1.1249998  1.5529943
# From the model result we can see that, for the states which has higher divorce rate, they also have lower median age of marriage as well as lower percentage of LDS.

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.

# Let P_i = the price of gas
#     W_i = Average time of weekly exercise
#     E_i = Average times of weekly eating out
#          U_i=a + b*P_i + c* W_i + d*E_i