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.
You can install fitzRoy from github with:
The fitzRoy
package can be used to simply get data from various sources. Some minimal working examples are below.
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.
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)
library(dplyr)
library(ggplot2)
results <- get_match_results()
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 15609 2019-09-07 QF Richmond 18 4 112
#> 2 15610 2019-09-13 SF Geelong 13 10 88
#> 3 15611 2019-09-14 SF GWS 12 11 83
#> 4 15612 2019-09-20 PF Richmond 12 13 85
#> 5 15613 2019-09-21 PF GWS 8 8 56
#> # … 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 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>
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 = "2000-01-01", end_date = "2018-06-01")
#> Returning data from 2000-01-01 to 2018-06-01
#> Finished getting afltables data
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 "Per… 37575 Fremantle 3
#> 2 2018 10 2018-05-27 1440 "Per… 37575 Fremantle 3
#> 3 2018 10 2018-05-27 1440 "Per… 37575 Fremantle 3
#> 4 2018 10 2018-05-27 1440 "Per… 37575 Fremantle 3
#> 5 2018 10 2018-05-27 1440 "Per… 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>
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> <dbl> <chr> <chr> <chr>
#> 1 2019-03-21 19:25:00 2019 1 1 Carlton Richmond MCG
#> 2 2019-03-22 19:50:00 2019 1 1 Collingwo… Geelong MCG
#> 3 2019-03-23 13:45:00 2019 1 1 Melbourne Port Adel… MCG
#> 4 2019-03-23 16:05:00 2019 1 1 Adelaide Hawthorn Adela…
#> 5 2019-03-23 19:20:00 2019 1 1 Brisbane … West Coast Gabba
#> # … with 1 more row
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.
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.
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
dat <- get_footywire_stats(ids = 9927)
#> Getting data from https://www.footywire.com
#> Finished getting data
head(dat)
#> 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
#> 1 Home 9927 9 17 20 76.9 0 0 0 5 2 0 1 6 317
#> 2 Home 9927 12 11 19 86.4 1 2 2 2 3 0 3 8 372
#> 3 Home 9927 7 14 12 54.5 0 1 0 2 0 0 4 3 366
#> 4 Home 9927 3 18 20 90.9 1 0 0 5 0 0 0 5 570
#> 5 Home 9927 8 14 14 63.6 0 0 1 0 1 2 1 9 559
#> 6 Home 9927 11 9 17 81.0 0 1 0 2 0 2 4 6 268
#> TO ITC T5 TOG K HB D M G B T HO GA1 I50 CL CG R50 FF FA AF SC
#> 1 2 9 0 96 14 12 26 7 0 0 6 0 0 0 1 1 6 3 0 114 120
#> 2 5 0 1 86 11 11 22 4 4 0 1 0 2 4 3 4 0 2 2 91 137
#> 3 6 1 0 87 19 3 22 1 0 1 3 0 1 2 4 5 1 1 2 74 86
#> 4 2 5 0 89 17 5 22 12 0 0 3 0 0 2 0 1 5 0 1 106 102
#> 5 5 3 0 66 13 9 22 2 1 0 1 0 0 8 3 0 1 0 0 73 96
#> 6 3 4 1 78 9 12 21 2 0 0 6 0 1 5 6 3 3 2 1 80 98
Note - as of v0.2.0 this has been removed
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 url id
#> 1 Squiggle https://live.squiggle.com.au/ 1
#> 2 The Arc https://thearcfooty.com/ 2
#> 3 Figuring Footy http://figuringfooty.com/ 3
#> 4 Matter of Stats http://www.matterofstats.com/ 4
#> 5 Punters 5
#> 6 Footy Maths Institute https://footymaths.blogspot.com.au 6
# Get all tips
tips <- get_squiggle_data("tips")
head(tips)
#> margin updated year sourceid err hteamid gameid correct
#> 1 1.00 2017-07-11 13:59:46 2017 1 42.00 3 1 1
#> 2 NA 2017-04-10 12:18:02 2017 3 NA 3 1 1
#> 3 5.39 2017-07-11 13:59:46 2017 4 48.39 3 1 0
#> 4 10.31 2017-07-11 13:59:46 2017 4 3.69 4 2 1
#> 5 17.00 2017-07-11 13:59:46 2017 1 3.00 4 2 1
#> 6 3.00 2017-07-11 13:59:46 2017 1 53.00 1 8 1
#> date ateam bits venue
#> 1 2017-03-23 19:20:00 Richmond 0.0000 M.C.G.
#> 2 2017-03-23 19:20:00 Richmond 0.2141 M.C.G.
#> 3 2017-03-23 19:20:00 Richmond -0.2076 M.C.G.
#> 4 2017-03-24 19:50:00 Western Bulldogs 0.3265 M.C.G.
#> 5 2017-03-24 19:50:00 Western Bulldogs 0.3103 M.C.G.
#> 6 2017-03-26 15:20:00 Greater Western Sydney 0.0000 Adelaide Oval
#> source tipteamid round hteam tip hconfidence
#> 1 Squiggle 14 1 Carlton Richmond 50.0
#> 2 Figuring Footy 14 1 Carlton Richmond 42.0
#> 3 Matter of Stats 3 1 Carlton Carlton 56.7
#> 4 Matter of Stats 18 1 Collingwood Western Bulldogs 37.3
#> 5 Squiggle 18 1 Collingwood Western Bulldogs 38.0
#> 6 Squiggle 1 1 Adelaide Adelaide 50.0
#> confidence ateamid
#> 1 50.0 14
#> 2 58.0 14
#> 3 56.7 14
#> 4 62.7 18
#> 5 62.0 18
#> 6 50.0 9
# Get` just tips from round 1, 2018
tips <- get_squiggle_data("tips", round = 1, year = 2018)
head(tips)
#> tip ateamid hconfidence confidence bits ateam tipteamid round
#> 1 Adelaide 1 44.00 56.00 -0.1844 Adelaide 1 1
#> 2 Adelaide 1 40.20 59.80 -0.3147 Adelaide 1 1
#> 3 Adelaide 1 40.50 59.50 -0.3040 Adelaide 1 1
#> 4 Essendon 1 52.08 52.08 0.0588 Adelaide 5 1
#> 5 Adelaide 1 34.00 66.00 -0.5564 Adelaide 1 1
#> 6 Adelaide 1 44.84 55.16 -0.1571 Adelaide 1 1
#> hteam source venue updated correct
#> 1 Essendon Squiggle Docklands 2018-03-23 22:54:38 0
#> 2 Essendon The Arc Docklands 2018-03-23 22:54:38 0
#> 3 Essendon Matter of Stats Docklands 2018-03-23 22:54:38 0
#> 4 Essendon Punters Docklands 2018-03-23 22:54:38 1
#> 5 Essendon Footy Maths Institute Docklands 2018-03-23 22:54:38 0
#> 6 Essendon PlusSixOne Docklands 2018-03-23 22:54:38 0
#> date sourceid year hteamid gameid err margin
#> 1 2018-03-23 19:50:00 1 2018 5 373 23.00 11.00
#> 2 2018-03-23 19:50:00 2 2018 5 373 21.00 9.00
#> 3 2018-03-23 19:50:00 4 2018 5 373 21.78 9.78
#> 4 2018-03-23 19:50:00 5 2018 5 373 NA NA
#> 5 2018-03-23 19:50:00 6 2018 5 373 33.00 21.00
#> 6 2018-03-23 19:50:00 7 2018 5 373 20.00 8.00