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.
# Weather condition may affect yeast activity. If it too cold or too hot, bread dough may not rise as yeast would be inactive.
# The major could be interacting with years of education to predict income.
# Current status of the car (working condition) can interact with gasoline 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.).
# It invokes interaction between cooking temperature and onion dryness to predict caramelization.
# Iteraction between number of cylinders and quality of fuel injector.
# Interaction between type of people who acquire political beliefs from either parents or friends AND parent’s / friend’s political beliefs.
# Interaction between sociality status (highly social or no social at all) and manipulative appendages.

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

#1. Caramelized_i ~ Normal(mu_i,sigma)
#   mu_i = alpha + beta_heat*heat_i + beta_dryness*dryness_i + beta_hd*heat_i*dryness_i

#2. Speed_i ~ Nomral(mu_i,sigma)
#   mu_i = alpha + beta_cylinder * cylinder_i + beta_injector * injector_i

#3. Political Belief_i ~ Normal(mu_i,sigma)
#   mu_i = alpha + beta_from_parents*parents_belief_i*(1-belief_source_i) + beta_from_friends*friends_belief_i*belief_source_i

#4. Intelligence_i ~ Normal(mu_i,sigma)
#   mu_i = alpha + beta_socialness* socialness_i + beta_appendages*appendages_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?

# Tempreture interacts with both water and shade. When tempreture is hot, shade and water won't have any effect.

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

# blooms_i ~ Normal(mu_i,sigma)
# mu_i = alpha + beta_shade*shade_i + beta_water*water_i + beta_temprature*temperature_i +
#        beta_ws*water_i*shade_i + 
#        tempreture_i*(beta_shade*shade_i + beta_water*water_i + beta_ws*water_i*shade_I) 

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?

prey <- as.integer(rnorm(100, 100, 20) ) 
wolves <- as.integer(rnorm(100, 15, 8))
ravens <- as.integer(rnorm(100, 2 + 0.6*prey + 6*wolves + 0.5*wolves*prey, 5))

d <- data.frame(prey, wolves, ravens)
par(mfrow=c(1,2))
plot(ravens ~ prey, data=d)
plot(ravens ~ 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)
## Loading required package: rstan
## Warning: package 'rstan' was built under R version 4.0.2
## Loading required package: StanHeaders
## Loading required package: ggplot2
## rstan (Version 2.21.1, 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)
## Loading required package: parallel
## Loading required package: dagitty
## rethinking (Version 2.01)
## 
## Attaching package: 'rethinking'
## 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$blooms_std <- d$blooms / max(d$blooms)
d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)

bw_d<- abs(rnorm(nrow(d),0,0.25))
bs_d<- (-abs(rnorm(nrow(d),0,0.25)))

m_tulip <- quap(
  alist(
    blooms_std ~ dnorm( mu , sigma ) ,
    mu <- a + bw*water_cent + bs*shade_cent + bws*water_cent*shade_cent,
    a ~ dnorm( 0.5, 0.25) ,
    bw ~ dnorm(bw_d) ,
    bs ~ dnorm(bs_d) ,
    bws ~ dnorm( 0 , 0.25 ),
    sigma ~ dexp( 1 )
    ) ,
  data=d )

precis(m_tulip)
##             mean         sd        5.5%       94.5%
## a      0.3579842 0.02391303  0.31976654  0.39620184
## bw     0.2100972 0.02908152  0.16361927  0.25657505
## bs    -0.1158436 0.02908201 -0.16232225 -0.06936491
## bws   -0.1431567 0.03567113 -0.20016604 -0.08614732
## sigma  0.1248144 0.01692843  0.09775951  0.15186931

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.

d$bed_id <- coerce_index(d$bed) 
d$blooms_std <- d$blooms / max(d$blooms)
d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)

m_bed <- quap(
  alist(
    blooms_std ~ dnorm(mu, sigma),
    mu <- a + bb*bed_id + bw*water_cent + bs*shade_cent + bws*water_cent*shade_cent,
    a ~ dnorm(0.5,0.25),
    bw ~ dnorm(0,0.25),
    bs ~ dnorm(0,0.25),
    bws ~ dnorm(0,0.25),
    bb ~ dnorm(0, 0.25),
    sigma ~ dunif(0, 100)
    ), 
    data=d
)
precis(m_bed,depth = 2)
##              mean         sd        5.5%       94.5%
## a      0.23320357 0.05535621  0.14473364  0.32167349
## bw     0.20729393 0.02619482  0.16542955  0.24915831
## bs    -0.11377027 0.02618968 -0.15562643 -0.07191411
## bws   -0.14374415 0.03199178 -0.19487320 -0.09261510
## bb     0.06272045 0.02569131  0.02166078  0.10378012
## sigma  0.11171858 0.01524572  0.08735298  0.13608419