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. Make sure to include plots if the question requests them.

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

8-1. 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?

# Shade levels and water depend on temperature. There could be a 3-way interaction and 3 2-way interactions(water shade, water temperature or shade temperature).

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

# μi = 𝛼+𝛽𝑊𝑊𝑖+𝛽𝑆𝑆𝑖+𝛽𝑇𝑇𝑖+𝛽𝑊𝑆𝑇𝑊𝑖𝑆𝑖𝑇𝑖+𝛽𝑊𝑆𝑊𝑖𝑆𝑖+𝛽𝑊𝑇𝑊𝑖𝑇𝑖+𝛽𝑆𝑇𝑆𝑖𝑇𝑖 

8-3. 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 and visualize. What do these prior assumptions mean for the interaction prior, if anything?

library(rethinking)
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)

tulip_analy <- 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( 1 , 0.25 ) ,
    bs ~ dnorm( -1 , 0.25 ) ,
    bws ~ dnorm( 0 , 0.25 ) ,
    sigma ~ dexp( 1 )
  ),
  data = d)

precis(tulip_analy)
##             mean         sd        5.5%       94.5%
## a      0.3579964 0.02404860  0.31956205  0.39643066
## bw     0.2205145 0.02953043  0.17331919  0.26770986
## bs    -0.1272573 0.02956781 -0.17451239 -0.08000225
## bws   -0.1431296 0.03587119 -0.20045866 -0.08580048
## sigma  0.1255282 0.01721828  0.09801003  0.15304630
# There is a correlation among water, shades and tulip. As shade levels increase, water is needed more, and vise versa. 

8-4. 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.

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)

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

8-5. Use WAIC to compare the model from 8-4 to a model that omits bed. What do you infer from this comparison? Can you reconcile the WAIC results with the posterior distribution of the bed coefficients?

m_omit <- 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(m_omit)
##            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
compare(m_dummy, m_omit)
##             WAIC        SE    dWAIC      dSE    pWAIC    weight
## m_dummy 294.5674  9.747307 0.000000       NA 9.625416 0.7308868
## m_omit  296.5656 10.796005 1.998253 7.892784 6.922059 0.2691132
post <- extract.samples(m_dummy)
post.a <- post$a
post.b <- post$a + post$bBb
post.c <- post$a + post$bBc
dens(post.a, col = "red", xlim = c(50, 200), ylim = c(0, 0.035))
dens(post.b, col = "blue", add = TRUE)
dens(post.c, col = "black", add = TRUE)

# The dummy one is better than the other one. Bed “a” had particularly fewer blooms than the other beds.