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. temperature
# 2. major / industry
# 3. condition of the engine

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, 3, 4 invokes interaction
# 1. Interaction between heat and dryness is invoked here to make sure onions don't dry out.
# 3. To predict the person's personal belief, it invokes interaction between parents and friends belief
# 4. Being Social and having manipulative appendages invokes an interaction to predict intelligence in animals

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

#1.𝜇_𝑖=𝛽_𝐻𝐻_𝑖+𝛽_𝐷𝐷_𝑖+𝛽_𝐻𝐷𝐻_𝑖𝐷_𝑖
#2.𝜇_𝑖=𝛽_𝐶𝐶_𝑖+𝛽_𝑄𝑄_𝑖
#3.𝜇_𝑖=𝛽_𝑇𝑃𝑇_𝑖𝑃_𝑖+𝛽_𝑇𝐹𝑇_𝑖𝐹_𝑖
#4.𝜇_𝑖=𝛽_𝑆𝑆_𝑖+𝛽_𝐴𝐴_𝑖+𝛽_𝑆𝐴𝑆_𝑖𝐴_𝑖

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?

# Since there are three predictor variables now (Water, Shade, and Temperature), we would have a single three-way interaction and three two-way interactions: WST, WS, WT, and ST.

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

## The algebraic form of the regression equation would be: 
## μi=α+βWWi+βSSi+βTTi+βWSTWiSiTi+βWSWiSi+βWTWiTi+βSTSiTi

## assuming all predictors are equal to 1:
## μi|Wi=1,Si=1,T=1=α+βW+βS+βT+βWST+βWS+βWT+βST

## When Ti=0, the full regression equation would be:
## μi=α+βWWi+βSSi−αTi−βWSWiSiTi+βWSWiSi−βWWiTi−βSSiTi

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.12)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
## 
##     rstudent
data(tulips)
data(rugged)
data(nettle)
set.seed(7)

N <- 300 # simulation size
rPW <- 0.6 # correlation between prey and wolves
bP <- 0.3 # regression coefficient for prey
bW <- 0.1 # regression coefficient for wolves
bPW <- 0.5 # regression coefficient for prey-by-wolves interaction
# Simulate data
prey <- rnorm(n = N, mean = 0, sd = 1)
wolves <- rnorm(n = N, mean = rPW * prey, sd = sqrt(1 - rPW^2))
ravens <- rnorm(n = N, mean = bP*prey + bW*wolves + bPW*prey*wolves, sd = 1)
d <- data.frame(ravens, prey, wolves)
str(d)
## 'data.frame':    300 obs. of  3 variables:
##  $ ravens: num  1.6876 0.2429 -0.4894 -0.0254 0.2049 ...
##  $ prey  : num  2.287 -1.197 -0.694 -0.412 -0.971 ...
##  $ wolves: num  1.793 -0.939 -0.373 -0.558 -0.916 ...
model <- map(alist(ravens ~ dnorm(mu, sigma), mu <- a + bP*prey + bW*wolves + bPW*prey*wolves,    a ~ dnorm(0, 1), bW ~ dnorm(0, 1), bP ~ dnorm(0, 1), bPW ~ dnorm(0, 1), sigma ~ dunif(0, 5)),
  data = d, start = list(a = 0, bP = 0, bW = 0, bPW = 0, sigma = 1))
precis(model)
##              mean         sd        5.5%     94.5%
## a     -0.05405944 0.06211429 -0.15333007 0.0452112
## bP     0.41977288 0.07388771  0.30168604 0.5378597
## bW     0.03945499 0.06901645 -0.07084663 0.1497566
## bPW    0.46053230 0.04233811  0.39286782 0.5281968
## sigma  0.93983549 0.03836862  0.87851502 1.0011560

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?

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 ); sum( a < 0 | a > 1 ) / length( a )
## [1] 0.6102
model1 <- 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 )

  
model2 <- 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)) 
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( model1 , 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 ...
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.23320204 0.05535598  0.14473249  0.32167160
## bw     0.20729387 0.02619472  0.16542965  0.24915809
## bs    -0.11377071 0.02618958 -0.15562671 -0.07191471
## bws   -0.14374407 0.03199166 -0.19487292 -0.09261521
## bb     0.06272068 0.02569121  0.02166117  0.10378020
## sigma  0.11171816 0.01524557  0.08735279  0.13608352