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 temperature

#2 school rank

#3 weight of car

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 heat x water content
# 2 no interaction 
# 3 no interaction 
# 4 sociality * manipulative appendages

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

#1.mu = aX + bY + cXY
#2.mu = aX +bY
#3.mu = aX + bY
#4.mu = aX + bY + cXY

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?

## from the description, it seems like temperature may have an intercation with the rest of the predictor

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

#          model: B = a +
#                 e water * shade  + 
#                 f water*tempreture + 
#                 g shade * tempreture +
#                 h water*shade* tempereture
#When e + f + g + h = 0, the equation should always be 0.

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

d$water_cent <- d$water - mean(d$water)
d$shade_cent <- d$shade - mean(d$shade)


m_tulip <- ulam(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- a + bw*water_cent - bs*shade_cent + bws*water_cent*shade_cent,
    a ~ dnorm(0.5, 1),
    bw ~ half_normal(0,1),
    bs ~ half_normal(0,1),
    bws ~ dnorm(0, 1),
    sigma ~ dexp(1)),
  data=d)
## Trying to compile a simple C file
## Running /usr/local/Cellar/r/4.0.2_1/lib/R/bin/R CMD SHLIB foo.c
## clang -I"/usr/local/Cellar/r/4.0.2_1/lib/R/include" -DNDEBUG   -I"/usr/local/lib/R/4.0/site-library/Rcpp/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/4.0/site-library/BH/include" -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppParallel/include/"  -I"/usr/local/lib/R/4.0/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:88:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:16: error: expected ';' after top level declarator
## namespace Eigen {
##                ^
##                ;
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
##          ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
## 
## SAMPLING FOR MODEL 'aba234a00140d91bc8804629f172faa4' NOW (CHAIN 1).
## Chain 1: 
## Chain 1: Gradient evaluation took 4.2e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.42 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1: 
## Chain 1: 
## Chain 1: Iteration:   1 / 1000 [  0%]  (Warmup)
## Chain 1: Iteration: 100 / 1000 [ 10%]  (Warmup)
## Chain 1: Iteration: 200 / 1000 [ 20%]  (Warmup)
## Chain 1: Iteration: 300 / 1000 [ 30%]  (Warmup)
## Chain 1: Iteration: 400 / 1000 [ 40%]  (Warmup)
## Chain 1: Iteration: 500 / 1000 [ 50%]  (Warmup)
## Chain 1: Iteration: 501 / 1000 [ 50%]  (Sampling)
## Chain 1: Iteration: 600 / 1000 [ 60%]  (Sampling)
## Chain 1: Iteration: 700 / 1000 [ 70%]  (Sampling)
## Chain 1: Iteration: 800 / 1000 [ 80%]  (Sampling)
## Chain 1: Iteration: 900 / 1000 [ 90%]  (Sampling)
## Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
## Chain 1: 
## Chain 1:  Elapsed Time: 0.089418 seconds (Warm-up)
## Chain 1:                0.050799 seconds (Sampling)
## Chain 1:                0.140217 seconds (Total)
## Chain 1:
precis(m_tulip)
##             mean        sd        5.5%     94.5%    n_eff     Rhat4
## a      1.0588867 0.9486111 -0.45106563  2.543771 405.3404 1.0057366
## bw     0.8840478 0.6310667  0.06564424  2.066389 334.9013 0.9980224
## bs     0.8433420 0.6619519  0.08460911  2.094890 466.0659 0.9993778
## bws   -0.1304659 0.9536003 -1.72952410  1.335556 406.9103 0.9979989
## sigma 79.6572676 5.0763138 71.91946185 88.148074 360.7291 0.9980132

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$bedb <- d$bed == "b"
d$bedc <- d$bed == "c"

