---
title: "4.3 — Categorical Data & Interactions — R Practice"
author: "Your Name Here!"
date: "October 31, 2022"
format:
html:
self-contained: true # so we don't need other files (like plot images)
toc: true # show a table of contents
toc-location: left
theme: default
df-print: paged # by default, show tables (tibbles) as paged tables
editor: visual
execute:
echo: true # shows all code on rendered document
---
# Required Packages & Data
Load all the required packages we will use (**note I have installed them already into the cloud project**) by running (clicking the green play button) the chunk below:
```{r}
#| label: load-packages
#| warning: false
#| message: false
library(tidyverse) # your friend and mine
library(broom) # for tidy regression
library(modelsummary) # for nice regression tables
```
We are returning to the speeding tickets data that we began to explore in [R Practice 4.1 on Multivariate Regression](http://metricsf22.classes.ryansafner.com/r/4.1-r-practice). Download and read in (`read_csv`) the data below.
- [ `speeding_tickets.csv`](http://metricsf21.classes.ryansafner.com/data/speeding_tickets.csv)
```{r}
# run or edit this chunk (if you want to rename the data)
# read in data from url
# or you could download and upload it to this project instead
speed <- read_csv("https://metricsf22.classes.ryansafner.com/files/data/speeding_tickets.csv")
```
This data comes from a paper by Makowsky and Strattman (2009) that we will examine later. Even though state law sets a formula for tickets based on how fast a person was driving, police officers in practice often deviate from that formula. This dataset includes information on all traffic stops. An amount for the fine is given only for observations in which the police officer decided to assess a fine. There are a number of variables in this dataset, but the one's we'll look at are:
| `Amount` | Amount of fine (in dollars) assessed for speeding |
| `Age` | Age of speeding driver (in years) |
| `MPHover` | Miles per hour over the speed limit |
| `Black` | Dummy $=1$ if driver was black, $=0$ if not |
| `Hispanic` | Dummy $=1$ if driver was Hispanic, $=0$ if not |
| `Female` | Dummy $=1$ if driver was female, $=0$ if not |
| `OutTown` | Dummy $=1$ if driver was not from local town, $=0$ if not |
| `OutState` | Dummy $=1$ if driver was not from local state, $=0$ if not |
| `StatePol` | Dummy $=1$ if driver was stopped by State Police, $=0$ if stopped by other (local) |
We want to explore **who gets fines, and how much**. We'll come back to the other variables (which are categorical) in this dataset in later lessons.
## Question 1
We will have to do a little more cleaning to get some of the data into a more usable form.
### Part A
Inspect the data with `str()` or `head()` or `glimpse()` to see what it looks like.
```{r}
# type your code below in this chunk
```
What `class` of variable are `Black`, `Hispanic`, `Female`, `OutTown`, and `OutState`?
### Part B
Notice that when importing the data from the `.csv` file, `R` interpreted these variables as `numeric` (`num`) or `double` (`dbl`), but we want them to be `factor` (`fct`) variables, to ensure `R` recognizes that there are two groups (categories), 0 and 1.
You could convert the variables one at a time to factors using `as.factor()` inside a `mutate()` command. But there is a special `mutate()` command that allows you to apply a transformation (like changing a variable's class to `factor`), which you can run the following chunk to execute:
```{r}
# run or edit this chunk
speed <- speed %>%
mutate_at(c("Black", "Hispanic", "Female", "OutTown", "OutState"), factor)
speed
```
Confirm that these are now `factor` (`fct`) variables.
### Part C
Finally, recall from the last time we worked with this data that there are many `NA`s for `Amount` (these are people that were stopped but did not receive a fine). Let's `filter()` only those observations for which `Amount` is a positive number, and save this in your dataframe (assign and overwrite it, or make a new dataframe).
```{r}
# run or edit this chunk
# see the NAs
speed %>%
select(Amount) %>%
summary()
# overwrite data to keep only positive Amounts
speed <- speed %>%
filter(Amount > 0)
# see there are no more NAs
speed %>%
select(Amount) %>%
summary()
```
## Question 2
Does the sex of the driver affect the fine? There's already a dummy variable `Female` in the dataset, so create a scatterplot between `Amount` (as `y`) and Female (as `x`).
Hint: Use `geom_jitter()` instead of `geom_point()` to better see the points, and play around with `width` settings inside `geom_jitter()`
```{r}
# type your code below in this chunk
```
As an aside, if you had not made `Female` a `factor`, and kept it `numeric`, it might alter the plot.
You can make `Female` a `factor` just for plotting purposes by setting `aes(x = as.factor(Female))` inside the `aes()` layer of your `ggplot()` code.
## Question 3
Now let's start looking at the distribution conditionally to find the different group means.
### Part A
Find the mean and standard deviation of `Amount` for *male* drivers and again for *female* drivers.
Hint: properly `filter()` the data and then use the `summarize()` command.
```{r}
# type your code below in this chunk
```
### Part B
What is the difference between the average Amount for Males and Females?
```{r}
# type your code below in this chunk
```
### Part C
We did not go over how to do this in class, but you can run a **t-test for the difference in group means** to see if the difference is statistically significant. The syntax is similar for a regression:
```{r}
# run or edit this chunk
t.test(Amount ~ Female,
data = speed)
```
Is there a statistically significant difference between `Amount` for male and female drivers? Hint: this is like any hypothesis test. Here $H_0: \text{difference}=0$. A $t$-value needs to be large enough to be greater than a critical value of $t$. Check the $p$-value and see if it is less than our standard of $\alpha=0.05.$
## Question 4
### Part A
Now run the following regression to ensure we get the same result as the t-test.
$$\widehat{\text{Amount}}_i=\hat{\beta_0}+\hat{\beta_1} \, \text{Female}_i$$
```{r}
# type your code below in this chunk
```
### Part B
Write out the estimated regression equation.
### Part C
Use the regression coefficients to find
i. the average `Amount` for men
ii. the average `Amount` for women
iii. the difference in average `Amount` between men and women
## Question 5
Let's recode the sex variable to `Male` instead of `Female`.
### Part A
Make a new variable called `Male` and save it in your dataframe using the `ifelse()` command:
```{r}
# run or edit this chunk
speed <- speed %>% # overwrite or save as another dataframe
mutate(Male = ifelse(test = Female == 0, # test indiv. to see if Female is 0
yes = 1, # if yes (a Male), code Male as 1
no = 0), # if no (a Female), code Male as 0
)
# Verify it worked
speed %>%
select(Female, Male)
```
### Part B
Run the same regression as in question 4, but use `Male` instead of `Female`.
$$\widehat{\text{Amount}}_i=\hat{\beta_0}+\hat{\beta_1} \, \text{Male}_i$$
```{r}
# type your code below in this chunk
```
### Part C
Write out the estimated regression equation.
### Part D
Use the regression coefficients to find
i. the average `Amount` for men
ii. the average `Amount` for women
iii. the difference in average `Amount` between men and women
## Question 6
Run a regression of `Amount` on `Male` and `Female`. What happens, and why?
$$\widehat{\text{Amount}}_i=\hat{\beta_0}+\hat{\beta_1} \, \text{Male}_i + \hat{\beta_2} \, \text{Female}_i$$
```{r}
# type your code below in this chunk
```
## Question 7
`Age` probably has a lot to do with differences in fines, perhaps also age affects fines differences between males and females.
### Part A
Run a regression of `Amount` on `Age` and `Female.` How does the coefficient on `Female` change?
```{r}
# type your code below in this chunk
```
### Part B
Now let's see if the difference in fine between men and women are different depending on the driver's age. Run the regression again, but add an **interaction term** between `Female` and `Age`, using `Female*Age` or `Female:Age`.
$$\widehat{\text{Amount}}_i=\hat{\beta_0}+\hat{\beta_1} \, \text{Age}_i + \hat{\beta_2} \, \text{Female}_i + \hat{\beta_3} \, (\text{Age}_i \times \text{Female}_i)$$
```{r}
# type your code below in this chunk
```
### Part C
Write out your estimated regression equation.
### Part D
Interpret the interaction effect. Is it statistically significant?
### Part E
Plugging in 0 or 1 as necessary, rewrite (on your paper) this regression as *two separate* equations, one for Males and one for Females.
### Part F
Let's try to visualize this. Make a scatterplot of `Age` (X) and `Amount` (Y) and include a regression line.
Try adding `color = Female` inside your original (global) `aes()` layer. This will produce two sets of points and regression lines colored by `Female`.
By the way, if it isn't a `factor` variable already, we can ensure that it is with `as.factor(Female)`. We shouldn't need to in *this* case because we already reset `Female` as a faction in question 1.
```{r}
# type your code below in this chunk
```
### Part G
Add a final facet layer to the plot make two different sub-plots by sex with `facet_wrap( ~ Female)`.
```{r}
# type your code below in this chunk
```
## Question 8
Now let's look at the possible interaction between sex (`Male` or `Female`) and whether a driver is from In-State or Out-of-State (`OutState`).
### Part A
Use `R` to examine the data and find the average fine for:
i. Males In-State
ii. Males Out-of-State
iii. Females In-State
iv. Females Out-of-State
```{r}
# type your code below in this chunk
```
### Part B
Now run a regression of the following model:
$$\widehat{\text{Amount}}_i=\hat{\beta_0}+\hat{\beta_1} \, \text{Female}_i+\hat{\beta_2} \, \text{OutState}_i+\hat{\beta_3} \, (\text{Female}_i \times \text{OutState}_i)$$
```{r}
# type your code below in this chunk
```
### Part C
Write out the estimated regression equation.
### Part D
What does each coefficient mean?
### Part E
Using the regression equation, what are the average fine for
i. Males In-State
ii. Males Out-of-State
iii. Females In-State
iv. Females Out-of-State
Compare to your answers in part A.
## Question 9
Collect your regressions from questions 4, 5b, 7a, 7b, and 8b and output them in a regression table with `modelsummary()`.
```{r}
# type your code below in this chunk
```