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. 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. Time- Time it takes to rise dough because of yeast
#2. Occupation- Occupations in demand have higher income
#3. Combustion- Gasoline combusts and releases energy for the car to go 

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. Caramelization of onions depends on temperature and dryness
#4. Intelligence in animal species depends on being social or manipulative appendages

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

#1. yi = βh*xh + βd*xd + βhd*xd*xh
#2. yi = βc*xc + βi*xi 
#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?

#An interaction allows the relationship between a predictor and an outcome to depend upon the value of another predictor. Since the question is stating that the relationships between blooms and water and between blooms and shade depend upon the value of temperature, interaction effects can be used here. 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?

# The algebraic form of the regression equation was shown below:
# μi=α+βW*Wi+βS*Si+βT*Ti+βWST*Wi*Si*Ti+βWS*Wi*Si+βWT*Wi*Ti+βST*Si*Ti
# After invention, the regression equation that would make the bloom size zero, whenever the temperature is hot
# μi=α+βW*Wi+βS*Si−αTi–βWS*Wi*Si*Ti+βWS*Wi*Si–βW*Wi*Ti–βS*Si*Ti

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
## rethinking (Version 2.13)
## 
## Attaching package: 'rethinking'
## The following object is masked from 'package:stats':
## 
##     rstudent
data(tulips)
df <- tulips
df$blooms_std = df$blooms / max(df$blooms)
df$shade_cent = df$shade - mean(df$shade)
df$water_cent = df$water - mean(df$water)

m1 <-quap(
  alist(
    blooms_std ~ dnorm(mu, sigma),
    mu<- α + βs*shade_cent + βw*water_cent + βsw*shade_cent*water_cent,
    α ~ dnorm(0.5, 0.25),
    βs ~ dnorm(0,0.25),
    βw ~ dnorm(0.5,0.25),
    βsw ~ dnorm(0,0.25),
    sigma ~ dexp(1)
  ), data=df)
precis(m1)
##             mean         sd       5.5%       94.5%
## α      0.3579849 0.02392278  0.3197517  0.39621809
## βs    -0.1134600 0.02923232 -0.1601789 -0.06674108
## βw     0.2135646 0.02924862  0.1668196  0.26030951
## βsw   -0.1431599 0.03568548 -0.2001922 -0.08612765
## sigma  0.1248657 0.01694877  0.0977783  0.15195312

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
df$blooms_std = df$blooms / max(df$blooms)
df$shade_cent = df$shade - mean(df$shade)
df$water_cent = df$water - mean(df$water)
df$bed2 = coerce_index(df$bed)

m2 <-quap(
  alist(
    blooms_std ~ dnorm(mu, sigma),
    mu<- α + βs*shade_cent + βw*water_cent + βb*bed2 +βsw*shade_cent*water_cent,
    α ~ dnorm(0.5, 0.25),
    βs ~ dnorm(0,0.25),
    βw ~ dnorm(0.5,0.25),
    βb ~ dnorm(0,0.25),
    βsw ~ dnorm(0,0.25),
    sigma ~ dexp(1)
  ), data=df)
precis(m2)
##              mean         sd        5.5%       94.5%
## α      0.23314775 0.05525369  0.14484167  0.32145382
## βs    -0.11377562 0.02613936 -0.15555137 -0.07199987
## βw     0.21276785 0.02615114  0.17097328  0.25456241
## βb     0.06274469 0.02564351  0.02176140  0.10372797
## βsw   -0.14375339 0.03193062 -0.19478469 -0.09272208
## sigma  0.11150168 0.01517264  0.08725288  0.13575049

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. 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)
df2 <- Wines2012
df2_list = list(s = standardize(df2$score),
                 wine = as.integer(df2$wine),
                 judge = as.integer(df2$judge))
