---
title: "Homework 4.2"
author: "Your Name Here"
institute: "Old Dominion University"
format: 
  html:
    theme: lux # Check here for more themes: https://quarto.org/docs/output-formats/html-themes.html
    code-tools: true
    code-fold: true
    code-summary: "Code"
    code-copy: hover
    link-external-newwindow: true
    tbl-cap-location: top
    fig-cap-location: bottom

self-contained: true
editor: source
---

```{r setup, include=FALSE}
# DO NOT EDIT THIS

knitr::opts_chunk$set(fig.align = 'center')
knitr::opts_chunk$set(out.width = '90%')
knitr::opts_chunk$set(results = 'hold')
knitr::opts_chunk$set(fig.show = 'hold')
knitr::opts_chunk$set(error = TRUE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
par(mar = c(4.1, 4.1, 1.1, 4.1))

hooks = knitr::knit_hooks$get()
hook_foldable = function(type) {
  force(type)
  function(x, options) {
    res = hooks[[type]](x, options)
    
    if (is.na(as.logical(options[[paste0("fold.", type)]]))) {
      
      res
    } else if (isFALSE(as.logical(options[[paste0("fold.", type)]]))){
      
      # return(res)
      
      paste0(
      "<details open><summary>", gsub("^p", "P", gsub("^o", "O", type)), "</summary>\n\n",
      res,
      "\n\n</details>"
      )
    } else {
      
      paste0(
      "<details><summary>", gsub("^p", "P", gsub("^o", "O", type)), "</summary>\n\n",
      res,
      "\n\n</details>"
      )
    }
  }
}

knitr::knit_hooks$set(
  output = hook_foldable("output"),
  plot = hook_foldable("plot")
)
knitr::opts_chunk$set(fold.output=TRUE)
knitr::opts_chunk$set(fold.plot=TRUE)

Q <- 0
```

## Question `r Q <- Q+1; Q`

```{r include=FALSE}
q <- 0
```

In the early 2000s, two economists ran an experiment where they sent fictitious resumes in response to job ads in Chicago and Boston. The authors randomly varied the qualities of the fictitious resumes as well as the applicants' names.  Some resumes were randomly given stereotypically white-sounding names (Emily, Brendan) and others African-American-sounding names (Lakisha, Jamal).^[The process of determining which names are stereotypically black/white is described in detail in [the published draft](https://www.aeaweb.org/articles?id=10.1257/0002828042002561).] Intrigued students may read [a non-technical summary of the paper here.](https://www.ala.org/ala/hrdr/placementservice/hrpractice2.pdf)^[In addition, [similar research on ban-the-box](https://insights.grcglobalgroup.com/ban-the-box-an-effective-component-of-reintegration-efforts/) finds that these policies *increase* racial discrimination.]

In this part of the homework, you are going to investigate whether employers engage in racial discrimination when sifting through resumes using data collected by these researchers ([data](https://vincentarelbundock.github.io/Rdatasets/csv/AER/ResumeNames.csv); [documentation](https://vincentarelbundock.github.io/Rdatasets/doc/AER/ResumeNames.html)).

`r q <- q+1; letters[q]`. Read the data into a data frame called `resume`. (**2 Points**)

```{r}
# Your code here...
```


`r q <- q+1; letters[q]`. Create the following binary variables (**4 Points**):
    - A variable called `chicago` that is equal to one if `city` is equal to "chicago" and zero otherwise.
    - A variable called `female` that is equal to one if `gender` is equal to "female" and zero otherwise.
    - A variable called `black` that is equal to one if `ethnicity` is equal to "afam" and zero otherwise.
    - A variable called `callback` equal to one if `call` is equal to "yes" and zero otherwise.

```{r}
# Your code here...
```


`r q <- q+1; letters[q]`. Estimate and display the coefficients (using `summary()` is fine) of the following regression (**2 Points**):

$$\begin{aligned}\text{Callback}_i = \ &\beta_0 + \beta_1 \text{Jobs}_i + \beta_2 \text{Experience}_i + \beta_3 \text{Female}_i \\&+ \beta_4 \text{Chicago}_i + \beta_5 \text{Black}_i+ \epsilon_i\end{aligned}$$

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Interpret each coefficient in words. (**4 Points**)

*Your answer here...*



`r q <- q+1; letters[q]`. Estimate and display the coefficients (using `summary()` is fine) of the following regression (**2 Points**):

$$\begin{aligned}\text{Callback}_i = \ &\beta_0 + \beta_1 \text{Jobs}_i + \beta_2 \text{Experience}_i + \beta_3 \text{Female}_i \\&+ \beta_4 \text{Chicago}_i + \beta_5 \text{Black}_i + \beta_6 (\text{Chicago}_i \times \text{Black}_i)+ \epsilon_i\end{aligned}$$

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Interpret the estimates for $\beta_4$, $\beta_5$, and $\beta_6$ from the previous regression. (**4 Points**)

*Your answer here...*

`r q <- q+1; letters[q]`. Re-estimate the regression once using data only from Chicago, and another using data only from Boston.  Report the coefficients using `modelsummary`.  Discuss any coefficients that result in different conclusions for the two cities. (**4 Points**)


```{r, warning=F, message = F}
# Your code here...
```

*Your answer here...*













## Question `r Q <- Q+1; Q`

```{r include=FALSE}
q <- 0
```

For this question, you will explore a sample of crash records ([data](https://vincentarelbundock.github.io/Rdatasets/csv/DAAG/nassCDS.csv); [documentation](https://vincentarelbundock.github.io/Rdatasets/doc/DAAG/nassCDS.html)) reported by police across the country from 1997-2002. Each record in these data contains information about the individual and vehicle involved in the crash, as well as some information about the circumstances and outcomes of the crash.

`r q <- q+1; letters[q]`. As a first step, read the data into a data.frame called `crash`. Subset the data to include only drivers. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Review the data documentation, especially for the variable `injSeverity`. Remove observations where `injSeverity` is either missing (`NA`), `unknown`, or `prior death`. Then, create a new variable called `y` that is equal to one if the individual sustained an incapacitating injury or worse, and zero otherwise. This variable will represent the crash causing a substantial injury. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. There is another variable in the dataset called `dvcat`, which estimates impact speeds in km/h. Convert this to a `factor` variable, and re-level it such that the reference level is the slowest impact speed. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Re-define the seatbelt and airbag variables to binary indicators. (**2 Points**)

```{r}
# Your code here...
```

`r q <- q+1; letters[q]`. Estimate a basic regression where major injury is explained by the estimated impact speed, age of the occupant, and year of the vehicle. Interpret the coefficients of the model. *Note*: you do not need to interpret the coefficients for impact speed. Rather, discuss the pattern of the coefficients for that variable. (**4 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Re-estimate the model above, but include the variables for the vehicle's safety features (`seatbelt` and `airbag`). What changes about the model? Why do you think you see these changes? (**4 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Add the variable `deploy` to the model, and output the coefficients. What does this variable measure? How does this variable change the interpretation of the model? (**4 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. Finally, in addition to what is already in the model, incorporate an interaction between `deploy` and `seatbelt`. Again, how does the interpretation of the model change? (**4 Points**)

```{r}
# Your code here...
```

*Your answer here...*

`r q <- q+1; letters[q]`. What, if anything, surprised you about the results in the analysis above? (**4 Points**)

*Your answer here...*