m2 <- ulam(
  alist(
    blooms ~ dnorm(mu, sigma),
    mu <- a + bw*water_cent - bs*shade_cent + bws*water_cent*shade_cent + bbb *bedb + bbc * bedc ,
    a ~ dnorm(0.5, 1),
    bw ~ half_normal(0,1),
    bs ~ half_normal(0,1),
    bws ~ dnorm(0, 1),
    bbb ~ dnorm(0, 1),
    bbc ~ dnorm(0, 1),
    sigma ~ dexp(1)),
  data=d)
## Trying to compile a simple C file
## Running /usr/local/Cellar/r/4.0.2_1/lib/R/bin/R CMD SHLIB foo.c
## clang -I"/usr/local/Cellar/r/4.0.2_1/lib/R/include" -DNDEBUG   -I"/usr/local/lib/R/4.0/site-library/Rcpp/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/4.0/site-library/BH/include" -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppParallel/include/"  -I"/usr/local/lib/R/4.0/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:88:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:16: error: expected ';' after top level declarator
## namespace Eigen {
##                ^
##                ;
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
##          ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
## 
## SAMPLING FOR MODEL '547989e986d7fd5569c9afc4c240e5b5' NOW (CHAIN 1).
## Chain 1: 
## Chain 1: Gradient evaluation took 4.4e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.44 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1: 
## Chain 1: 
## Chain 1: Iteration:   1 / 1000 [  0%]  (Warmup)
## Chain 1: Iteration: 100 / 1000 [ 10%]  (Warmup)
## Chain 1: Iteration: 200 / 1000 [ 20%]  (Warmup)
## Chain 1: Iteration: 300 / 1000 [ 30%]  (Warmup)
## Chain 1: Iteration: 400 / 1000 [ 40%]  (Warmup)
## Chain 1: Iteration: 500 / 1000 [ 50%]  (Warmup)
## Chain 1: Iteration: 501 / 1000 [ 50%]  (Sampling)
## Chain 1: Iteration: 600 / 1000 [ 60%]  (Sampling)
## Chain 1: Iteration: 700 / 1000 [ 70%]  (Sampling)
## Chain 1: Iteration: 800 / 1000 [ 80%]  (Sampling)
## Chain 1: Iteration: 900 / 1000 [ 90%]  (Sampling)
## Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
## Chain 1: 
## Chain 1:  Elapsed Time: 0.128487 seconds (Warm-up)
## Chain 1:                0.075411 seconds (Sampling)
## Chain 1:                0.203898 seconds (Total)
## Chain 1:
precis(m2)
##             mean        sd        5.5%     94.5%     n_eff     Rhat4
## a      1.0899301 1.0577380 -0.54827332  2.749180  710.4804 0.9980118
## bw     0.8943973 0.6465748  0.07753824  2.111452  351.8366 1.0124741
## bs     0.8799166 0.6116734  0.09484212  2.026966  588.6738 0.9979986
## bws   -0.1095253 0.9419500 -1.58962941  1.356250  595.6737 0.9994414
## bbb    0.2349969 1.0131099 -1.40625338  1.890519  420.3432 0.9982855
## bbc    0.1540268 1.0150497 -1.35949188  1.649695  424.8987 1.0006564
## sigma 79.6236547 4.6630819 72.47759103 87.287516 1081.1532 0.9983056

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)
d = Wines2012

d2 = list(S = standardize(d$score),
          judge = as.integer(d$judge),
          wine = as.integer(d$wine))