m3 <- ulam(alist(
              s ~ dnorm(mu, sigma),
              mu <- z[judge] + x[wine] ,
              x[wine] ~ dnorm(0, 0.5),
              z[judge] ~ dnorm(0, 0.5),
              sigma ~ dexp(1)),
         data = df2_list, 
         chains = 4,
         cores = 4)
precis(m3, 2)
##               mean         sd        5.5%        94.5%    n_eff     Rhat4
## x[1]   0.117739449 0.25204453 -0.29998669  0.504509171 2130.178 1.0006916
## x[2]   0.087358202 0.25374372 -0.33082181  0.488801870 3094.723 0.9991933
## x[3]   0.228297956 0.25043349 -0.17249632  0.611138185 3033.018 0.9988904
## x[4]   0.466013001 0.27222556  0.02295754  0.896403973 2717.710 0.9987760
## x[5]  -0.102666075 0.26008312 -0.51713780  0.323338301 1972.001 1.0017106
## x[6]  -0.316170352 0.25576081 -0.72145326  0.087669684 2164.614 1.0004791
## x[7]   0.243489354 0.25736090 -0.18067937  0.647996857 2331.985 0.9993292
## x[8]   0.226589730 0.24669652 -0.16358835  0.630445315 2807.553 0.9990635
## x[9]   0.074575024 0.25856469 -0.33127792  0.474195322 2765.506 0.9995083
## x[10]  0.096416857 0.26167172 -0.31666086  0.513520855 2649.870 0.9989783
## x[11] -0.013293840 0.26053406 -0.43441959  0.404368744 2495.557 0.9986959
## x[12] -0.029846148 0.24671414 -0.43038972  0.367516153 2330.914 0.9994101
## x[13] -0.088167953 0.25806399 -0.50153397  0.329813156 1911.291 0.9992903
## x[14]  0.009818149 0.25919320 -0.40286240  0.421666540 2679.620 0.9988608
## x[15] -0.187542630 0.25327623 -0.58798746  0.211582949 2373.282 0.9993036
## x[16] -0.175530924 0.25770105 -0.58715281  0.256552862 2711.112 0.9994930
## x[17] -0.117722874 0.25462139 -0.51692484  0.288807992 2515.546 0.9989546
## x[18] -0.725685928 0.25691473 -1.13437960 -0.321043437 2590.559 0.9999091
## x[19] -0.137948244 0.26133182 -0.56266094  0.274315199 2819.789 1.0003328
## x[20]  0.330101599 0.25954575 -0.09075756  0.727776594 2576.632 0.9995325
## z[1]  -0.279100705 0.19221164 -0.58867780  0.027149451 1740.743 0.9996751
## z[2]   0.218627031 0.19588269 -0.09662057  0.522674036 1881.724 1.0000743
## z[3]   0.207193433 0.20304198 -0.12438133  0.530365152 1912.067 0.9997291
## z[4]  -0.536935130 0.18576726 -0.81940307 -0.239556741 1767.498 1.0008358
## z[5]   0.798548678 0.19858835  0.49065086  1.119936973 2099.411 1.0001929
## z[6]   0.482622729 0.19215970  0.18514616  0.785007056 1874.834 1.0005315
## z[7]   0.133580940 0.19034090 -0.16233412  0.431370516 1867.163 0.9990715
## z[8]  -0.653311160 0.20075037 -0.97663794 -0.327265954 2017.640 1.0015918
## z[9]  -0.343072825 0.20486516 -0.65110686 -0.009692329 1711.372 0.9994025
## sigma  0.847468340 0.04909989  0.77225824  0.929971617 2021.599 0.9999828
traceplot(m3)
## [1] 1000
## [1] 1
## [1] 1000
## Waiting to draw page 2 of 2

#It can be observed noticeable differences between various wines and judges from the traceplot. On an average, wine 4 seems to be with highest rating and wine 18 with least rating. And judge 5 seems to have highest average rating and judge 8 seems to have least average rating based on the mean values and traceplot.