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?

# The influence of water and shade depend on one another
# Both and their interaction depend on temperature: if the temperature raises to hot, it prevents all blooms, regardless of the water and shade levels, or their interaction.

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

# Let Ti be a 0/1 indicator of whether or not the temperature was hot. Then:

\[ B_i ~ Normal (\mu_i, \sigma)\] \[\mu_i = (\alpha + \beta_w W_i+\beta_s S_i+\beta_ws W_i S_i)(1-T_i) \]

# when Ti = 1, meaning the temperature is high, the regression model becomes 0, regardless of the value in the W, S and their interaction

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)

# a prior: a ~ dnorm( 0.5 , 0.25 )
# Centering the prior for α at 0.5 implies that, when both water and shade are at their mean values, the model expects blooms to be halfway to the observed maximum. 

# bw prior: bw ~ dnorm( 0.25 , 0.08 )
# The range of water is from -1 to 1. To take us from minimum value of blossom from 0 to 1, the maximum slope will be 1/2 (0.5). We want to constrain the effect of water from 0 to 0.5. So if we assign mu of bw to be 0.25 and a standard deviation of 0.08, then 99.7% (3σ) of the prior slops from 0 to 0.5

# bs prior: bs ~ dnorm( -0.25 , 0.08 )
# The range of shade is from -1 to 1. To take us from maximum value of blossom from 1 to 0, the minimum slope will be -1/2 (-0.5). We want to constrain the effect of shade from -0.5 to 0. So if we assign mu of bw to be -0.25 and a standard deviation of 0.08, then 99.7% (3σ) of the prior slops from -0.5 to 0

# inreraction bws prior: bws ~ dnorm( -0.25 , 0.08 )
# Suppose the strongest plausible interaction is one in which high enough shade makes water have zero effect. That implies:
# bw + bws*Si = 0.
# If we set Si =1 (the maximum in the sample), then this means the interaction needs to be the same magnitude as the main effect, but reversed: βWS = −βW. 
# Therefore, bws ~ dnorm( -0.25 , 0.08 )

# Check a prior
set.seed(7)
a <- rnorm( 1e4 , 0.5 , 0.25 ); sum( a < 0 | a > 1 ) / length( a )
## [1] 0.0471
# Check bw prior
bw <- rnorm(1e4, 0.25,0.08); sum( bw >0 ) / length( bw )
## [1] 0.9991
sum( bw >0 & bw<0.5 ) / length( bw )
## [1] 0.9986
# Check bs prior
bs <- rnorm(1e4, -0.25,0.08); sum( bs < 0 ) / length( bs )
## [1] 0.9995
sum( bs <0 & bs>-0.5 ) / length( bs )
## [1] 0.9988
m8.3 <- 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.25 , 0.08  ),
    bs ~ dnorm( -0.25 , 0.08 ) ,
    bws ~ dnorm( -0.25 , 0.08 ) ,
    sigma ~ dexp( 1 )
) , data=d )

precis(m8.3)
##             mean         sd       5.5%       94.5%
## a      0.3580099 0.02416335  0.3193922  0.39662755
## bw     0.2144968 0.02789302  0.1699184  0.25907523
## bs    -0.1314104 0.02814971 -0.1763990 -0.08642169
## bws   -0.1639599 0.03339022 -0.2173239 -0.11059583
## sigma  0.1261324 0.01741925  0.0982931  0.15397175
set.seed(7)
prior <- extract.prior(m8.3)
par(mfrow=c(1,3)) # 3 plots in 1 row

# prior simulation check water vs. blooms
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, main =  paste("prior: shade = ",s ))
  mu <- link( m8.3 , data=data.frame( shade_cent=s , water_cent=-1:1 ),post=prior )
  for ( i in 1:20 ) lines( -1:1 , mu[i,] , col=col.alpha("black",0.3) )
}

# prior simulation check shade vs. blooms
for ( s in -1:1 ) {
  idx <- which( d$water_cent==s )
  plot( d$shade_cent[idx] , d$blooms_std[idx] , xlim=c(-1,1) , ylim=c(0,1) ,
        xlab="shade" , ylab="blooms" , pch=16 , col=rangi2, main =  paste("prior: water = ",s ))
  mu <- link( m8.3 , data=data.frame( water_cent=s , shade_cent=-1:1 ),post=prior )
  for ( i in 1:20 ) lines( -1:1 , mu[i,] , col=col.alpha("black",0.3) )
}

# we can define the prior for inreraction from the prior assumptions for bw and bs.
# As we mentioned above, suppose the strongest plausible interaction is one in which high enough shade makes water have zero effect. That implies:
# bw + bws*Si = 0. If we set Si =1 (the maximum in the sample), then this means the interaction needs to be the same magnitude as the main effect, but reversed: βWS = −βW. 

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.

# In order to not increase the uncertainty due to the categories, I will use the index variable method.

d$bed_idx <- coerce_index( d$bed )
m8.4 <- map(
  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
 )
precis(m8.4,depth=2)
##             mean         sd        5.5%       94.5%
## a[1]   0.2732642 0.03571439  0.21618574  0.33034271
## a[2]   0.3963987 0.03569693  0.33934807  0.45344926
## a[3]   0.4091115 0.03569587  0.35206259  0.46616039
## bw     0.2074352 0.02537453  0.16688184  0.24798863
## bs    -0.1138485 0.02536989 -0.15439450 -0.07330254
## bws   -0.1438917 0.03099534 -0.19342826 -0.09435519
## sigma  0.1081852 0.01469372  0.08470182  0.13166864

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?

# First, we construct the model that omits bed, m_nobed
m_nobed <- 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(m_nobed)
##             mean         sd        5.5%       94.5%
## a      0.3579823 0.02391900  0.31975514  0.39620952
## bw     0.2067294 0.02923476  0.16000664  0.25345223
## bs    -0.1134608 0.02922774 -0.16017238 -0.06674925
## bws   -0.1431612 0.03567992 -0.20018463 -0.08613782
## sigma  0.1248459 0.01694076  0.09777128  0.15192049
# By comparing the parameter estimates results between m8.4 and m_nobed, they are quite similar: Beds “b” and “c” did better than bed “a”. Bed “c” did appear to grow a little better than bed “b”. The posterior contrast between the b and c intercepts are:
post <- extract.samples(m8.4)
diff_b_c <- post$a[,2] - post$a[,3]
HPDI( diff_b_c )
##       |0.89       0.89| 
## -0.09679788  0.06668801
# The difference of the distribution is not on one side of zero.Therefore, we can conclude that including the bed in the analysis doesn’t really improve the inference about the experiment, even though there probably were differences between the beds.

# Then we checked the WAIC:
compare( m8.4 , m_nobed, func=WAIC )
##              WAIC       SE    dWAIC    dSE    pWAIC   weight
## m8.4    -23.61920  9.93881 0.000000     NA 9.570557 0.739532
## m_nobed -21.53212 10.89626 2.087075 8.3476 6.905323 0.260468
# The model with bed, m8.4, does a little bit better in the WAIC comparison. But the difference is very small. 
# The standard error of the difference in WAIC between two models is larger than the differece itself, which suggests the risk of overfit. This results also reconcile to our previous conclusion: adding bed into the model does not improve the inference much.