Overview

The goal of fitzRoy is to provide a set of functions that allows for users to easily get access to AFL data from sources such as afltables.com and footywire.com. There are also tools for processing and cleaning that data. Future versions will include basic ELO processing functions.

Installation

You can install fitzRoy from github with:

# install.packages("devtools")
devtools::install_github("jimmyday12/fitzRoy")

Usage

The fitzRoy package can be used to simply get data from various sources. Some minimal working examples are below.

Getting Data

Primarily, the tool can be used to access data from various sources. Data is included in the package and can be access directly however this will not be up to date. Each source of data has functions for updating data during the season.

AFL Tables match results

You can access the basic afl tables match results data. This includes all matches from 1897-current. It is generally updated on the day after a round finishes.

You can access the data directly from the package using match_results. This will be updated periodically but you will need to update your R package to get access to the latest data. It is better to use get_match_results directly, as this will give you up to date results.

library(fitzRoy)
results <- get_match_results()
#> Warning: package 'bindrcpp' was built under R version 3.4.4

tail(results)
#> # A tibble: 6 x 16
#>     Game Date       Round Home.Team    Home.Goals Home.Behinds Home.Points
#>    <dbl> <date>     <chr> <chr>             <int>        <int>       <int>
#> 1 15375. 2018-08-11 R21   Port Adelai…          9            4          58
#> 2 15376. 2018-08-11 R21   GWS                  15           16         106
#> 3 15377. 2018-08-11 R21   Collingwood          14           20         104
#> 4 15378. 2018-08-12 R21   North Melbo…         12           13          85
#> 5 15379. 2018-08-12 R21   Melbourne            10           18          78
#> # ... with 1 more row, and 9 more variables: Away.Team <chr>,
#> #   Away.Goals <int>, Away.Behinds <int>, Away.Points <int>, Venue <chr>,
#> #   Margin <int>, Season <dbl>, Round.Type <chr>, Round.Number <int>

You can also convert this format into a more analysis friendly “long” format using the helper function convert_results.

results_long <- convert_results(results)

head(results_long)
#> # A tibble: 6 x 13
#>    Game Date       Round Venue       Margin Season Round.Type Round.Number
#>   <dbl> <date>     <chr> <chr>        <dbl>  <dbl> <chr>             <int>
#> 1    1. 1897-05-08 R1    Brunswick …    33.  1897. Regular               1
#> 2    1. 1897-05-08 R1    Brunswick …   -33.  1897. Regular               1
#> 3    2. 1897-05-08 R1    Victoria P…    25.  1897. Regular               1
#> 4    2. 1897-05-08 R1    Victoria P…   -25.  1897. Regular               1
#> 5    3. 1897-05-08 R1    Corio Oval    -23.  1897. Regular               1
#> # ... with 1 more row, and 5 more variables: Status <chr>, Behinds <chr>,
#> #   Goals <chr>, Points <chr>, Team <chr>

AFL Tables player results

A new function will return all detailed player stats from afltables.com. Primarily, the easiest way to use this is simply to call get_afltables_stats with your required start_date and end_date.

#stats <- get_afltables_stats(start_date = "2000-01-01", end_date = "2018-06-01")

#tail(stats)

Fixture

You can access the fixture using get_fixture function. This will download the fixture for the current calendar year by default.

fixture <- get_fixture()

head(fixture)
#> # A tibble: 6 x 7
#>   Date                Season Season.Game Round Home.Team  Away.Team Venue 
#>   <dttm>               <int>       <int> <int> <chr>      <chr>     <chr> 
#> 1 2018-03-22 19:25:00   2018           1     1 Richmond   Carlton   MCG   
#> 2 2018-03-23 19:50:00   2018           1     1 Essendon   Adelaide  Etiha…
#> 3 2018-03-24 15:35:00   2018           1     1 St Kilda   Brisbane… Etiha…
#> 4 2018-03-24 16:05:00   2018           1     1 Port Adel… Fremantle Adela…
#> 5 2018-03-24 18:25:00   2018           1     1 Gold Coast North Me… Cazal…
#> # ... with 1 more row

Footywire Advanced Player Stats

Footywire data is available in the form of advanced player match statistics from 2010 games onwards. This is when advanced statistics became available.

Footywire data from 2010-2017 is included in the package. This will be updated periodically but you will need to update your R package to get access to the latest data.

