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.
# Yeast is only active under certain temperature. So temperature is a third variable that would impact yeast during bread baking. 
# Where you receive your education and the quality of the schools you attend is a third variable that could interact with education to predict income. 
# Car's condition is a third variable that would interact with gas to predict car movement.

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.).
# There is an interaction between heat and dryness and onion caramelization. Onion only carmelizes under certain heat condition and dryness condition. 
# There is no interaction in this setence. More cyliners and better fuel injector cause faster car movement. 
# There is an interaction in this setence. There are two ways to acquire political beliefs and we can use it to predict individuals' political beliefs. 
# There is an interaction in this setence. There is an interaction between highly social and have manipulative appendages for intelligent animal species.  

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

#ui=βHHi+βDDi+βHDHiDi (H=heat, D=dryness)
#μi=βCCi+βFFi (C= cylinders, F=fule injector)
#μi=βPPi+βFFi (P=parents, F=friends)
#μi=βSSi+βAAi+βSASiAi (S=social, A=appendages)

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?

# A three-way interation: wather, shade, and temperature 
# A two-way interatction: water and shate 
# A two-way interatction: water and temperature
# A two-way interatction: shade and temperature

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

#μi=α+βWWi+βSSi+βTTi+βWSTWiSiTi+βWSWiSi+βWTWiTi+βSTSiTi

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(purrr)
## Warning: package 'purrr' was built under R version 3.6.3
# μi=α+βPPi+βWWi+βPWPiWi

N <- 500 
rPW <- 0.6 # correlation between prey and wolf
bP <- 0.3 # regression coefficient for prey
bW <- 0.1 # regression coefficient for wolf
bPW <- 0.5 # regression coefficient for prey-by-wolf interaction
# Simulate data
prey <- rnorm(
  n = N, 
  mean = 0, 
  sd = 1
)
wolf <- rnorm(
  n = N, 
  mean = rPW * prey, 
  sd = sqrt(1 - rPW^2)
)
raven <- rnorm(
  n = N, 
  mean = bP*prey + bW*wolf + bPW*prey*wolf, 
  sd = 1
)
d <- data.frame(raven, prey, wolf)
str(d)
## 'data.frame':    500 obs. of  3 variables:
##  $ raven: num  -1.3505 0.3603 -0.5604 0.0874 -0.4389 ...
##  $ prey : num  0.498 -0.843 0.509 -0.208 -0.666 ...
##  $ wolf : num  0.406 -0.243 -1.449 0.897 -0.126 ...
plot(raven~prey, data=d)

plot(raven~wolf, 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?

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.

library(rethinking)
## Loading required package: rstan
## Warning: package 'rstan' was built under R version 3.6.3
## Loading required package: StanHeaders
## Warning: package 'StanHeaders' was built under R version 3.6.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.3
## rstan (Version 2.19.3, 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)
## For improved execution time, we recommend calling
## Sys.setenv(LOCAL_CPPFLAGS = '-march=corei7 -mtune=corei7')
## although this causes Stan to throw an error on a few processors.
## Loading required package: parallel
## Loading required package: dagitty
## rethinking (Version 2.01)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:purrr':
## 
##     map
## The following object is masked from 'package:stats':
## 
##     rstudent
data(tulips)
d <- tulips
str(d)
## '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 ...
d$shade.c <- d$shade - mean(d$shade)
d$water.c <- d$water - mean(d$water)
# Dummy variables
d$bedb <- d$bed == "b"
d$bedc <- d$bed == "c"

m_dummy <- 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 = d,
  start = list(a = mean(d$blooms), bW = 0, bS = 0, bWS = 0, bBb = 0, bBc = 0, sigma = sd(d$blooms))
)
precis(m_dummy)
##            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