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 15618 2020-03-21 R1    Adelaide          11            5          71 Sydney   
#> 2 15619 2020-03-21 R1    GWS               17            3         105 Geelong  
#> 3 15620 2020-03-21 R1    Gold Coa…          4            5          29 Port Ade…
#> 4 15621 2020-03-22 R1    North Me…          8            8          56 St Kilda 
#> 5 15622 2020-03-22 R1    Hawthorn          14            6          90 Brisbane…
#> # … 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>      <dbl> <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.. <dbl>, 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)
#>                    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
#>                                                          icon
#> 1                   /wp-content/uploads/2017/02/logo7-250.png
#> 2        /wp-content/themes/squiggle/assets/images/TheArc.jpg
#> 3 /wp-content/themes/squiggle/assets/images/FiguringFooty.gif
#> 4           /wp-content/themes/squiggle/assets/images/MoS.png
#> 5                                                            
#> 6           /wp-content/themes/squiggle/assets/images/FMI.png
# Get all tips
tips <- get_squiggle_data("tips")
head(tips)  
#>   ateamid          source         venue confidence              tip correct
#> 1      14        Squiggle        M.C.G.       50.0         Richmond       1
#> 2      14  Figuring Footy        M.C.G.       58.0         Richmond       1
#> 3      14 Matter of Stats        M.C.G.       56.7          Carlton       0
#> 4      18 Matter of Stats        M.C.G.       62.7 Western Bulldogs       1
#> 5      18        Squiggle        M.C.G.       62.0 Western Bulldogs       1
#> 6       9        Squiggle Adelaide Oval       50.0         Adelaide       1
#>     err hconfidence                  ateam       hteam round sourceid margin
#> 1 42.00        50.0               Richmond     Carlton     1        1   1.00
#> 2    NA        42.0               Richmond     Carlton     1        3     NA
#> 3 48.39        56.7               Richmond     Carlton     1        4   5.39
#> 4  3.69        37.3       Western Bulldogs Collingwood     1        4  10.31
#> 5  3.00        38.0       Western Bulldogs Collingwood     1        1  17.00
#> 6 53.00        50.0 Greater Western Sydney    Adelaide     1        1   3.00
#>               updated    bits                date hteamid year tipteamid gameid
#> 1 2017-07-11 13:59:46  0.0000 2017-03-23 19:20:00       3 2017        14      1
#> 2 2017-04-10 12:18:02  0.2141 2017-03-23 19:20:00       3 2017        14      1
#> 3 2017-07-11 13:59:46 -0.2076 2017-03-23 19:20:00       3 2017         3      1
#> 4 2017-07-11 13:59:46  0.3265 2017-03-24 19:50:00       4 2017        18      2
#> 5 2017-07-11 13:59:46  0.3103 2017-03-24 19:50:00       4 2017        18      2
#> 6 2017-07-11 13:59:46  0.0000 2017-03-26 15:20:00       1 2017         1      8
# Get` just tips from round 1, 2018
tips_round <- get_squiggle_data("tips", round = 1, year = 2018)
head(tips_round)
#>        tip correct                source ateamid     venue confidence sourceid
#> 1 Adelaide       0              Squiggle       1 Docklands      56.00        1
#> 2 Adelaide       0               The Arc       1 Docklands      59.80        2
#> 3 Adelaide       0       Matter of Stats       1 Docklands      59.50        4
#> 4 Essendon       1               Punters       1 Docklands      52.08        5
#> 5 Adelaide       0 Footy Maths Institute       1 Docklands      66.00        6
#> 6 Adelaide       0            PlusSixOne       1 Docklands      55.16        7
#>   hconfidence   err    ateam round    hteam    bits                date margin
#> 1       44.00 23.00 Adelaide     1 Essendon -0.1844 2018-03-23 19:50:00  11.00
#> 2       40.20 21.00 Adelaide     1 Essendon -0.3147 2018-03-23 19:50:00   9.00
#> 3       40.50 21.78 Adelaide     1 Essendon -0.3040 2018-03-23 19:50:00   9.78
#> 4       52.08    NA Adelaide     1 Essendon  0.0588 2018-03-23 19:50:00     NA
#> 5       34.00 33.00 Adelaide     1 Essendon -0.5564 2018-03-23 19:50:00  21.00
#> 6       44.84 20.00 Adelaide     1 Essendon -0.1571 2018-03-23 19:50:00   8.00
#>               updated tipteamid gameid hteamid year
#> 1 2018-03-23 22:54:38         1    373       5 2018
#> 2 2018-03-23 22:54:38         1    373       5 2018
#> 3 2018-03-23 22:54:38         1    373       5 2018
#> 4 2018-03-23 22:54:38         5    373       5 2018
#> 5 2018-03-23 22:54:38         1    373       5 2018
#> 6 2018-03-23 22:54:38         1    373       5 2018

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>