library(tidyverse) # your friend and mine
library(broom) # for tidy regression
library(modelsummary) # for nice regression tables
library(car) # for vif command
4.1 — Multivariate OLS Estimators — R Practice
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:
Download and read in (read_csv
) the data below.
# 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
<- read_csv("https://metricsf22.classes.ryansafner.com/files/data/speeding_tickets.csv") speed
Rows: 68357 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (9): Black, Hispanic, Female, Amount, MPHover, Age, OutTown, OutState, S...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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:
Variable | Description |
---|---|
Amount |
Amount of fine (in dollars) assessed for speeding |
Age |
Age of speeding driver (in years) |
MPHover |
Miles per hour over the speed limit |
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
How does the age of a driver affect the amount of the fine? Make a scatterplot of the Amount
of the fine (y
) and the driver’s Age
(x
) along with a regression line.
# type your code below in this chunk
ggplot(data = speed)+
aes(x = Age,
y = Amount)+
geom_point()+
geom_smooth(method = "lm")
`geom_smooth()` using formula 'y ~ x'
Warning: Removed 36683 rows containing non-finite values (stat_smooth).
Warning: Removed 36683 rows containing missing values (geom_point).