## Show the top of player_stats
head(fitzRoy::player_stats)
#>         Date Season   Round Venue         Player     Team Opposition
#> 1 2010-03-25   2010 Round 1   MCG Daniel Connors Richmond    Carlton
#> 2 2010-03-25   2010 Round 1   MCG Daniel Jackson Richmond    Carlton
#> 3 2010-03-25   2010 Round 1   MCG  Brett Deledio Richmond    Carlton
#> 4 2010-03-25   2010 Round 1   MCG    Ben Cousins Richmond    Carlton
#> 5 2010-03-25   2010 Round 1   MCG  Trent Cotchin Richmond    Carlton
#> 6 2010-03-25   2010 Round 1   MCG  Dustin Martin Richmond    Carlton
#>   Status Match_id CP UP ED   DE CM GA MI5 One.Percenters BO TOG  K HB  D M
#> 1   Home     5089  8 15 16 66.7  0  0   0              1  0  69 14 10 24 3
#> 2   Home     5089 11 10 14 60.9  1  0   0              0  0  80 11 12 23 2
#> 3   Home     5089  7 14 16 76.2  0  0   0              0  0  89 12  9 21 5
#> 4   Home     5089  9 10 11 57.9  0  1   0              0  0  69 13  6 19 1
#> 5   Home     5089  8 10 13 68.4  1  0   0              0  1  77 11  8 19 6
#> 6   Home     5089  6 12 16 88.9  0  0   0              1  0  81  5 13 18 4
#>   G B T HO GA1 I50 CL CG R50 FF FA AF SC CCL SCL SI MG TO ITC T5
#> 1 0 0 1  0   0   2  2  4   6  2  0 77 85  NA  NA NA NA NA  NA NA
#> 2 0 0 5  0   0   8  5  4   1  2  0 85 89  NA  NA NA NA NA  NA NA
#> 3 1 0 6  0   0   4  3  4   3  1  2 94 93  NA  NA NA NA NA  NA NA
#> 4 1 0 1  0   1   1  2  3   4  1  0 65 70  NA  NA NA NA NA  NA NA
#> 5 0 0 1  0   0   2  3  3   2  0  2 65 63  NA  NA NA NA NA  NA NA
#> 6 0 0 3  0   0   2  3  1   0  0  1 62 72  NA  NA NA NA NA  NA NA

We can also use the update_footywire_stats function to get the most up to date data. This will merge data from 2010-current with any new data points.

## Update footywire data
dat <- update_footywire_stats()
#> Getting match ID's...
#> Downloading new data for 63 matches...
#> 
#> Checking Github
#> Warning in ids == git_ids: longer object length is not a multiple of
#> shorter object length
#> Finished getting data

tail(dat)
#>             Date Season    Round         Venue             Player     Team
#> 78535 2018-07-29   2018 Round 19 Optus Stadium       James Worpel Hawthorn
#> 78536 2018-07-29   2018 Round 19 Optus Stadium       Jarman Impey Hawthorn
#> 78537 2018-07-29   2018 Round 19 Optus Stadium  Ryan Schoenmakers Hawthorn
#> 78538 2018-07-29   2018 Round 19 Optus Stadium    Jonathon Ceglar Hawthorn
#> 78539 2018-07-29   2018 Round 19 Optus Stadium Brendan Whitecross Hawthorn
#> 78540 2018-07-29   2018 Round 19 Optus Stadium       Paul Puopolo Hawthorn
#>       Opposition Status Match_id CP UP ED   DE CM GA MI5 One.Percenters BO
#> 78535  Fremantle   Away     9675  5 10 12 75.0  0  1   1              0  1
#> 78536  Fremantle   Away     9675  3 13 10 66.7  0  0   0              3  0
#> 78537  Fremantle   Away     9675  6  8  8 61.5  2  0   4              2  0
#> 78538  Fremantle   Away     9675  4  8  9 75.0  1  0   1              3  0
#> 78539  Fremantle   Away     9675  3  8  9 90.0  0  1   0              1  0
#> 78540  Fremantle   Away     9675  6  5  8 80.0  0  1   0              1  0
#>       TOG  K HB  D M G B T HO GA1 I50 CL CG R50 FF FA AF SC CCL SCL SI  MG
#> 78535  83  8  8 16 2 0 1 3  0   1   5  2  0   0  0  0 59 72   0   2  8 296
#> 78536  77 11  4 15 4 0 0 3  0   0   1  1  3   2  0  1 62 63   0   1  4 227
#> 78537  82  7  6 13 6 2 2 3  0   0   2  0  0   0  0  0 77 81   0   0  8 172
#> 78538  76  7  5 12 4 0 0 3 27   0   1  1  3   1  1  1 80 63   1   0  4 188
#> 78539  74  6  4 10 6 0 0 4  0   1   2  0  1   1  0  0 60 51   0   0  4 179
#> 78540  80  4  6 10 1 1 0 5  0   1   2  0  1   0  1  0 54 76   0   0  4 100
#>       TO ITC T5
#> 78535  3   2  2
#> 78536  5   1  0
#> 78537  2   0  2
#> 78538  2   0  0
#> 78539  1   1  2
#> 78540  1   3  2

Weather

We have also included weather data for the 2017 season. This is a work in progress but includes rainfall data from the nearest observation station to each ground. This data is included in the package as results_weather.

library(ggplot2)
library(dplyr)

