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

## Introduction

**In Lessons 14 and 15 we learned more about linear regression in the form of GLMs. The following examples help illustrate how these approaches can be used in research using `R`.**

**1. Let's start by loading the `tidyverse` package, the `caret` package, the `furniture` package, and the `educ7610` package.**

```{r, message=FALSE}

```

**2. Import the `Titanic` dataset into R, filter to include only complete cases.**

```{r}

```

## Generalized Linear Models

### Logistic Regression

**3.Does the passenger's `Fare`  and biological `Sex` predict whether or not they `Survived` the sinking of the Titanic? (`Survived`; dichotomous where 1 = survived, 0 = did not survive). Let's use a logistic regression to understand the relationship. (Hint: Don't forget to check the class of your variables).**

```{r}

```

**4. What do the coefficients from this model mean here? Is this in an intuitive metric?**

Select the correct interpretation (a-d) of the coefficient for `Fare` in Kahoot, click [here]()

a) For each 1 unit increase in ticket fare price, there is an associated 1.216% percentage point increase of having survived the titanic, controlling for sex.

b) For each $1 increase in ticket fare price, there is an associated 0.01216 increase in the log-odds of having survived the titanic, controlling for sex.

c) For each $1 increase in ticket fare price, the odds of having survived the titanic increases by a factor of 0.01216, controlling for sex.

d) On average, for each $1 increase in ticket fare price, the chance of having survived the titanic increases by 0.01216 percentage points, controlling for sex.


**5. We can get more interpretable effects using Odds Ratios and Average Marginal Effects (AME). First, we'll get the odds ratios using the `educ7610::oddsratios()` function.**

```{r}

```


**6. Interpret the odds ratios and the confidence intervals of these odds ratios in context of the research question.**

Type your answer below. Discuss with a partner, did you come to the same conclusions?


**7. Now we'll get the AMEs. We'll use the `margins` package again.**

```{r}

```

**8. Interpret these AMEs in context of the research question.**

Type your answer below. Discuss with a partner, did you come to the same conclusions?

**9. Let's now examine how well the model fits the data by examining its predictive accuracy. We'll begin by generating a confusion matrix of predicted vs actual observations.**

```{r}

```

How can we easily calculate the classification accuracy from the confusion matrix? How about the missclassification rate?

Answer in Kahoot then calculate in R.

```{r}

```


**10. Now let's examine the model's sensitivity and specificity**

```{r}

```

**11. Which is higher the sensitivity or specificity? In this case, could you argue that one is more important than the other? Can you think of an example where the reverse might be the case?**

Type answers below/discuss with a partner.

Answer one question in Kahoot.

### Cross-Validation

**12 While it seems like we had pretty high predictive accuracy, remember that we're cheating in a sense, by predicting onto the same data we trained the model on. To prevent this *over-fitting* of the model, let's employ 10-fold cross-validation. Don't forget to set your seed (for reproducability purposes)!**

```{r}


```

**13. Let's re-examine the confusion matrix, has it changed?**

```{r}

```

**14. Now let's re-examine the classification accuracy, missclassification rate, sensitivity and specificity.**

```{r}


```

How have the results changed? Why would we consider these results to be more representative of the model's true predictive accuracy?

### Poisson Regression

**15. To see how Poisson regression works, we'll use `tvhours` from the `gss` data set. Does education (`educ`) predict number of hours spent watching TV per day when controlling for `age` and `sex`?**

```{r}

```

**14. What does the coefficient mean here on `educ`?**

Select the correct interpretation in Kahoot:

a) For a one one-unit increase in education, there is an associated 0.0458 decrease in the log-odds of watching the mean number of TV hours a day, controlling for sex and age.

b) For each one-unit increase in education, there is a 0.0458 percentage point decrease in hours of TV watched per day, controlling for sex and age.

c) For each one-unit increase in education, there is an associated 0.0458 unit decrease in hours of TV watched per day, controlling for sex and age.  

d) For a one-unit increase in education, there is an associated ~4.58% decrease in hours of TV watched per day, controlling for sex and age.


**15. We can also get risk ratios for Poisson regression with `educ7610::riskratios()`. *Note that the risk ratio and the odds ratios functions do the exact same thing: grab the coefficients and confidence intervals and exponentiate them.* **

```{r}

```

**16. Interpret the risk ratios in context of the research question.**

Type the answer below, discuss with a partner.

**17. Let's also get the AMEs.**

```{r}

```

**18. Interpret these in context of the research question.**

Type the answer below, discuss with a partner.

## Lesson 16

### Mediation Analysis

Lesson 16 discussed mediation analysis, a way of understanding the process of an effect from one variable to another. Mediation is built on linear regression as we've discussed but combines paths to create a single conceptual model.

**1. Let's start by loading the `furniture` package, the `lavaan` and the `MarginalMediation` package.**

```{r}

```

**2. Import the `FacialBurns` data set found in `lavaan` into R.**

```{r}

```


**3. We hypothesize that anxiety (`HADS`) mediates the effect of `TBSA` on `Selfesteem`. Let's test this out first with `lavaan`. We will create the model object with two regression models and then we define the indirect effect and the total effect from the coefficients of the two regressions.**

```{r}

```

**4. The output gives us the a, b, and c paths (as we defined them) along with the defined parameters `ind` and `tot`. Is the indirect path (from TBSA -> HADS -> Self-esteem) significant? What does it mean here?**

Answer in Kahoot:

**5. Let's get the standardized indirect and total effects.**

```{r}

```

**6. Interpret the standardized indirect effect. How much of the total effect is explained by the process through anxiety?**

Type answer below and discuss with a partner.


