## 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 would be the third variable with which yeast interacts so that the bread dough rises
# 2) Field of study, level of education (graduate, bachelors) can help in predicting whether higher income.
# 3) weather (winter/ summer) can help in predicting whether car will go or not on gasoline. 

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 and 4 explanations invokes an interaction and 2 explanation doesn't invoke an interaction. 

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

# 1) yi = xHHi + xDDi + xHDHiDi
# 2) yi = xCylinderi + xfueli
# 3) yi = BTPTiPi + BTFTiFi
# 4) ui = BSSi + BAAi +BSASiAi 

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?

# Interaction will be :

#  WTS, WS, TS, WT

# (S - Shade, W - Water, T - Temperature)

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

# Regression equation

# ui = BWWi + BSSi + BWSWiSi - BWWiTi - BSSiTi - aTi - BWSWiSiTi

# When Ti = 0, ui = a + BWWi + BSSi - aTi - BWSWiSiTi + BWSWiSi - BWWiTi - BSSiTi

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 'rstan' was built under R version 4.0.5
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$bloomsstd <- d$blooms / max(d$blooms) d$watercent <- d$water - mean(d$water)

d$shadecent <- d$shade - mean(d$shade) bwd <- abs(rnorm(nrow(d), 0,0.25)) bsd <- (-abs(rnorm(nrow(d),0,0.25))) mtulip <- quap( alist( bloomsstd ~ dnorm(mu, sigma), mu <- a + bw*watercent + bs*shadecent + bws*watercent*shadecent, a ~ dnorm(0.5, 0.25), bw ~ dnorm(bwd), bs ~ dnorm(bsd), bws ~ dnorm(0,0.25), sigma ~ dexp(1)), data=d) precis(mtulip) ## mean sd 5.5% 94.5% ## a 0.3579825 0.02391632 0.31975956 0.39620536 ## bw 0.2108732 0.02908714 0.16438629 0.25736004 ## bs -0.1169196 0.02908947 -0.16341014 -0.07042896 ## bws -0.1431617 0.03567596 -0.20017881 -0.08614466 ## sigma 0.1248318 0.01693500 0.09776635 0.15189716 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$watercent <- d$water - mean(d$water)
d$shadecent <- d$shade - mean(d$shade) d$bloomsstd <- d$blooms / max(d$blooms)

d$bedIndex <- coerce_index(d$bed)

bedVar <- quap(
alist(
bloomsstd ~ dnorm (mu, sigma),
a[bedIndex] ~ dnorm(0.5,0.25),
bw ~ dnorm(0,0.25),
bws ~ dnorm(0, 0.25),
bs ~ dnorm(0,0.25),
sigma ~ dunif(0,100)
),
data = d
)

precis(bedVar, depth=2)
##             mean         sd        5.5%       94.5%
## a[1]   0.2732858 0.03578549  0.21609366  0.33047790
## a[2]   0.3964085 0.03576781  0.33924459  0.45357232
## a[3]   0.4091197 0.03576673  0.35195757  0.46628187
## bw     0.2074283 0.02542544  0.16679350  0.24806304
## bws   -0.1438819 0.03105721 -0.19351729 -0.09424645
## bs    -0.1138447 0.02542076 -0.15447195 -0.07321739
## sigma  0.1084044 0.01476829  0.08480177  0.13200693

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?

library(rethinking)
data(Wines2012)
d1 = Wines2012
d1list = data.frame(list(s = standardize(d1$score), wine = as.integer(d1$wine),
judge = as.integer(d1\$judge)))
m <- 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 = d1list,
chains = 4,
cores = 4)

precis(m, 2)
##                mean         sd        5.5%       94.5%    n_eff     Rhat4
## w[1]   0.1182260717 0.26936142 -0.31124443  0.55947916 3604.301 0.9986372
## w[2]   0.0865514357 0.25679917 -0.31746675  0.49964925 3154.003 0.9987394
## w[3]   0.2307124467 0.25609558 -0.17618154  0.63282132 4042.197 0.9984111
## w[4]   0.4630700788 0.25382850  0.06997345  0.85792820 2637.223 0.9988732
## w[5]  -0.1028747852 0.25498909 -0.52306610  0.30719692 3297.396 0.9992149
## w[6]  -0.3099786773 0.25787754 -0.71612746  0.09737103 3464.025 0.9990747
## w[7]   0.2435188461 0.25684950 -0.18106979  0.65160649 2865.394 0.9988988
## w[8]   0.2284937453 0.25790843 -0.18547505  0.64781134 3138.040 0.9992207
## w[9]   0.0736743967 0.26050923 -0.34203414  0.49468230 3373.160 0.9985946
## w[10]  0.0996298860 0.25605139 -0.31470900  0.50401058 2767.583 0.9988956
## w[11] -0.0048744369 0.25604960 -0.40491998  0.40261208 2772.895 0.9987261
## w[12] -0.0246480092 0.25866911 -0.42792989  0.38112914 2758.065 0.9982539
## w[13] -0.0860429899 0.25054905 -0.49480461  0.31807976 3151.327 0.9985475
## w[14] -0.0009799571 0.24566708 -0.37189127  0.39596479 3083.212 0.9986193
## w[15] -0.1834868894 0.26123378 -0.59432526  0.22393425 3081.826 0.9991966
## w[16] -0.1604557102 0.25623288 -0.56731363  0.25365735 3125.749 0.9997999
## w[17] -0.1192364765 0.26069482 -0.54200503  0.28276165 2768.716 0.9993905
## w[18] -0.7272473865 0.24566876 -1.11584606 -0.33400945 2713.432 1.0001664
## w[19] -0.1392173982 0.25368822 -0.53831259  0.27357131 2955.858 0.9998448
## w[20]  0.3293840071 0.25188483 -0.07069148  0.73860864 3140.903 0.9986050
## j[1]  -0.2848626293 0.19285541 -0.58824541  0.02907001 2475.959 0.9989360
## j[2]   0.2094064870 0.19874173 -0.12182322  0.52836218 2330.748 0.9992578
## j[3]   0.2038161796 0.18888461 -0.09726954  0.50772767 2419.951 0.9993587
## j[4]  -0.5403830430 0.18416827 -0.83362551 -0.25004258 2232.179 0.9985371
## j[5]   0.7926252596 0.19147194  0.48426613  1.09436957 2354.250 0.9990873
## j[6]   0.4774325210 0.19915360  0.15821862  0.79630533 2064.203 0.9987244
## j[7]   0.1320183864 0.19434311 -0.18181982  0.43590354 2444.758 0.9991549
## j[8]  -0.6573514431 0.19024770 -0.95908921 -0.35324845 2297.514 0.9991783
## j[9]  -0.3495003187 0.19495830 -0.65655173 -0.03968087 2472.817 0.9996008
## sigma  0.8473477487 0.04842978  0.77499274  0.92853695 3166.685 0.9985825
traceplot(m)
## [1] 1000
## [1] 1
## [1] 1000