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.bread dough rises because of yeast, the hypothetical third variable can be time of processing for the yeast to be formed.
##2 Education leads to higher income, the hypothetical third variable can be level of qualifications like the degree.
##3. Gasoline makes a car go, the third variable could be the type of gasoline with higher ethanol, like 89 or 93 fuel.
8E2. Which of the following explanations invokes an interaction?
#The following invokes an interaction
## 1. Caramelizing onions requires cooking over low heat and making sure the onions do not dry out.
## 4. Intelligent animal species tend to be either highly social or have manipulative appendages (hands, tentacles, etc.).
8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.
##1. yi = βl*xl + βd*xd + βld*xd*xl
##2. yi = βc*xc + βf*xf
##3. yi = βp*xp + βf*xf
##4. yi = βs*xs + βm*xm + βsm*xs*xm
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 results that no plants grow with hot temperature because the temperature is the main factor with water and shade depending on it.
## Or there could be an interaction with temperature and water, temperature and shade or could have an effect with both.
8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?
##μi = α + βht*xht + βw*xw + βs*xs + βhtws*xht*xw*xs + βhtw*xht*xw + βws*xw*xs + βhts*xht*xs
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?
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)
## Loading required package: parallel
## Loading required package: dagitty
## rethinking (Version 2.01)
##
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
##
## rstudent
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)
a <- rnorm( 1e4 , 0.5 , 1 ) #8.21
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.6094
a <- rnorm( 1e4 , 0.5 , 0.25 ) #8.22
sum( a < 0 | a > 1 ) / length( a )
## [1] 0.0445
m8.6 <- quap(
alist(
blooms_std ~ dnorm( mu , sigma ) ,
mu <- a + bw*water_cent + bs*shade_cent ,
a ~ dnorm( 0.5 , 0.25 ) ,
bw ~ dnorm( 0 , 0.25 ) ,
bs ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
) ,
data=d )
m8.7 <- 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( 0 , 0.25 ) ,
bs ~ dnorm( 0 , 0.25 ) ,
bws ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
) ,
data=d )
par(mfrow=c(1,3)) # 3 plots in 1 row
for ( s in -1:1 ) {
idx <- which( d$shade_cent==s )
plot( d$water_cent[idx] , d$blooms_std[idx] , xlim=c(-1,1) , ylim=c(0,1) ,
xlab="water" , ylab="blooms" , pch=16 , col=rangi2 )
mu <- link( m8.6 , data=data.frame( shade_cent=s , water_cent=-1:1 ) )
for ( i in 1:20 ) lines( -1:1 , mu[i,] , col=col.alpha("black",0.3) )
}
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.
df <- tulips
pairs(df)
df$shade.c <- df$shade - mean(df$shade)
df$water.c <- df$water - mean(df$water)
#index
df$bed.idx <- coerce_index(df$bed)
m1 <- map(
alist(
blooms ~ dnorm( mu , sigma ) ,
mu <- a + bW*water.c + bS*shade.c + bWS*water.c*shade.c ,
a ~ dnorm( 130 , 100 ) ,
bW ~ dnorm( 0 , 100 ) ,
bS ~ dnorm( 0 , 100 ) ,
bWS ~ dnorm( 0 , 100 ) ,
sigma ~ dunif( 0 , 100 )
) ,
data=df ,
start=list(a=mean(df$blooms),bW=0,bS=0,bWS=0,sigma=sd(df$blooms)) )
precis(m1,depth=2)
## mean sd 5.5% 94.5%
## a 129.00797 8.670771 115.15041 142.86554
## bW 74.95946 10.601997 58.01542 91.90350
## bS -41.14054 10.600309 -58.08188 -24.19920
## bWS -51.87265 12.948117 -72.56625 -31.17906
## sigma 45.22497 6.152982 35.39132 55.05863
m2 <- map(
alist(
blooms ~ dnorm( mu , sigma ) ,
mu <- a[bed.idx] + bW*water.c + bS*shade.c + bWS*water.c*shade.c ,
a[bed.idx] ~ dnorm( 130 , 100 ) ,
bW ~ dnorm( 0 , 100 ) ,
bS ~ dnorm( 0 , 100 ) ,
bWS ~ dnorm( 0 , 100 ) ,
sigma ~ dunif( 0 , 100 )
) ,
data=df ,
start=list(a=c(mean(df$blooms),mean(df$blooms),mean(df$blooms)),bW=0,bS=0,bWS=0,sigma=sd(df$blooms)),
method="Nelder-Mead" ,
control=list(maxit=1e4))
precis(m2,depth=2)
## mean sd 5.5% 94.5%
## a[1] 97.60558 12.947359 76.91320 118.29796
## a[2] 142.31041 12.946928 121.61872 163.00210
## a[3] 147.00198 12.946923 126.31029 167.69366
## bW 75.14027 9.195049 60.44480 89.83573
## bS -41.24169 9.193827 -55.93520 -26.54818
## bWS -52.18491 11.237170 -70.14408 -34.22574
## sigma 39.16980 5.329814 30.65173 47.68787
compare(m1,m2)
## WAIC SE dWAIC dSE pWAIC weight
## m2 295.1091 10.12907 0.00000 NA 9.980759 0.7264202
## m1 297.0621 10.78564 1.95307 8.392442 7.146228 0.2735798
## results appear similar