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.
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?
#Tulips don't appear to bloom at higher temperatures, resulting in a three-way interaction. Blooming is influenced not just by the combination of water and shade, but also by temperature. No amount of shade or water will make the tulip blossom if the temperature is too high.
8-2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# # Let Mi stand for the ordinary linear model. Then let Ti be a 0-1 indicator of whether temperature was hot/cold.
# Ui = Mi(1 − Ti)
# When Ti = 1, the entire model above is zero, regardless of the value of Li.
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)
dt <- tulips
# For convenience of calculation, instead of using the shade variable directly, construct a new variable named "light" with the same magnitude as shade and the opposite orientation.
# Adjust the variables
dt$light <- -1 * dt$shade
dt$blooms_std <- dt$blooms / max(dt$blooms)
dt$water_cent <- dt$water - mean(dt$water)
dt$shade_cent <- dt$shade - mean(dt$shade)
dt$light_cent <- dt$light - mean(dt$light)
# Create a Bayesian multiple linear regression model with bound to be positive coefficients for bw, bl, and bwl.
m1 <- quap(
alist(
blooms_std ~ dnorm(mu, sigma),
mu <- a + bw*water_cent + bl*light_cent + bwl*water_cent*light_cent,
a ~ dnorm(0.5, 0.25),
bw ~ dlnorm(0, 0.25),
bl ~ dlnorm(0, 0.25),
bwl ~ dlnorm(0, 0.25),
sigma ~ dexp(1)
),data = dt)
summary(m1)
## mean sd 5.5% 94.5%
## a 0.3674216 0.06888979 0.2573224 0.4775208
## bw 0.4310705 0.07919204 0.3045063 0.5576347
## bl 0.3905161 0.07900444 0.2642517 0.5167804
## bwl 0.4487585 0.08977663 0.3052781 0.5922390
## sigma 0.3712685 0.09546506 0.2186969 0.5238401
# Plot posterior predictions
par(mfrow=c(1,3)) # 3 plots in 1 row
for (l in -1:1) {
idx <- which(dt$light_cent == l)
plot( dt$water_cent[idx], dt$blooms_std[idx], xlim=c(-1,1), ylim=c(0,1),
xlab="water", ylab="blooms", pch=16, col=rangi2)
mu <- link(m1, data=data.frame( light_cent=l , water_cent=-1:1))
for (i in 1:20) lines( -1:1, mu[i,], col=col.alpha("black",0.3))
}
#Plot prior predictions
set.seed(7)
prior <- extract.prior(m1)
par(mfrow=c(1,3)) # 3 plots in 1 row
for (l in -1:1) {
idx <- which(dt$light_cent == l)
plot( dt$water_cent[idx], dt$blooms_std[idx], xlim=c(-1,1), ylim=c(0,1),
xlab="water", ylab="blooms", pch=16, col=rangi2)
mu <- link(m1, post = prior, data=data.frame( light_cent=l , water_cent=-1:1))
for (i in 1:20) lines( -1:1, mu[i,], col=col.alpha("black",0.3))
}
# The prior predction plots imply that the effect of water on bloom varies depending on light. When there is not enough light, water does not lead to bloom.
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.
library(dplyr)
dt2 <- mutate(dt, bed = as.numeric(bed))
dt2$bed_cent <- dt2$bed - mean(dt2$bed)
m2 <- quap(
alist(
blooms_std ~ dnorm(mu, sigma),
mu <- a + bw*water_cent + bl*light_cent + bwl*water_cent*light_cent + bb*bed_cent,
a ~ dnorm(0.5, 0.25),
bw ~ dlnorm(0, 0.25),
bl ~ dlnorm(0, 0.25),
bwl ~ dlnorm(0, 0.25),
bb ~ dlnorm(0, 0.25),
sigma ~ dexp(1)
),data = dt2)
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?
compare(m1, m2)
## WAIC SE dWAIC dSE pWAIC weight
## m1 41.90494 15.22998 0.00000 NA 10.701334 9.999513e-01
## m2 61.76480 10.28730 19.85986 14.16128 5.264072 4.869281e-05
plot(compare(m1,m2))