Football Analytics with Python and R code
This repository contains code for the book Football Analytics with Python and R by Eric Eager and Richard Erickson.
The repository contains two files for each chapter, a Python file that ends in *.py
and an R file that ends in *.R
.
The Preface only includes R code because we used R to create the figures in this chapter and the code is not included in the book.
Likewise, Chapter 9 only includes R code because the example code was not used in the book.
The few lines of code from Appendix A are not included because these code are linear and trivial to write from the book.
Note: Line breaks differ in these files compared to the book because code was not limited by page width.
Note: Some figures were replotted to be higher resolution. These code files also include that code as additions.
Tip: Recreate the chapters using updated data. Our examples were written before the 2022 season, so anytime after the start of the season will have updated data for you to explore.
Python files
To download the required libraries for the Python examples, execute the following command:
$ pip install -r requirements.txt
Coding Note
We coded our examples with a focus on being easy to explain and use. We did not optimize function or code. Thus, as you learn more about programming in either R or Python, you will likely discover more efficient methods (but likely more difficult to understand for an novice) for the methods in these example files.
Data Note
The code will cache (that is, save code locally for use later) to avoid multiple downloads. We include a folder to storte this data in the data. However, we were unable to include the data files due to GitHubs limits for free repository data files. That being said, we strongly encourage you to use updated data from future (post-2022) season.
Repo Contents
This repository contains the following files:
./data/
is a folder to hold a cache copy of data pulled by the code.README.md
: This file- 'requirements.txt': Requirements file containing requirement python libraries for installation with pip.
00_preface.R
: R code of the preface [R only]01_Football_analytics.py
: Python code for Chapter 101_Football_analytics.R
: R code for Chapter 102_Exploratory_Data_Analysis.py
: Python code for Chapter 202_Exploratory_Data_Analysis.R
: R code for Chapter 203_Simple_Linear_Regression.py
: Python code for Chapter 303_Simple_Linear_Regression.R
: R code for Chapter 304_Multiple_Regression.py
: Python code for Chapter 404_Multiple_Regression.R
: R code for Chapter 405_Generalized_Linear_Models.py
: Python code for Chapter 505_Generalized_Linear_Models.R
: R code for Chapter 506_Using_Data_Science_for_Sports_Betting.py
: Python code for Chapter 606_Using_Data_Science_for_Sports_Betting.R
: R code for Chapter 607_Web_Scraping.py
: Python code for Chapter 707_Web_Scraping.R
: Python code for Chapter 708_Principal_Component_Analysis_and_Clustering.py
: Python code for Chapter 808_Principal_Component_Analysis_and_Clustering.R
: R code for Chapter 809_Advanced_Tools_and_Next_steps.R
: R code for Bayesian example [R only]11_Appendix_B.py
: Python code from Appendix B11_Appendix_B.R
: R code from Appendix B12_Appendix_C.py
: Python code for Appendix C12_Appendix_C.R
: R code for Appendix C
Disclaimer
The code provided in this repository is provided "as is" without any warranty or guarantees. However, pull requests are welcome.