# Get 2017 weather data
weather <- fitzRoy::results_weather %>%
  filter(Season == 2017)

# Plot total rainfal for each home team
ggplot(dat = weather, aes(x = Home.Team, y = Rainfall)) +
  geom_col() + 
  coord_flip()

Squiggle Data

You can access data from the Squiggle API where the tips of well known AFL tipping models are collected. See full instructions on the above link.

# You can get the sources
sources <- get_squiggle_data("sources")
head(sources)
#>                    name id                                url
#> 1              Squiggle  1      https://live.squiggle.com.au/
#> 2               The Arc  2           https://thearcfooty.com/
#> 3        Figuring Footy  3          http://figuringfooty.com/
#> 4       Matter of Stats  4      http://www.matterofstats.com/
#> 5               Punters  5                                   
#> 6 Footy Maths Institute  6 https://footymaths.blogspot.com.au
# Get all tips
tips <- get_squiggle_data("tips")
head(tips)  
#>   ateamid gameid hteamid margin sourceid   err    bits          source
#> 1      14      1       3   1.00        1 42.00  0.0000        Squiggle
#> 2      14      1       3     NA        3    NA  0.2141  Figuring Footy
#> 3      14      1       3   5.39        4 48.39 -0.2076 Matter of Stats
#> 4      18      2       4  10.31        4  3.69  0.3265 Matter of Stats
#> 5      18      2       4  17.00        1  3.00  0.3103        Squiggle
#> 6       9      8       1   3.00        1 53.00  0.0000        Squiggle
#>           venue       hteam hconfidence tipteamid              tip round
#> 1        M.C.G.     Carlton        50.0        14         Richmond     1
#> 2        M.C.G.     Carlton        42.0        14         Richmond     1
#> 3        M.C.G.     Carlton        56.7         3          Carlton     1
#> 4        M.C.G. Collingwood        37.3        18 Western Bulldogs     1
#> 5        M.C.G. Collingwood        38.0        18 Western Bulldogs     1
#> 6 Adelaide Oval    Adelaide        50.0         1         Adelaide     1
#>   year correct                date confidence                  ateam
#> 1 2017       1 2017-03-23 19:20:00       50.0               Richmond
#> 2 2017       1 2017-03-23 19:20:00       58.0               Richmond
#> 3 2017       0 2017-03-23 19:20:00       56.7               Richmond
#> 4 2017       1 2017-03-24 19:50:00       62.7       Western Bulldogs
#> 5 2017       1 2017-03-24 19:50:00       62.0       Western Bulldogs
#> 6 2017       1 2017-03-26 15:20:00       50.0 Greater Western Sydney
#>               updated
#> 1 2017-07-11 13:59:46
#> 2 2017-04-10 12:18:02
#> 3 2017-07-11 13:59:46
#> 4 2017-07-11 13:59:46
#> 5 2017-07-11 13:59:46
#> 6 2017-07-11 13:59:46
# Get` just tips from round 1, 2018
tips <- get_squiggle_data("tips", round = 1, year = 2018)
head(tips)
#>   year confidence margin hconfidence                source      tip
#> 1 2018      56.00  11.00       44.00              Squiggle Adelaide
#> 2 2018      59.80   9.00       40.20               The Arc Adelaide
#> 3 2018      59.50   9.78       40.50       Matter of Stats Adelaide
#> 4 2018      52.08     NA       52.08               Punters Essendon
#> 5 2018      66.00  21.00       34.00 Footy Maths Institute Adelaide
#> 6 2018      55.16   8.00       44.84            PlusSixOne Adelaide
#>      hteam correct             updated   err hteamid sourceid    ateam
#> 1 Essendon       0 2018-03-23 22:54:38 23.00       5        1 Adelaide
#> 2 Essendon       0 2018-03-23 22:54:38 21.00       5        2 Adelaide
#> 3 Essendon       0 2018-03-23 22:54:38 21.78       5        4 Adelaide
#> 4 Essendon       1 2018-03-23 22:54:38    NA       5        5 Adelaide
#> 5 Essendon       0 2018-03-23 22:54:38 33.00       5        6 Adelaide
#> 6 Essendon       0 2018-03-23 22:54:38 20.00       5        7 Adelaide
#>   gameid round    bits tipteamid     venue                date ateamid
#> 1    373     1 -0.1844         1 Docklands 2018-03-23 19:50:00       1
#> 2    373     1 -0.3147         1 Docklands 2018-03-23 19:50:00       1
#> 3    373     1 -0.3040         1 Docklands 2018-03-23 19:50:00       1
#> 4    373     1  0.0588         5 Docklands 2018-03-23 19:50:00       1
#> 5    373     1 -0.5564         1 Docklands 2018-03-23 19:50:00       1
#> 6    373     1 -0.1571         1 Docklands 2018-03-23 19:50:00       1

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.