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?

# It seems like tulips don't blossom under higher temperatures, which makes a three-way interaction Blooming isn't just subject to the interaction among water and shade, however this association relies upon the temperature too. In the event that the temperature is too high, no measure of shade and water will make the tulip blossom.

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

#If we code temperature as an index variable with a 0 for cold and a 1 for hot, we can multiply the entire initial model with 1 - temperature. If the temperature is hot (= 0), the entire model will liken to zero bloom. Cold temperature, then again has no impact on the bloom as the model simply gets multiplied with 1.

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

data_2$Blooms_STD <- data_2$blooms / max(data_2$blooms)
data_2$Water_Cent <- data_2$water - mean(data_2$water)
data_2$Shade_Cent <- data_2$shade - mean(data_2$shade)



model_2<- 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=data_2 )
precis(model_2)
##            mean         sd       5.5%     94.5%
## a     0.3587657 0.03021822 0.31047112 0.4070602
## bw    0.2050330 0.03688874 0.14607771 0.2639884
## bs    0.1125340 0.03687485 0.05360089 0.1714672
## sigma 0.1581507 0.02144253 0.12388143 0.1924200
par(mfrow=c(1,3))
for (s in -1:1) {idx <- which(data_2$Shade_Cent==s)
    plot( data_2$Water_Cent[idx] , data_2$Blooms_STD[idx], xlim=c(-1,1), ylim=c(0,1),
        xlab="Water", ylab="Blooms", pch=16, col=rangi2)
    mu <- link(model_2, data=data.frame( Shade_Cent=s , Water_Cent=-1:1))
    for (i in 1:20) lines( -1:1, mu[i,], col=col.alpha("Gray",0.3))}

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.

data(tulips)
d <- tulips
d$bed_id <- coerce_index(d$bed)
d$blooms_std <- d$blooms / max(d$blooms) 
d$shade_cent <- d$shade - mean(d$shade) 
d$water_cent <- d$water - mean(d$water) 

set.seed(20) 
m1 <- quap(alist(
  blooms ~ dnorm(mu, sigma),
  mu <- a[bed_id] + bW * water_cent + bS * shade_cent + bWS * water_cent * shade_cent,
  a[bed_id] ~ dnorm(130, 100),
  bW ~ dnorm(0, 100),
  bS ~ dnorm(0, 100),
  bWS ~ dnorm(0, 100),
  sigma ~ dunif(0, 100)
),
data = d
)
precis(m1, depth = 2)
##            mean        sd      5.5%     94.5%
## a[1]   97.54986 12.951192  76.85135 118.24837
## a[2]  142.41547 12.950773 121.71763 163.11330
## a[3]  147.11128 12.950771 126.41344 167.80911
## bW     75.12289  9.197989  60.42272  89.82305
## bS    -41.23747  9.196690 -55.93555 -26.53938
## bWS   -52.23345 11.240444 -70.19785 -34.26905
## sigma  39.18206  5.333939  30.65740  47.70673

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?

m2 <- quap(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- a + bW * water_cent + bS * shade_cent + bWS * water_cent * shade_cent,
    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(m2)
##            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(m1, m2)
##        WAIC        SE    dWAIC      dSE     pWAIC    weight
## m2 295.0441  9.873189 0.000000       NA  6.062662 0.6776652
## m1 296.5302 10.544146 1.486126 8.181914 10.771689 0.3223348
post <- extract.samples(m1)
post.a <- post$a[, 1]
post.b <- post$a[, 2]
post.c <- post$a[, 3]
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)