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.different major
#3.car is broken

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

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

#μa=βHHi+βDDi+βHDHiDi
#μb=βCCi+βQQi
#μc=βTPTiPi+βTFTiFi
#μd=βSSi+βAAi+βSASiAi

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 the relationships between blossoms and water and between blossoms and shade depend on temperature, there are interaction effects. Since there are three predictor variables now (water, shade, and temperature), there is 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?

#μ=α+β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?

#μi=α+βPPi+βWWi+βPWPiWi 
# simulation size
N <- 500 
# correlation between prey and wolf
rPW <- 0.6 
# regression coefficient for prey
bP <- 0.4 
# regression coefficient for wolf
bW <- 0.2 
# regression coefficient for prey-by-wolf interaction interaction
bPW <- 0.5 
# Simulate data
prey <- rnorm(
  n = N, 
  mean = 0, 
  sd = 1
)
wolf <- rnorm(
  n = N, 
  mean = rPW * prey, 
  sd = sqrt(1 - rPW^2)
)
raven <- rnorm(
  n = N, 
  mean = bP*prey + bW*wolf + bPW*prey*wolf, 
  sd = 1
)
d <- data.frame(raven, prey, wolf)
str(d)
## 'data.frame':    500 obs. of  3 variables:
##  $ raven: num  -0.1984 2.6928 -1.7628 2.9948 -0.0711 ...
##  $ prey : num  0.188 0.483 -0.79 0.675 1.692 ...
##  $ wolf : num  0.786 0.531 0.286 1.725 0.564 ...
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
m <- map(
  alist(
    raven ~ dnorm(mu, sigma),
    mu <- a + bP*prey + bW*wolf + bPW*prey*wolf,
    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(m)
##             mean         sd        5.5%      94.5%
## a     0.01318217 0.04972470 -0.06628750 0.09265184
## bP    0.38259016 0.05722698  0.29113039 0.47404993
## bW    0.15804265 0.05823920  0.06496516 0.25112015
## bPW   0.42919435 0.03983877  0.36552430 0.49286440
## sigma 0.99557654 0.03148283  0.94526091 1.04589218
#From the analysis we could see that there are interaction effect and it is linear. 

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

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.3579848 0.02391792  0.31975939  0.39621031
## bw     0.2098447 0.02908715  0.16335777  0.25633154
## bs    -0.1180718 0.02909824 -0.16457640 -0.07156719
## bws   -0.1431605 0.03567832 -0.20018132 -0.08613964
## sigma  0.1248401 0.01693852  0.09776912  0.15191117

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
d$shade.c <- d$shade - mean(d$shade)
d$water.c <- d$water - mean(d$water)
# Dummy variables
d$bedb <- d$bed == "b"
d$bedc <- d$bed == "c"
# Index variable
d$bedx <- coerce_index(d$bed)
#with dummy variables
m_dummy <- map(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- a + bW*water.c + bS*shade.c + bWS*water.c*shade.c + bBb*bedb + bBc*bedc,
    a ~ dnorm(130, 100),
    bW ~ dnorm(0, 100),
    bS ~ dnorm(0, 100),
    bWS ~ dnorm(0, 100),
    bBb ~ dnorm(0, 100),
    bBc ~ dnorm(0, 100),
    sigma ~ dunif(0, 100)
  ),
  data = d,
  start = list(a = mean(d$blooms), bW = 0, bS = 0, bWS = 0, bBb = 0, bBc = 0, sigma = sd(d$blooms))
)
precis(m_dummy)
##            mean        sd      5.5%     94.5%
## a      99.36131 12.757521  78.97233 119.75029
## bW     75.12433  9.199747  60.42136  89.82730
## bS    -41.23103  9.198481 -55.93198 -26.53008
## bWS   -52.15060 11.242951 -70.11901 -34.18219
## bBb    42.41139 18.039255  13.58118  71.24160
## bBc    47.03141 18.040136  18.19979  75.86303
## sigma  39.18964  5.337920  30.65862  47.72067
#with index variable
m_index <- map(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- a[bedx] + bW*water.c + bS*shade.c + bWS*water.c*shade.c,
    a[bedx] ~ dnorm(130, 100),
    bW ~ dnorm(0, 100),
    bS ~ dnorm(0, 100),
    bWS ~ dnorm(0, 100),
    sigma ~ dunif(0, 200)
  ),
  data = d
)
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
precis(m_index, depth = 2)
## Warning in sqrt(diag(vcov(model))): NaNs produced

## Warning in sqrt(diag(vcov(model))): NaNs produced

## Warning in sqrt(diag(vcov(model))): NaNs produced
##            mean       sd      5.5%       94.5%
## a[1]   67.36072 31.86963  16.42689 118.2945464
## a[2]   82.25569 23.45502  44.77003 119.7413401
## a[3]  154.93807 33.99271 100.61115 209.2649906
## bW     71.53302 24.80534  31.88930 111.1767406
## bS    -39.11498 24.85142 -78.83234   0.6023833
## bWS   -43.82852 29.76080 -91.39204   3.7349914
## sigma 108.96159      NaN       NaN         NaN
coeftab(m_dummy, m_index)
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
## Warning in sqrt(diag(vcov(model))): NaNs produced
##       m_dummy m_index
## a       99.36      NA
## bW      75.12   71.53
## bS     -41.23  -39.11
## bWS    -52.15  -43.83
## bBb     42.41      NA
## bBc     47.03      NA
## sigma   39.19  108.96
## a[1]       NA   67.36
## a[2]       NA   82.26
## a[3]       NA  154.94
## nobs       27      27
#We could see that the result is very similar