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?
# Temperature is the fundamental factor while shade and water relies upon it. We could have a three-way association (water, shade and temperature) or two-way associations (water-shade, water-temperature, temperature-shade).
8-2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# Bloom_Size = α + (β_Water * Water) + (β_Shade * Shade) + (β_Temp * Temp) + (β_WS * Water * Shade) + (β_WT * Water * Temp) + (β_ST * Shade * Temp) + (β_WST * Water * Shade * Temp)
# Temperature should be a binary variable, with 0 being hot and 1 being cold. Water and shade are both binary variables, with the presence of water equaling 1 and the presence of shade equaling 1.
# when temperature is hot
# Bloom_size = α + (β_Water * Water) + (β_Shade * Shade) + (β_ws * Water * Shade)
# When water and shade is present
#Bloom_Size = α + β_Water + β_Shade + β_WS
# Now, in order for Bloom Size to be 0, the water*shade interaction coefficient must be 0:
# β_ws = -(α + β_water + β_shade)
# As a result, in hot temperatures, the bloom size will be zero if the preceding equation holds true.
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
str(d)
## 'data.frame': 27 obs. of 4 variables:
## $ bed : Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 1 1 1 2 ...
## $ water : int 1 1 1 2 2 2 3 3 3 1 ...
## $ shade : int 1 2 3 1 2 3 1 2 3 1 ...
## $ blooms: num 0 0 111 183.5 59.2 ...
d$blooms_std <- d$blooms / max(d$blooms)
d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)
a <- rnorm( 1e4 , 0.5 , 1 );
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.619
a <- rnorm( 1e4 , 0.5 , 0.25 );
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.046
m8.3 <- 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=d )
precis(m8.3, depth=2)
## mean sd 5.5% 94.5%
## a 0.3587634 0.03021763 0.31046978 0.4070570
## bw 0.2050362 0.03688800 0.14608203 0.2639903
## bs 0.1125285 0.03687417 0.05359649 0.1714606
## sigma 0.1581476 0.02144148 0.12388001 0.1924153
par(mfrow=c(1,3)) # 3 plots in 1 row
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=16 , col=rangi2 )
mu <- link( m8.3 , 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) )
}
# Tulips are affected by both water and shade. When the light is strong, water has a higher effect on blooms, and when the light is insufficient, water has a less effect.
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
str(d)
## 'data.frame': 27 obs. of 4 variables:
## $ bed : Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 1 1 1 2 ...
## $ water : int 1 1 1 2 2 2 3 3 3 1 ...
## $ shade : int 1 2 3 1 2 3 1 2 3 1 ...
## $ blooms: num 0 0 111 183.5 59.2 ...
d$blooms_std <- d$blooms / max(d$blooms)
d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)
# Dummy variables
d$bedb <- d$bed == "b"
d$bedc <- d$bed == "c"
# Index variable
d$bedi <- coerce_index(d$bed)
#Using dummy variables
m8.4 <- map(
alist(
blooms ~ dnorm(mu, sigma),
mu <- a + (bW * water_cent) + (bS * shade_cent) + (bWS * water_cent * shade_cent) + (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(m8.4, depth=2)
## 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?
m8.5 <- map(
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(m8.5, depth=2)
## 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
set.seed(11)
compare( m8.4, m8.5, func=WAIC )
## WAIC SE dWAIC dSE pWAIC weight
## m8.4 295.1020 10.18953 0.0000000 NA 9.958808 0.5957375
## m8.5 295.8774 10.47512 0.7754712 8.034999 6.541469 0.4042625
# The WAIC of the model with the bed dummy variables was higher than the WAIC of the model without the bed variable and most of the weight. This, along with the bed intercepts, leads me to believe that there was a lot of variation in bloom size between flower beds. There were fewer blooms in Bed "a" than in the other beds.
post <- extract.samples(m8.4)
post.a <- post$a
post.b <- post$a + post$bBb
post.c <- post$a + post$bBc
dens(post.a, col = "green", xlim = c(50, 200), ylim = c(0, 0.035))
dens(post.b, col = "blue", add = TRUE)
dens(post.c, col = "black", add = TRUE)