Skip to content

Commit

Permalink
20240521 - add data from nflfastR
Browse files Browse the repository at this point in the history
  • Loading branch information
isaactpetersen committed May 22, 2024
1 parent d1ba3e7 commit 2461ff8
Showing 6 changed files with 301 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@ Imports:
tidyverse,
downlit,
xml2,
nflfastR,
gsisdecoder,
here,
petersenlab
Remotes:
DevPsyLab/petersenlab
3 changes: 3 additions & 0 deletions data/nfl_actualStats_career.RData
Git LFS file not shown
3 changes: 3 additions & 0 deletions data/nfl_actualStats_seasonal.RData
Git LFS file not shown
3 changes: 3 additions & 0 deletions data/nfl_actualStats_weekly.RData
Git LFS file not shown
3 changes: 3 additions & 0 deletions data/nfl_pbp.RData
Git LFS file not shown
286 changes: 286 additions & 0 deletions getting-started.qmd
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ To get started, follow the following steps:
1. Install `R`: <https://cran.r-project.org>
1. Install `RStudio Desktop`: <https://posit.co/download/rstudio-desktop>
1. After installing `RStudio`, open `RStudio` and run the following code in the console to install several key `R` packages:

```{r}
#| eval: false
@@ -32,3 +33,288 @@ You can install R packages using the following syntax:
install.packages("INSERT_PACKAGE_NAME_HERE")
```

For instance, you can use the following code to install the `nflfastR` package:

```{r}
#| eval: false
install.packages("nflfastR")
```

## Load Packages {#sec-loadPackages}

```{r}
library("nflfastR")
library("tidyverse")
```

## Download Football Data {#sec-downloadFootballData}

### Play-By-Play Data {#sec-downloadPlayByPlay}

To download play-by-play data from prior weeks and seasons, we can use the `load_pbp()` function of the `nflfastR` package:

```{r}
#| eval: false
nfl_pbp <- nflfastR::load_pbp(seasons = TRUE)
```

```{r}
#| eval: false
#| include: false
library("here")
save(
nfl_pbp,
file = here("data", "nfl_pbp.RData")
)
```

```{r}
#| include: false
library("here")
load(file = here("data", "nfl_pbp.RData"))
```

### Player Info {#sec-downloadPlayerInfo}

## Calculations {#sec-calculations}

### Historical Actual Player Statistics {#sec-calculateActualStats}

TODO: compute player stats for defenses (`calculate_player_stats_def()`) and kickers (`calculate_player_stats_kicking()`).

Note: The following code takes a while to run.

First, subset the data by year:

```{r}
#| eval: false
nfl_pbp_1999 <- nfl_pbp %>% filter(season == 1999)
nfl_pbp_2000 <- nfl_pbp %>% filter(season == 2000)
nfl_pbp_2001 <- nfl_pbp %>% filter(season == 2001)
nfl_pbp_2002 <- nfl_pbp %>% filter(season == 2002)
nfl_pbp_2003 <- nfl_pbp %>% filter(season == 2003)
nfl_pbp_2004 <- nfl_pbp %>% filter(season == 2004)
nfl_pbp_2005 <- nfl_pbp %>% filter(season == 2005)
nfl_pbp_2006 <- nfl_pbp %>% filter(season == 2006)
nfl_pbp_2007 <- nfl_pbp %>% filter(season == 2007)
nfl_pbp_2008 <- nfl_pbp %>% filter(season == 2008)
nfl_pbp_2009 <- nfl_pbp %>% filter(season == 2009)
nfl_pbp_2010 <- nfl_pbp %>% filter(season == 2010)
nfl_pbp_2011 <- nfl_pbp %>% filter(season == 2011)
nfl_pbp_2012 <- nfl_pbp %>% filter(season == 2012)
nfl_pbp_2013 <- nfl_pbp %>% filter(season == 2013)
nfl_pbp_2014 <- nfl_pbp %>% filter(season == 2014)
nfl_pbp_2015 <- nfl_pbp %>% filter(season == 2015)
nfl_pbp_2016 <- nfl_pbp %>% filter(season == 2016)
nfl_pbp_2017 <- nfl_pbp %>% filter(season == 2017)
nfl_pbp_2018 <- nfl_pbp %>% filter(season == 2018)
nfl_pbp_2019 <- nfl_pbp %>% filter(season == 2019)
nfl_pbp_2020 <- nfl_pbp %>% filter(season == 2020)
nfl_pbp_2021 <- nfl_pbp %>% filter(season == 2021)
nfl_pbp_2022 <- nfl_pbp %>% filter(season == 2022)
nfl_pbp_2023 <- nfl_pbp %>% filter(season == 2023)
```

Second, compute the weekly stats:

```{r}
#| eval: false
nfl_actualStats_weekly <- nflfastR::calculate_player_stats(
nfl_pbp,
weekly = TRUE)
```

```{r}
#| eval: false
#| include: false
save(
nfl_actualStats_weekly,
file = here("data", "nfl_actualStats_weekly.RData")
)
```

Third, compute the career stats:

```{r}
#| eval: false
nfl_actualStats_career <- nflfastR::calculate_player_stats(
nfl_pbp,
weekly = FALSE)
```

```{r}
#| eval: false
#| include: false
save(
nfl_actualStats_career,
file = here("data", "nfl_actualStats_career.RData")
)
```

Fourth, compute seasonal stats by year:

