Import data
# excel file
prem <- read_excel("../00_data/Prem.xlsx")
prem
## # A tibble: 380 × 22
## Date HomeTeam AwayTeam FTHG FTAG FTR HTHG HTAG HTR Referee HS
## <chr> <chr> <chr> <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl>
## 1 13/02/20… Newcast… Aston V… 1 0 H 1 0 H C Paws… 10
## 2 25/09/20… Watford Newcast… 1 1 D 0 1 A J Gill… 15
## 3 01/12/20… Watford Chelsea 1 2 A 1 1 D D Coote 13
## 4 14/08/20… Watford Aston V… 3 2 H 2 0 H M Dean 13
## 5 13/03/20… Leeds Norwich 2 1 H 1 0 H S Attw… 13
## 6 13/03/20… Chelsea Newcast… 1 0 H 0 0 D D Coote 8
## 7 23/01/20… Chelsea Tottenh… 2 0 H 0 0 D P Tier… 15
## 8 18/09/20… Liverpo… Crystal… 3 0 H 1 0 H A Madl… 25
## 9 02/01/20… Brentfo… Aston V… 2 1 H 1 1 D C Paws… 10
## 10 26/02/20… Brighton Aston V… 0 2 A 0 1 A J Broo… 12
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## # HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
Introduction
Questions
Variation
Visualizing distributions
prem %>%
ggplot(mapping = aes(x = HS)) +
geom_bar()

prem %>%
ggplot(mapping = aes(x = AS)) +
geom_bar()

Typical values
prem %>%
filter(HS > 15) %>%
# Plot
ggplot(aes(x = HS)) +
geom_histogram(binwidth = 0.5)

prem %>%
filter(AS > 15) %>%
# Plot
ggplot(aes(x = AS)) +
geom_histogram(binwidth = 0.5)

Unusual values
prem %>%
ggplot(aes(x = HS)) +
geom_histogram(binwidth = .75) +
coord_cartesian(ylim = c(0, 15))

Missing Values
prem %>%
mutate(HS_rev = ifelse(HS < 3 | HS > 28, NA, HS)) %>%
ggplot(aes(x = HS_rev, y = FTHG)) +
geom_point()
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

Covariation
A categorical and continuous variable
prem %>%
ggplot(aes(x = HomeTeam, y = HS)) +
geom_boxplot() +
coord_flip()

Two categorical variables
prem %>%
count(HomeTeam, FTR) %>%
ggplot(aes(x = FTR, y = HomeTeam, fill = n)) +
geom_tile()

Two continous variables
prem %>%
ggplot(aes(x = HS, y = FTHG)) +
geom_hex(bins = 15)

Patterns and models
mod_prem <- lm(FTHG ~ HS, data = prem)
prem_resid <- prem %>%
add_residuals(mod_prem)
prem_resid %>%
ggplot(aes(x = FTR, y = resid)) +
geom_boxplot()
