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

## Lesson 10

### Calculating \& Interpretting Effect Size

1. Let's start by loading the following packages:
```{r}

```

2. Import the `mafs` dataset into R.
```{r}

```

3. Let's run a simple regression predicting relationship `Satisfaction` from the `Love_Expert` that couple's worked with on the show
```{r}

```

4. So we know there is a significant difference in relationship satisfaction dependent upon which love expert couples worked with, but what is the magnitude of this difference? Remember that we can calculate *Cohen's d* for a dichotomous predictor variable by standardizing the outcome variable. Let's try it:

```{r}

```

What is the cohen's d value?

Answer in [here](https://kahoot.it/) in Kahoot

5. Now let's calculate *Cohen's d* using the `cohen.d` package from `psych`.
```{r}

```

Are the values approximately the same?

6. Let's say we now want to add in a covariate, but still want to calculate the effect size of`Love_Expert`. Now we will need to calculate the *Adjusted Cohen's d*. Add `Age_Differential` into the model.
```{r}

```

Is the relationship between satisfaction and love expert still significant?

Answer in Kahoot:

7. Now let's calculate the *Adjusted Cohen's d* for `Love_Expert`:
```{r}

```
What is the *Adjusted Cohen's d* value for `Love_Expert`?

Answer in Kahoot:

8. Next let's try examining effect size through standardizing our coefficients. Run a multiple regression predicting `Satisfaction` from `Age_Differential` controlling for `Combined_Income`.

```{r}

```

How does standardizing the coefficients change their interpretation? 

Answer in Kahoot: 

Type your interpretation for each coefficient and share with a partner. Did you come up with the same thing?

9. Another readily available effect size metric in R is the `R^2` and Adjusted `R^2`. Adjusted `R^2` adjusts the `R^2` value for the number of variables in the model, so that the value does not become inflated simply by adding in more predictors. Generally you will report Adjusted `R^2`. Let's rerun the model without standardization:

```{r}

```

Do the `R^2` and Adjusted `R^2` values differ in this model as compared to the standardized model?

Answer in Kahoot:

How is the Adjusted `R^2` value interpreted?

Select one and answer in Kahoot:

a) Couples' age differential explains 76.19% of the variance in relationship satisfaction.
b) The model explains 0.769% of the variance in relationship satisfaction
c) The model explains 76.19% of the variance in relationship satisfaction
d) For each one unit increase in age_differential there is an associated 0.7619 percentage point increase in relationship satisfaction


## Lesson 11

Lesson 11 covers 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`, `furniture`, `jtools`, `interactions` and the `educ7610` package.
```{r, message = FALSE, warning = FALSE}

```

2. Import the `mafs` data set into R.
```{r}

```

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}

```

