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:
Gasoline makes a car go.
The condition of the car can interact with gasoline to predict if a car can go or not.
8E2. Which of the following explanations invokes an interaction?
Intelligent animal species tend to be either highly social or have manipulative appendages (hands, tentacles, etc.).
It involves an interaction. This explanation involves an interaction between degree of sociality and possession manipulative appendages to predict animals’ intelligence.
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
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?
Suppose that W = water, T = Temperature, S = Shade An interaction allows the relationship between a predictor and outcome to be dependent on the value of another predictor. In this case, there are three predicts, and following scenarios can be considered: 1. a three-way interaction: WTS and 2. three two-way interactions: WT, WS, TS.
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
The algebraic form of the regression equation is: μ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
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?
prey <- as.integer(rnorm(100, mean=50, sd=15) )
wolves <- as.integer(rnorm(100, mean=25, sd=7))
ravens <- as.integer(rnorm(100, mean= 5 + 0.3*prey + -1*wolves + 0.4*wolves*prey, sd=9))
d <- data.frame(prey=prey, wolves=wolves, ravens=ravens)
par(mfrow=c(1,2))
plot(ravens ~ prey, data=d)
plot(ravens ~ wolves, data=d)
The biological interaction is probably not linear, since both the amount of prey and predator (wolves and ravens) depend upon each other. The more prey there is, the more predator is there and the more predator there are, the less prey there is.
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
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.3579835 0.02391851 0.31975712 0.39620992
## bw 0.2090260 0.02908818 0.16253746 0.25551451
## bs -0.1182673 0.02910043 -0.16477538 -0.07175915
## bws -0.1431655 0.03567916 -0.20018772 -0.08614333
## sigma 0.1248433 0.01693982 0.09777017 0.15191638
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)
# Dummy variables
d$bedb <- d$bed == "b"
d$bedc <- d$bed == "c"
# Index variable
d$bedx <- coerce_index(d$bed)
# with the dummy variable
m_dummy <- 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(m_dummy)
## 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
# with an index variable
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
)
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
precis(m_index, depth = 2)
## Warning in sqrt(diag(vcov(model))): NaNs produced
## Warning in sqrt(diag(vcov(model))): NaNs produced
## Warning in sqrt(diag(vcov(model))): NaNs produced
## mean sd 5.5% 94.5%
## a[1] -41.99094 37.04614 -101.19782 17.215940
## a[2] 100.65126 52.91118 16.08898 185.213536
## a[3] 126.36679 53.91419 40.20150 212.532080
## bW -31.92694 20.75573 -65.09861 1.244729
## bS -60.85830 41.03085 -126.43353 4.716918
## bWS -78.84580 48.14574 -155.79199 -1.899617
## sigma 193.70041 NaN NaN NaN
coeftab(m_dummy, m_index)
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
## Caution, model may not have converged.
## Code 1: Maximum iterations reached.
## Warning in sqrt(diag(vcov(model))): NaNs produced
## m_dummy m_index
## a 99.36 NA
## bW 75.12 -31.93
## bS -41.23 -60.86
## bWS -52.15 -78.85
## bBb 42.41 NA
## bBc 47.03 NA
## sigma 39.19 193.70
## a[1] NA -41.99
## a[2] NA 100.65
## a[3] NA 126.37
## nobs 27 27
The results are similar, the difference is the form in which the bed-specific intercepts are presented. The intercepts are different between the 2 approaches. for dummy variable, a, bBb and bBc are the intercepts for a,b,c. For index variable, the intercept for bed is a[1], a[2] and a[3] for a,b,c.