```{r}
#| eval: false
nfl_actualStats_seasonal_1999 <- nflfastR::calculate_player_stats(
nfl_pbp_1999,
weekly = FALSE)
nfl_actualStats_seasonal_2000 <- nflfastR::calculate_player_stats(
nfl_pbp_2000,
weekly = FALSE)
nfl_actualStats_seasonal_2001 <- nflfastR::calculate_player_stats(
nfl_pbp_2001,
weekly = FALSE)
nfl_actualStats_seasonal_2002 <- nflfastR::calculate_player_stats(
nfl_pbp_2002,
weekly = FALSE)
nfl_actualStats_seasonal_2003 <- nflfastR::calculate_player_stats(
nfl_pbp_2003,
weekly = FALSE)
nfl_actualStats_seasonal_2004 <- nflfastR::calculate_player_stats(
nfl_pbp_2004,
weekly = FALSE)
nfl_actualStats_seasonal_2005 <- nflfastR::calculate_player_stats(
nfl_pbp_2005,
weekly = FALSE)
nfl_actualStats_seasonal_2006 <- nflfastR::calculate_player_stats(
nfl_pbp_2006,
weekly = FALSE)
nfl_actualStats_seasonal_2007 <- nflfastR::calculate_player_stats(
nfl_pbp_2007,
weekly = FALSE)
nfl_actualStats_seasonal_2008 <- nflfastR::calculate_player_stats(
nfl_pbp_2008,
weekly = FALSE)
nfl_actualStats_seasonal_2009 <- nflfastR::calculate_player_stats(
nfl_pbp_2009,
weekly = FALSE)
nfl_actualStats_seasonal_2010 <- nflfastR::calculate_player_stats(
nfl_pbp_2010,
weekly = FALSE)
nfl_actualStats_seasonal_2011 <- nflfastR::calculate_player_stats(
nfl_pbp_2011,
weekly = FALSE)
nfl_actualStats_seasonal_2012 <- nflfastR::calculate_player_stats(
nfl_pbp_2012,
weekly = FALSE)
nfl_actualStats_seasonal_2013 <- nflfastR::calculate_player_stats(
nfl_pbp_2013,
weekly = FALSE)
nfl_actualStats_seasonal_2014 <- nflfastR::calculate_player_stats(
nfl_pbp_2014,
weekly = FALSE)
nfl_actualStats_seasonal_2015 <- nflfastR::calculate_player_stats(
nfl_pbp_2015,
weekly = FALSE)
nfl_actualStats_seasonal_2016 <- nflfastR::calculate_player_stats(
nfl_pbp_2016,
weekly = FALSE)
nfl_actualStats_seasonal_2017 <- nflfastR::calculate_player_stats(
nfl_pbp_2017,
weekly = FALSE)
nfl_actualStats_seasonal_2018 <- nflfastR::calculate_player_stats(
nfl_pbp_2018,
weekly = FALSE)
nfl_actualStats_seasonal_2019 <- nflfastR::calculate_player_stats(
nfl_pbp_2019,
weekly = FALSE)
nfl_actualStats_seasonal_2020 <- nflfastR::calculate_player_stats(
nfl_pbp_2020,
weekly = FALSE)
nfl_actualStats_seasonal_2021 <- nflfastR::calculate_player_stats(
nfl_pbp_2021,
weekly = FALSE)
nfl_actualStats_seasonal_2022 <- nflfastR::calculate_player_stats(
nfl_pbp_2022,
weekly = FALSE)
nfl_actualStats_seasonal_2023 <- nflfastR::calculate_player_stats(
nfl_pbp_2023,
weekly = FALSE)
nfl_actualStats_seasonal <- bind_rows(
nfl_actualStats_seasonal_1999,
nfl_actualStats_seasonal_2000,
nfl_actualStats_seasonal_2001,
nfl_actualStats_seasonal_2002,
nfl_actualStats_seasonal_2003,
nfl_actualStats_seasonal_2004,
nfl_actualStats_seasonal_2005,
nfl_actualStats_seasonal_2006,
nfl_actualStats_seasonal_2007,
nfl_actualStats_seasonal_2008,
nfl_actualStats_seasonal_2009,
nfl_actualStats_seasonal_2010,
nfl_actualStats_seasonal_2011,
nfl_actualStats_seasonal_2012,
nfl_actualStats_seasonal_2013,
nfl_actualStats_seasonal_2014,
nfl_actualStats_seasonal_2015,
nfl_actualStats_seasonal_2016,
nfl_actualStats_seasonal_2017,
nfl_actualStats_seasonal_2018,
nfl_actualStats_seasonal_2019,
nfl_actualStats_seasonal_2020,
nfl_actualStats_seasonal_2021,
nfl_actualStats_seasonal_2022,
nfl_actualStats_seasonal_2023,
.id = "id"
)
nfl_actualStats_seasonal$season <- NA
nfl_actualStats_seasonal$id <- as.integer(nfl_actualStats_seasonal$id)
for(i in 1:max(nfl_actualStats_seasonal$id)){
nfl_actualStats_seasonal$season[which(nfl_actualStats_seasonal$id == i)] <- 1998 + i
}
```

```{r}
#| eval: false
#| include: false
save(
nfl_actualStats_seasonal,
file = here("data", "nfl_actualStats_seasonal.RData")
)
```

### Historical Actual Fantasy Points {#sec-calculateActualPoints}

0 comments on commit 2461ff8

Please sign in to comment.