Chapter 8 - Conditional Manatees

This chapter introduced interactions, which allow for the association between a predictor and an outcome to depend upon the value of another predictor. While you can’t see them in a DAG, interactions can be important for making accurate inferences. Interactions can be difficult to interpret, and so the chapter also introduced triptych plots that help in visualizing the effect of an interaction. No new coding skills were introduced, but the statistical models considered were among the most complicated so far in the book.

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

8E1. For each of the causal relationships below, name a hypothetical third variable that would lead to an interaction effect:

  1. Bread dough rises because of yeast.
  2. Education leads to higher income.
  3. Gasoline makes a car go.
#1. Room temperature: temperature would influence the effectiveness of the yeast.
#2. Years of work experience
#3. The condition of car's gasoline engine

8E2. Which of the following explanations invokes an interaction?

  1. Caramelizing onions requires cooking over low heat and making sure the onions do not dry out.
  2. A car will go faster when it has more cylinders or when it has a better fuel injector.
  3. Most people acquire their political beliefs from their parents, unless they get them instead from their friends.
  4. Intelligent animal species tend to be either highly social or have manipulative appendages (hands, tentacles, etc.).
# 1 (heat & dryness), 3 (parents' belief & friends' belief), 4 (sociality & manipulative appendages) invoke interactions.

8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.

#1. μi = βH*Hi +βD*Di + βHD*HiDi (H represents heat over the cook; D represents dryness of the onion)
#2. μi = βC*Ci +βF*Fi (C represents cylinders; F represents fuel injector)
#3. μi = βTP*TiPi + βTF*TiFi
#4. μi = βS*Si + βA*Ai + βSA*SiAi

8M1. Recall the tulips example from the chapter. Suppose another set of treatments adjusted the temperature in the greenhouse over two levels: cold and hot. The data in the chapter were collected at the cold temperature. You find none of the plants grown under the hot temperature developed any blooms at all, regardless of the water and shade levels. Can you explain this result in terms of interactions between water, shade, and temperature?

# Since we have three predictors here (the dependent variable is bloom here), the interactions include two-ways and three-ways: WST, WT, ST, and WS. Note: W represents water; S represents shade; T represents temperature.

8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?

# the regression model can simulated as following, which include the effect of water, shade, temperature, as well as all the interactions mentioned above.
# μi = α + βW*Wi + βS*Si + βT*Ti + βWT*Wi*Ti + βST*Si*Ti + βWS*Wi*Si  +βWST*Wi*Si*Ti

# Based on the question, μi = 0 when Ti = 1, which can be resembled as:
# μi = α  - α*Ti + βW*Wi - βW*Wi*Ti + βS*Si - βS*Si*Ti + βWS*Wi*Si - βWS*Wi*Si*Ti

8M3. In parts of North America, ravens depend upon wolves for their food. This is because ravens are carnivorous but cannot usually kill or open carcasses of prey. Wolves however can and do kill and tear open animals, and they tolerate ravens co-feeding at their kills. This species relationship is generally described as a “species interaction.” Can you invent a hypothetical set of data on raven population size in which this relationship would manifest as a statistical interaction? Do you think the biological interaction could be linear? Why or why not?

# I believe the biological interaction could be linear because the estimates below are pretty close to the input at the beginning, which indicates a massive interaction.
N <- 500
cPW <- 0.65 # correlation for prey and wolf
P <- 0.35
W <- 0.15
PW <- 0.5 # the coefficient for prey-by-wolf interaction