m3 <- ulam(
    alist(
        S ~ dnorm(mu, sigma),
        mu <- a[judge] + b[wine],
        a[judge] ~ dnorm(0, 0.5),
        b[wine] ~ dnorm(0, 0.5),
        sigma ~ dexp(1)
        
    ),
    data = d2,
    chains = 4 ,
    cores = 4
)
## Trying to compile a simple C file
## Running /usr/local/Cellar/r/4.0.2_1/lib/R/bin/R CMD SHLIB foo.c
## clang -I"/usr/local/Cellar/r/4.0.2_1/lib/R/include" -DNDEBUG   -I"/usr/local/lib/R/4.0/site-library/Rcpp/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppEigen/include/unsupported"  -I"/usr/local/lib/R/4.0/site-library/BH/include" -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/src/"  -I"/usr/local/lib/R/4.0/site-library/StanHeaders/include/"  -I"/usr/local/lib/R/4.0/site-library/RcppParallel/include/"  -I"/usr/local/lib/R/4.0/site-library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:88:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/src/Core/util/Macros.h:613:16: error: expected ';' after top level declarator
## namespace Eigen {
##                ^
##                ;
## In file included from <built-in>:1:
## In file included from /usr/local/lib/R/4.0/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Dense:1:
## /usr/local/lib/R/4.0/site-library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
##          ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
precis(m3, 2)
##               mean         sd        5.5%       94.5%    n_eff     Rhat4
## a[1]  -0.285278022 0.19294361 -0.59456546  0.01861243 2858.487 1.0009197
## a[2]   0.210208771 0.18894729 -0.08550004  0.50348012 2305.201 0.9990215
## a[3]   0.201877669 0.19722539 -0.10760081  0.52238769 2596.750 1.0008166
## a[4]  -0.551216354 0.20012650 -0.87195809 -0.23513783 2305.156 0.9992733
## a[5]   0.787569269 0.19165998  0.48512113  1.08860219 2239.270 0.9987000
## a[6]   0.466931555 0.19290174  0.15464190  0.77569103 2215.530 0.9997653
## a[7]   0.122830568 0.18871917 -0.17940479  0.42170607 2325.111 0.9994278
## a[8]  -0.660616938 0.19025333 -0.96024191 -0.36403353 2160.257 0.9993020
## a[9]  -0.347750447 0.19665460 -0.67566340 -0.03271635 2633.127 1.0002055
## b[1]   0.128633716 0.26104426 -0.28944488  0.54759827 3313.564 0.9994239
## b[2]   0.092482371 0.25606981 -0.31337531  0.50374632 3577.381 0.9981908
## b[3]   0.230610084 0.26087247 -0.17937301  0.64860734 3192.558 0.9991149
## b[4]   0.472420831 0.26282654  0.04413659  0.89396168 2882.908 0.9986741
## b[5]  -0.104392260 0.25748271 -0.51150501  0.30379706 3348.675 0.9985956
## b[6]  -0.302440781 0.26856529 -0.74092416  0.11712068 2940.482 1.0001348
## b[7]   0.253430488 0.24753693 -0.13778187  0.65223260 3385.460 0.9992370
## b[8]   0.234864668 0.26050883 -0.18817219  0.64688578 3550.073 1.0005460
## b[9]   0.068536889 0.25199225 -0.32062477  0.48276740 3330.530 0.9992606
## b[10]  0.107960065 0.25031360 -0.28936665  0.49715080 3268.217 0.9988724
## b[11] -0.003774061 0.26296025 -0.43364312  0.41433830 3657.321 0.9984977
## b[12] -0.023002106 0.25799796 -0.43457162  0.37854722 2624.576 0.9991585
## b[13] -0.080657247 0.24818875 -0.47125405  0.32320795 3722.788 0.9985382
## b[14]  0.014696745 0.26016787 -0.39869056  0.41831125 3279.591 0.9991666
## b[15] -0.173566967 0.26085321 -0.59442923  0.23582334 3356.837 0.9992485
## b[16] -0.164886830 0.26529856 -0.58386958  0.25659282 3101.079 0.9983963
## b[17] -0.109421133 0.25944086 -0.52637265  0.30625095 2712.967 0.9995240
## b[18] -0.714081204 0.25793935 -1.11043088 -0.30510737 2958.746 0.9982627
## b[19] -0.130881722 0.24809759 -0.54792577  0.25553987 2964.017 0.9985946
## b[20]  0.326301990 0.26394478 -0.09578365  0.75329048 2764.653 0.9991122
## sigma  0.848228792 0.04735658  0.77792610  0.92705882 3526.705 1.0000506