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. 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:
#Yeast requires a specific temperature to rise and thrive, this causes the dough to rise. Therefore a third variable in this situation would be temperature.
#Quality of education is a great factor within the concept of education. But different industries can require different level of education. Hence, industry could be a third variable.
#Different gasoline type makes the car run differently. So, the gasoline type could be a third variable here.
8E2. Which of the following explanations invokes an interaction?
#1. Carmelization of onion requires the precise amount of heat and wetness. This would be the invoked interaction between heat & dryness
#2. No specific interaction is invoked, because 2 factors cylinders and fuel type are not dependent.
#3. The interaction between people and their parents/friends and the effect that parents/friends have on one's political beliefs is an interaction.
#4. The interaction is invoked by the combination of intelligence & socialability or intelligent & manipulative appendages.
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
#1.μi=α+βHHi+βDDi+βHDHiDi #(H = heat, D = Dryness)
#2.μi=α+βCCi+βFFi #(C = cylinder, F = fuel injection)
#3.μi=α+βMPMiPi+betaMFMiFi #(F = friends, P = parents)
#4.μi=α+βSSi+βMMi+βSMSiMi #(S = social, M = manipultive appendages)
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?
#The temperature change is now a new variable introduced into the situation. The value of the temperature will have a direct impact on the relationship between the water and the shade. so this could result in a three way interaction between the water, shade, and temperature, or three 2 way interactions between all three variables.
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
# μi=α+βWWi+βSSi+βTTi+βWTWiTi+βSTSiTi+βWSWiSi+ βWSTWiSiTi
# The first 3 are the water, shade, and temperature by themselves, the next 3 are 2 way interactions between water & temperature, shade &temperature, and water & shade. Lastly is the 3 way interaction between the water, shade, and temperature.
#therefore bloom size = 0( μi = 0), when temperature is hot (Ti = 1),
# μi= α+βWWi+βSSi-αTi-βWTWiTi+βSTSiTi+βWSWiSi- βWSTWiSiTi
8M3. In parts of North America, ravens depend upon wolves for their food. This is because ravens are carnivorous but cannot usually kill or open carcasses of prey. Wolves however can and do kill and tear open animals, and they tolerate ravens co-feeding at their kills. This species relationship is generally described as a “species interaction.” Can you invent a hypothetical set of data on raven population size in which this relationship would manifest as a statistical interaction? Do you think the biological interaction could be linear? Why or why not?
library(rethinking)
## Loading required package: rstan
## Loading required package: StanHeaders
## Loading required package: ggplot2
## rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## options(mc.cores = parallel::detectCores()).
## To avoid recompilation of unchanged Stan programs, we recommend calling
## rstan_options(auto_write = TRUE)
## Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file
## Loading required package: parallel
## rethinking (Version 2.13)
##
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
##
## rstudent
Pcof <- 0.4
Wcof <- 0.1
PWcof <- 0.6
p <- rnorm(1000, 0,1)
w <- rnorm(1000,20,8)
r <- rnorm(1000, Pcof*p + Wcof*w + PWcof*p*w, 1 )
d <- data.frame(r,p,w)
plot(r~p,d)
#Based on the results of provided data - the interaction could be linear. We can see the slop start as a regression and the assumption is that the interaction between the wolves and the ravens are pretty significant.
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?
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)
tulip_analy <- 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( -1 , 0.25 ) ,
bws ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
),
data = d)
precis(tulip_analy)
## mean sd 5.5% 94.5%
## a 0.3579995 0.02404850 0.31956534 0.39643365
## bw 0.2205044 0.02953002 0.17330973 0.26769909
## bs -0.1272488 0.02956742 -0.17450328 -0.07999439
## bws -0.1431287 0.03587104 -0.20045752 -0.08579982
## sigma 0.1255276 0.01721801 0.09800989 0.15304531
#The negative effect of shade & positive effect of water make the bws a negative mean value
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)
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.2732852 0.03578114 0.21609999 0.33047035
## a[2] 0.3964083 0.03576347 0.33925134 0.45356522
## a[3] 0.4091176 0.03576239 0.35196241 0.46627282
## bw 0.2074268 0.02542234 0.16679695 0.24805657
## bs -0.1138478 0.02541763 -0.15447010 -0.07322552
## bws -0.1438817 0.03105343 -0.19351106 -0.09425230
## sigma 0.1083909 0.01476372 0.08479567 0.13198622