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.
# Commercial baking yeast is inactive below 40∘F and dies above 130∘F,thus temperature would likely interact with yeast in the process of bread dough rising as the third variable.

# Different major/industry have vastly different distributions of expected income, therefore the major/industry would likely be the third variable and interact with years of education to effect the income.

# A variable representing each car’s engine functioning (or repair status) would likely interact with gasoline to predict movement.

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?

# The temperature change is now the enew variable introduced into the space. The value of the temperature will have a direct impact on the relationship between the water and the shade. 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?

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

#therefore 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.3579948 0.02404881  0.31956012  0.39642940
## bw     0.2205064 0.02953048  0.17331095  0.26770176
## bs    -0.1272532 0.02956795 -0.17450850 -0.07999790
## bws   -0.1431366 0.03587147 -0.20046612 -0.08580704
## sigma  0.1255293 0.01721862  0.09801063  0.15304800
# #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)

# bed index
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.2732845 0.03578479  0.21609347  0.33047547
## a[2]   0.3964056 0.03576711  0.33924281  0.45356830
## a[3]   0.4091197 0.03576604  0.35195869  0.46628078
## bw     0.2074276 0.02542495  0.16679366  0.24806163
## bs    -0.1138486 0.02542025 -0.15447511 -0.07322218
## bws   -0.1438818 0.03105661 -0.19351626 -0.09424733
## sigma  0.1084022 0.01476756  0.08480081  0.13200363

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?

data(Wines2012)
d2 <- Wines2012

d2_list = list(s = standardize(d2$score),
                 wine = as.integer(d2$wine),
                 judge = as.integer(d2$judge))

str(d2_list)
## List of 3
##  $ s    : num [1:180] -1.5766 -0.4505 -0.0751 0.3003 -2.3274 ...
##   ..- attr(*, "scaled:center")= num 14.2
##   ..- attr(*, "scaled:scale")= num 2.66
##  $ wine : int [1:180] 1 3 5 7 9 11 13 15 17 19 ...
##  $ judge: int [1:180] 4 4 4 4 4 4 4 4 4 4 ...
H8.5 <- ulam(alist(
              s ~ dnorm(mu, sigma),
              mu <- j[judge] + w[wine] ,
              w[wine] ~ dnorm(0, 0.5),
              j[judge] ~ dnorm(0, 0.5),
              sigma ~ dexp(1)),
         data = d2_list, 
         chains = 4,
         cores = 4)

precis(H8.5, 2)
##               mean         sd        5.5%       94.5%    n_eff     Rhat4
## w[1]   0.107968175 0.25661349 -0.30505227  0.51619630 2954.025 1.0000916
## w[2]   0.082299735 0.26252074 -0.34818129  0.49815322 2915.782 0.9990529
## w[3]   0.226067946 0.25028402 -0.17739179  0.62264604 2452.770 0.9992830
## w[4]   0.464467833 0.26137387  0.04610123  0.88748526 3363.423 0.9991440
## w[5]  -0.105792522 0.25747154 -0.51967360  0.30666012 3036.902 0.9989730
## w[6]  -0.310645232 0.25014002 -0.70908318  0.09606134 3563.626 0.9983045
## w[7]   0.238137234 0.26269538 -0.18442221  0.64938033 3099.016 0.9995912
## w[8]   0.222718054 0.25563401 -0.18474260  0.62835170 2980.828 0.9992467
## w[9]   0.067840779 0.25244261 -0.33730178  0.47030156 3301.989 0.9985074
## w[10]  0.092283628 0.26819312 -0.33548692  0.53120547 3271.137 0.9995492
## w[11] -0.005785435 0.25664250 -0.41322555  0.40641919 2862.325 0.9986934
## w[12] -0.032309845 0.26472651 -0.45143617  0.39617559 3212.418 0.9992326
## w[13] -0.096381924 0.25540610 -0.51107988  0.30852374 4208.448 0.9992580
## w[14]  0.000558189 0.25741812 -0.41890343  0.40370464 2322.540 0.9998449
## w[15] -0.186039343 0.26179109 -0.59393859  0.24187014 2594.229 0.9994554
## w[16] -0.169398832 0.24791104 -0.57004315  0.23305871 2753.515 1.0001676
## w[17] -0.127265904 0.26430807 -0.54733830  0.30286593 2450.559 0.9987243
## w[18] -0.725335508 0.26350322 -1.14460106 -0.29500157 3339.197 0.9989308
## w[19] -0.140233866 0.26123012 -0.55641195  0.29121113 2717.421 0.9993542
## w[20]  0.323041482 0.25435103 -0.08340270  0.74096258 3004.027 0.9997741
## j[1]  -0.277310524 0.19759464 -0.59518756  0.03741366 2082.255 0.9994401
## j[2]   0.217935965 0.19569399 -0.08480448  0.53076714 2450.243 1.0004239
## j[3]   0.206476663 0.20085687 -0.10880952  0.52198957 2226.372 0.9988022
## j[4]  -0.538680346 0.19594261 -0.85758239 -0.21997137 2686.580 0.9986407
## j[5]   0.805135152 0.19229494  0.50017550  1.11419785 2475.408 0.9989541
## j[6]   0.484199893 0.20293347  0.16222218  0.80523808 2675.172 0.9994458
## j[7]   0.133343690 0.19709668 -0.17573325  0.45655865 2413.058 0.9993065
## j[8]  -0.651426664 0.19419468 -0.95780400 -0.33925080 2227.846 0.9989511
## j[9]  -0.338524537 0.19364016 -0.64423223 -0.03402611 2038.136 0.9999004
## sigma  0.846622954 0.04944024  0.77053059  0.92780098 2717.647 1.0008897
traceplot(H8.5)
## [1] 1000
## [1] 1
## [1] 1000

## Waiting to draw page 2 of 2
# As can be seen from the results above, wine 4 seems have the highest rating and wine 18 have the least rating. Judge 5 seems to give the highest average rating and judge 8 seems to give the least average rating based on the mean values and traceplot.