```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```

## Introduction

Chapter 13 and 14 cover interactions (also known as moderation), including their use, interpretation, and limitations. The use in `R` is straightforward as we'll highlight below.

## Interactions (Moderation)

1. Let's start by loading the `tidyverse` package, the `furniture` package, and the `educ7610` package.
```{r, message = FALSE, warning = FALSE}
library(tidyverse)
library(furniture)
library(educ7610)
library(jtools)
library(interactions)
```
2. Import the `hosp` data set into R.
```{r}
data(hosp)
```
3. Because when a data set was imported from SPSS (as this one originally was), there are `labelled` variable types, we need to adjust these. In this data set, all variables can be re-classified as numeric so we can use the code below to do this.
```{r}
hosp <- hosp %>%
  map_df(~as.numeric(.x))
```
4. Interactions are often either theory-driven or data-driven. Of the two, theory-driven is often the most respected reason to test and report on an interaction. Herein, we'll explore some relationships with visualizations that we'll then test (since we don't have any good theories about this data set).
```{r}
hosp %>%
  ggplot(aes(exhaust, safety, group = sex, color = sex)) +
    geom_point(alpha = .6) +
    geom_smooth(method = "lm")
```

5. There looks like there could be an interaction between `exhaust` and `sex` on `safety`. To test this, let's run a linear regression model with the cross product of `safety` and `sex`. R is smart enough that if we put `exhaust * sex` in the formula, it will include all the right effects for us.
```{r}
hosp %>%
  lm(safety ~ exhaust * sex, data = .) %>%
  summary()
```

### Interpretation

6. If `exhaust` is how exhausted the individual feels, `safety` is the amount the individual works around safety protocols (avoids performing the full safety protocol) and `sex` is the biological sex of the individual (0 = female, 1 = male), what is the interpretation of each coefficient?
7. Does this interpretation match the figure we created before? Why or why not?
8. Let's include some covariates in the model; namely, `tenure` (the job experience the individual has) and `age` (the individual's age in years).
```{r}
hosp %>%
  lm(safety ~ exhaust * sex + tenure + age, data = .) %>%
  summary()
```
9. First, is the interaction still significant? Did it decrease or increase with the covariates added?
10. Next, what does the coefficient on `exhaust` mean here? What about `sex`?
11. Finally, interpret both `tenure` and `age`.

### Conditional Effects

12. Given that the coefficient in the previous model on `sex` was interpretted when `exhaust = 0` (something not actually seen in the data), let's look at the difference between males and females when exhaust is at its mean value.
```{r}
hosp %>%
  mutate(exhaust = exhaust - mean(exhaust, na.rm=TRUE)) %>%
  lm(safety ~ exhaust * sex + tenure + age, data = .) %>%
  summary()
```
13. What does the coefficient on `sex` mean now? Compare it to the previously run model. What is different?

### Probing Interactions

14. Most aspects of probing an interaction are not necessary the need for assessing the interaction in more detail. We will use the Johnson-Neyman Technique for this as it applies in several situations. Here, we are using the book's example of `exhaust * tenure`. A new function `jtools::summ()` gives some nice summary information of the results.
```{r}
interaction_model <- lm(safety ~ exhaust * tenure + age + sex, data = hosp)
jtools::summ(interaction_model)
```

15. We can then look at the difference in the slope of `exhaust` based on different levels of `tenure` using `interactions::sim_slopes()` and `interactions::interact_plot()`. Both will give us similar, useful information. (See [https://interactions.jacob-long.com/index.html](https://interactions.jacob-long.com/index.html) for lots of examples on how to use the `interactions` package.)
```{r}
interactions::sim_slopes(interaction_model, pred = exhaust, modx = tenure)
interactions::interact_plot(interaction_model, 
                            pred = exhaust, 
                            modx = tenure, 
                            plot.points = TRUE, 
                            interval = TRUE)
```

16. For what values of `tenure` is the effect of `exhaust` significantly different than zero?
17. When would knowing when the effect of exhaust is different from zero be important?



## Conclusion

Interactions are an essential element of understanding real-world phenomena. There are many ways to organize, understand, and communicate their meaning. This walk-through helped demonstrate some of the basic ways to understand these interesting relationships.

