{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"suppressMessages({\n    library(dplyr)\n    library(ggplot2)\n    library(sf)\n    library(units)\n})","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:06:52.621367Z","iopub.execute_input":"2022-08-28T23:06:52.626777Z","iopub.status.idle":"2022-08-28T23:06:52.671626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the data\n\nLoading just the races tracking data.","metadata":{}},{"cell_type":"code","source":"tt <- read.csv(\"../input/big-data-derby-2022/nyra_tracking_table.csv\")\ntt$track_id <- factor(tt$track_id)\ntt$race_date <- as.Date(tt$race_date)\ntt$program_number <- factor(trimws(tt$program_number))","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-08-28T23:04:38.835028Z","iopub.execute_input":"2022-08-28T23:04:38.873540Z","iopub.status.idle":"2022-08-28T23:05:46.825705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tracks shape\n\nLearning about the tracks. See what is the shape of the tracks and races. No proper coordinates scaling is applied. The tracks look skewed. But that's ok for now. I am just interested in general shape.","metadata":{}},{"cell_type":"code","source":"options(repr.plot.width = 15, repr.plot.height = 10)\ntt %>% slice_sample(n=10000) %>% ggplot() + geom_point(aes(latitude,longitude)) + facet_wrap(~track_id, scales=\"free\")","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:46.827882Z","iopub.execute_input":"2022-08-28T23:05:46.829244Z","iopub.status.idle":"2022-08-28T23:05:48.179430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Horse speed","metadata":{}},{"cell_type":"markdown","source":"Using R `sf` package to calulate distance and speed. According to the competition information horse location is captured every 1/4 of a second.\n\nFirst transform the source data frame to `sf` format. Using coordinate reference system EPSG:4326.","metadata":{}},{"cell_type":"code","source":"tt_sf <- st_as_sf(x = tt, coords = c('longitude', 'latitude')) %>%  st_set_crs(\"EPSG:4326\")\nhead(tt_sf)","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:48.183186Z","iopub.execute_input":"2022-08-28T23:05:48.185641Z","iopub.status.idle":"2022-08-28T23:05:54.821441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Look at the actual numbers, see if they make sence","metadata":{}},{"cell_type":"code","source":"hspeed <- function(p1) st_distance(lag(p1), p1, by_element=TRUE) / 0.25\ntt_sf %>% filter(track_id=='SAR', race_date=='2019-07-27', race_number==3,program_number==\"6\") %>% \narrange(trakus_index) %>% mutate(speed=hspeed(geometry)) -> x","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:54.824902Z","iopub.execute_input":"2022-08-28T23:05:54.827081Z","iopub.status.idle":"2022-08-28T23:05:56.013458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note: as of Aug 2022 kaggle version of Jupyter fails to display data frame that is the result of previous call. Explicitly using `print()` call for now.","metadata":{}},{"cell_type":"code","source":"print(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:56.016242Z","iopub.execute_input":"2022-08-28T23:05:56.017954Z","iopub.status.idle":"2022-08-28T23:05:56.047868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"See summary of speed distribution in that one race","metadata":{}},{"cell_type":"code","source":"summary(x$speed)","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:56.050640Z","iopub.execute_input":"2022-08-28T23:05:56.052464Z","iopub.status.idle":"2022-08-28T23:05:56.071473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot each horse speed in one random race.","metadata":{}},{"cell_type":"code","source":"options(repr.plot.width = 15, repr.plot.height = 10)\ntt_sf %>% filter(track_id=='SAR', race_date=='2019-07-27', race_number==3) %>% \ngroup_by(program_number) %>% arrange(trakus_index) %>% mutate(speed=hspeed(geometry)) %>% na.omit() %>%\nggplot() + geom_line(aes(trakus_index,speed,col=program_number))","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:06:58.322072Z","iopub.execute_input":"2022-08-28T23:06:58.323650Z","iopub.status.idle":"2022-08-28T23:06:59.439728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Take a look at the trajectories of all horses during the same randomly selected race.","metadata":{}},{"cell_type":"code","source":"options(repr.plot.width = 15, repr.plot.height = 10)\ntt %>% filter(track_id=='SAR', race_date=='2019-07-27', race_number==3) %>% \narrange(trakus_index) %>% ggplot() + geom_path(aes(longitude,latitude,col=program_number))","metadata":{"execution":{"iopub.status.busy":"2022-08-28T23:05:56.659091Z","iopub.execute_input":"2022-08-28T23:05:56.660617Z","iopub.status.idle":"2022-08-28T23:05:58.222593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}