{"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Load Libraries and Data","metadata":{}},{"cell_type":"code","source":"library(tidyverse)\nlibrary(rio)\nlibrary(ggthemes)\nlibrary(RColorBrewer)\n\nplayers <- import('../input/nfl-big-data-bowl-2022/players.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:38:29.446487Z","iopub.execute_input":"2021-11-29T03:38:29.449157Z","iopub.status.idle":"2021-11-29T03:38:29.487507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What colleges did our players come from?\nIt looks like the SEC, Big 10 and ACC schools dominate our players database. Here we can see the top 10 colleges by the number of players that appear in the data.","metadata":{}},{"cell_type":"code","source":"players %>%\n    group_by(collegeName) %>%\n    summarise(n = n()) %>%\n    arrange(-n) %>%\n    slice(1:10) %>%\n    ggplot(aes(x = n, y = reorder(collegeName, n), label = n, fill = collegeName)) + \n    geom_bar(stat = 'identity') + \n    geom_text(nudge_x = 3) + \n    labs(x = 'Total Players',\n        y = 'College',\n        title = 'Total Players in data by College Attended') + \n    theme_classic() + \n    theme(legend.position = 'none')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:43:13.182335Z","iopub.execute_input":"2021-11-29T03:43:13.184079Z","iopub.status.idle":"2021-11-29T03:43:13.436017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What does the breakdown by position look like?\nWe would like to see which positions are most commonly represented on special teams plays and try to squash out any weird positional values.","metadata":{}},{"cell_type":"code","source":"players %>% \n    group_by(Position) %>%\n    summarise(n = n()) %>%\n    ggplot(aes(x = n, y = reorder(Position, n), label = n)) + \n    geom_bar(stat = 'identity') + \n    geom_text(nudge_x = 10) + \n    labs(x = '', \n        y =  '',\n        title = 'Total Players per position') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:38:31.335473Z","iopub.execute_input":"2021-11-29T03:38:31.337565Z","iopub.status.idle":"2021-11-29T03:38:31.610648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like wide recievers and cornerbacks are the most commonly represented positions on special teams plays. We likely don't see a lot of kickers and punters since these are positions with little turnover from year to year and each team typically only carries one fo each on the roster at a time. \n\nWe have some values (OT, S, OG, and HB) which can probably be mapped to other corresponding values. Below is a cleaned up version of the distribution.","metadata":{}},{"cell_type":"code","source":"players$Position <- plyr::mapvalues(players$Position,\n                                    from = c('OT', 'S', 'OG', 'HB'),\n                                    to = c('T', 'FS', 'G', 'RB'))\n\nplayers %>% \n    group_by(Position) %>%\n    summarise(n = n()) %>%\n    ggplot(aes(x = n, y = reorder(Position, n), label = n)) + \n    geom_bar(stat = 'identity') + \n    geom_text(nudge_x = 10) + \n    labs(x = '', \n        y =  '',\n        title = 'Total Players per position') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:38:48.461659Z","iopub.execute_input":"2021-11-29T03:38:48.464528Z","iopub.status.idle":"2021-11-29T03:38:48.762695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What does the player height distribution look like?","metadata":{}},{"cell_type":"code","source":"players %>% \n    group_by(height) %>%\n    summarise(n = n()) %>%\n    ggplot(aes(x = height, y = n, label = n)) + \n    geom_bar(stat = 'identity') + \n    geom_text(nudge_y = 10) +\n    labs(x = 'Height', \n        y =  'Total Players',\n        title = 'Distribution of player heights') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:39:42.382828Z","iopub.execute_input":"2021-11-29T03:39:42.384579Z","iopub.status.idle":"2021-11-29T03:39:42.657475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are clearly some issues here but this looks fairly normally distributed. To fix this lets convert all heights to inches and confirm our suspicion of normally distributed hieghts.","metadata":{}},{"cell_type":"code","source":"players$height <- ifelse(grepl('-', players$height),\n                           as.numeric(sub('-.*', '', players$height)) * 12 + as.numeric(sub('.*-', '', players$height)),\n                           players$height)\n\nplayers %>% \n    group_by(height) %>%\n    summarise(n = n()) %>%\n    ggplot(aes(x = height, y = n, label = n)) + \n    geom_bar(stat = 'identity') + \n    geom_text(nudge_y = 10) +\n    labs(x = 'Height\\n(in inches)', \n        y =  'Total Players',\n        title = 'Distribution of player heights') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:45:28.638362Z","iopub.execute_input":"2021-11-29T03:45:28.640590Z","iopub.status.idle":"2021-11-29T03:45:28.929857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks pretty normal to me. Nice.","metadata":{}},{"cell_type":"markdown","source":"## What does the weight distribution look like?","metadata":{}},{"cell_type":"code","source":"players %>%\n    ggplot(aes(x = weight)) + \n    geom_density(kernel = 'epanechnikov') + \n    labs(x = 'Weight\\n(in pounds)', \n        y =  'Density',\n        title = 'Distribution of player weights') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:42:09.686363Z","iopub.execute_input":"2021-11-29T03:42:09.688031Z","iopub.status.idle":"2021-11-29T03:42:09.910602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Strangely this is not quite look as normal as height. It looks like we potentially have three local maxima. It is possible that weight is normal on a position to position basis, and the distribution of positions impacts the distribution of weights. \n\nLet's test this by looking at the distribution of weight across three different position groups on the defense. I have tested the weight distributions using cornerbacks, outside linebackers, and nose tackles. I expect all three of these positions to have normal weight distributions with different means.","metadata":{}},{"cell_type":"code","source":"players %>% \n    filter(Position %in% c('CB', 'OLB', 'NT')) %>%\n    ggplot(aes(x = weight, color = Position)) + \n    geom_density(kernel = 'epanechnikov', alpha = .8) + \n    scale_fill_manual(palette = 'Set3') +\n    labs(x = 'Weight\\n(in pounds)', \n        y =  'Density',\n        title = 'Distribution of player weights by position groups') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T03:41:59.835608Z","iopub.execute_input":"2021-11-29T03:41:59.837414Z","iopub.status.idle":"2021-11-29T03:42:00.216132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like the assumption was correct! Cornerbacks look like they are significantly lighter than outside linebackers, and outside linebackers lighter than nose tackles. All our positional groups seem to have normally distributed weights. Great!","metadata":{}},{"cell_type":"markdown","source":"## Does weight and height have a relationship for the players?\n\nWe would expect the height and weight to be positively correlated, and for those correlations to hold across positional groups.","metadata":{}},{"cell_type":"code","source":"players %>%\n    ggplot(aes(x = as.numeric(height), y = as.numeric(weight))) + \n    geom_jitter() + \n    geom_smooth(formula = y ~ x, method = loess) +\n    labs(x = 'Height\\n(in inches)', \n        y =  'Weight\\n(in pounds)',\n        title = 'Relationship between height and weight') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:09:11.096421Z","iopub.execute_input":"2021-11-29T04:09:11.098187Z","iopub.status.idle":"2021-11-29T04:09:11.792948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like we do see the expected positive relationship. The relationship looks like it is probably linear for most positonal groupings excpet for the heaviest (e.g. offensive and defensive linemen). It looks like there quite a bit of variation in height for those groups, but the relationship with weight is only weakly positive.","metadata":{}},{"cell_type":"code","source":"players %>%\n    filter(Position %in% c('CB', 'OLB', 'NT')) %>%\n    ggplot(aes(x = as.numeric(height), y = as.numeric(weight), color = Position)) + \n    geom_jitter() + \n    geom_smooth(formula = y ~ x, method = glm) +\n    labs(x = 'Height\\n(in inches)', \n        y =  'Weight\\n(in pounds)',\n        title = 'Relationship between height and weight by positional Group') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:09:38.529696Z","iopub.execute_input":"2021-11-29T04:09:38.531746Z","iopub.status.idle":"2021-11-29T04:09:38.985719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players %>%\n    filter(Position %in% c('CB', 'OLB', 'NT')) %>%\n    ggplot(aes(x = as.numeric(height), y = as.numeric(weight), color = Position)) + \n    geom_density2d() + \n    labs(x = 'Height\\n(in inches)', \n        y =  'Weight\\n(in pounds)',\n        title = 'Distribution of height and weight by positional group') +\n    theme_classic()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T04:10:09.406252Z","iopub.execute_input":"2021-11-29T04:10:09.407823Z","iopub.status.idle":"2021-11-29T04:10:10.509609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here I break out height and weight by offensive positional groupings. The results follow expectations, in that outside linebackers tend to be taller and heavier than cornerbacks, and nose tackles tend to be the some of the heaviest and tallest players on the field.","metadata":{}}]}