README.md

statcrewRC

statcrewRC is an R package designed for the Roanoke College Stat Crew to assist in the analysis of game data. It includes a variety of functions intended to make analysis easier, keep students from having to deal with coding monstrosities, and keep a consistent style for graphics.

This package is a work in progress. Most functions as of version 0.2.0 deal with basketball.

Installation

statcrewRC is not on CRAN, so you must install it through the GitHub repository.

If you do not have the devtools package installed, you will have to run the first line in the code below as well.

# install.packages('devtools')
devtools::install_github('mistermichaelll/statcrewRC')

Using the Functions

dist_transform

  1. dist_transform: returns a vector of distance values from xy coordinates. It is usable for any of the sports that the Stat Crew tracks (basketball, lacrosse, soccer). This vector should be assigned to a new column in the original dataframe.

Arguments: - data: a dataframe object - sport_type: character argument which can be one of bball, lax, or soccer - x_col: column which contains x coordinates - y_col: column which contains y coordinates.

Example of use:

# base R
# -----------------
library(statcrewRC)
game_data_df$distance <- dist_transform(game_data_df, "bball", x_coordinates, y_coordinates)

# tidyverse
# -----------------
library(statcrewRC)
library(dplyr)
game_data_df <- 
      game_data_df %>%
      mutate(distance = dist_transform(game_data_df, "bball", x_coordinates, y_coordinates)

get_bb_shot_class

  1. get_bb_shot_class: returns a vector of basketball shot classes. Like the dist_transform function, this vector should be assigned to a new column in the original dataframe.

Arguments: - data: a dataframe object - x_col: column of x coordinates - y_col: column of y coordinates

Example of use:

# base R
# -----------------
library(statcrewRC)
game_data_df$shot_class <- get_bb_shot_class(game_data_df, x_coordinates, y_coordinates)

# tidyverse
# -----------------
library(statcrewRC)
library(dplyr)
game_data_df <- 
      game_data_df %>%
      mutate(shot_class = get_bb_shot_class(game_data_df, x_coordinates, y_coordinates)

gg_glm

  1. gg_glm is to be used with the ggplot2 package which can be downloaded from CRAN. This function simply gathers the arguments needed to make a binomial regression line. Previous versions of this function accepted different arguments and created the entire plot. I found this to be poor form, so the plot must be built with a ggplot2 pipe now as demonstrated below.

Arguments: - point_size: size of points in the chart (default is 1.5) - line_size: size of line (default is .55) - line_color: color of the line (default is red)

Example of use:

# tidyverse
# -----------------
library(statcrewRC)
library(dplyr)
library(ggplot2)
game_data %>%
    ggplot()+
    aes(x = dist, y = madeMiss)+
    gg_glm(line_color = "blue", line_size = .75)+
    theme_minimal(base_size = 12)

Output:

theme_gg_court

  1. theme_gg_court is similar to the previous argument and is used in a ggplot2 pipe to remove extra graphical elements.

This function has no arguments.

Example of use:

library(statcrewRC)
library(dplyr)
library(ggplot2)

# use custom court image asset
# ----------------------------------------
courtIMG <- jpeg::readJPEG("assets/halfcourt.jpg") 
court <- grid::rasterGrob(courtIMG, width = unit(1, "npc"), height = unit(1, "npc"))

# plot data
# ----------------
game_data %>% 
    filter(team == "ROA") %>% 
    filter(shotClass != "FoulShot") %>%
    filter(madeMiss != "NA") %>% 
    ggplot()+
    annotation_custom(court)+
    aes(x = -x_one_side, y = -y_one_side)+
    geom_point(aes(col = factor(madeMiss)), alpha = .6, size = .8)+
    coord_flip()+
    theme_gg_court()+
    xlim(-500, 0)+
    ylim(-490, 0)+
    labs(title = "ROA")+
    scale_color_discrete(name = "",
                         labels = c("Miss", "Made"))

Output:

bb_get_one_side

  1. bb_get_one_side transforms xy coordinates to one side. This makes creating half-court graphics much easier, as all shots are now represented on the same side. The function returns a modified dataframe with two new columns: x_one_side and y_one_side

Arguments: - data: a dataframe - x_col: a column of x coordinates - y_col: a column of y coordinates

Example of use:

library(statcrewRC)
game_data <- bb_get_one_side(game_data, x, y)


mistermichaelll/statcrewRC documentation built on June 1, 2019, 3:57 a.m.