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:
#1. Temperature will probably interact with yeast the amount of bread dough.
#2. Major of study may interact with education to predict income.
#3. The age of the car will probably interact with gasoline to predict movement.
8E2. Which of the following explanations invokes an interaction?
# 1, 3 and 4 invoke an interaction.
#1 invokes an interaction between head and dryness of the onion.
#2 the explanation provides a hint that the number of cylinders and the quality of injector is independent factors that affects car speed.
#3 invokes an interaction between where they acquire their political belief.
#4 invokes an interaction between between sociality and the possession of manipulative appendages
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
#1.๐_๐=๐ฝ_๐ป๐ป_๐+๐ฝ_๐ท๐ท_๐+๐ฝ_๐ป๐ท๐ป_๐๐ท_๐
#2.๐_๐=๐ฝ_๐ถ๐ถ_๐+๐ฝ_๐๐_๐
#3.๐_๐=๐ฝ_๐๐๐_๐๐_๐+๐ฝ_๐๐น๐_๐๐น_๐
#4.๐_๐=๐ฝ_๐๐_๐+๐ฝ_๐ด๐ด_๐+๐ฝ_๐๐ด๐_๐๐ด_๐
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?
# When the temperature is introduced as a new variable, it will have a direct impact on the relationship between the water and the shade, which would lead a three way interaction between the water, shade, and temperature, or three 2 way interactions
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+ฮฒTTi+ฮฒWSTWiSiTi+ฮฒWSWiSi+ฮฒWTWiTi+ฮฒSTSiTi
# And when Ti=0, the full regression equation is as follow:
# ฮผi=ฮฑ+ฮฒWWi+ฮฒSSiโฮฑTiโฮฒWSWiSiTi+ฮฒWSWiSiโฮฒWWiTiโฮฒSSiTi
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
## Warning: package 'rstan' was built under R version 3.6.2
## Loading required package: StanHeaders
## Warning: package 'StanHeaders' was built under R version 3.6.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.2
## 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)
## Loading required package: parallel
## Loading required package: dagitty
## Warning: package 'dagitty' was built under R version 3.6.2
## rethinking (Version 2.01)
##
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
##
## rstudent
N <- 300 # size
# correlation between prey and wolf
rPW <- 0.6
# regression coefficient for prey
bP <- 0.3
# regression coefficient for wolf
bW <- 0.1
# regression coefficient for prey-by-wolf interaction
bPW <- 0.5
prey <- rnorm(
n = N,
mean = 0,
sd = 1
)
wolf <- rnorm(
n = N,
mean = rPW * prey,
sd = sqrt(1 - rPW^2)
)
raven <- rnorm(
n = N,
mean = bP*prey + bW*wolf + bPW*prey*wolf,
sd = 1
)
d <- data.frame(raven, prey, wolf)
str(d)
## 'data.frame': 300 obs. of 3 variables:
## $ raven: num 1.641 -1.769 1.702 1.13 -0.545 ...
## $ prey : num -1.6845 0.9051 -0.5389 0.7495 0.0332 ...
## $ wolf : num -0.854 -0.573 -0.149 1.822 -0.559 ...
m <- quap(
alist(
raven ~ dnorm(mu, sigma),
mu <- a + bP*prey + bW*wolf + bPW*prey*wolf,
a ~ dnorm(0, 1),
bW ~ dnorm(0, 1),
bP ~ dnorm(0, 1),
bPW ~ dnorm(0, 1),
sigma ~ dunif(0, 5)
),
data = d,
start = list(a = 0, bP = 0, bW = 0, bPW = 0, sigma = 1)
)
precis(m)
## mean sd 5.5% 94.5%
## a -0.05044997 0.06196052 -0.14947485 0.0485749
## bP 0.27898131 0.07134278 0.16496177 0.3930008
## bW 0.07783385 0.06796233 -0.03078308 0.1864508
## bPW 0.56086067 0.04053470 0.49607839 0.6256430
## sigma 0.93848092 0.03831328 0.87724889 0.9997129
#Based on the data result above, the interaction could be linear.
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)
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.3579908 0.02391811 0.3197651 0.39621659
## bw 0.2093094 0.02908737 0.1628221 0.25579661
## bs -0.1170345 0.02909212 -0.1635294 -0.07053972
## bws -0.1431911 0.03567838 -0.2002121 -0.08617018
## sigma 0.1248410 0.01693794 0.0977709 0.15191111
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$shade.c <- d$shade - mean(d$shade)
d$water.c <- d$water - mean(d$water)
d$bedb <- d$bed == "b" #Using dummy variables
d$bedc <- d$bed == "c"#Using dummy variables
# Index variable
m1 <- map(
alist(
blooms ~ dnorm(mu, sigma),
mu <- a + bW*water.c + bS*shade.c + bWS*water.c*shade.c + bBb*bedb + bBc*bedc,
a ~ dnorm(130, 100),
bW ~ dnorm(0, 100),
bS ~ dnorm(0, 100),
bWS ~ dnorm(0, 100),
bBb ~ dnorm(0, 100),
bBc ~ dnorm(0, 100),
sigma ~ dunif(0, 100)
),
data = d,
start = list(a = mean(d$blooms), bW = 0, bS = 0, bWS = 0, bBb = 0, bBc = 0, sigma = sd(d$blooms))
)
precis(m1)
## mean sd 5.5% 94.5%
## a 99.36131 12.757521 78.97233 119.75029
## bW 75.12433 9.199747 60.42136 89.82730
## bS -41.23103 9.198481 -55.93198 -26.53008
## bWS -52.15060 11.242951 -70.11901 -34.18219
## bBb 42.41139 18.039255 13.58118 71.24160
## bBc 47.03141 18.040136 18.19979 75.86303
## sigma 39.18964 5.337920 30.65862 47.72067
# Index variable
d$bedx <- coerce_index(d$bed)
m_index <- map(
alist(
blooms ~ dnorm(mu, sigma),
mu <- a[bedx] + bW*water.c + bS*shade.c + bWS*water.c*shade.c,
a[bedx] ~ dnorm(130, 100),
bW ~ dnorm(0, 100),
bS ~ dnorm(0, 100),
bWS ~ dnorm(0, 100),
sigma ~ dunif(0, 200)
),
data = d
)
precis(m_index, depth = 2)
## mean sd 5.5% 94.5%
## a[1] 97.71484 12.957148 77.00681 118.42286
## a[2] 142.36911 12.956320 121.66241 163.07581
## a[3] 146.97491 12.956444 126.26801 167.68181
## bW 75.14827 9.201794 60.44202 89.85451
## bS -41.24409 9.200611 -55.94845 -26.53974
## bWS -52.19969 11.245378 -70.17198 -34.22741
## sigma 39.19897 5.339761 30.66500 47.73294
# The result is very similar to that using index variable.