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. Problems are labeled Easy (E), Medium (M), and Hard(H).
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.
8E1. For each of the causal relationships below, name a hypothetical third variable that would lead to an interaction effect:
# 1. it is important to keep the temperature in the oven, so temperature would be the third variable
# 2. the Income of different fields (majors) differ a lot, so field of Major Subject would be the third variable.
# 3. Gasoline only makes a car go if the engine works, so engine status would be the third variable.
8E2. Which of the following explanations invokes an interaction?
# 1. Invokes interaction between heat and dryness in predicting caramelization of onion.
# 2. Does not invoke an interaction.
# 3. Does not invoke an interaction.
# 4.Invokes interaction between being social and the possessing of manipulative appendages in predicting the intelligence of animal species.
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
# 1. D = aA + bB + cAB
# 2. C = aA + bB
# 3. D = aAB + bAC
# 4. D = aA + bB + cAB
8M1. 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?
#water, shade, and temperature are three predictors, there would be a single three-way interaction and three two-way interactions (WST, WS, WT, and ST).
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
#B = a + bW + cS + dT + eWS + fWT + gST + hWST
# let W=1, S=1, T=1
# B = a + b + c + d + e + f + g + h
# whenever T=1, B should be 0:
# we have a + d = 0, b + f = 0, c + g = 0, e + h = 0
# thus B = a + bW + cS - aT + eWS - bWT - cST -eWST
8M4. 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. What do these prior assumptions mean for the interaction prior, if anything? Visualize the prior simulation.
#library(rethinking)
#data(tulips)
#df <- tulips
#str(df)
#df$blooms_std <- df$blooms / max(df$blooms)
#df$water_cent <- df$water - mean(df$water)
#df$shade_cent <- df$shade - mean(df$shade)
#a <- rnorm( 1e4 , 0.5 , 1 )
#sum( a < 0 | a > 1 ) / length( a )
#a <- rnorm( 1e4 , 0.5 , 0.25 )
#sum( a < 0 | a > 1 ) / length( a )
#m8.6 <- 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 )
#m8.7 <- 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=df )
#par(mfrow=c(1,3)) # 3 plots in 1 row
#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( m8.6 , 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) )
#}
8H1. 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)
#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)
# bed index
#df$bed_index <- coerce_index(df$bed)
#bed_var <- quap(
# alist(
# blooms_std ~ dnorm( mu , sigma ) ,
# mu <- a[bed_index] + bw*water_cent + bs*shade_cent + bws*water_cent*shade_cent,
#a[bed_index] ~ dnorm(0.5, 0.25),
#bw ~ dnorm(0, 0.25),
#bs ~ dnorm(0, 0.25),
#bws ~ dnorm(0, 0.25),
#sigma ~ dunif(0, 100)
#),
#data = df
#)
#precis(bed_var, depth = 2 )
8H5. Consider the data(Wines2012) data table. These data are expert ratings of 20 different French and American wines by 9 different French and American judges. Your goal is to model score, the subjective rating assigned by each judge to each wine. I recommend standardizing it. In this problem, consider only variation among judges and wines. Construct index variables of judge and wine and then use these index variables to construct a linear regression model. Justify your priors. You should end up with 9 judge parameters and 20 wine parameters. Plot the parameter estimates. How do you interpret the variation among individual judges and individual wines? Do you notice any patterns, just by plotting the differences? Which judges gave the highest/lowest ratings? Which wines were rated worst/best on average?
#data(Wines2012)
#df <- Wines2012
#d2_list = list(s = standardize(df$score),
# wine = as.integer(df$wine),
# judge = as.integer(df$judge))
#str(df_list)
#wines <- ulam(alist(
# s ~ dnorm(mu, sigma),
# mu <- j[judge] + w[wine] ,
# w[wine] ~ dnorm(0, 0.5),
# j[judge] ~ dnorm(0, 0.5),
# sigma ~ dexp(1)),
# data = df_list,
# chains = 4,
# cores = 4)
#precis(wines, 2)
# As can be seen from the results above, wine 4 seems have the highest rating and wine 18 have the least rating. Judge 5 seems to give the highest average rating and judge 8 seems to give the least average rating based on the mean values and traceplot.