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

## Introduction

Chapter 8 talks about how we can assess the importance of an individual or a set of predictors. The principles discussed herein help us be able to test whether two predictors in the same regression model have the same effect on the outcome.

## Relative Importance of Predictors (Categorical)

1. Let's start by loading the `tidyverse`, the `furniture` package, and `educ7610` packages.
```{r}
library(tidyverse)
library(furniture)
library(educ7610)
```
2. Import the `risk` data set into R.
```{r}
data(risk)
```
3. We want to know, first, if having been in foster care (`dfoster == 1`) or if having a lack of water access (`water == 0`) in the home are predictive of how happy they rate a negative image (`neg3sad`).
```{r}
risk %>%
  lm(neg3happy ~ dfoster + water,
     data = .) %>%
  summary()
```
4. Because of how the dummy coding was done, these are going in the opposite direction but that doesn't mean that they affect the outcome in opposite ways. Rather, it is just an artifact of the way we dummy coded it. In order to test if one of these has a bigger effect than the other, we will use `car`'s `linearHypothesis()`. Instead of looking at if they are equal:
$$
dfoster - water = 0
$$
we will do:
$$
dfoster - -water = 0
$$
or, in other words,
$$
dfoster + water = 0
$$
```{r}
risk %>%
    lm(neg3happy ~ dfoster + water,
     data = .) %>%
  car::linearHypothesis("dfosterFoster + waterYes = 0")
```
5. Is the effect of `dfoster` significantly more or less than `water`?

## Relative Importance of Predictors (Continuous)
6. These types of comparisons are a bit easier with categorical predictors. When it comes to continuous predictors, we need to make sure they are in comparable units. Often, the best way to do this is to standardize the predictors first. To compare these, let's use the `poverty.xlsx` to look at this (can be downloaded from Canvas or from this site).
```{r, eval = FALSE}
poverty <- readxl::read_excel("poverty.xlsx")
```
```{r, echo = FALSE}
poverty <- readxl::read_excel(here::here("Poverty_Data/poverty.xlsx"))
```
7. We want to look at if poverty (`poverty_pct`) or violent crime (`ViolentCrime`) predict teen birthrate (`TeenBirth`). Let's take a look at a multiple regression model.
```{r}
poverty %>%
  lm(TeenBirth ~ poverty_pct + ViolentCrime,
     data = .) %>%
  summary()
```
8. It looks like poverty has a larger effect on teen birth rate than violent crime. Is the effect of poverty significantly different than the effect of violent crime?
```{r}
poverty %>%
  lm(TeenBirth ~ poverty_pct + ViolentCrime,
     data = .) %>%
  car::linearHypothesis("poverty_pct = ViolentCrime")
```
9. Is this sufficient to say they are different? Why or why not?
10. Let's standardize the two predictors and see if this changes anything.
```{r}
poverty %>%
  mutate(poverty_std = scale(poverty_pct),
         violent_std = scale(ViolentCrime)) %>%
  lm(TeenBirth ~ poverty_std + violent_std,
     data = .) %>%
  summary()

poverty %>%
  mutate(poverty_std = scale(poverty_pct),
         violent_std = scale(ViolentCrime)) %>%
  lm(TeenBirth ~ poverty_std + violent_std,
     data = .) %>%
  car::linearHypothesis("poverty_std = violent_std")
```
11. Now that the variables are in the same overall units (standard deviation units), are the effects significantly different?
12. Which one, the unstandardized or the standardized, would be a more trustworthy comparison?

## Dominance Analysis
13. We can also do dominance analysis using a (still in development) package called `dominanceAnalysis`. Let's install and load the package.
```{r}
#devtools::install_github("clbustos/dominanceAnalysis")
library(dominanceanalysis)
```
14. We can see which of the two adversity variables (`totadversity` and `commadversity`) is more dominant.
```{r}
dominanceAnalysis(lm(TeenBirth ~ poverty_pct + ViolentCrime,
                     data = poverty))
```


## Conclusion

Comparing predictors in the same regression model is often of substantive interest. Knowing how, and when, to use this can be a great benefit to your research.
