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.
Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1) # only necessary for Linux without the nodejs library / headers
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
## Installing package into 'C:/Users/keren/Documents/R/win-library/4.0'
## (as 'lib' is unspecified)
## package 'rstan' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rstan'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\keren\Documents\R\win-library\4.0\00LOCK\rstan\libs\x64\rstan.dll to C:
## \Users\keren\Documents\R\win-library\4.0\rstan\libs\x64\rstan.dll: Permission
## denied
## Warning: restored 'rstan'
## 
## The downloaded binary packages are in
##  C:\Users\keren\AppData\Local\Temp\RtmpYr6Cbx\downloaded_packages
# 1. humidity. humidity will impact the bread dough.
# 2. Major, Location will impact the income level.
# 3. Brand, Engine will also impact the car's move.

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 invokes an interaction of heat and dryness.
# 2. No interaction is invoked in this.
# 3. Acquiring political beliefts from parents or friends invokes interactions.
# 4. Interlligent animals are not always both social and having manipulative appendages. there are some iteraction in it.

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

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
## Loading required package: dagitty
## rethinking (Version 2.01)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
## 
##     rstudent
# y_i = β_H * H_i + β_D * D_i + β_HD * H_i * D_i
# H = Heat, D = Dry

# y_i = β_C * C_i + β_F * F_i
# H = Cylinders, F = Fuel Injector

#y_i = β_P * P_i + β_F * P_i * F_i
# P = Parents, F = Friends

#y_i = β_S * S_i + β_A * A_i + β_SA * Si * Ai
# 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?

# Hot temperautre with no blooms is dependent on the intereactions of the following:
# 1. water, shade and temperature;
# 2. water and temperature;
# 3. water and shade;
# 4. shade and temperature.
# any of the intereactions above will result in no blooms with hot water.

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

# b_i = α + β_t*x_t + β_w*x_w + β_s*x_s + β_tws*x_t*x_w*x_s + β_tw*x_t*x_w + β_ws*x_w*x_s + β_ts*x_t*x_s
# b = bloom size, t=temperature, w=water, s=shade
# for μi = 0:
# if temperature is hot, x_t=0, then the equation is 
# b_i = α + β_w*x_w + β_s*x_s + β_ws*x_w*x_s
# if water x_w=1, shade x_s=1, then the equation is:
# b_i = α + β_w + β_s + β_ws =0
# then β_w should be -(α + β_s + β_ws)

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)
set.seed(111)

N <- 1000 # simulation size
β_w<-0.3
β_p<-0.1
β_pw<-0.6
r_PW <- 0.6 # correlation between prey and wolf

# Simulate data
p_i <- rnorm(n = N, mean = 0, sd = 1)
w_i <- rnorm(n = N, mean = r_PW * p_i, sd = sqrt(1 - r_PW^2)
)
r_i <- rnorm(n = N, mean = β_p*p_i + β_w*w_i + r_PW*p_i*w_i, sd = 1)
data_t <- data.frame(p_i, r_i, w_i)

model <- map(
  alist(
    r_i ~ dnorm(mu, sigma),
    mu <- α + β_p*p_i + β_w*w_i + β_pw*p_i*w_i,
    α ~ dnorm(0, 1),
    β_w ~ dnorm(0, 1),
    β_p ~ dnorm(0, 1),
    β_pw ~ dnorm(0, 1),
    sigma ~ dunif(0, 5)
  ),
  data=data_t,
  start = list(α=0, β_p=0, β_w=0, β_pw=0, sigma=1)
)
precis(model)
##             mean         sd         5.5%     94.5%
## a     0.05009320 0.03626003 -0.007857324 0.1080437
## ß_p   0.08284406 0.04189450  0.015888559 0.1497996
## ß_w   0.30453210 0.04104584  0.238932911 0.3701313
## ß_pw  0.55281775 0.02650679  0.510454779 0.5951807
## sigma 1.02087537 0.02282665  0.984393975 1.0573568
plot(model)

# it could be linear because there are interaction impacts.

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)
data_t2 <- tulips
summary(data_t2)
##  bed       water       shade       blooms      
##  a:9   Min.   :1   Min.   :1   Min.   :  0.00  
##  b:9   1st Qu.:1   1st Qu.:1   1st Qu.: 71.11  
##  c:9   Median :2   Median :2   Median :111.04  
##        Mean   :2   Mean   :2   Mean   :128.99  
##        3rd Qu.:3   3rd Qu.:3   3rd Qu.:190.30  
##        Max.   :3   Max.   :3   Max.   :361.66
data_t2$blooms_std <- data_t2$blooms / max(data_t2$blooms)
data_t2$water_cent <- data_t2$water - mean(data_t2$water)
data_t2$shade_cent <- data_t2$shade - mean(data_t2$shade)

model_2<- quap(
  alist(
    blooms_std ~ dnorm(mu , sigma), 
    mu <- α + ß_w*water_cent - ß_s*shade_cent-ß_sw*shade_cent*water_cent,
    α ~ dnorm(0.5, 0.25), 
    ß_w ~ dnorm(0, 0.25), 
    ß_s ~ dnorm(0, 0.25),
    ß_sw ~ dnorm(0, 0.25),
    sigma ~ dexp(1)) , 
    data=data_t2 )
precis(model_2)
##            mean         sd       5.5%     94.5%
## a     0.3579733 0.02392073 0.31974331 0.3962032
## ß_w   0.2067249 0.02923694 0.15999865 0.2534512
## ß_s   0.1134603 0.02922988 0.06674536 0.1601753
## ß_sw  0.1431375 0.03568266 0.08610968 0.2001652
## sigma 0.1248552 0.01694392 0.09777551 0.1519348
# Both water and shade will impact tulip blooms.
# Shade has negative impact while water has positive impact, which makes the interaction term negative as well.

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)
data_t3 <- tulips

data_t3$bed_id <- coerce_index(data_t3$bed) 

data_t3$water_cent <- data_t3$water - mean(data_t3$water)
data_t3$shade_cent <- data_t3$shade - mean(data_t3$shade)

data_t3$bedb <- data_t3$bed == "b"
data_t3$bedc <- data_t3$bed == "c"



model_3_1 <- map(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- α + ß_w*water_cent - ß_s*shade_cent- ß_sw*shade_cent*water_cent + ß_Bb*bedb + ß_Bc*bedc,
    α ~ dnorm(50, 150), 
    ß_w ~ dnorm(0, 150), 
    ß_s ~ dnorm(0, 150), 
    ß_sw ~ dnorm(0, 150), 
    ß_Bb ~ dnorm(0, 150),
    ß_Bc ~ dnorm(0, 150), 
    sigma ~ dunif(0, 150)), 
    data = data_t3,
  start = list(a = mean(data_t3$blooms), ß_w = 0, ß_s = 0, ß_sw = 0, ß_Bb = 0, ß_Bc = 0, sigma = sd(data_t3$blooms)))

precis(model_3_1)
##           mean        sd     5.5%     94.5%
## a     97.67014 12.915281 77.02903 118.31126
## ß_w   75.50158  9.217519 60.77021  90.23296
## ß_s   41.43808  9.217268 26.70711  56.16905
## ß_sw  52.51896 11.278391 34.49392  70.54401
## ß_Bb  44.54368 18.265177 15.35240  73.73496
## ß_Bc  49.15189 18.265518 19.96006  78.34371
## sigma 39.17914  5.332685 30.65648  47.70180