Chapter 8 - Conditional Manatees

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.

Questions

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.
  2. Education leads to higher income.
  3. Gasoline makes a car go.
# Quality of dough / Baker's expertise / temperature
# Type of Job / Sector of Job / Experience
# Power of Engine

8E2. Which of the following explanations invokes an interaction?

  1. Caramelizing onions requires cooking over low heat and making sure the onions do not dry out.
  2. A car will go faster when it has more cylinders or when it has a better fuel injector.
  3. Most people acquire their political beliefs from their parents, unless they get them instead from their friends.
  4. Intelligent animal species tend to be either highly social or have manipulative appendages (hands, tentacles, etc.).
# 1. Onion caramelizationAn invokes interaction  between heat and dryness. Specifically, it implies that caramelization will only occur when both heat and dryness are low.

# 2. This explanation invokes main effects of number of cylinders and fuel injector quality on car speed but does not explicitly invoke an interaction. Specifically, it seems to imply that adding cylinders and increasing the quality of the fuel injector are independent routes to increasing car speed.

# 3. This explanation implies that there are two types of people: those who acquire their beliefs from their parents and those who acquire their beliefs from their friends. The implied model seems to predict individuals’ political beliefs using a linear combination of the interactions between type and parents’ beliefs on the one hand and between type and friends’ beliefs on the other hand.

# 4. There seems an interaction between sociality and the possession of manipulative appendages in predicting a species’ intelligence. Specifically, it seems to imply that intelligent species are high on sociality or have manipulative appendages but are not both high on sociality and in possession of manipulative appendages.

8E3. For each of the explanations in 8E2, write a linear model that expresses the stated relationship.

# 1. μ_i = α + β_H*H_i + β_D*D_i + β_HD*H_i*D_i 
#    (H = heat, D = Dryness)

# 2. μ_i = α + β_C*C_i + β_F*F_i
#    (C = cylinder, F = fuel injection)

# 3. μ_i = α + β_TP*T_i*P_i + β_TF*T_i*F_i
#    (T = type of people, F = friends, P = parents)

# 4. μ_i = α + β_S*S_i + β_M*M_i + β_SM*S_i*M_i
#    (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?

# Since there are three predictor variables now (Water, Shade, and Temperature), we would have a single three-way interaction and three two-way interactions: WST, WS, WT, and ST.

8M2. Can you invent a regression equation that would make the bloom size zero, whenever the temperature is hot?

# According to the text the regression varibale the regression formula resembles:
# μ_i = α + β_W*W_i + β_S*S_i + β_T*T_i + β_WT*W_i*T_i + β_ST*S_i*T_i + β_WS*W_i*S_i + β_WST*W_i*S_i*T_i

# 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.

# bloom size = 0( μi = 0), when temperature is hot (Ti = 1),

# μi = α  - α*T_i + β_W*W_i - β_W*W_i*T_i + β_S*S_i - β_S*S_i*T_i + β_WS*W_i*S_i - β_WS*W_i*S_i*T_i

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)

m8.4 <- 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(m8.4)
##             mean         sd        5.5%       94.5%
## a      0.3579971 0.02404869  0.31956265  0.39643154
## bw     0.2205118 0.02953046  0.17331644  0.26770719
## bs    -0.1272565 0.02956789 -0.17451173 -0.08000134
## bws   -0.1431295 0.03587132 -0.20045878 -0.08580020
## sigma  0.1255286 0.01721842  0.09801025  0.15304696
#The negative effect of shade & positive effect of water make "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.

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)

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
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.67596 12.951194  76.97745 118.37447
## a[2]  142.37109 12.950457 121.67376 163.06842
## a[3]  146.98659 12.950580 126.28906 167.68412
## bW     75.15974  9.197545  60.46028  89.85919
## bS    -41.25255  9.196396 -55.95017 -26.55494
## bWS   -52.20600 11.240248 -70.17009 -34.24191
## sigma  39.18095  5.333594  30.65684  47.70506
coeftab(m_dummy, m_index)
##       m_dummy m_index
## a       99.36      NA
## bW      75.12   75.16
## bS     -41.23  -41.25
## bWS    -52.15  -52.21
## bBb     42.41      NA
## bBc     47.03      NA
## sigma   39.19   39.18
## a[1]       NA   97.68
## a[2]       NA  142.37
## a[3]       NA  146.99
## nobs       27      27
#The results are very similar. The main differences are the form in which the bed-specific intercepts are presented.

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?