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 and Footywire. There are also tools for processing and cleaning that data. Future versions will include basic ELO processing functions.

Load packages

First we need to grab a few packages. If you don’t have any of these, you’ll need to install them.

library(dplyr)
library(elo)
library(lubridate)
library(fitzRoy)

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.

results <- get_match_results()
tail(results)
#> # A tibble: 6 x 16
#>    Game Date       Round Home.Team Home.Goals Home.Behinds Home.Points Away.Team
#>   <dbl> <date>     <chr> <chr>          <int>        <int>       <int> <chr>    
#> 1 15609 2019-09-07 QF    Richmond          18            4         112 Brisbane…
#> 2 15610 2019-09-13 SF    Geelong           13           10          88 West Coa…
#> 3 15611 2019-09-14 SF    GWS               12           11          83 Brisbane…
#> 4 15612 2019-09-20 PF    Richmond          12           13          85 Geelong  
#> 5 15613 2019-09-21 PF    GWS                8            8          56 Collingw…
#> # … with 1 more row, and 8 more variables: 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 Status
#>   <dbl> <date>     <chr> <chr>  <dbl>  <dbl> <chr>             <int> <chr> 
#> 1     1 1897-05-08 R1    Brun…     33   1897 Regular               1 Home  
#> 2     1 1897-05-08 R1    Brun…    -33   1897 Regular               1 Away  
#> 3     2 1897-05-08 R1    Vict…     25   1897 Regular               1 Home  
#> 4     2 1897-05-08 R1    Vict…    -25   1897 Regular               1 Away  
#> 5     3 1897-05-08 R1    Cori…    -23   1897 Regular               1 Home  
#> # … with 1 more row, and 4 more variables: Behinds <chr>, Goals <chr>,
#> #   Points <chr>, Team <chr>

AFL Tables player results

A new function will return all detailed player stats from AFLtables. 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 = "2018-01-01", end_date = "2018-06-01")
tail(stats)
#> # A tibble: 6 x 59
#>   Season Round Date       Local.start.time Venue Attendance Home.team  HQ1G
#>    <dbl> <chr> <date>                <int> <chr>      <int> <chr>     <int>
#> 1   2018 10    2018-05-27             1440 Pert…      37575 Fremantle     3
#> 2   2018 10    2018-05-27             1440 Pert…      37575 Fremantle     3
#> 3   2018 10    2018-05-27             1440 Pert…      37575 Fremantle     3
#> 4   2018 10    2018-05-27             1440 Pert…      37575 Fremantle     3
#> 5   2018 10    2018-05-27             1440 Pert…      37575 Fremantle     3
#> # … with 1 more row, and 51 more variables: HQ1B <int>, HQ2G <int>, HQ2B <int>,
#> #   HQ3G <int>, HQ3B <int>, HQ4G <int>, HQ4B <int>, Home.score <int>,
#> #   Away.team <chr>, AQ1G <int>, AQ1B <int>, AQ2G <int>, AQ2B <int>,
#> #   AQ3G <int>, AQ3B <int>, AQ4G <int>, AQ4B <int>, Away.score <int>,
#> #   First.name <chr>, Surname <chr>, ID <dbl>, Jumper.No. <dbl>,
#> #   Playing.for <chr>, Kicks <dbl>, Marks <dbl>, Handballs <dbl>, Goals <dbl>,
#> #   Behinds <dbl>, Hit.Outs <dbl>, Tackles <dbl>, Rebounds <dbl>,
#> #   Inside.50s <dbl>, Clearances <dbl>, Clangers <dbl>, Frees.For <dbl>,
#> #   Frees.Against <dbl>, Brownlow.Votes <dbl>, Contested.Possessions <dbl>,
#> #   Uncontested.Possessions <dbl>, Contested.Marks <dbl>,
#> #   Marks.Inside.50 <dbl>, One.Percenters <dbl>, Bounces <dbl>,
#> #   Goal.Assists <dbl>, Time.on.Ground.. <int>, Substitute <int>,
#> #   Umpire.1 <chr>, Umpire.2 <chr>, Umpire.3 <chr>, Umpire.4 <chr>,
#> #   group_id <int>

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>               <dbl>       <int> <dbl> <chr>        <chr>       <chr>   
#> 1 2019-03-21 19:25:00   2019           1     1 Carlton      Richmond    M.C.G.  
#> 2 2019-03-22 19:50:00   2019           1     1 Collingwood  Geelong     M.C.G.  
#> 3 2019-03-23 13:45:00   2019           1     1 Melbourne    Port Adela… M.C.G.  
#> 4 2019-03-23 16:05:00   2019           1     1 Adelaide     Hawthorn    Adelaid…
#> 5 2019-03-23 19:20:00   2019           1     1 Brisbane Li… West Coast  Gabba   
#> # … 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.

