library(rethinking)
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?
# Under the cold condition, the bloom for tulips has been jointly decided by the factors of water and shades, with an interaction effect. However, the temperature could also be an extra factors which has additional interaction effects towards water and shades when predicting the bloom. In fact, if the temperature is too high, then tulip might not bloom as well as it is in a cold temperature. Therefore the temperature could have a combined three way interaction effect together with the other two factors (water and shades), together with two-way interactions with water and shades individually. Temperature itself could be the most important main effect factors for the bloom.
8-2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# We could let the Temperature factor be an indicator factor which every other factors (water and shades) have interaction effects with it
# mu_i = Temp (alpha + beta_w * water + beta_s *shades + beta_ws * water * shades)
# Temp = 0 means hot
# Temp = 1 means cold
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?
# Following the scripts from 8.23-8.25 in the text book, we could find by restricting the signs for water and shade, the interaction effects gets even stronger than the original model. From the graph we have observed that when the shade/water shifts from -1 to 1, the slope of water/shade is significantly different. The plots has presented a significant interaction effect between water and shade.
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)
model = 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 ~ dlnorm(0, 0.25),
bs ~ dlnorm(0, 0.25),
bws ~ dnorm(0, 0.25),
sigma ~ dexp(1)
), data=d)
par(mfrow=c(1,3))
for (s in -1:1) {
idx = which(d$shade_cent == s)
plot(d$water_cent[idx], d$blooms_std[idx], xlim=c(-1, 1), ylim=c(0, 1), xlab='water', ylab='blooms', pch=15, col=rangi2)
mu <- link(model, data=data.frame(shade_cent=s, water_cent=-1:1))
for (i in 1:20) lines(-1:1, mu[i,], col=col.alpha("black", 0.3))
}
par(mfrow=c(1,3))
for (w in -1:1) {
idx = which(d$water_cent == w)
plot(d$shade_cent[idx], d$blooms_std[idx], xlim=c(-1, 1), ylim=c(0, 1), xlab='shade', ylab='blooms', pch=15, col=rangi2)
mu <- link(model, data=data.frame(shade_cent=-1:1, water_cent=w))
for (i in 1:20) lines(-1:1, mu[i,], col=col.alpha("black", 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.
set.seed(10)
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)
d$bed_idx = as.numeric(unclass(d$bed))
model_bed = quap(alist(
blooms_std ~ dnorm(mu, sigma),
mu <- a[bed_idx] + bw * water_cent + bs * shade_cent + bws * water_cent * shade_cent,
a[bed_idx] ~ dnorm(0.5, 0.25),
bw ~ dnorm(0, 0.25),
bs ~ dnorm(0, 0.25),
bws ~ dnorm(0, 0.25),
sigma ~ dexp(1)
), data=d)
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?
set.seed(10)
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)
model_no_bed = 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(0, 0.25),
bs ~ dnorm(0, 0.25),
bws ~ dnorm(0, 0.25),
sigma ~ dexp(1)
), data=d)
precis(model_bed,depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.2732492 0.03571139 0.21617545 0.3303229
## a[2] 0.3964184 0.03569417 0.33937221 0.4534646
## a[3] 0.4091132 0.03569303 0.35206879 0.4661575
## bw 0.2074310 0.02537251 0.16688081 0.2479811
## bs -0.1138640 0.02536779 -0.15440665 -0.0733214
## bws -0.1438918 0.03099285 -0.19342433 -0.0943592
## sigma 0.1081764 0.01469073 0.08469781 0.1316550
compare(model_bed, model_no_bed)
## WAIC SE dWAIC dSE pWAIC weight
## model_bed -23.05823 10.13737 0.000000 NA 9.968949 0.6431404
## model_no_bed -21.88018 10.57132 1.178042 7.951282 6.698663 0.3568596
# Comment : Since the model2 (with bed) has a smaller WAIC number (-23.1), therefore it is a better model than without bed (WAIC = -21.9).
# By observing the coefficients for model with bed, We could notice the coefficients for bed A are clearly different from bed B and bed C, which suggests the bed categorical factor is an effective factor to be included. THis could reconcile with the WAIC results which the model with bed is a better fitted model.