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 - Amount of sugar in the flour.
#2 - The reputation of the school ones graduated from is likely to influence ones income as well as having an education in the first place.
#3 - Fuel pump reliability.
8E2. Which of the following explanations invokes an interaction?
# 1, 3, and 4 invoke interaction...
# 1- the temperature level and onions hydration level my interact in producing caramelized onions.
# 3- Friends may interact in political beliefs acquisition between people and their parents.
# 4- Predicting animal species' intelligence invokes an interaction between their social skills and manipulative appendages.
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
# 1)- T: Temperature , H: Hydration --> #μi = βTTi + βHHi + βTHTiHi
# 2)- C: Cylinder Count , F: Fuel Injector Performance --> μi = βCCi + βFFi
# 3)- P: Beliefs from Parents , F; Beliefs from Friends , H: People Type --> μi = βHPHiPi + βHFHiFi
# 4)- S: Social Skill Level , A: Appendage Manipulation Skills Level --> μi = βSSi + βAAi + βSASiAi
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?
# W= Water
# S= Shade
# T= Temperature
# The example had 2 variables predicting tulips blossom: Water and Shade. By introducing Temperature as a third predicting variable, we can observe 2 kinds of interactions:
# A Single 3-way interaction between all predictors: WST
# Three 2-way interactions between 2 variables at a time: WS, ST, WT
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# μi = α + βWWi + βSSi - βTTi + βWSWiSi - βSTSiTi - βWTWiTi - βWSTWiSiTi
# Hot Temp: T=1
# Cold Temp: T=0
# With the equation above, when Ti=1, μi = 0
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)
Tulips <- tulips
Tulips$blooms_std <- Tulips$blooms/max(Tulips$blooms)
Tulips$water_cent <- Tulips$water-mean(Tulips$water)
Tulips$shade_cent <- Tulips$shade-mean(Tulips$shade)
m <- 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( 1 , 0.25 ) ,
bs ~ dnorm( -0.25 , 0.25 ) ,
bws ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
),
data = Tulips)
precis(m)
## mean sd 5.5% 94.5%
## a 0.3579760 0.02397084 0.31966597 0.39628602
## bw 0.2204379 0.02943254 0.17339902 0.26747679
## bs -0.1168773 0.02929196 -0.16369149 -0.07006307
## bws -0.1431381 0.03575656 -0.20028402 -0.08599225
## sigma 0.1251190 0.01705250 0.09786582 0.15237219
The mean of interaction between water and shade as predictors for tulip blossom came out negative, due to shade interaction being negative and water interaction being positive.
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.
Tulips1 <- tulips
Tulips1$blooms_std <- Tulips1$blooms / max(Tulips1$blooms)
Tulips1$water_cent <- Tulips1$water - mean(Tulips1$water)
Tulips1$shade_cent <- Tulips1$shade - mean(Tulips1$shade)
# Constructing the bed variable through as an index:
Tulips1$bed_idx <- coerce_index(Tulips1$bed)
# Model:
h <- quap(
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 ~ dunif(0, 10)
),
data = Tulips1
)
precis(h, depth = 2 )
## mean sd 5.5% 94.5%
## a[1] 0.2732832 0.03578382 0.21609373 0.33047262
## a[2] 0.3964189 0.03576621 0.33925760 0.45358022
## a[3] 0.4091240 0.03576510 0.35196448 0.46628356
## bw 0.2074301 0.02542425 0.16679728 0.24806300
## bs -0.1138380 0.02541960 -0.15446347 -0.07321263
## bws -0.1438787 0.03105579 -0.19351187 -0.09424557
## sigma 0.1083993 0.01476656 0.08479944 0.13199907
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)
Wines <- Wines2012
# standardizing the variables judge and wine to arithmetic lists (J1 to J9, and W1 to W20)
WinesList = list(s = standardize(Wines$score),
wine = as.integer(Wines$wine),
judge = as.integer(Wines$judge))
# Modeling the scores given to each of teh 20 wines by the 9 judges:
h1 <- ulam(alist(
s ~ dnorm(mu, sigma),
mu <- j[judge] + w[wine],
w[wine] ~ dnorm(0, 1),
j[judge] ~ dnorm(0, 1),
sigma ~ dexp(1)),
data = WinesList,
chains = 4,
cores = 4)
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
## namespace Eigen {
## ^
## ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
## ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
precis(h1,2)
## mean sd 5.5% 94.5% n_eff Rhat4
## w[1] 0.137878499 0.32525768 -0.37412208 0.66371438 761.4301 1.0046790
## w[2] 0.100215949 0.33210791 -0.42701990 0.62153292 744.1946 1.0083418
## w[3] 0.275860694 0.33082450 -0.25578219 0.81663453 748.3284 1.0050351
## w[4] 0.559688345 0.32058325 0.03824090 1.07548061 765.1055 1.0037932
## w[5] -0.133820363 0.31884978 -0.63007097 0.39687327 701.4409 1.0076573
## w[6] -0.392877959 0.32311108 -0.91240115 0.12538586 733.2154 1.0065895
## w[7] 0.285980601 0.31254713 -0.21526873 0.79568336 657.4958 1.0049979
## w[8] 0.274645774 0.32951244 -0.25024493 0.81541938 675.3620 1.0089855
## w[9] 0.074532073 0.31928749 -0.42443090 0.58221997 768.9331 1.0043163
## w[10] 0.116527927 0.32988119 -0.39715420 0.63740218 701.9195 1.0104724
## w[11] -0.023026794 0.32424448 -0.54017431 0.48931231 758.5243 1.0096726
## w[12] -0.041144985 0.32401462 -0.56186970 0.47346828 738.3871 1.0065704
## w[13] -0.114897252 0.32625229 -0.65040064 0.40399685 861.6891 1.0067929
## w[14] -0.006370416 0.32598504 -0.52486294 0.52091344 807.7896 1.0074077
## w[15] -0.234534688 0.32535098 -0.75064925 0.30048580 674.0342 1.0070933
## w[16] -0.211267425 0.33980660 -0.72858823 0.33630387 668.7802 1.0051539
## w[17] -0.150336585 0.33463326 -0.68828939 0.37773348 728.2955 1.0079248
## w[18] -0.891059304 0.32086911 -1.40396584 -0.36942066 715.3127 1.0091177
## w[19] -0.170486040 0.32707437 -0.70779600 0.32813907 783.5118 1.0080923
## w[20] 0.382092330 0.31627811 -0.09838544 0.88904976 643.9790 1.0058798
## j[1] -0.299079933 0.25970227 -0.70838630 0.11935363 468.9437 1.0130381
## j[2] 0.241398654 0.25972477 -0.17687053 0.65461579 465.4317 1.0150836
## j[3] 0.235506196 0.26498797 -0.18327485 0.66292316 464.4859 1.0140479
## j[4] -0.588120480 0.25496846 -0.98822510 -0.17604288 509.5540 1.0093873
## j[5] 0.883531243 0.25969190 0.47215582 1.29876991 460.2230 1.0148549
## j[6] 0.538853598 0.26167140 0.13253143 0.96833658 494.9718 1.0136281
## j[7] 0.151483919 0.25990893 -0.26097483 0.56528953 488.9341 1.0134403
## j[8] -0.718478234 0.26189644 -1.14183683 -0.31143459 457.0031 1.0138716
## j[9] -0.369406901 0.25888700 -0.77159313 0.04351736 467.2324 1.0104951
## sigma 0.852335983 0.05073298 0.77635577 0.93950300 2073.1292 0.9988835
Based on the mean results: - Overall best rated wine: Wine 4 - overall worst rated wine: Wine 18 - Overall most generous judge: Judge 5 - Overall most severe judge: Judge 8