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.room temperature
#2.location
#3.car modals

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,3,4

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

#1. μ_i=β_HH_i+β_DD_i+β_HD*H_iD_i
#2. μ_i=β_CC_i+β_QQ_i
#3. μ_i=β_TP*T_iP_i+β_TF*T_iF_i
#4. μ_i=β_SS_i+β_AA_i+β_SA*S_iA_i

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?

# Based on what we learned, one interaction could make the relationship between a predictor and an outcome to depend upon the value of another predictor. In thsi case, there are three predictor variables: water, shade, and temperature. Therefore, there is one 3-way interaction (water-shade-temperature) and three 2-way interactions (water-shade,water-temperature,shade-temperature).

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

# μi=α+βWWi+βSSi+βTTi+βWSTWiSiTi+βWSWiSi+βWTWiTi+βSTSiTi

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)
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.3579830 0.02392117  0.31975234  0.39621365
## βs    -0.1134616 0.02923037 -0.16017737 -0.06674583
## βw     0.2135644 0.02924667  0.16682253  0.26030620
## βsw   -0.1431598 0.03568313 -0.20018832 -0.08613126
## sigma  0.1248573 0.01694592  0.09777445  0.15194015

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.2331392 0.05525172  0.14483625  0.32144209
## βs    -0.1137724 0.02613847 -0.15554670 -0.07199804
## βw     0.2127683 0.02615024  0.17097515  0.25456141
## βb     0.0627494 0.02564258  0.02176760  0.10373120
## βsw   -0.1437523 0.03192953 -0.19478191 -0.09272278
## sigma  0.1114978 0.01517131  0.08725111  0.13574447

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)
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)
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/unsupported"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/src/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/include/"  -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp'  -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1   -I/usr/local/include   -fPIC  -Wall -g -O2  -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
## namespace Eigen {
##                ^
##                ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/4.0/Resources/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
## x[1]   0.104060218 0.2562796 -0.30615087  0.51267775 3069.002 0.9990024
## x[2]   0.085707584 0.2617330 -0.32944967  0.51413154 2628.011 0.9985061
## x[3]   0.222735965 0.2594351 -0.18126237  0.63397112 2922.327 0.9991729
## x[4]   0.461660946 0.2472401  0.05954454  0.84395935 3374.710 0.9986682
## x[5]  -0.106145825 0.2585961 -0.50503977  0.30827744 3480.001 0.9985890
## x[6]  -0.311627252 0.2648136 -0.74705903  0.10262336 2810.908 0.9990170
## x[7]   0.241095902 0.2574274 -0.17838719  0.63957230 2680.663 0.9985763
## x[8]   0.227550402 0.2632807 -0.19234696  0.65169043 2947.980 1.0000387
## x[9]   0.067913047 0.2663997 -0.37086313  0.48542685 2589.096 1.0012003
## x[10]  0.095432793 0.2661910 -0.32797560  0.51382107 2721.943 0.9989850
## x[11] -0.010931028 0.2530232 -0.40852577  0.38437719 2420.646 0.9993709
## x[12] -0.030949814 0.2617594 -0.44647648  0.38842590 2890.128 0.9988218
## x[13] -0.093374012 0.2565342 -0.49235513  0.31512117 3077.809 0.9987928
## x[14]  0.002528489 0.2579713 -0.42845482  0.40061016 2821.084 0.9998857
## x[15] -0.180811851 0.2497389 -0.57856363  0.21330982 2443.858 0.9993470
## x[16] -0.164675725 0.2557395 -0.56854668  0.23739533 2499.267 0.9991380
## x[17] -0.122033100 0.2613640 -0.54174832  0.29546396 2887.557 1.0000308
## x[18] -0.717595307 0.2751693 -1.15981511 -0.28506991 3127.514 0.9990337
## x[19] -0.138037090 0.2449546 -0.52793919  0.25428410 3103.174 0.9994385
## x[20]  0.320460791 0.2645235 -0.09588946  0.74018309 2723.106 0.9999012
## z[1]  -0.278705591 0.2049169 -0.60073706  0.04054033 1913.137 0.9988335
## z[2]   0.215841954 0.2003130 -0.09390972  0.53414840 2132.360 0.9990654
## z[3]   0.209660325 0.1883073 -0.08770904  0.51313903 2239.720 0.9997600
## z[4]  -0.541806613 0.1993161 -0.86858614 -0.22387710 2042.289 0.9989526
## z[5]   0.797150133 0.1960776  0.48142870  1.10796424 1913.652 0.9999408
## z[6]   0.475591968 0.1939517  0.17825518  0.78802774 2090.332 0.9996153
## z[7]   0.132128708 0.1991801 -0.17747942  0.44708345 2063.545 0.9987174
## z[8]  -0.654629966 0.1979897 -0.96160514 -0.33454035 2085.890 1.0003594
## z[9]  -0.342267973 0.2029508 -0.66600963 -0.01802023 2273.208 0.9982832
## sigma  0.847012096 0.0476090  0.77359678  0.92394095 2984.037 0.9991576
traceplot(m3)
## [1] 1000
## [1] 1
## [1] 1000