Does there appear to be an interaction? Answer [here](https://kahoot.it/) in Kahoot.

### Continuous x Categorical Interaction

5. There looks like there could be an interaction between `Combined_Income` and `Love_Expert` on `Relation_Length`. To test this, let's run a linear regression model with the cross product of `Combined_Income` and `Love_Expert`. 
```{r}

```

### Interpretation

6. If `Combined_Income` is couples' combined annual income (in thousands), `Relation_Length` is the duration of their relationship (in months) and `Love_Expert` is the love expert the couples worked with while on the show *Married at First Sight*, what is the interpretation of each coefficient?

Type the interpretation for each coefficient below, and then share with a partner. Did you come up with the same conclusions?


7. Does this interpretation match the figure we created before? Why or why not?


### Continuous x Continuous Interaction

8. Maybe we can find a second interaction in our dataset, this time between two continuous variables. We can probe a continuous x continuous interaction by looking at the difference in the slope of `Combined_Income` based on different levels of `Age_Differntial` using `interactions::interact_plot()`. (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}

```

10. Is the interaction significant?
```{r}

```

11. If `Combined_Income` is couples' combined annual income (in thousands), `Relation_Length` is the duration of their relationship (in months) and `Age_Differential` is the number of years in couples' age gap, what is the interpretation of each coefficient?

Type the interpretation of each coefficient below, then discuss with a partner. Did you get the same answer?

### Categorical x Categorical Interaction

12. Let's check out one more interaction, we think maybe there could be an interaction between couple's `Location` and the `Love_Expert` they worked with on the show, on their `Relationship_Length`. This is just a random hunch, so let's plot it and see if there is any evidence of an interaction using the `cat_plot` function.

```{r}

```

13. There doesn't seem to be an interaction here, but just to be sure, let's run the model.

```{r}

```

14. Are either of the interactions statistically significant?

Answer in Kahoot:

### Covariates

15. Let's go back to our first (cateogircal x continuous) model and include some covariates in the model; namely, `Age_Differential` and `Satisfaction` (couples' rating of relationship satisfaction).
```{r}

```

16. First, is the interaction still significant? Did the interaction coefficient decrease or increase with the covariates added?

Answer both in Kahoot:

17. Next, what does the coefficient on `Combined_Income` mean here? What about `Love_Expert`? Type the answer below and then share with a partner:


18. Finally, interpret both `Age_Differential` and `Satisfaction`. Type the answer below and share with a partner:

### Conditional Effects

19. Given that the coefficient in the previous model on `Love_Expert` was interpreted when `Combined_Income = 0` (something not actually seen in the data), let's look at the difference between Dr. Coles and Pepper when Combined_Income is at its mean value.
```{r}

```

20. What does the coefficient on `Love_Expert` mean now? Compare it to the previously run model. What is different? Type the answer below and share with a partner:


### Probing Interactions

21. 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 use a new example of `Combined_Income * Age_Differential`. A new function `jtools::summ()` gives some nice summary information of the results.
```{r}

```
Is the model fit significant? How about the interaction effect?

What percentage of the variance in `Relation_Length` is explained by the model?

Answer in Kahoot:


## 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.





# Lesson 12

Lesson 12 introduces the idea of modeling nonlinear relationships using linear regression. This is one of the many ways regression is flexible to many important situations. A nice, but incomplete, list of useful functions can be found [here](https://cran.r-project.org/doc/contrib/Ricci-refcard-regression.pdf).

## Nonlinear Relationships

1. Let's start by loading the `tidyverse`, `furniture`, `ggplot2`, `dplyr` and the `educ7610` package.
```{r, message=FALSE}

```
2. Import the `poli` data set into R.
```{r}

```

### Descriptives and Visualizations 

3. Let's get some descriptive statistics of the variables of interest: political knowledge (`pknow`), age (`age`), news use (`npnews`), and SES (`ses`).
```{r}

```

4. Let's plot the relationship between political knowledge (`pknow`) and age (`age`).
```{r}

```


### Polynomial Regression

5. Let's model the relationship that we see in question 5. That is, `pknow` regressed on `age` where we also include the quadratic effect of `age`. To add the quadratic effect directly in the model we can use `I(age^2)`. 
```{r}

```

6. Is there evidence that there is a quadratic effect of age on political knowledge?

Answer in Kahoot [here](https://kahoot.it/)

7. We could similarly create a new variable and put that in the model instead as below.
```{r}

```

8. With these results, what is the peak age for political knowledge? How do we know that this is a peak (max) and not a min?

Answer in Kahoot:

9. We haven't mean-centered age, yet. Let's do that and re-run the analysis.
```{r}

```
10. What changed from the mean-centered model as compared to the previous one?

Answer in Kahoot

11. Finally, let's control for some covariates (SES and news use).
```{r}

```

12. A) Is age or age$^2$ significant any more? B) Why would it change so much?

A) Answer in Kahoot

B) Discuss with a partner, do you agree on the reason?

13. Let's look at the relationship of age with political knowledge after controlling for SES and news use.
```{r}

```

15. Does this plot look like what the model is telling us? 

16. Let's remove the quadratic effect of age since it doesn't look important once we are controlling for SES and news use.
```{r}

```

17. Using this final model, what is the interpretation of this model? Include the interpretation of the covariates as well. (Note: the relationship between `pknow` and `npnews` is likely non-linear as well but we won't look into that here; see spline regression below.)

Type your interpretation, then share with a partner. Did you come up with the same interpretation?

### Spline Regression

18. Based on the plot below, there may be two joints: one at `npnews == 1.5` and `npnews == 6`. Let's try these two and see what we find.
```{r}

```

19. We can define our joints manually for a spline regression. Let's do that below.
```{r}

```

20. What do these results tell us about the relationship between `npnews` and `pknow`? Is spline regression necessary here?

Answer both in Kahoot

21. We can further look at the predictions of the model and see how well they fit the observations. First, we'll save a model object and use that to get the predictions.
```{r}

```

22. Does the shape of the line match the results of the model?


### Monotonic Transformations
#### Log

23. Now, let's use a log transformation using `log()`. Since there are some zeros, the natural log isn't defined. To fix this, we add 1 to `pknow` before taking the log. This model would be considered a "log-lin" model.
```{r}

```

24. What is the interpretation of the coefficient on `npnews`?

Choose one of the following in Kahoot:

a) for each one unit increase in npnews, there is an associated 4.547% increase in pknow
b) for each one unit increase in npnews, there is an associated 0.04547 log-unit increase in pknow
c) for each one unit increase in npnews, the average increase in pknow is 2.26647 units
d) for each 1% increase in npnews, there is anssociated 0.04547 unit increase in pknow

25. Let's also do a log transformation of `npnews` in the previous model. It has the same issue as `pknow` so we can just add 1 to the values. This model would be considered a "log-log" model.
```{r}

```

26. Let's see how well this fits our data. We can do that as we've done before by getting the model predictions and plotting them with the observed values. Let's do the log-lin model.
```{r}

```

27. Let's try one more common transformation type, the square root transformation. This can be helpful when you have 0 values, but can't be used if you have negative values (since you can't take the square root of a negative).

```{r}

```

Is the square root transformed coefficient for npnews statistically significant? Does the `R^2` or Adjusted `R^2` increase compared to the log-log and log-lin models?

## Conclusion

Ironically, linear regression can model non-linear relationships very naturally. Each approach covered here are useful for specific situations and each has a decent interpretation.