prey <- rnorm(
  n = N, 
  mean = 0, 
  sd = 1
)
wolf <- rnorm(
  n = N, 
  mean = cPW * prey, 
  sd = sqrt(1 - cPW^2)
)
raven <- rnorm(
  n = N, 
  mean = P*prey + W*wolf + PW*prey*wolf, 
  sd = 1
)
d <- data.frame(raven, prey, wolf)
str(d)
## 'data.frame':    500 obs. of  3 variables:
##  $ raven: num  -1.986 3.03 0.951 -0.173 1.566 ...
##  $ prey : num  -0.559 1.129 0.273 -0.578 0.669 ...
##  $ wolf : num  -0.017 0.761 0.996 0.893 0.152 ...
# estimate the linear model
m <- map(
  alist(
    raven ~ dnorm(mu, sigma),
    mu <- a + P*prey + W*wolf + PW*prey*wolf,
    a ~ dnorm(0, 1),
    W ~ dnorm(0, 1),
    P ~ dnorm(0, 1),
    PW ~ dnorm(0, 1),
    sigma ~ dunif(0, 5)
  ),
  data = d,
  start = list(a = 0, P = 0, W = 0, PW = 0, sigma = 1)
)
precis(m)
##              mean         sd         5.5%      94.5%
## a     -0.01648556 0.05118254 -0.098285143 0.06531402
## P      0.42714511 0.05891631  0.332985475 0.52130475
## W      0.09214312 0.06105469 -0.005434069 0.18972031
## PW     0.54344886 0.04281136  0.475028041 0.61186968
## sigma  1.01815018 0.03219559  0.966695405 1.06960495

8M4. Repeat the tulips analysis, but this time use priors that constrain the effect of water to be positive and the effect of shade to be negative. Use prior predictive simulation. What do these prior assumptions mean for the interaction prior, if anything?

# Based on the output, the negative coefficient for Shade and positive coefficient for Water make the interaction between Shade and Water negative.
data("tulips")
t <- tulips
str(t)
## 'data.frame':    27 obs. of  4 variables:
##  $ bed   : Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 1 1 1 2 ...
##  $ water : int  1 1 1 2 2 2 3 3 3 1 ...
##  $ shade : int  1 2 3 1 2 3 1 2 3 1 ...
##  $ blooms: num  0 0 111 183.5 59.2 ...
#rescale
t$water_d <- t$water - mean(t$water)
t$shade_d <- t$shade - mean(t$shade)
t$blooms_std <- t$blooms/max(t$blooms)

m2 <- quap(
  alist(
    blooms_std ~ dnorm( mu , sigma ) ,
    mu <- a + bw*water_d + bs*shade_d + bws*water_d*shade_d,
    a ~ dnorm( 0.5, 0.25) ,
    bw ~ dnorm( 1 , 0.25 ) ,
    bs ~ dnorm( -1 , 0.25 ) ,
    bws ~ dnorm( 0 , 0.25 ) ,
    sigma ~ dexp( 1 )
  ),
  data = t)
precis(m2)
##             mean         sd        5.5%       94.5%
## a      0.3579970 0.02404814  0.31956345  0.39643061
## bw     0.2205112 0.02952978  0.17331687  0.26770544
## bs    -0.1272589 0.02956730 -0.17451319 -0.08000469
## bws   -0.1431317 0.03587050 -0.20045971 -0.08580372
## sigma  0.1255258 0.01721744  0.09800896  0.15304256

8H1. Return to the data(tulips) example in the chapter. Now include the bed variable as a predictor in the interaction model. Don’t interact bed with the other predictors; just include it as a main effect. Note that bed is categorical. So to use it properly, you will need to either construct dummy variables or rather an index variable, as explained in Chapter 5.

# bed variable
t$BED <- coerce_index(t$bed)
m_index <- quap(
    alist(
    blooms_std ~ dnorm( mu , sigma ) ,
    mu <- a[BED] + bw*water_d + bs*shade_d + bws*water_d*shade_d,
    a[BED] ~ dnorm(0.5, 0.25),
    bw ~ dnorm(0, 0.25),
    bs ~ dnorm(0, 0.25),
    bws ~ dnorm(0, 0.25),
    sigma ~ dunif(0, 100)
    ),
    data = t
)

precis(m_index, depth = 2 )
##             mean         sd        5.5%       94.5%
## a[1]   0.2732830 0.03578135  0.21609753  0.33046855
## a[2]   0.3964084 0.03576370  0.33925106  0.45356567
## a[3]   0.4091222 0.03576263  0.35196664  0.46627784
## bw     0.2074012 0.02542266  0.16677084  0.24803149
## bs    -0.1138317 0.02541785 -0.15445429 -0.07320902
## bws   -0.1438823 0.03105362 -0.19351197 -0.09425259
## sigma  0.1083916 0.01476399  0.08479594  0.13198735