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,5

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 = α + Bxi + Czi
#x is animal diversity, z is plant diversity, μ is latitude

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 = α + Dxi + Ezi
#x is amount of funding, z is size of lab, μ is PHD degree
#both slope parameters(D,E)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}\]

#1,3,4,5 are inferentially equivalent because you could compute one posterior distribution from the posterior distribution of another model

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

#This example is between the money one person spent on food and his weight that vanishes when income is included.
N <- 100
income <- rnorm(n = 200, mean = 0, sd = 1)
weight <- rnorm(n = N, mean = income, sd = 2)
money<- rnorm(n = N, mean = income, sd = 1)
d <- data.frame(money, weight, income)
pairs(d)

#This shows the relationship between the money one person spent on food and his weight
c1 <- map(alist(weight ~ dnorm(mu, sigma),mu <- a + bo * money,a ~ dnorm(0, 5),bo ~ dnorm(0, 5),sigma ~ dunif(0, 5)),
data = d)
precis(c1)
##             mean        sd       5.5%      94.5%
## a     -0.1464873 0.1495985 -0.3855747 0.09260001
## bo     0.5181928 0.1046729  0.3509054 0.68548028
## sigma  2.1005684 0.1050283  1.9327129 2.26842383
#This shows the relationship between the money one person spent on food and his weight reduced when income is added
c2 <- map(alist(weight ~ dnorm(mu, sigma),mu <- a + bo * money + bi * income,a ~ dnorm(0, 5),bo ~ dnorm(0, 5),bi ~ dnorm(0, 5),sigma ~ dunif(0, 5)),data = d)
precis(c2)
##             mean        sd       5.5%      94.5%
## a     -0.1644016 0.1475427 -0.4002033 0.07140004
## bo     0.4309933 0.1089767  0.2568275 0.60515911
## bi     0.4158979 0.1682361  0.1470241 0.68477177
## sigma  2.0691742 0.1034586  1.9038273 2.23452110

```

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.

#That the income could be predicted by number of degree and the easiness of work
N <- 100
rho <- 0.6
degree <- rnorm(n = N, mean = 0, sd = 1)
easiness <- rnorm(n = N, mean = rho * degree, sd = sqrt(1 - rho^2))
income <- rnorm(n = N, mean = degree - easiness, sd = 1)
d <- data.frame(income, degree, easiness)
pairs(d)

c1 <- map(alist(income ~ dnorm(mu, sigma),mu <- a + ba * degree,a ~ dnorm(0, 5),ba ~ dnorm(0, 5),sigma ~ dunif(0, 5)),data = d)
precis(c1)
##             mean        sd       5.5%       94.5%
## a     -0.2676977 0.1298697 -0.4752546 -0.06014073
## ba     0.4358747 0.1289998  0.2297080  0.64204132
## sigma  1.2936229 0.0914727  1.1474318  1.43981391
c2 <- map(alist(income ~ dnorm(mu, sigma),mu <- a + bi * easiness,a ~ dnorm(0, 5),bi ~ dnorm(0, 5),sigma ~ dunif(0, 5)),data = d)
precis(c2)
##             mean         sd       5.5%       94.5%
## a     -0.2647642 0.12927917 -0.4713773 -0.05815108
## bi    -0.4731033 0.13383977 -0.6870051 -0.25920152
## sigma  1.2874005 0.09103268  1.1419127  1.43288832
c3 <- map(alist(income ~ dnorm(mu, sigma),mu <- a + ba * degree + bi * easiness,a ~ dnorm(0, 5),ba ~ dnorm(0, 5),bi ~ dnorm(0, 5),sigma ~ dunif(0, 5)),data = d)
precis(c3)
##             mean         sd       5.5%      94.5%
## a     -0.1135524 0.09880226 -0.2714575  0.0443527
## ba     1.0287675 0.11755844  0.8408865  1.2166486
## bi    -1.0844565 0.12255975 -1.2803307 -0.8885824
## sigma  0.9685852 0.06848771  0.8591286  1.0780418
#The more the degree, the higher the income. However, the higher the easiness, the lower the income. And the degree number and the easiness of work is correlated. 

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 higher divorce rate cause a higher marriage rate. We could se using multiple regression by regressing marriage rate on both divorce rate and re-marriage rate. 

```

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)

simplehist(d$LDS)

`` ```

c1 <- 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 = d)
precis(c1)
##              mean         sd        5.5%      94.5%
## a     35.46974010 6.59969528 24.92215239 46.0173278
## bm     0.02512554 0.07355579 -0.09243082  0.1426819
## ba    -1.00140487 0.21436211 -1.34399692 -0.6588128
## bl    -5.81630004 2.19713453 -9.32774537 -2.3048547
## sigma  1.34246548 0.13480081  1.12702775  1.5579032
#states with older median age at marriage or higher percentage LDS 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.

#Gas means gas price, exercise means the number of exercise, dineout means the number of dineout 
#obesity=α+βaGas+βbExercise+βcDineout

```