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. 
#2. nation. different nation has different education and job market
#3. the quality of car

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 and dryness
#2. no interaction since cylinders and fuel don't interact with each other
#3. people can be affected by either of parents and friends, or both of them
#4. intelligence and manipulative appendages have interaction

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

# 1. μ_i= α + β_H*H_i+β_D*D_i+β_HD*H_i*D_i 

# 2. μ_i= α  +β_C*C_i+β_F*F_i 

# 3. μ_i= α + β_P*_i*P_i+β_F*_i*F_i (T=Type of people, P= Parent's belief, F= Friend's belief)

# 4. μ_i= α +β_S*S_i+β_A*A_i+β_SA*S_i*A_i 

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?

# two-way interaction: WS, WT,ST,  three-way interaction: WST

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

#μi = α + βt*xt + βw*xw + βs*xs + βtws*xt*xw*xs + βtw*xt*xw + βws*xw*xs + βts*xt*xs

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?

library(rethinking)
## Loading required package: rstan
## Loading required package: StanHeaders
## Loading required package: ggplot2
## rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## options(mc.cores = parallel::detectCores()).
## To avoid recompilation of unchanged Stan programs, we recommend calling
## rstan_options(auto_write = TRUE)
## Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
## Loading required package: parallel
## rethinking (Version 2.13)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
## 
##     rstudent
set.seed(200)
N <- 500
bw <- 0.6
bp <- 0.5
bwp <- 0.4
rwp <- 0.8
Preys <- rnorm(n = N, mean = 0, sd = 1)
Wolves <- rnorm(n = N, mean = rwp*Preys, sd = sqrt(1-rwp^2))
Raven <- rnorm(n = N, mean = bp*Preys + bw*Wolves + bwp*Preys*Wolves, sd = 1)
d <- data.frame(Raven, Preys, Wolves)
par(mfrow = c(1,2))
plot(Raven ~ Preys, data = d)
plot(Raven ~ Wolves, data = d)

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?

library(rethinking)
data(tulips)
d2 <- tulips
d2$blooms_std <- d2$blooms / max(d2$blooms)
d2$water_cent <- d2$water - mean(d2$water)
d2$shade_cent <- d2$shade - mean(d2$shade)

bw <- abs(rnorm(nrow(d2),0,0.25))
bs <- (-abs(rnorm(nrow(d2),0,0.25)))
a <- rnorm(1e4, 0.5, 0.25);sum(a<0|a>1)/length(a)
## [1] 0.044
m <- quap(alist(blooms_std ~ dnorm(mu , sigma), mu <- a + bw*water_cent - bs*shade_cent,
        a ~ dnorm(0.5, 0.25), bw ~ dnorm(0, 0.25), bs ~ dnorm(0, 0.25), sigma ~ dexp(1)) , data=d2 )
precis(m)
##            mean         sd      5.5%     94.5%
## a     0.3587682 0.03021836 0.3104734 0.4070630
## bw    0.2050317 0.03688889 0.1460761 0.2639873
## bs    0.1125281 0.03687502 0.0535947 0.1714615
## sigma 0.1581514 0.02144275 0.1238817 0.1924210
par(mfrow=c(1,3))
for (s in -1:1) {idx <- which(d2$shade_cent==s)
    plot( d2$water_cent[idx] , d2$blooms_std[idx], xlim=c(-1,1), ylim=c(0,1),
        xlab="water", ylab="blooms", pch=16, col=rangi2)
    mu <- link(m, data=data.frame( shade_cent=s , water_cent=-1:1))
    for (i in 1:20) lines( -1:1, mu[i,], col=col.alpha("black",0.3))}

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.

pairs(d2)

d2$shade.c <- d2$shade - mean(d2$shade)
d2$water.c <- d2$water - mean(d2$water)

d2$bedb <- d2$bed == "b"
d2$bedc <- d2$bed == "c"


d2$bedx <- coerce_index(d2$bed)

m2 <- map(alist(blooms ~ dnorm(mu, sigma),
    mu <- a + bW*water.c + bS*shade.c + bWS*water.c*shade.c + bBb*bedb + bBc*bedc,
    a ~ dnorm(130, 100), bW ~ dnorm(0, 100), bS ~ dnorm(0, 100), bWS ~ dnorm(0, 100), bBb ~ dnorm(0, 100),
    bBc ~ dnorm(0, 100), sigma ~ dunif(0, 100)), data = d2,
  start = list(a = mean(d2$blooms), bW = 0, bS = 0, bWS = 0, bBb = 0, bBc = 0, sigma = sd(d2$blooms)))
precis(m2)
##            mean        sd      5.5%     94.5%
## a      99.36131 12.757521  78.97233 119.75029
## bW     75.12433  9.199747  60.42136  89.82730
## bS    -41.23103  9.198481 -55.93198 -26.53008
## bWS   -52.15060 11.242951 -70.11901 -34.18219
## bBb    42.41139 18.039255  13.58118  71.24160
## bBc    47.03141 18.040136  18.19979  75.86303
## sigma  39.18964  5.337920  30.65862  47.72067