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. Temperature
#2. Major
#3. Age of a 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. Caramelizing invokes an interaction between heat and dryness.
#2. No interaction because the "or".
#3. Invokes an interaction between where a person acquire political belief. If a person acquires political believe from friends, he/she doesn't get it from friends.
#4. No interaction because the "or".

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?

# Ravens_i ~ Normal(mu_i,sigma)
# mu_i = alpha + beta_wolve*wolve_i + beta_prey*prey_i + beta_wp*wolve_i*prey_i

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?

# The interaction prior is constrained to be negative.
library(rethinking)
## Loading required package: rstan
## Loading required package: StanHeaders
## Loading required package: ggplot2
## 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: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.3579877 0.02391447  0.31976780  0.39620769
## bw     0.2087797 0.02908372  0.16229825  0.25526106
## bs    -0.1165167 0.02908558 -0.16300110 -0.07003235
## bws   -0.1431615 0.03567320 -0.20017419 -0.08614886
## sigma  0.1248219 0.01693129  0.09776243  0.15188137

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.

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$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.23320447 0.05535626  0.14473447  0.32167447
## bw     0.20729485 0.02619483  0.16543046  0.24915925
## bs    -0.11377118 0.02618969 -0.15562737 -0.07191500
## bws   -0.14374759 0.03199179 -0.19487664 -0.09261854
## bb     0.06272027 0.02569133  0.02166057  0.10377997
## sigma  0.11171866 0.01524574  0.08735302  0.13608429