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?

# We can assure that Temperature is an important feature and deciding factor for the model. With high temperature, plants can't bloom, so temperature interacts with water moist directly, then this water interacts with blooms indirectly. These interactions may resulted in plants not blooming at all under the hot temperature.
#The influence of high temperature on blooms is dependent upon the interaction between temperature, water and shade or the interaction between temperature and water or water and shade or temperature and shade. 
# When comming to cold temperature, the interaction between water and shade starts to matter 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 have zero to denote hot temperature and can have 1 to denote cold temperature. 
# Ti stands for temperature at sample i. When we can add T and interactions between T and other into the equation.Since T is always 1 inside of the parentheses, it wouldn't have any effect on the model mathematically.

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)
df <- tulips

df$Blooms_STD <- df$blooms / max(df$blooms)
df$Water_Cent <- df$water - mean(df$water)
df$Shade_Cent <- df$shade - mean(df$shade)



model_1<- 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=df )
precis(model_1)
##            mean         sd       5.5%     94.5%
## a     0.3587648 0.03021845 0.31046984 0.4070597
## bw    0.2050352 0.03688899 0.14607951 0.2639910
## bs    0.1125308 0.03687514 0.05359717 0.1714644
## sigma 0.1581519 0.02144293 0.12388198 0.1924219
par(mfrow=c(1,3))
for (s in -1:1) {idx <- which(df$Shade_Cent==s)
    plot( df$Water_Cent[idx] , df$Blooms_STD[idx], xlim=c(-1,1), ylim=c(0,1),
        xlab="Water", ylab="Blooms", pch=16, col=rangi2)
    mu <- link(model_1, data=data.frame( Shade_Cent=s , Water_Cent=-1:1))
    for (i in 1:20) lines( -1:1, mu[i,], col=col.alpha("Gray",0.3))}

# Results clearly shows us with less shade, the tulips need less water and they are in more shade. Which means tilips need more water for 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.

data(tulips)
data <- tulips
data$bed_id <- coerce_index(data$bed)
data$blooms_std <- data$blooms / max(data$blooms) 
data$shade_cent <- data$shade - mean(data$shade) 
data$water_cent <- data$water - mean(data$water) 

set.seed(20) 
model_2 <- quap(alist(
  blooms ~ dnorm(mu, sigma),
  mu <- a[bed_id] + bW * water_cent + bS * shade_cent + bWS * water_cent * shade_cent,
  a[bed_id] ~ dnorm(130, 100),
  bW ~ dnorm(0, 100),
  bS ~ dnorm(0, 100),
  bWS ~ dnorm(0, 100),
  sigma ~ dunif(0, 100)
),
data = data
)
precis(model_2, depth = 2)
##            mean        sd      5.5%     94.5%
## a[1]   97.54986 12.951192  76.85135 118.24837
## a[2]  142.41547 12.950773 121.71763 163.11330
## a[3]  147.11128 12.950771 126.41344 167.80911
## bW     75.12289  9.197989  60.42272  89.82305
## bS    -41.23747  9.196690 -55.93555 -26.53938
## bWS   -52.23345 11.240444 -70.19785 -34.26905
## sigma  39.18206  5.333939  30.65740  47.70673

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?

model_3 <- 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(model_3)
##            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
compare(model_1, model_2)
##              WAIC        SE    dWAIC      dSE    pWAIC       weight
## model_1 -11.52108  9.219819   0.0000       NA  5.64853 1.000000e+00
## model_2 295.14003 10.111254 306.6611 8.492196 10.00074 2.566773e-67
post <- extract.samples(model_2)
post.a <- post$a[, 1]
post.b <- post$a[, 2]
post.c <- post$a[, 3]
dens(post.a, col = "red", xlim = c(50, 200), ylim = c(0, 0.035))
dens(post.b, col = "blue", add = TRUE)
dens(post.c, col = "black", add = TRUE)

# The results show that the model is excluding the variable (m2) shows a slightly better WAIC comparing with variable (m1), the most of the Akaike weight. There is a lot of variability between the flower beds which model m1 addresses.