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?

# Temperature is the primary factor that drives blooms while shade and water depend on temperature. Thus, we could have a three-way interaction (water, shade and temperature) or a two-way interaction (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?

#This is the algebraic qen:
#μi=α+βWWi+βSSi+βTTi+βWSTWiSiTi+βWSWiSi+βWTWiTi+βSTSiTi

## assuming all predictors are equal to 1:
# μi|Wi=1,Si=1,T=1=α+βW+βS+βT+βWST+βWS+βWT+βST

# When Ti=0, the full regression equation would be:
# μi=α+βWWi+βSSi−αTi−βWSWiSiTi+βWSWiSi−βWWiTi−βSSiTi

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= tulips
str(data)
## '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 ...
data$blooms_std = data$blooms / max(data$blooms)
data$water_cent = data$water - mean(data$water)
data$shade_cent = data$shade - mean(data$shade)

bw_d= abs(rnorm(nrow(data),0,0.25))
bs_d= (-abs(rnorm(nrow(data),0,0.25)))

model1 = 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(bw_d) ,
    bs ~ dnorm(bs_d) ,
    bws ~ dnorm( 0 , 0.25 ),
    sigma ~ dexp( 1 )
    ) ,
  data=data )

precis(model1)
##             mean         sd        5.5%       94.5%
## a      0.3579823 0.02391302  0.31976468  0.39619993
## bw     0.2095579 0.02908121  0.16308049  0.25603526
## bs    -0.1170682 0.02908621 -0.16355361 -0.07058284
## bws   -0.1431631 0.03567108 -0.20017240 -0.08615385
## sigma  0.1248144 0.01692894  0.09775865  0.15187007

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$bed_id = coerce_index(data$bed)
model_bed = quap(
  alist(
    blooms_std ~ dnorm(mu, sigma),
    mu <- a + bb*bed_id + 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),
    bb ~ dnorm(0, 0.25),
    sigma ~ dunif(0, 100)
    ), 
    data=data
)
precis(model_bed,depth = 2)
##              mean         sd        5.5%       94.5%
## a      0.23320276 0.05535068  0.14474168  0.32166384
## bw     0.20729131 0.02619210  0.16543128  0.24915135
## bs    -0.11377176 0.02618694 -0.15562355 -0.07191998
## bws   -0.14374218 0.03198847 -0.19486594 -0.09261843
## bb     0.06271913 0.02568875  0.02166354  0.10377472
## sigma  0.11170679 0.01524169  0.08734762  0.13606596

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?

model2 = 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 = data,
  start = list(a = mean(data$blooms), bW = 0, bS = 0, bWS = 0, sigma = sd(data$blooms))
)
precis(model2)
##            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