Note - as of v0.2.0, all internal data has been removed from the package. Please use the relevant functions instead.

The following code no longer works.

## Show the top of player_stats
head(fitzRoy::player_stats)

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()

Alternatively, we can just return one game if we know it’s ID. This can be found by looking at the URL of the match you want. For example, the ID of the 2019 AFL Grand Final is 9927.

https://www.footywire.com/afl/footy/ft_match_statistics?mid=9927

## Update footywire data
stats_gf <- get_footywire_stats(ids = 9927)
head(stats_gf)
#>         Date Season       Round Venue          Player     Team Opposition
#> 1 2019-09-28   2019 Grand Final   MCG    Bachar Houli Richmond        GWS
#> 2 2019-09-28   2019 Grand Final   MCG   Dustin Martin Richmond        GWS
#> 3 2019-09-28   2019 Grand Final   MCG    Dion Prestia Richmond        GWS
#> 4 2019-09-28   2019 Grand Final   MCG   Nick Vlastuin Richmond        GWS
#> 5 2019-09-28   2019 Grand Final   MCG Marlion Pickett Richmond        GWS
#> 6 2019-09-28   2019 Grand Final   MCG   Shane Edwards Richmond        GWS
#>   Status Match_id CP UP ED   DE CM GA MI5 One.Percenters BO CCL SCL SI  MG TO
#> 1   Home     9927  9 17 20 76.9  0  0   0              5  2   0   1  6 317  2
#> 2   Home     9927 12 11 19 86.4  1  2   2              2  3   0   3  8 372  5
#> 3   Home     9927  7 14 12 54.5  0  1   0              2  0   0   4  3 366  6
#> 4   Home     9927  3 18 20 90.9  1  0   0              5  0   0   0  5 570  2
#> 5   Home     9927  8 14 14 63.6  0  0   1              0  1   2   1  9 559  5
#> 6   Home     9927 11  9 17 81.0  0  1   0              2  0   2   4  6 268  3
#>   ITC T5 TOG  K HB  D  M G B T HO GA1 I50 CL CG R50 FF FA  AF  SC
#> 1   9  0  96 14 12 26  7 0 0 6  0   0   0  1  1   6  3  0 114 120
#> 2   0  1  86 11 11 22  4 4 0 1  0   2   4  3  4   0  2  2  91 137
#> 3   1  0  87 19  3 22  1 0 1 3  0   1   2  4  5   1  1  2  74  86
#> 4   5  0  89 17  5 22 12 0 0 3  0   0   2  0  1   5  0  1 106 102
#> 5   3  0  66 13  9 22  2 1 0 1  0   0   8  3  0   1  0  0  73  96
#> 6   4  1  78  9 12 21  2 0 0 6  0   1   5  6  3   3  2  1  80  98

Weather

Note - as of v0.2.0 this has been removed

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

Create Ladder

You can recreate the ladder for every round of the home and away season since 1897. You can either pass in a dataframe extracted using get_match_results (ideal as get_match_results doesn’t need to be executed every time return_ladder is called):

ladder <- return_ladder(match_results_df = results)
head(ladder)
#> # A tibble: 6 x 8
#>   Season Team  Round.Number Season.Points Score.For Score.Against Percentage
#>    <dbl> <chr>        <int>         <dbl>     <dbl>         <dbl>      <dbl>
#> 1   1897 Fitz…            1             4        49            16      3.06 
#> 2   1897 Coll…            1             4        41            16      2.56 
#> 3   1897 Esse…            1             4        47            24      1.96 
#> 4   1897 Melb…            1             4        44            27      1.63 
#> 5   1897 Sydn…            1             0        27            44      0.614
#> # … with 1 more row, and 1 more variable: Ladder.Position <int>

Or leave the match_results_df argument blank (which will execute the get_match_results() function internally):

ladder <- return_ladder()

Alternatively, we can also return the ladder for any round, or any season, or a combination of both round and season:

ladder_round <- return_ladder(match_results_df = results, season_round = 15, season = 2018)
head(ladder_round)
#> # A tibble: 6 x 8
#>   Season Team  Round.Number Season.Points Score.For Score.Against Percentage
#>    <dbl> <chr>        <int>         <dbl>     <dbl>         <dbl>      <dbl>
#> 1   2018 Rich…           15            44      1358          1004       1.35
#> 2   2018 Coll…           15            40      1328          1089       1.22
#> 3   2018 West…           15            40      1290          1066       1.21
#> 4   2018 Sydn…           15            40      1215          1006       1.21
#> 5   2018 Port…           15            40      1204          1047       1.15
#> # … with 1 more row, and 1 more variable: Ladder.Position <int>