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.
#1. temperature will interact with yeast to predict how bread dough rises

#2. Majors, ranking and the master or bachelor can predict the income

#3. the volume and car condition interact with gasoline can predict whether the car will go or not

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. interaction will be between heat and dryness to predict caramelizing process

#2. number of cylinders and quality of fuel. there's no interaction because either will increase car speed.

#3. interaction takes place because most people gain political beliefs from relatives

#4. interaction between manipulative appendages and the degree of sociality  to predict the animal intelligence

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

# μi = βHHi + βDDi + βHDHiDi
# μi = βCCi + βQQi
# μi = βTPTiPi + βTFTiFi
# μi = βSSi + βAAi + βSASiAi

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?

# Let's take the following: W = water, T = temperature, S = Shade
# INteractions: WTS, WT,TS, WS

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

# Equation would be: μi = α + βWWi + βSSi − αTi − βWSWiSiTi + βWSWiSi − βWWiTi − βSSiTi

#Let's take predictors = 1: μi|Wi=1,Si=1,T=1=α+βW+βS+βT+βWST+βWS+βWT+βST

# When Ti=0: μi=α+βWWi+βSSi−αTi−βWSWiSiTi+βWSWiSi−βWWiTi−βSSiTi

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)
## Warning: package 'ggplot2' was built under R version 4.0.5
data(tulips)
dd <- tulips
str(dd)
## '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 ...
dd$blooms_std <- dd$blooms / max(dd$blooms)
dd$water_cent <- dd$water - mean(dd$water)
dd$shade_cent <- dd$shade - mean(dd$shade)

bw_d<- abs(rnorm(nrow(dd),0,0.25))
bs_d<- (-abs(rnorm(nrow(dd),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=dd )

precis(m_tulip)
##             mean         sd        5.5%       94.5%
## a      0.3579818 0.02391458  0.31976167  0.39620190
## bw     0.2097640 0.02908311  0.16328353  0.25624440
## bs    -0.1169961 0.02908774 -0.16348391 -0.07050827
## bws   -0.1431604 0.03567340 -0.20017336 -0.08614739
## sigma  0.1248226 0.01693168  0.09776249  0.15188269

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)
dd <- tulips

dd$blooms_std <- dd$blooms / max(dd$blooms)
dd$water_cent <- dd$water - mean(dd$water)
dd$shade_cent <- dd$shade - mean(dd$shade)

# bed index
dd$bed_index <- coerce_index(dd$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 = dd
)

precis(bed_var, depth = 2 )
##             mean         sd        5.5%       94.5%
## a[1]   0.2732725 0.03578311  0.21608423  0.33046087
## a[2]   0.3963983 0.03576551  0.33923806  0.45355845
## a[3]   0.4091147 0.03576446  0.35195615  0.46627318
## bw     0.2074430 0.02542374  0.16681093  0.24807501
## bs    -0.1138457 0.02541914 -0.15447037 -0.07322099
## bws   -0.1438812 0.03105525 -0.19351344 -0.09424887
## sigma  0.1083974 0.01476588  0.08479865  0.13199612

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 ...
Awines <- 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(Awines, 2)
##               mean         sd        5.5%       94.5%    n_eff     Rhat4
## w[1]   0.107031938 0.26144424 -0.30898521  0.53658694 3038.858 0.9988275
## w[2]   0.085140737 0.25890827 -0.33027355  0.48830198 3231.024 0.9984083
## w[3]   0.223595540 0.24483521 -0.16577339  0.60286439 2621.406 0.9993449
## w[4]   0.464340703 0.24887369  0.06589690  0.85667941 2398.604 0.9986571
## w[5]  -0.106575133 0.25676392 -0.52649576  0.31046671 3035.596 1.0019281
## w[6]  -0.303955100 0.25108163 -0.69829346  0.09156760 2584.425 0.9995088
## w[7]   0.239543073 0.25664960 -0.16029041  0.64123293 2741.480 0.9986644
## w[8]   0.225415978 0.24635880 -0.18311921  0.62352341 2837.270 1.0001426
## w[9]   0.073175221 0.26456623 -0.34862120  0.51178910 3314.502 0.9997141
## w[10]  0.100213555 0.24818822 -0.29333398  0.49744457 3154.635 0.9994906
## w[11] -0.015938794 0.26175173 -0.43666257  0.41222958 3260.885 1.0000268
## w[12] -0.021239614 0.25706069 -0.44309411  0.38322643 2637.259 0.9993133
## w[13] -0.090492710 0.24228205 -0.47493540  0.28718405 2470.918 1.0001891
## w[14]  0.007367331 0.26016181 -0.40500894  0.42164432 2733.704 0.9989624
## w[15] -0.184042510 0.25936811 -0.58891246  0.22859660 3301.054 0.9998247
## w[16] -0.166551166 0.26048076 -0.57662634  0.22818975 3078.463 0.9993540
## w[17] -0.123300242 0.25412569 -0.51416086  0.29259701 2893.209 0.9990513
## w[18] -0.717820551 0.25642898 -1.11726645 -0.30475408 2559.245 0.9995883
## w[19] -0.138473637 0.25028651 -0.55834936  0.25031718 2904.890 0.9990916
## w[20]  0.319835269 0.24969938 -0.06586945  0.71128849 3382.593 0.9989847
## j[1]  -0.271655670 0.18466918 -0.56400092  0.02531531 2175.646 1.0006493
## j[2]   0.220182503 0.19433357 -0.08449718  0.53006618 2665.536 0.9992365
## j[3]   0.203117768 0.19251948 -0.09367630  0.51208323 2535.770 0.9991040
## j[4]  -0.543673389 0.19361935 -0.85875725 -0.23311134 2254.802 0.9990596
## j[5]   0.802398184 0.19075349  0.50159596  1.11628862 1969.348 0.9997973
## j[6]   0.476656813 0.18154900  0.18859901  0.77236994 2097.887 0.9997213
## j[7]   0.134706668 0.19351331 -0.17220217  0.43973580 2410.609 0.9990120
## j[8]  -0.653270382 0.18969088 -0.95999231 -0.34549827 2494.399 0.9985123
## j[9]  -0.348880265 0.18855778 -0.64414392 -0.05569657 1960.795 0.9992630
## sigma  0.848670337 0.04798154  0.77441530  0.92645473 2386.904 0.9983125
traceplot(Awines)
## [1] 1000
## [1] 1
## [1] 1000

#Wine 4 is the highest rating and wine 18 is lowest rating
#Judge 5 gives highest rating and judge 8 gives lowest rating