class: center, top, inverse background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/WhittenOval.jpg) background-size: cover # Using R to Analyse, Visualise and Report on Team-Sport Athlete Data ## Alice Sweeting, PhD .large[Workshop - WCSF | Friday 7th June 2019]<br><br><br> --- class: center, left # A little about me! I am a Research Fellow at Victoria University within the Institute for Health and Sport. -- My role is embedded at the Western Bulldogs Football Club (AFL). -- I mainly program in R and use it daily to analyse, visualise and report on athlete/ team performance data. -- Work aside, I love being outdoors, running, and baking. -- I also have a miniature dachshund puppy named Dudley! `#RDogLadies` <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/DudleyAndI.jpg" width="35%" /> --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFLW_WhittenOval.JPG) background-size: cover # What programs do you use to analyse athlete data? --- class: center, top # Advantages of Excel .left[ * Easy to download, install, open. ] -- .left[ * Create a worksheet/ workbook and type away! ] -- .left[ * Interactive, (fairly) friendly to save, send, open and edit ] -- .left[ * Point and click to enter data, create figures, calculate math ] -- .left[ * Many tabs = data entry, analysis, visualisation, (dashboard) reporting ] -- .left[ * Can even run (basic to advanced) statistics! ] -- .left[ * Help is never far away... ] <img src="https://media.giphy.com/media/eCz0zwSl7qxB6/giphy.gif" width="45%"/> --- class: center, top # But before long... <img src="https://media.giphy.com/media/l1IY5NRhxdCJYxsmA/giphy.gif" align="middle" width="95%"/> --- class: center, top # We end up here! <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/ExcelStoppedWorking.png" align="middle" width="85%"/> .footnote[ Credit: [**Dr Jacquie Tran, ESSA Forum, 2019**](https://github.com/jacquietran/2019_essa_forum) ] --- class: center, top # But do not despair... <img src="https://media.giphy.com/media/q7OOPXp9llxWE/giphy.gif" align="middle" width="85%"/> --- class: center, top # For there are better (reproducible) ways! .left[ * Type a line of code, run and see output ] -- .left[ * Create a project, run analysis, create a dashboard and/ or report ] -- .left[ * Reproducibility = Create a GitHub repo, clone, commit, push, push ] -- .left[ * Hard to manipulate raw data ] -- .left[ * Automate reporting processes in .pdf or .html output ] --- class: center, top # For there R better (reproducible) ways! .left[ * Type a line of code, run and see output ] .left[ * Create a project, run analysis, create a dashboard and/ or report ] .left[ * Reproducibility = Create a GitHub repo, clone, commit, push, push ] .left[ * Hard to manipulate raw data ] .left[ * Automate reporting processes in .pdf or .html output ] <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/RLogo.jpg" width="50%"/> --- class: center, top # Today - a (quick) overview on using R <img src="https://media.giphy.com/media/MOTkF7sT8u2Z2/giphy.gif" align="middle" width="85%"/> --- class: center, top # What learning to use R feels like... <img src="https://media.giphy.com/media/7NoNw4pMNTvgc/giphy.gif" align="middle" width="65%"/> --- class: center, top # What others using R looks like... <img src="https://media.giphy.com/media/A06UFEx8jxEwU/giphy.gif" align="middle" width="85%" /> --- class: center, top # But it isn't that daunting! <img src="https://media.giphy.com/media/4BcZozfM0Dxa8/giphy.gif" align="middle" width="85%" /> --- class: center, top # Starting out in R .left[ * Highly recommended using [RStudio IDE](https://www.rstudio.com/products/rstudio/), as it is a friendly interface. ] -- .left[ * Need to install [R](https://www.r-project.org/) (required) before installing RStudio. ] -- .left[ * Plenty of online resources to help learn, including [R Tutorial](http://www.r-tutor.com/r-introduction/data-frame). ] -- .left[ * Start out with a small dataset, with analysis that you want to replicate. ] -- .left[ * When in doubt, visit [Stack Overflow!](https://stackoverflow.com/)<br><br> ] <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/RForDataScience.png" align="middle" width="85%"/> .footnote[ Image Credit: [Hadley Wickham, **R For Data Science**](https://r4ds.had.co.nz/introduction.html) ] --- class: center, top # Creating A Project in RStudio .left[ **Using RStudio to Your Advantage** ] .left[ * Create a project and name the folder appropriately ] -- .left[ * In RStudio, click `File -> New Project -> New Directory -> New Project` ] -- .left[ * Using this approach will contain all your files in one location ] -- .left[ * Avoids having multiple .R and .RData files scattered on Dropbox! ] -- .left[ * Working directory will fire when the project is opened ] <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/CreateNewProject.png" width="55%"/> --- class: center, top # Creating A Project in RStudio .left[ **Be A Janitor, not a Ninja!** * Store .R files in a Scripts folder, figures in Figures folder etc<br> * Aim for reproducibility and always comment your code! ]<br><br> <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/CodeJanitor.png" width="85%"/> --- class: center, top # Example of This Project in RStudio <img src="https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/CleanProject.png" width="65%"/> --- background-image: url(https://www.tidyverse.org/images/tidyverse-default.png) background-size: cover --- class: center, top # The Tidyverse .left[ Install the complete package collection with: ```r install.packages("tidyverse") ``` ] <img src="https://lsru.github.io/tv_course/img/01_tidyverse_data_science.png" width="70%"/> .footnote[ Image Credit: [University of Luxemborg, **Life Sciences Research Unit**](https://lsru.github.io/tv_course/lecture01_intro.html#1) ] --- class: center, top # Importing Data .left[ **Importing a .csv file into the Global Environment** ```r RawData <- read.csv("C:/Users/AJSweeting/Desktop/RawData.csv") ``` ] <br> -- .left[ **Importing a .xlsx file into the Global Environment** ```r library(readxl) RawData <- read_excel(("C:/Users/AJSweeting/Desktop/RawData.xlsx"), sheet = "Tab 1") ``` ] <br> -- .left[ **Importing a .csv file from a project folder** ```r RawData <- read_excel(("data/RawGroinBarData.csv"), sheet = "Tab 1") ``` ] --- class: left, top # Importing Data from Smartabase First, install and load required packages ```r library(rvest) library(plyr) library(dplyr) library(qdap) ``` -- Then, connect to an existing form on Smartabase (can be "live" or historical data) ```r WellnessDataURL <- html_session("https://username:password@my2.smartabase.com/yourteamsname/live?report=WellnessData&updategroup=true") ``` -- ```r # Read in data WellnessData <- read_html(WellnessDataURL) # Identify the table WellnessDataTable <- html_nodes(WellnessData, "table") # Collect only table data WellnessDataTable1 <- html_table(WellnessDataTable[1], fill = TRUE) ``` -- ```r # Make data.frame HistoricalWellnessData <- as.data.frame(WellnessDataTable1) ``` --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFLW2_WhittenOval.jpg) background-size: cover # Analysing Athlete Data - Working Example --- class: center, top # Setting up the R Environment .left[ Download R and RStudio. Create a new project in RStudio: `File -> New Project -> New Directory -> New Project (WCSF Workshop)`. Create a folder within this project (can be manual) named "Data". ] -- .left[ Create a folder within this project (can be manual) named "Data". ] -- .left[ Visit [http://bit.ly/WCSF_WorkshopInR](http://bit.ly/WCSF_WorkshopInR) to download the required workshop materials. ] -- .left[ Ensure the packages below are up-to-date and installed: ```r WCSFWorkshopPackages <- c( 'readr', 'tidyverse', 'qicharts2', 'ggridges', 'kableExtra', 'here', 'usethis') install.packages(WCSFWorkshopPackages) ``` ]<br> --- class: center, top # Example Data .left[ Import example data, using the `here` package, which helps locate a specific file within your project ```r library(here) library(readr) RawGroinBarData <- read_csv( here("Data/RawGroinBarData.csv"), col_names = TRUE, col_types = NULL) ``` ] -- .left[ Example data is hip adduction strength force (N), on athletes over time, assessed via [GroinBar](https://www.valdperformance.com/groinbar-hip-strength-testing-system/)<br><br> ] <img src="https://47e0bue9c3g1vuv0432ksik1-wpengine.netdna-ssl.com/wp-content/uploads/2017/09/GroinBar-Adjustment2.png" width="50%"/> .footnote[ Image Credit: [Vald Performance**](https://www.valdperformance.com/welcome-to-vald-performance/) ] --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFLW2_WhittenOval.jpg) background-size: cover # First Step - How to Assess Example Data? --- class: center, top # Assessing Example Data .left[ ```r # Assess the structure of the data.frame str(RawGroinBarData) ``` ``` ## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 249 obs. of 4 variables: ## $ Date : chr "4/12/2017" "7/12/2017" "9/12/2017" "9/12/2017" ... ## $ Athlete : chr "Gus Smith" "Gus Smith" "Gus Smith" "Gus Smith" ... ## $ X45.Degrees.Adduction..LEFT. : num 294 310 349 433 303 474 345 325 358 366 ... ## $ X45.Degrees.Adduction..RIGHT.: num 297 315 360 412 307 457 354 336 395 373 ... ## - attr(*, "spec")= ## .. cols( ## .. Date = col_character(), ## .. Athlete = col_character(), ## .. X45.Degrees.Adduction..LEFT. = col_double(), ## .. X45.Degrees.Adduction..RIGHT. = col_double() ## .. ) ``` ] --- class: center, top # Cleaning Example Data .left[ ```r # Ensure example data is a data.frame RawGroinBarData <- as.data.frame(RawGroinBarData) ``` ] -- .left[ ```r # View the top four rows head(RawGroinBarData, 5) ``` ``` ## Date Athlete X45.Degrees.Adduction..LEFT. ## 1 4/12/2017 Gus Smith 294 ## 2 7/12/2017 Gus Smith 310 ## 3 9/12/2017 Gus Smith 349 ## 4 9/12/2017 Gus Smith 433 ## 5 11/12/2017 Gus Smith 303 ## X45.Degrees.Adduction..RIGHT. ## 1 297 ## 2 315 ## 3 360 ## 4 412 ## 5 307 ``` ] -- .left[ ```r # Rename columns - make them tidy colnames(RawGroinBarData) <- c("Date", "Athlete", "Left", "Right") ``` ] --- class: center, top # From Wide to Long Format .left[ ```r # Move from many columns to many rows library(tidyr) GroinBarData_Long <- gather(RawGroinBarData, Limb, Force, Left:Right) ``` ] -- .left[ ```r # Inspect data.frame now head(GroinBarData_Long, 10) ``` ``` ## Date Athlete Limb Force ## 1 4/12/2017 Gus Smith Left 294 ## 2 7/12/2017 Gus Smith Left 310 ## 3 9/12/2017 Gus Smith Left 349 ## 4 9/12/2017 Gus Smith Left 433 ## 5 11/12/2017 Gus Smith Left 303 ## 6 11/12/2017 Gus Smith Left 474 ## 7 14/12/2017 Gus Smith Left 345 ## 8 16/12/2017 Gus Smith Left 325 ## 9 18/12/2017 Gus Smith Left 358 ## 10 9/01/2018 Gus Smith Left 366 ``` ] --- class: center, top # From Wide to Long Format .left[ ```r # Move from many columns to many rows library(tidyr) GroinBarData_Long <- gather(RawGroinBarData, Limb, Force, Left:Right) ``` ] .left[ ```r # Inspect data.frame now tail(GroinBarData_Long, 10) ``` ``` ## Date Athlete Limb Force ## 489 24/05/2018 Hudson Lee Right 422 ## 490 29/05/2018 Hudson Lee Right 452 ## 491 31/05/2018 Hudson Lee Right 440 ## 492 6/06/2018 Hudson Lee Right 428 ## 493 11/06/2018 Hudson Lee Right 440 ## 494 13/06/2018 Hudson Lee Right 416 ## 495 19/06/2018 Hudson Lee Right 426 ## 496 21/06/2018 Hudson Lee Right 449 ## 497 3/07/2018 Hudson Lee Right 422 ## 498 5/07/2018 Hudson Lee Right 420 ``` ] --- class: center, top # Visual Inspection of Example Data .left[ ```r # Create a plot of force over time library(ggplot2) ggplot(GroinBarData_Long, aes(x = Date, y = Force)) + geom_point() ``` ] -- <img src="WCSFWorkshopSlides_files/figure-html/Plot2-1.png" width="720" /> --- class: center, top # (Better) Visual Inspection of Example Data .left[ ```r # Create many plots, by athlete and limb, over time library(ggplot2) ggplot(GroinBarData_Long, aes(x = Date, y = Force, colour = Limb)) + geom_point() + ylab("\n Max Force (N)") + ggtitle("45 Degrees Adduction") + scale_y_continuous(expand = c(0, 0)) + facet_wrap(~Athlete) ``` ] -- <img src="WCSFWorkshopSlides_files/figure-html/Plot4-1.png" width="720" /> --- class: center, top # (Better) Visual Inspection of Example Data .left[ ```r # Add a time stamp column GroinBarData_Long$TimeStamp <- as.POSIXct(paste(GroinBarData_Long$Date), format="%d/%m/%Y") # Create many plots, by athlete and limb, over time library(ggplot2) ggplot(GroinBarData_Long, aes(x = TimeStamp, y = Force, colour = Limb)) + geom_point() + ggtitle("45 Degrees Adduction \n") + ylab("Max Force (N) \n") + scale_x_datetime(date_labels = "%b") + scale_y_continuous(expand = c(0, 0)) + theme_classic() + theme(axis.line.x = element_line(size = .5, colour = "black"), axis.line.y = element_line(size = .5, colour = "black"), axis.ticks.x = element_line(size = .5, colour = "black"), axis.ticks.y = element_line(size = .5, colour = "black"), axis.text.x = element_text(size = 10, colour = "black", angle = 35, hjust = 1, vjust = 1.15), axis.text.y = element_text(size = 10, colour = "black"), axis.title.x = element_blank(), axis.title.y = element_text(face = "bold", size = 12, colour = "black"), legend.position = "bottom", plot.title = element_text(face = "bold", size = 14, colour = "black", hjust = 0.5)) + facet_wrap(~Athlete) ``` ] --- class: center, top # (Better) Visual Inspection of Example Data <img src="WCSFWorkshopSlides_files/figure-html/Plot6-1.png" width="792" /> --- class: center, top # (Sparking) Joy Plots of Example Data .left[ ```r library(ggridges) ggplot(GroinBarData_Long, aes(x = Force, y = Athlete)) + geom_density_ridges(scale = 3) + ggtitle("45 Degrees Adduction \n") + ylab("Athlete \n") + xlab("\n Force (N)") + theme_classic() + theme(axis.line.x = element_line(size = .5, colour = "black"), axis.line.y = element_line(size = .5, colour = "black"), axis.ticks.x = element_line(size = .5, colour = "black"), axis.ticks.y = element_line(size = .5, colour = "black"), axis.text.x = element_text(size = 10, colour = "black"), axis.text.y = element_text(size = 10, colour = "black"), axis.title.x =element_text(face = "bold", size = 12, colour = "black"), axis.title.y = element_blank(), plot.title = element_text(face = "bold", size = 14, colour = "black", hjust = 0.5)) + facet_wrap(~Limb, scales = "free_x") ``` ] --- class: center, top #(Sparking) Joy Plots of Example Data <img src="WCSFWorkshopSlides_files/figure-html/Plot8-1.png" width="792" /> --- class: center, top #(Sparking) Joy Plots of Example Data <img src="WCSFWorkshopSlides_files/figure-html/Plot10-1.png" width="792" /> --- class: center, top #(Sparking) Joy Plots of Example Data <img src="WCSFWorkshopSlides_files/figure-html/Plot11-1.png" width="792" /> --- class: center, top #(Sparking) Joy Plots of Example Data <img src="WCSFWorkshopSlides_files/figure-html/Plot12-1.png" width="792" /> --- class: center, top #(Sparking) Joy Plots of Example Data .left[ ```r # Colourblind palette: cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") # Load required package library(ggridges) # Spark joy! ggplot(GroinBarData_Long, aes(x = Force, y = Athlete, fill = Athlete)) + stat_density_ridges(scale = 3, quantile_lines = TRUE, quantiles = 2) + scale_fill_cyclical(values = cbPalette) + ggtitle("45 Degrees Adduction \n") + ylab("Athlete \n") + xlab("\n Force (N)") + theme_classic() + theme(axis.line.x = element_line(size = .5, colour = "black"), axis.line.y = element_line(size = .5, colour = "black"), axis.ticks.x = element_line(size = .5, colour = "black"), axis.ticks.y = element_line(size = .5, colour = "black"), axis.text.x = element_text(size = 10, colour = "black"), axis.text.y = element_text(size = 10, colour = "black"), axis.title.x =element_text(face = "bold", size = 12, colour = "black"), axis.title.y = element_blank(), plot.title = element_text(face = "bold", size = 14, colour = "black", hjust = 0.5)) + facet_wrap(~Limb, scales = "free_x") ``` ] --- class: center, top # Calculating Summary Statistics .left[ ```r # Make limb a factor and calculate summary stats, by limb library(plyr) library(dplyr) GroinBarData_Long$Limb <- as.factor(GroinBarData_Long$Limb) SummaryStats <- GroinBarData_Long %>% group_by(Limb) %>% summarise(Mean = mean(Force), SD = sd(Force)) %>% mutate_if(is.numeric, ~round(., 0)) ``` ] <br> -- .left[ ```r # Make limb a factor and calculate summary stats, by limb and athlete GroinBarData_Long$Limb <- as.factor(GroinBarData_Long$Limb) SummaryStats <- GroinBarData_Long %>% group_by(Limb, Athlete) %>% summarise(Mean = mean(Force), SD = sd(Force)) %>% mutate_if(is.numeric, ~round(., 0)) ``` ] --- background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFLWPhoto3.JPG) background-size: cover class: inverse, center, top # But What About Individual Change Over Time? --- class: center, top # Statistical Process Control .left[ * Quantitative approach to monitoring and detecing change in a process, over time ] -- .left[ * Widely tilised in manufacturing and software development ] -- .left[ * Utilises control charts to demonstrate a measure (y axis) over time (x axis) ] -- .left[ * Centre line represents (weighted) mean or median ] -- .left[ * Two horizontal lines that represent the boundaries of common cause variation ] -- .left[ .bold[Common cause variation]: Present in any process/ system, often called random variation (noise) ] -- .left[ .bold[Special cause variation]: Sometimes present, makes process unpredictable, often called non-random variation (signal) ] --- # Statistical Process Control - Example Data ```r # Load required package library(qicharts2) # Subset to include only one athlete's data HudsonData <- subset(GroinBarData_Long, Athlete=="Hudson Lee") ``` -- ```r # Create Control Chart ControlChart <- qic(Force, data = HudsonData, chart = 'i', title = NULL, xlab = NULL, x = TimeStamp, ncol = 1, show.labels = TRUE, point.size = 3, scales = "free_y", facets = ~ Limb) ``` --- class: center, top # Statistical Process Control - Example Data <img src="WCSFWorkshopSlides_files/figure-html/ControlChart-1.png" width="792" /> --- class: center, top # Statistical Process Control - Example Data <img src="WCSFWorkshopSlides_files/figure-html/ControlChart2-1.png" width="792" /> --- class: center, top # Statistical Process Control - Example Data <img src="WCSFWorkshopSlides_files/figure-html/ControlChartDiffData-1.png" width="792" /> --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFL_WCE.JPG) background-size: cover # Example 2 - Player Tracking Data --- class: center, top # Working with Example Tracking .left[ ```r # Load packages library(readr) library(here) # Import raw tracking data RawTrackingData <- read_csv( here("Data/RawTrackingData.csv"), col_names = TRUE, col_types = NULL) ``` ] -- .left[ ```r # Make data.frame RawTrackingData <- as.data.frame(RawTrackingData) # Assess structure str(RawTrackingData) ``` ] --- class: center, top # Assess Tracking Data Structure .left[ ```r # Make data.frame RawTrackingData <- as.data.frame(RawTrackingData) # Assess structure str(RawTrackingData, list.len = 19) ``` ``` ## 'data.frame': 150105 obs. of 71 variables: ## $ Period : num 1 1 1 1 1 1 1 1 1 1 ... ## $ Frame : num 1 2 3 4 5 6 7 8 9 10 ... ## $ Time : num 0.04 0.08 0.12 0.16 0.2 0.24 0.28 0.32 0.36 0.4 ... ## $ TeamA_Player1_X : num 0.0265 0.0265 0.0265 0.0265 0.0265 ... ## $ TeamA_Player1_Y : num 0.507 0.507 0.507 0.507 0.507 ... ## $ TeamA_Player2_X : num -0.0436 -0.0436 -0.0436 -0.0446 -0.0456 ... ## $ TeamA_Player2_Y : num -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 ... ## $ TeamA_Player3_X : num -0.0436 -0.0436 -0.0436 -0.0446 -0.0456 ... ## $ TeamA_Player3_Y : num -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 ... ## $ TeamA_Player4_X : num -0.0436 -0.0436 -0.0436 -0.0446 -0.0456 ... ## $ TeamA_Player4_Y : num -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05 ... ## $ TeamA_Player5_X : num 0.314 0.314 0.314 0.314 0.314 ... ## $ TeamA_Player5_Y : num 0.268 0.268 0.268 0.268 0.268 ... ## $ TeamA_Player6_X : num 0.436 0.436 0.436 0.436 0.436 ... ## $ TeamA_Player6_Y : num 0.647 0.647 0.647 0.658 0.659 ... ## $ TeamA_Player7_X : num 0.371 0.371 0.371 0.371 0.372 ... ## $ TeamA_Player7_Y : num 0.525 0.525 0.525 0.525 0.525 ... ## $ TeamA_Player8_X : num 0.421 0.421 0.421 0.421 0.421 ... ## $ TeamA_Player8_Y : num 0.499 0.499 0.499 0.499 0.499 ... ## [list output truncated] ## - attr(*, "spec")= ## .. cols( ## .. Period = col_double(), ## .. Frame = col_double(), ## .. Time = col_double(), ## .. TeamA_Player1_X = col_double(), ## .. TeamA_Player1_Y = col_double(), ## .. TeamA_Player2_X = col_double(), ## .. TeamA_Player2_Y = col_double(), ## .. TeamA_Player3_X = col_double(), ## .. TeamA_Player3_Y = col_double(), ## .. TeamA_Player4_X = col_double(), ## .. TeamA_Player4_Y = col_double(), ## .. TeamA_Player5_X = col_double(), ## .. TeamA_Player5_Y = col_double(), ## .. TeamA_Player6_X = col_double(), ## .. TeamA_Player6_Y = col_double(), ## .. TeamA_Player7_X = col_double(), ## .. TeamA_Player7_Y = col_double(), ## .. TeamA_Player8_X = col_double(), ## .. TeamA_Player8_Y = col_double(), ## .. TeamA_Player9_X = col_double(), ## .. TeamA_Player9_Y = col_double(), ## .. TeamA_Player10_X = col_double(), ## .. TeamA_Player10_Y = col_double(), ## .. TeamA_Player11_X = col_double(), ## .. TeamA_Player11_Y = col_double(), ## .. TeamA_Player12_X = col_double(), ## .. TeamA_Player12_Y = col_double(), ## .. TeamA_Player13_X = col_double(), ## .. TeamA_Player13_Y = col_double(), ## .. Ball_X = col_double(), ## .. Ball_Y = col_double(), ## .. TeamB_Player1_X = col_double(), ## .. TeamB_Player1_Y = col_double(), ## .. TeamB_Player2_X = col_double(), ## .. TeamB_Player2_Y = col_double(), ## .. TeamB_Player3_X = col_double(), ## .. TeamB_Player3_Y = col_double(), ## .. TeamB_Player4_X = col_double(), ## .. TeamB_Player4_Y = col_double(), ## .. TeamB_Player5_X = col_double(), ## .. TeamB_Player5_Y = col_double(), ## .. TeamB_Player6_X = col_double(), ## .. TeamB_Player6_Y = col_double(), ## .. TeamB_Player7_X = col_double(), ## .. TeamB_Player7_Y = col_double(), ## .. TeamB_Player8_X = col_double(), ## .. TeamB_Player8_Y = col_double(), ## .. TeamB_Player9_X = col_double(), ## .. TeamB_Player9_Y = col_double(), ## .. TeamB_Player10_X = col_double(), ## .. TeamB_Player10_Y = col_double(), ## .. TeamB_Player11_X = col_double(), ## .. TeamB_Player11_Y = col_double(), ## .. TeamB_Player12_X = col_double(), ## .. TeamB_Player12_Y = col_double(), ## .. TeamB_Player13_X = col_double(), ## .. TeamB_Player13_Y = col_double(), ## .. TeamB_Player14_X = col_double(), ## .. TeamB_Player14_Y = col_double(), ## .. Team = col_character(), ## .. Type = col_character(), ## .. Subtype = col_character(), ## .. Period.y = col_double(), ## .. StartFrame = col_double(), ## .. `StartTime[s]` = col_double(), ## .. EndFrame = col_double(), ## .. `EndTime[s]` = col_double(), ## .. StartX = col_double(), ## .. StartY = col_double(), ## .. EndX = col_double(), ## .. EndY = col_double() ## .. ) ``` ] --- class: center, top # Visualising Tracking Data <img src="WCSFWorkshopSlides_files/figure-html/TrackingDataPlot2-1.png" width="792" /> --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFL_Tackle.JPG) background-size: cover # What is Missing? --- class: center, top # Analysing An Athlete's Data .left[ ```r # Make new data.frame, create new column ExampleAthleteData <- RawTrackingData %>% select(Period, Frame, Time, TeamB_Player5_X, TeamB_Player5_Y) %>% mutate(Half = case_when(Period <= 1 ~ "First Half", Period >= 2 ~ "Second Half")) ``` ] -- .left[ ```r # Rename columns colnames(ExampleAthleteData) <- c("Period", "Frame", "Time", "X", "Y", "Half") # Inspect head(ExampleAthleteData, 10) ``` ``` ## Period Frame Time X Y Half ## 1 1 1 0.04 0.6595 0.70651 First Half ## 2 1 2 0.08 0.6595 0.70651 First Half ## 3 1 3 0.12 0.6595 0.70651 First Half ## 4 1 4 0.16 0.6595 0.70651 First Half ## 5 1 5 0.20 0.6595 0.70651 First Half ## 6 1 6 0.24 0.6595 0.70651 First Half ## 7 1 7 0.28 0.6595 0.70651 First Half ## 8 1 8 0.32 0.6595 0.70651 First Half ## 9 1 9 0.36 0.6595 0.70651 First Half ## 10 1 10 0.40 0.6595 0.70651 First Half ``` ] --- class: center, top # Analysing An Athlete's Data .left[ ```r # Calculate distance for each 'Sample' across the different data acquisitions DisplacementData <- ExampleAthleteData %>% arrange(Frame, Half) %>% group_by(Half) %>% mutate( lagX = lag(X, order_by = Frame), lagY = lag(Y, order_by = Frame)) %>% rowwise() %>% mutate(Distance = dist( matrix( c(X,Y,lagX,lagY),nrow=2,byrow=TRUE) )) %>% select(-lagX, -lagY) # Make Half a factor DisplacementData$Half <- as.factor(DisplacementData$Half) # Inspect head(DisplacementData, 5) ``` ``` ## Source: local data frame [5 x 7] ## Groups: <by row> ## ## # A tibble: 5 x 7 ## Period Frame Time X Y Half Distance ## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <dbl> ## 1 1 1 0.04 0.660 0.707 First Half 1.66 ## 2 1 2 0.08 0.660 0.707 First Half 1.66 ## 3 1 3 0.12 0.660 0.707 First Half 1.66 ## 4 1 4 0.16 0.660 0.707 First Half 1.66 ## 5 1 5 0.2 0.660 0.707 First Half 1.66 ``` ] --- class: center, top # Analysing An Athlete's Data .left[ ```r # Set the SampleRate SampleRate <- 25 k <- as.integer(SampleRate) # Calculate Velocity VelocityData <- DisplacementData %>% arrange(Frame, Half) %>% group_by(Half) %>% mutate(Velocity = sqrt( (X - lag(X) )^2 + (Y - lag(Y) )^2 ) * SampleRate) # Inspect head(VelocityData, 5) ``` ``` ## # A tibble: 5 x 8 ## # Groups: Half [1] ## Period Frame Time X Y Half Distance Velocity ## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <dbl> <dbl> ## 1 1 1 0.04 0.660 0.707 First Half 1.66 NA ## 2 1 2 0.08 0.660 0.707 First Half 1.66 0 ## 3 1 3 0.12 0.660 0.707 First Half 1.66 0 ## 4 1 4 0.16 0.660 0.707 First Half 1.66 0 ## 5 1 5 0.2 0.660 0.707 First Half 1.66 0 ``` ] --- class: center, top # Visualising An Athlete's Data <img src="WCSFWorkshopSlides_files/figure-html/CalcVelocityExampleAthlete2-1.png" width="792" /> --- class: center, top # Visualising An Athlete's Data <img src="WCSFWorkshopSlides_files/figure-html/CalcVelocityExampleAthlete3-1.png" width="792" /> --- class: inverse, center, top background-image: url(https://raw.githubusercontent.com/SportStatisticsRSweet/WCSF_WorkshopInR/master/Figures/AFL_Hunter.JPG) background-size: cover # Over To You! --- class: center, top # Resources on Creating in R Markdown "Working with Data in Elite Sport" [RLadies Auckland 2019 talk by Dr Jacquie Tran](https://jacquietran.github.io/2019_may_rladies_akl/R/#1). "Making Slides in R Markdown" [RStudio Conf 2019 presentation by Alison Hill](https://arm.rbind.io/slides/xaringan.html#1). "Reproducibility in R" [RLadies Melbourne 2019 talk by Lavinia Gordon](https://mrslaviniag.github.io/xaringan_presentations/rladies_presentation_22_May_2019.html#1). "Presentation Ninja with xaringan" [Yihui Xie](https://slides.yihui.name/xaringan/#1).<br><br> Install the xaringan package from Github: ```r devtools::install_github("yihui/xaringan") ``` <img src="https://user-images.githubusercontent.com/163582/45438104-ea200600-b67b-11e8-80fa-d9f2a99a03b0.png" align="bottom" width="25%"/> --- class: center, middle # Happy Coding in R!
[SportStatisticsRSweet](http://sportstatisticsrsweet.rbind.io/)<br><br>
[Alice.Sweeting@vu.edu.au](mailto:Alice.Sweeting@vu.edu.au)<br><br>
[alicesweeting](https://twitter.com/alicesweeting)<br><br>
[SportStatisticsRSweet](https://github.com/SportStatisticsRSweet)<br><br> <img src="https://media.giphy.com/media/mCRJDo24UvJMA/giphy.gif" width="50%"/>