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) Bread dough rises because of yeast.
#  The amount of flour/eggs/water and temperature may affect the result
#
# (2) Education leads to higher income.
#   country, industry, age 

# (3) Gasoline makes a car go.
# let's assume the predicted variable is speed
# volume of the engine of the car/brand
#

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 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.).

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

#1 caramelizing ~ heat + dryness + heat*dryness
#2 max_speed ~ n_cylinders + fuel_injector
#3 political_beliefs ~ parents_beliefs*(1-has_political_friends) + has_political_friends*friends_beliefs
#4 intelligence ~ social_level + has_manipulalative_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?

# Temperature is the main factor, shade and water depends on it. 
# or there is an interaction between T and W, T and S, T and W and S

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

# bloom ~ is_temp_cold * (a + bW*w + bS*s + bWS*w*s)

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?

# The dataset contains two variables - "raven population size" and "wolf population size"  measured across different areas. 
# I expect that these variables are correlated. Also, I assume, this relation is non-linear. As with the increase of wolves population but constant amount of food on the area, wolves tend to eat all food not leaving anything to ravens. 
# So hypothetical relation look like  
#  raven_pop_size ~ beta_wolf*wolf_pop_size +beta_wolf2*raven_pop_size^2 with beta_wolf2<0
# Extended data set would consist of 4 variables
#  1) raven population 
#  2) wolves population 
#  3) amount of potential food (herbivorous population in are) 
#  4) area size
# Then the model will be like 
#  raven_pop_size ~ a1 + (beta_food + beta_wolf*wolf_pop_size)*food, that explicitely states that slope on food depends from wolves population size

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
## 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)
## 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)
a <- rnorm( 1e4 , 0.5 , 1 ) #8.21
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.6135
a <- rnorm( 1e4 , 0.5 , 0.25 ) #8.22
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.0454
m8.6 <- 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=d )
m8.7 <- 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( 0 , 0.25 ) ,
bs ~ dnorm( 0 , 0.25 ) ,
bws ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
) ,
data=d )
par(mfrow=c(1,3)) # 3 plots in 1 row 
for ( s in -1:1 ) {
idx <- which( d$shade_cent==s )
plot( d$water_cent[idx] , d$blooms_std[idx] , xlim=c(-1,1) , ylim=c(0,1) ,
xlab="water" , ylab="blooms" , pch=16 , col=rangi2 )
mu <- link( m8.6 , 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.

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 ...
pairs(d)

d$shade.c <- d$shade - mean(d$shade)
d$water.c <- d$water - mean(d$water)
#index
d$bed.idx <- coerce_index(d$bed)

m1 <- map(
  alist(
    blooms ~ dnorm( mu , sigma ) ,
    mu <- a + bW*water.c + bS*shade.c + bWS*water.c*shade.c ,
    a ~ dnorm( 130 , 100 ) ,
    bW ~ dnorm( 0 , 100 ) ,
    bS ~ dnorm( 0 , 100 ) ,
    bWS ~ dnorm( 0 , 100 ) ,
    sigma ~ dunif( 0 , 100 )
  ) ,
  data=d ,
  start=list(a=mean(d$blooms),bW=0,bS=0,bWS=0,sigma=sd(d$blooms)) )

precis(m1,depth=2)
##            mean        sd      5.5%     94.5%
## a     129.00797  8.670771 115.15041 142.86554
## bW     74.95946 10.601997  58.01542  91.90350
## bS    -41.14054 10.600309 -58.08188 -24.19920
## bWS   -51.87265 12.948117 -72.56625 -31.17906
## sigma  45.22497  6.152982  35.39132  55.05863
m2 <- map(
  alist(
    blooms ~ dnorm( mu , sigma ) ,
    mu <- a[bed.idx] + bW*water.c + bS*shade.c + bWS*water.c*shade.c ,
    a[bed.idx] ~ dnorm( 130 , 100 ) ,
    bW ~ dnorm( 0 , 100 ) ,
    bS ~ dnorm( 0 , 100 ) ,
    bWS ~ dnorm( 0 , 100 ) ,
    sigma ~ dunif( 0 , 100 )
  ) ,
  data=d ,
  start=list(a=c(mean(d$blooms),mean(d$blooms),mean(d$blooms)),bW=0,bS=0,bWS=0,sigma=sd(d$blooms)),
  method="Nelder-Mead" ,
  control=list(maxit=1e4))

precis(m2,depth=2)
##            mean        sd      5.5%     94.5%
## a[1]   97.59824 12.938259  76.92040 118.27607
## a[2]  142.30918 12.937842 121.63201 162.98635
## a[3]  146.95175 12.937926 126.27445 167.62906
## bW     75.15257  9.188483  60.46760  89.83754
## bS    -41.22717  9.187375 -55.91037 -26.54397
## bWS   -52.22666 11.229102 -70.17293 -34.28038
## sigma  39.14192  5.320321  30.63902  47.64482
#coeftab(m1,m2)
compare(m1,m2)
##        WAIC       SE    dWAIC      dSE    pWAIC    weight
## m2 294.2882  9.83669 0.000000       NA 9.556576 0.7209265
## m1 296.1863 10.60748 1.898124 8.142078 6.685179 0.2790735
#the results are similar