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:
# answer
# 1. the temperature will do the interaction with yeast in order to predict how bread dough rises
# 2. the Majors, ranking and the years (master or bachelor) can predict the income
# 3. the volume, condition of the car interact with gasoline can predict the car go or not
8E2. Which of the following explanations invokes an interaction?
# Answer
# 1. it has the interaction between heat and dryness when predicting the caramelization process
# 2. it include effect between the number of cylinders and the quality of fuel. it don't have interaction becase either or both options will increase car speed.
# 3. it involves interaction because most people acquire their own political beliefs from their relatives. the interaction could be both or either from their friends
# 4. it involes the interaction between the standard of manipulative appendages and the degree of sociality to predict the animal's intelligence
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
# μi = βHHi + βDDi + βHDHiDi
# μi = βCCi + βQQi
# μi = βTPTiPi + βTFTiFi
# μ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, T = temperature, S = Shade
# the interaction will have WTS, WT,TS, WS
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# The regression equation would be: μi = α + βWWi + βSSi − αTi − βWSWiSiTi + βWSWiSi − βWWiTi − βSSiTi
#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 is: μi=α+βWWi+βSSi−αTi−βWSWiSiTi+βWSWiSi−βWWiTi−βSSiTi
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)
d <- tulips
str(d)
## '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 ...
d$blooms_std <- d$blooms / max(d$blooms)
d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)
bw_d<- abs(rnorm(nrow(d),0,0.25))
bs_d<- (-abs(rnorm(nrow(d),0,0.25)))
m_tulip <- 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=d )
precis(m_tulip)
## mean sd 5.5% 94.5%
## a 0.3579840 0.02391524 0.31976282 0.39620517
## bw 0.2098345 0.02908393 0.16335272 0.25631620
## bs -0.1169650 0.02908837 -0.16345380 -0.07047612
## bws -0.1431578 0.03567438 -0.20017236 -0.08614326
## sigma 0.1248260 0.01693284 0.09776409 0.15188798
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)
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)
# bed index
d$bed_index <- coerce_index(d$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 = d
)
precis(bed_var, depth = 2 )
## mean sd 5.5% 94.5%
## a[1] 0.2732846 0.03578412 0.21609463 0.33047450
## a[2] 0.3964074 0.03576645 0.33924568 0.45356907
## a[3] 0.4091205 0.03576538 0.35196046 0.46628044
## bw 0.2074275 0.02542448 0.16679424 0.24806069
## bs -0.1138394 0.02541980 -0.15446516 -0.07321365
## bws -0.1438812 0.03105603 -0.19351477 -0.09424769
## sigma 0.1084002 0.01476687 0.08479986 0.13200047
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)
d2 <- Wines2012
d2_list = list(s = standardize(d2$score),
wine = as.integer(d2$wine),
judge = as.integer(d2$judge))
str(d2_list)
## List of 3
## $ s : num [1:180] -1.5766 -0.4505 -0.0751 0.3003 -2.3274 ...
## ..- attr(*, "scaled:center")= num 14.2
## ..- attr(*, "scaled:scale")= num 2.66
## $ wine : int [1:180] 1 3 5 7 9 11 13 15 17 19 ...
## $ judge: int [1:180] 4 4 4 4 4 4 4 4 4 4 ...
Awines <- 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 = d2_list,
chains = 4,
cores = 4)
precis(Awines, 2)
## mean sd 5.5% 94.5% n_eff Rhat4
## w[1] 0.109112132 0.2545357 -0.29475565 0.50964223 2926.171 0.9993263
## w[2] 0.090377543 0.2611510 -0.33161585 0.49733452 3170.838 0.9985954
## w[3] 0.225911710 0.2515377 -0.17971165 0.63615922 2736.432 0.9990304
## w[4] 0.469159979 0.2602264 0.06241986 0.89057186 3135.738 0.9992602
## w[5] -0.100011582 0.2544079 -0.51389387 0.30619038 2658.409 0.9986835
## w[6] -0.313036807 0.2627756 -0.73492460 0.10289161 2977.999 1.0007229
## w[7] 0.248263627 0.2681109 -0.17748687 0.68388754 2818.838 0.9990398
## w[8] 0.231080144 0.2601182 -0.19197165 0.64662969 3061.713 0.9995927
## w[9] 0.076247780 0.2680573 -0.33924302 0.52085186 3515.787 0.9988643
## w[10] 0.095848100 0.2610689 -0.30128042 0.51745440 3129.333 0.9983414
## w[11] -0.007011070 0.2618650 -0.42492240 0.40182260 3234.906 0.9990096
## w[12] -0.016106413 0.2603936 -0.42335116 0.39040003 2922.889 0.9989933
## w[13] -0.087770017 0.2626852 -0.51396858 0.33967141 3308.945 0.9985055
## w[14] 0.007773522 0.2605760 -0.39930670 0.41290322 3105.146 0.9991738
## w[15] -0.178142365 0.2558448 -0.58166132 0.23942188 3467.840 0.9989916
## w[16] -0.165036589 0.2582399 -0.56247547 0.25782468 2750.082 0.9985479
## w[17] -0.119516995 0.2587667 -0.52754178 0.28430748 2894.700 1.0000782
## w[18] -0.722168526 0.2668738 -1.16835619 -0.30460514 3100.880 0.9988582
## w[19] -0.141459491 0.2545136 -0.55766653 0.27998651 3007.981 1.0000687
## w[20] 0.325435304 0.2619767 -0.10057557 0.73634027 3178.724 0.9991594
## j[1] -0.280834855 0.2026772 -0.60095343 0.04534936 2705.359 0.9994214
## j[2] 0.210110795 0.1955370 -0.09483602 0.51821508 2238.023 0.9993471
## j[3] 0.202895634 0.2014192 -0.11849366 0.52730148 2678.865 0.9990162
## j[4] -0.544046208 0.1945837 -0.85148339 -0.24249701 2073.171 0.9995846
## j[5] 0.786852597 0.2009145 0.45452180 1.10969750 2353.581 0.9991004
## j[6] 0.466340362 0.1985670 0.14331769 0.78283113 2495.158 0.9990201
## j[7] 0.123907587 0.2037761 -0.20286682 0.44383496 2458.233 0.9990602
## j[8] -0.659335793 0.1977688 -0.97606840 -0.34665089 2509.122 0.9990222
## j[9] -0.345204239 0.1940868 -0.64782714 -0.02753486 2407.746 0.9991581
## sigma 0.847739388 0.0482266 0.77371361 0.92615977 3086.889 0.9987099
traceplot(Awines)
## [1] 1000
## [1] 1
## [1] 1000
# Answer: form the traceplot above, we can see that 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.