INSTALL PACKAGES Uses a specific package (nblR) to retrieve available NBL data; install.packages(“nblR”)
LOAD PACKAGES/DATA
suppressPackageStartupMessages({
library(nblR)
library(tidyverse)
library(DataExplorer)
library(fuzzywuzzyR)
library(dplyr)
library(readxl)
library(ggplot2)
library(rstatix)
library(lavaan)
library(nortest)
library(performance)
library(olsrr)
library(QuantPsyc)
library(car)
library(lme4)
library(caret)
library(MASS)
library(randomForest)
library(randomForestExplainer)
library(tidymodels)
library(plotly)
library(gridExtra)
library(DT)
library(knitr)
library(kableExtra)
})
team_box <- nbl_box_team()
## → Data last updated 2024-01-07 09:53:43 AEST
team_box <- team_box %>%
mutate(name = ifelse(name == "The Hawks", "Illawarra Hawks", name))
results_long <- nbl_results(wide_or_long="long")
## → Data last updated 2024-01-07 17:05:23 AEST
MERGE DATAFRAMES Merge ‘team_box’ and ‘results_long’, giving us 8 complete NBL season for analysis + the 2024 season to date.
df <- team_box %>% left_join(results_long,
by=c('match_id'='match_id',
'name'='team_name'))
CLEAN DATA Delete unnecessary columns, 30 Columns deleted; Delete rows with incomplete data (NA), 28 rows deleted; Deleted incomplete games, 1 incomplete game deleted; Repair names ‘name’, short_name, ‘code’, ‘opp_name’, ‘opp_short_name’, repaired; Renamed column ‘season.x’ to ‘season’;
# Delete unnecessary columns, 30 Columns deleted
df <- df[,!names(df) %in% c("time_leading",
"biggest_lead", "lead_changes",
"times_scores_level", "fouls_team", "rebounds_team",
"rebounds_team_defensive", "rebounds_team_offensive",
"efficiency_custom", "turnovers_team",
"biggest_scoring_run", "tot_eff_1", "tot_eff_2",
"tot_eff_3", "tot_eff_4", "tot_eff_5", "tot_eff_6",
"tot_eff_7", "fouls_total", "score", "opp_score",
'season.y', 'round_number', 'match_number',
'match_status', 'team_nickname', 'score_string',
'is_home_competitor', 'opp_team_name',
'opp_team_nickname', 'opp_score_string', 'match_time',
'match_time_utc', 'attendance', 'duration', 'minutes',
'team_id','opp_team_id', 'at_neutral_venue', 'points', 'ot_score')]
# Delete rows with incomplete data (NA), 28 rows deleted;
df <-df %>% drop_na("p1_score", "p2_score", "p3_score",
"p4_score", "full_score",
"opp_full_score")
#Delete incomplete games, 1 incomplete game deleted;
df <- df %>%
filter(match_id != "1307506")
# Repair names 'name', short_name, 'code', 'opp_name', 'opp_short_name', repaired
df <- df %>%
mutate(name = case_when(
name == "The Hawks" ~ "Illawarra Hawks",
TRUE ~ name
))
df <- df %>%
mutate(short_name = str_replace_all(short_name, c(
'ADL' = '36ers', '36ERS' = '36ers',
'Melbourne United' = 'United', 'MEL' = 'United',
'BRI' = 'Bullets', 'Brisbane Bullets' = 'Bullets',
'BULLETS' = 'Bullets', 'PER' = 'Wildcats',
'Wildcats' = 'Wildcats', 'Sydney' = 'Kings',
'Kings' = 'Kings', 'ILL' = 'Hawks', 'HAWKS' = 'Hawks',
'NZL' = 'Breakers', 'HAWKS' = 'Hawks',
'Cairns' = 'Taipans', 'CNS' = 'Taipans'
)))
df <- df %>%
mutate(code = str_replace_all(code, c('HWK' = 'ILL', 'HAWKS' = 'ILL', '36ERS' = 'ADL','Melbourne United' = 'MEL', 'BULLETS' = 'BRI',
'Wildcats' = 'PER', 'Sydney' = 'SYD', 'Kings' = 'SYD',
'Breakers' = 'NZL', 'Taipans' = 'CNS', 'PNX' = 'SEM')))
df <- df %>%
mutate(opp_name = ifelse(opp_name == "The Hawks", "Illawarra Hawks", opp_name))
df <- df %>%
mutate(opp_short_name = str_replace_all(opp_short_name, c(
'ADL' = '36ers', '36ERS' = '36ers',
'Melbourne United' = 'United', 'MEL' = 'United',
'BRI' = 'Bullets', 'Brisbane Bullets' = 'Bullets',
'BULLETS' = 'Bullets', 'PER' = 'Wildcats',
'Wildcats' = 'Wildcats', 'Sydney' = 'Kings',
'Kings' = 'Kings', 'ILL' = 'Hawks', 'HAWKS' = 'Hawks',
'NZL' = 'Breakers', 'HAWKS' = 'Hawks',
'Cairns' = 'Taipans', 'CNS' = 'Taipans'
)))
# Rename column 'season.x' to 'season';
df <- df %>%
rename('season' = 'season.x')
Create New Variables Create a win/loss variable; ‘result’ Create a team possessions variable, ‘possession’; Create an overall game pace variable normalised to 40 mins; Create adv. metrics derived from the box score of both teams in games, based on the literature;
df <-df %>%
mutate(
'result' = case_when(
full_score > opp_full_score ~ "win",
TRUE ~ "loss")) #Creates a win/loss variable
df <-df %>%
mutate('possessions' = field_goals_attempted-rebounds_offensive+turnovers
+(0.454*free_throws_attempted))
# Creates a team possessions variable (team pace)
# (Charamis, Marmarinos, and Ntzoufras, 2023) Estimating team possessions in
# high-level European basketball competition.
df <- df %>%
group_by(match_id) %>%
mutate(game_pace = (mean(possessions) /
(40 + (5 * extra_periods_used))) * 40) %>%
ungroup()
# Creates an overall game pace variable normalised
# to 40 mins
df <-df %>%
mutate('eFG_pct' = ((field_goals_made+(0.5*three_pointers_made))/
field_goals_attempted)*100)
# Creates an Effective Field Goal Percentage variable
df <-df %>%
mutate('TS_pct' = (full_score) / (2 * (field_goals_attempted + 0.44*
free_throws_attempted))*100)
# Creates a True Shooting Percentage variable
df <- df %>%
mutate('FTr' = (free_throws_made) / (field_goals_attempted)*100)
# Creates a free throw rate
df <- df %>%
group_by(match_id) %>%
mutate('DREB_pct' = rebounds_defensive / (rebounds_defensive +
sum(rebounds_offensive) - rebounds_offensive)*100) %>%
ungroup()
# Creates a % variable of available defensive rebounds gathered by a team
df <- df %>%
group_by(match_id) %>%
mutate('OREB_pct' = rebounds_offensive / (rebounds_offensive +
sum(rebounds_defensive) - rebounds_defensive)*100) %>%
ungroup()
# Creates a % variable of available defensive rebounds gathered by a team
df <- df %>%
group_by(match_id) %>%
mutate('TREB_pct' = rebounds_total / sum(rebounds_total)*100) %>%
ungroup()
# Creates a variable of the % of available rebounds gathered by the team
df <-df %>%
mutate('AST_pct' = (assists) / (field_goals_made)*100)
# Creates a % variable of field goals made given by assist
df <- df %>%
group_by(match_id) %>%
mutate('BLK_pct' = blocks / (sum(field_goals_attempted)-field_goals_attempted) *100) %>%
ungroup() #Creates a % of available rebounds gathered by a team variable
df <-df %>%
mutate('AST_TOV_ratio' = assists / turnovers)
# Creates a variable of the number of assists compared to the number of
# turnovers committed by a team
df <-df %>%
mutate('golden_hands' = (assists + steals) - turnovers)
# Creates a variable of the combined number of assists & steals compared
# to the number of turnovers committed by a team
df <-df %>%
mutate('score_margin' = full_score - opp_full_score)
# summary(df)
Rearrange variables Rearrange columns in preferred order;
df <-df %>%
select(match_id, season, match_type, match_name, name, code, short_name, opp_name, opp_short_name, venue_name, home_away, result, full_score, opp_full_score, score_margin, p1_score:p4_score,
extra_periods_used, possessions, game_pace, field_goals_made:fouls_on,
fouls:timeouts, points_from_turnovers:points_in_the_paint, eFG_pct:golden_hands) %>%
arrange(desc(match_id)) |> #Put yes at top
head(n = Inf) #Show all cases
CHECK DATA See first ten lines of tibble See full variable names and types Download a csv file
df #See first ten lines of tibble (tibble means table)
## # A tibble: 2,334 × 61
## match_id season match_type match_name name code short_name opp_name
## <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 2266972 2023-2024 REGULAR "" Tasmania … TAS JackJumpe… Cairns …
## 2 2266972 2023-2024 REGULAR "" Cairns Ta… CNS Taipans Tasmani…
## 3 2266970 2023-2024 REGULAR "" Illawarra… ILL Hawks South E…
## 4 2266970 2023-2024 REGULAR "" South Eas… SEM Phoenix Illawar…
## 5 2266968 2023-2024 REGULAR "" Sydney Ki… SYD Kings Melbour…
## 6 2266968 2023-2024 REGULAR "" Melbourne… MEL United Sydney …
## 7 2266966 2023-2024 REGULAR "" Tasmania … TAS JackJumpe… New Zea…
## 8 2266966 2023-2024 REGULAR "" New Zeala… NZL Breakers Tasmani…
## 9 2266965 2023-2024 REGULAR "" Cairns Ta… CNS Taipans Perth W…
## 10 2266965 2023-2024 REGULAR "" Perth Wil… PER Wildcats Cairns …
## # ℹ 2,324 more rows
## # ℹ 53 more variables: opp_short_name <chr>, venue_name <chr>, home_away <chr>,
## # result <chr>, full_score <int>, opp_full_score <int>, score_margin <int>,
## # p1_score <int>, p2_score <int>, p3_score <int>, p4_score <int>,
## # extra_periods_used <int>, possessions <dbl>, game_pace <dbl>,
## # field_goals_made <int>, field_goals_attempted <int>,
## # field_goals_percentage <int>, three_pointers_made <int>, …
glimpse(df) #See full variable names and types
## Rows: 2,334
## Columns: 61
## $ match_id <int> 2266972, 2266972, 2266970, 2266970, 2266968,…
## $ season <chr> "2023-2024", "2023-2024", "2023-2024", "2023…
## $ match_type <chr> "REGULAR", "REGULAR", "REGULAR", "REGULAR", …
## $ match_name <chr> "", "", "", "", "", "", "", "", "", "", "", …
## $ name <chr> "Tasmania JackJumpers", "Cairns Taipans", "I…
## $ code <chr> "TAS", "CNS", "ILL", "SEM", "SYD", "MEL", "T…
## $ short_name <chr> "JackJumpers", "Taipans", "Hawks", "Phoenix"…
## $ opp_name <chr> "Cairns Taipans", "Tasmania JackJumpers", "S…
## $ opp_short_name <chr> "Taipans", "JackJumpers", "Phoenix", "Hawks"…
## $ venue_name <chr> "MyState Bank Arena", "MyState Bank Arena", …
## $ home_away <chr> "home", "away", "home", "away", "home", "awa…
## $ result <chr> "win", "loss", "win", "loss", "loss", "win",…
## $ full_score <int> 103, 86, 116, 78, 85, 101, 93, 98, 102, 105,…
## $ opp_full_score <int> 86, 103, 78, 116, 101, 85, 98, 93, 105, 102,…
## $ score_margin <int> 17, -17, 38, -38, -16, 16, -5, 5, -3, 3, -4,…
## $ p1_score <int> 29, 20, 24, 15, 23, 18, 25, 19, 17, 26, 29, …
## $ p2_score <int> 22, 21, 29, 21, 17, 20, 15, 21, 26, 28, 19, …
## $ p3_score <int> 28, 18, 33, 23, 19, 35, 24, 30, 24, 22, 25, …
## $ p4_score <int> 24, 27, 30, 19, 26, 28, 29, 28, 35, 29, 17, …
## $ extra_periods_used <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ possessions <dbl> 83.264, 84.988, 82.534, 79.172, 84.718, 82.2…
## $ game_pace <dbl> 84.12600, 84.12600, 80.85300, 80.85300, 83.4…
## $ field_goals_made <int> 42, 33, 45, 31, 31, 38, 35, 33, 29, 32, 31, …
## $ field_goals_attempted <int> 79, 72, 72, 76, 67, 75, 77, 63, 72, 68, 66, …
## $ field_goals_percentage <int> 53, 45, 62, 40, 46, 50, 45, 52, 40, 47, 46, …
## $ three_pointers_made <int> 8, 5, 14, 6, 11, 12, 16, 6, 9, 5, 9, 10, 10,…
## $ three_pointers_attempted <int> 25, 20, 25, 26, 34, 30, 30, 18, 22, 16, 33, …
## $ three_pointers_percentage <int> 32, 25, 56, 23, 32, 40, 53, 33, 40, 31, 27, …
## $ two_pointers_made <int> 34, 28, 31, 25, 20, 26, 19, 27, 20, 27, 22, …
## $ two_pointers_attempted <int> 54, 52, 47, 50, 33, 45, 47, 45, 50, 52, 33, …
## $ two_pointers_percentage <int> 62, 53, 65, 50, 60, 57, 40, 60, 40, 51, 66, …
## $ free_throws_made <int> 11, 15, 12, 10, 12, 13, 7, 26, 35, 36, 19, 1…
## $ free_throws_attempted <int> 16, 22, 21, 18, 17, 16, 9, 34, 39, 40, 21, 2…
## $ free_throws_percentage <int> 68, 68, 57, 55, 70, 81, 77, 76, 89, 90, 90, …
## $ rebounds_defensive <int> 31, 27, 37, 19, 30, 30, 25, 26, 28, 35, 29, …
## $ rebounds_offensive <int> 12, 9, 11, 13, 6, 9, 18, 9, 10, 11, 7, 18, 7…
## $ rebounds_total <int> 43, 36, 48, 32, 36, 39, 43, 35, 38, 46, 36, …
## $ assists <int> 21, 18, 22, 14, 22, 21, 20, 15, 17, 19, 20, …
## $ turnovers <int> 9, 12, 12, 8, 16, 9, 11, 2, 13, 17, 11, 8, 1…
## $ steals <int> 7, 5, 3, 5, 4, 4, 1, 6, 11, 11, 4, 6, 4, 8, …
## $ blocks <int> 6, 1, 7, 5, 4, 1, 2, 1, 3, 3, 6, 3, 3, 4, 7,…
## $ blocks_received <int> 1, 6, 5, 7, 1, 4, 1, 2, 3, 3, 3, 6, 4, 3, 3,…
## $ fouls_personal <int> 20, 17, 18, 20, 19, 12, 27, 16, 28, 27, 24, …
## $ fouls_on <int> 17, 20, 19, 18, 12, 18, 16, 27, 27, 28, 18, …
## $ fouls <int> 4, 6, 5, 5, 9, 6, 10, 5, 11, 9, 5, 4, 5, 8, …
## $ timeouts <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ points_from_turnovers <int> 15, 13, 11, 9, 5, 18, 3, 17, 16, 11, 5, 11, …
## $ points_second_chance <int> 17, 8, 19, 18, 8, 17, 17, 17, 10, 13, 6, 16,…
## $ points_fast_break <int> 10, 13, 6, 8, 0, 12, 0, 5, 18, 4, 4, 21, 3, …
## $ bench_points <int> 30, 40, 51, 33, 28, 31, 31, 17, 34, 32, 32, …
## $ points_in_the_paint <int> 58, 54, 58, 46, 38, 52, 34, 50, 38, 48, 36, …
## $ eFG_pct <dbl> 58.22785, 49.30556, 72.22222, 44.73684, 54.4…
## $ TS_pct <dbl> 59.85588, 52.64447, 71.39340, 46.47283, 57.0…
## $ FTr <dbl> 13.924051, 20.833333, 16.666667, 13.157895, …
## $ DREB_pct <dbl> 77.50000, 69.23077, 74.00000, 63.33333, 76.9…
## $ OREB_pct <dbl> 30.76923, 22.50000, 36.66667, 26.00000, 16.6…
## $ TREB_pct <dbl> 54.43038, 45.56962, 60.00000, 40.00000, 48.0…
## $ AST_pct <dbl> 50.00000, 54.54545, 48.88889, 45.16129, 70.9…
## $ BLK_pct <dbl> 8.333333, 1.265823, 9.210526, 6.944444, 5.33…
## $ AST_TOV_ratio <dbl> 2.3333333, 1.5000000, 1.8333333, 1.7500000, …
## $ golden_hands <int> 19, 11, 13, 11, 10, 16, 10, 19, 15, 13, 13, …
unique(df$name)
## [1] "Tasmania JackJumpers" "Cairns Taipans"
## [3] "Illawarra Hawks" "South East Melbourne Phoenix"
## [5] "Sydney Kings" "Melbourne United"
## [7] "New Zealand Breakers" "Perth Wildcats"
## [9] "Adelaide 36ers" "Brisbane Bullets"
## [11] "Townsville Crocodiles"
unique(df$short_name)
## [1] "JackJumpers" "Taipans" "Hawks" "Phoenix" "Kings"
## [6] "United" "Breakers" "Wildcats" "36ers" "Bullets"
## [11] "Crocodiles"
unique(df$code)
## [1] "TAS" "CNS" "ILL" "SEM" "SYD" "MEL" "NZL" "PER" "ADL" "BRI" "TSV"
unique(df$opp_name)
## [1] "Cairns Taipans" "Tasmania JackJumpers"
## [3] "South East Melbourne Phoenix" "Illawarra Hawks"
## [5] "Melbourne United" "Sydney Kings"
## [7] "New Zealand Breakers" "Perth Wildcats"
## [9] "Adelaide 36ers" "Brisbane Bullets"
## [11] "Townsville Crocodiles"
unique(df$opp_short_name)
## [1] "Taipans" "JackJumpers" "Phoenix" "Hawks" "United"
## [6] "Kings" "Breakers" "Wildcats" "36ers" "Bullets"
## [11] "Crocodiles"
df %>%
plot_missing()
df %>%
plot_bar()
df %>%
plot_histogram()
qqnorm(df$field_goals_made)
qqline(df$field_goals_made)
#Normalise data per 100 possessions and then calculate data in relative form (to opposition)
#Normalise data per 100 possessions for all variables that are not proportions/percentages
df_norm.hund <- df %>%
mutate_at(vars(field_goals_made:golden_hands, -field_goals_percentage, -three_pointers_percentage, -two_pointers_percentage, -free_throws_percentage, -eFG_pct, -TS_pct, -FTr, -DREB_pct, -OREB_pct, -TREB_pct, -AST_pct, -BLK_pct, -AST_TOV_ratio), ~ (.x / possessions) * 100)
# write.csv(df_norm.hund, "\\\\chrisyou.homes.deakin.edu.au/my-home/UserData/Desktop/R files/NBL/data/NBL_data_norm100.csv", row.names = F)
# Creating relative score function -----------------------------------------------------------
RelFun <- function(x){
c(x[1] - x[2],
x[2] - x[1])
}
# use function above on particular variables and rename
RelScores <- df_norm.hund %>%
group_by(match_id) %>%
mutate_at(vars(possessions:golden_hands), RelFun) %>%
rename_at(vars(possessions:golden_hands),
~ paste0(., "_Rel"))
RelScores$game_pace <- df$game_pace
RelScores$possessions <- df$possessions
RelScores <- RelScores %>%
# dplyr::select(-game_pace_Rel) %>%
relocate(game_pace, possessions, .before = possessions_Rel)
RelScores <- RelScores %>%
mutate(result2 = ifelse(result == "win", 1, 0), #creates a dummy version of result with 1 = win, 0 = loss
result = as.factor(result), #declares result as factor
home_away2 = ifelse(home_away == "home", 1, 0)) %>% #creates a dummy version of home_away with 1 = home, 0 = away
relocate(result2,home_away,home_away2, code, short_name, opp_short_name, venue_name, .after = result)
# write.csv(RelScores, "\\\\chrisyou.homes.deakin.edu.au/my-home/UserData/Desktop/R files/NBL/results/norm_relative_df.csv", row.names = F)
Mean, Std, Median, & IQR For Winning & Losing Teams for both team total values and relative to opposition.
descriptives <- df %>%
mutate(result = forcats::fct_relevel(result, "win", "loss")) %>%
pivot_longer(cols = full_score:golden_hands, names_to = "variable") %>%
select(variable, result, value) %>%
group_by(variable, result) %>%
summarize(mean = round(mean(value, na.rm = TRUE), 2),
std = round(sd(value, na.rm = TRUE), 2),
median = round(median(value, na.rm = TRUE), 2),
iqr = round(IQR(value, na.rm = TRUE), 2),
.groups = "drop") %>%
pivot_wider(names_from = result,
values_from = c(mean, std, median, iqr)) %>%
arrange(match(variable, colnames(df)[!colnames(df) %in% c("result")]))
descriptives.2 <- RelScores %>%
mutate(result = forcats::fct_relevel(result, "win", "loss")) %>%
pivot_longer(cols = full_score:golden_hands_Rel, names_to = "variable") %>%
select(variable, result, value) %>%
group_by(variable, result) %>%
summarize(mean = round(mean(value, na.rm = TRUE), 2),
std = round(sd(value, na.rm = TRUE), 2),
median = round(median(value, na.rm = TRUE), 2),
iqr = round(IQR(value, na.rm = TRUE), 2),
.groups = "drop") %>%
pivot_wider(names_from = result,
values_from = c(mean, std, median, iqr)) %>%
arrange(match(variable, colnames(df)[!colnames(df) %in% c("result")]))
## Adding missing grouping variables: `match_id`
Random forest model to rank by purity, identifying KPI’s
#Win-Loss outcome
model_RelScores <- RelScores %>%
ungroup() %>%
dplyr::select(-c("match_id":"game_pace_Rel", timeouts_Rel, -result, -home_away2))
RelScores_split <- initial_split(model_RelScores, prop = 0.6)
RelScores_recipe <- training(RelScores_split) %>%
recipe(result~.) %>%
step_corr(all_predictors()) %>%
step_center(all_predictors(), -all_outcomes()) %>%
step_scale(all_predictors(), -all_outcomes()) %>%
prep()
RelScores_testing <- RelScores_recipe %>%
bake(testing(RelScores_split))
RelScores_training <- juice(RelScores_recipe)
RelScores_ranger <- rand_forest(trees = 100, mode = "classification") %>%
set_engine("ranger") %>%
fit(result ~ ., data = RelScores_training)
predict(RelScores_ranger, RelScores_testing)
## # A tibble: 934 × 1
## .pred_class
## <fct>
## 1 win
## 2 loss
## 3 loss
## 4 loss
## 5 win
## 6 win
## 7 loss
## 8 loss
## 9 win
## 10 loss
## # ℹ 924 more rows
RelScores_ranger %>%
predict(RelScores_testing) %>%
bind_cols(RelScores_testing) %>%
glimpse()
## Rows: 934
## Columns: 31
## $ .pred_class <fct> win, loss, loss, loss, win, win, loss, l…
## $ home_away2 <dbl> 0.9967907, -1.0025030, 0.9967907, 0.9967…
## $ field_goals_made_Rel <dbl> 1.635652688, -2.136508817, -1.330230469,…
## $ field_goals_attempted_Rel <dbl> 0.9707852, 0.8394821, -1.1090877, 1.4926…
## $ three_pointers_made_Rel <dbl> 0.674405911, -1.648840081, -0.269798371,…
## $ three_pointers_attempted_Rel <dbl> 0.61513731, 0.24637201, 0.35073296, 1.43…
## $ three_pointers_percentage_Rel <dbl> 0.53405462, -2.45844675, -0.58813339, 1.…
## $ two_pointers_made_Rel <dbl> 1.081733868, -0.819185158, -1.095227877,…
## $ two_pointers_attempted_Rel <dbl> 0.31561803, 0.52614370, -1.29473953, 0.0…
## $ two_pointers_percentage_Rel <dbl> 0.801046495, -1.359078885, 0.261015150, …
## $ free_throws_attempted_Rel <dbl> -0.59722324, -0.24798887, 0.04521621, -3…
## $ free_throws_percentage_Rel <dbl> -0.02792337, -0.15609620, -0.73287397, 0…
## $ rebounds_defensive_Rel <dbl> 0.67134001, -2.51008704, -0.11733630, -0…
## $ rebounds_offensive_Rel <dbl> 0.56074971, 0.45949726, -0.50430125, 1.6…
## $ assists_Rel <dbl> 0.57412198, -1.17409153, 0.09045032, 0.8…
## $ turnovers_Rel <dbl> -0.532039854, -0.715304618, 1.302909498,…
## $ steals_Rel <dbl> 0.54823568, 0.58217875, -0.02900781, -1.…
## $ blocks_Rel <dbl> 1.743324165, -0.626295113, 1.013700208, …
## $ fouls_personal_Rel <dbl> 0.62233835, 0.53583334, 1.20776923, 2.15…
## $ fouls_Rel <dbl> -0.54429407, 0.07289393, 0.82750362, 1.6…
## $ points_from_turnovers_Rel <dbl> 0.26703159, -0.18589151, -1.54291501, -1…
## $ points_second_chance_Rel <dbl> 1.251932356, -0.005976711, -1.224572996,…
## $ points_fast_break_Rel <dbl> -0.40742297, 0.37162221, -1.84570339, -0…
## $ bench_points_Rel <dbl> -0.58570944, -1.10851752, -0.21688155, 1…
## $ points_in_the_paint_Rel <dbl> 0.41607380, -0.83618923, -1.25951682, -1…
## $ eFG_pct_Rel <dbl> 0.8822228, -2.7084774, -0.4108814, -0.12…
## $ OREB_pct_Rel <dbl> 0.7639690, -0.9222844, -0.5432485, 1.313…
## $ AST_pct_Rel <dbl> -0.25068850, -0.20085011, 0.98330556, 0.…
## $ AST_TOV_ratio_Rel <dbl> 0.82572822, -0.07143729, -0.92782255, -5…
## $ golden_hands_Rel <dbl> 0.757538405, -0.125257832, -0.560771817,…
## $ result <fct> win, loss, loss, loss, win, loss, loss, …
RelScores_ranger %>%
predict(RelScores_testing) %>%
bind_cols(RelScores_testing) %>%
metrics(truth = result, estimate = .pred_class)
## # A tibble: 2 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 accuracy binary 0.933
## 2 kap binary 0.865
rf.fit <- randomForest(result ~ ., data=RelScores_training, ntree=1000,
keep.forest=FALSE, importance=TRUE)
varImpPlot(rf.fit)
# Get variable importance from the model fit
ImpData <- as.data.frame(importance(rf.fit))
ImpData$Var.Names <- row.names(ImpData)
# Reorder the data based on MeanDecreaseAccuracy
ImpData <- transform(ImpData, Var.Names = reorder(Var.Names, MeanDecreaseAccuracy))
# Plot the data
var_imp_plot <- ggplot(ImpData, aes(x = Var.Names, y = MeanDecreaseAccuracy)) +
geom_segment(aes(x = Var.Names, xend = Var.Names, y = 0, yend = MeanDecreaseAccuracy), color = "skyblue") +
geom_point(aes(size = MeanDecreaseGini), color = "blue", alpha = 0.6) +
theme_classic() +
coord_flip() +
theme(
legend.position = "bottom",
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank()
)
ggplotly(var_imp_plot)
#Score margin outcome (Regression) ###Retaining focus on Win-Loss outcome for purpose of analysis###
#Win-Loss outcome
# model_RelScores <- RelScores %>%
# dplyr::select(-c("match_id":"game_pace_Rel",-score_margin))
#
# RelScores_split <- initial_split(model_RelScores, prop = 0.6)
#
# RelScores_recipe <- training(RelScores_split) %>%
# recipe(score_margin~.) %>%
# step_corr(all_predictors()) %>%
# step_center(all_predictors(), -all_outcomes()) %>%
# step_scale(all_predictors(), -all_outcomes()) %>%
# prep()
#
# RelScores_testing <- RelScores_recipe %>%
# bake(testing(RelScores_split))
#
# RelScores_training <- juice(RelScores_recipe)
#
# RelScores_ranger <- rand_forest(trees = 100, mode = "regression") %>%
# set_engine("randomForest") %>%
# fit(score_margin ~ ., data = RelScores_training)
#
# predict(RelScores_ranger, RelScores_testing)
#
#
# RelScores_ranger %>%
# predict(RelScores_testing) %>%
# bind_cols(RelScores_testing) %>%
# glimpse()
#
# RelScores_ranger %>%
# predict(RelScores_testing) %>%
# bind_cols(RelScores_testing) %>%
# metrics(truth = score_margin, estimate = .rmse)
#
#
# rf.fit <- randomForest(score_margin ~ ., data=RelScores_training, ntree=100,
# keep.forest=FALSE, importance=TRUE)
#Visualisations
#Key performance indicators
ggplot(RelScores, aes(season, eFG_pct_Rel, colour = result
))+geom_boxplot()+theme_classic()
ggplot(RelScores, aes(season, field_goals_made_Rel, colour = result
))+geom_boxplot()+theme_classic()
ggplot(RelScores, aes(season, fouls_Rel, colour = result
))+geom_boxplot()+theme_classic()
ggplot(RelScores, aes(season, rebounds_defensive_Rel, colour = result
))+geom_boxplot()+theme_classic()
ggplot(RelScores, aes(season, free_throws_percentage_Rel, colour = result))+geom_boxplot()+theme_classic()
ggplot(RelScores, aes(season, three_pointers_percentage_Rel, colour = result))+geom_boxplot()+theme_classic()
variables_of_interest <- c("eFG_pct_Rel", "field_goals_made_Rel", "fouls_Rel", "rebounds_defensive_Rel", "free_throws_percentage_Rel","three_pointers_percentage_Rel")
sum_RelScores <- RelScores %>%
group_by(season, result) %>%
summarize(across(all_of(variables_of_interest), list(mean = mean, sd = sd)))
## `summarise()` has grouped output by 'season'. You can override using the
## `.groups` argument.
ggplot(sum_RelScores, aes(season, eFG_pct_Rel_mean, colour = result))+
geom_point()+
# geom_smooth(method = "lm", se = T)+
theme_classic()
ggplot_object <- ggplot(sum_RelScores, aes(season, eFG_pct_Rel_mean, colour = result)) +
geom_point() +
theme_classic()
# Convert ggplot to plotly
ggplotly(ggplot_object)
############################################################################################
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(eFG_pct, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(eFG_pct, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, eFG_pct, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
##########################################################################################
#Field goals made
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(field_goals_made, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(field_goals_made, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, field_goals_made, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Fouls personal
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(fouls_personal, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(fouls_personal, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, fouls_personal, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Fouls
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(fouls, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(fouls, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, fouls, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#free_throws_attempted
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(free_throws_attempted, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(free_throws_attempted, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, free_throws_attempted, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#three_pointers_percentage
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(three_pointers_percentage, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(three_pointers_percentage, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, three_pointers_percentage, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#rebounds_defensive
win_avg <- df_norm.hund %>%
dplyr::filter(result == "win") %>%
summarize(win_avg = mean(rebounds_defensive, na.rm = TRUE)) %>%
pull(win_avg)
loss_avg <- df_norm.hund %>%
dplyr::filter(result == "loss") %>%
summarize(loss_avg = mean(three_pointers_percentage, na.rm = TRUE)) %>%
pull(loss_avg)
#plot to use for KPI stability across 9 NBL seasons (EFG%)
ggplot(df_norm.hund, aes(season, rebounds_defensive, colour = result)) +
geom_boxplot() +
geom_hline(yintercept = win_avg, linetype = "dashed", color = "blue", linewidth = 1, alpha = 0.6) +
geom_hline(yintercept = loss_avg, linetype = "dashed", color = "red", linewidth = 1, alpha = 0.6) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot1 <-ggplot(df_norm.hund, aes(season, eFG_pct, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
guides(color = FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot2 <-ggplot(df_norm.hund, aes(season, field_goals_attempted, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
guides(color = FALSE)
plot3 <-ggplot(df_norm.hund, aes(season, fouls_personal, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot4 <-ggplot(df_norm.hund, aes(season, fouls, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
guides(color = FALSE)
plot5 <-ggplot(df_norm.hund, aes(season, free_throws_attempted, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
guides(color = FALSE)
plot6 <- ggplot(df_norm.hund, aes(season, three_pointers_percentage, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(legend.position = "bottom")
plot7 <-ggplot(df_norm.hund, aes(season, rebounds_defensive, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
guides(color = FALSE)
grid.arrange(plot1, plot2, plot4, plot5, plot6, plot7, nrow = 2, ncol = 3)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
# plotly_plot1 <- ggplotly(plot1)
# plotly_plot2 <- ggplotly(plot2)
# plotly_plot3 <- ggplotly(plot3)
# plotly_plot4 <- ggplotly(plot4)
# plotly_plot5 <- ggplotly(plot5)
# plotly_plot6 <- ggplotly(plot6)
# plotly_plot7 <- ggplotly(plot7)
#
# subplot(
# plotly_plot1,
# style(plotly_plot2,showlegend = F),
# # style(plotly_plot3,showlegend = F),
# style(plotly_plot4,showlegend = F),
# style(plotly_plot5,showlegend = F),
# style(plotly_plot6,showlegend = F),
# style(plotly_plot7,showlegend = F),
# nrows = 2, margin = 0.05
# )
plot1 <- ggplot(RelScores, aes(season, eFG_pct_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot2 <- ggplot(RelScores, aes(season, field_goals_attempted_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot3 <- ggplot(RelScores, aes(season, fouls_personal_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot4 <- ggplot(RelScores, aes(season, fouls_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot5 <- ggplot(RelScores, aes(season, free_throws_attempted_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot6 <- ggplot(RelScores, aes(season, three_pointers_percentage_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot7 <- ggplot(RelScores, aes(season, rebounds_defensive_Rel, colour = result)) +
geom_boxplot() +
geom_smooth(method = "loess", se = TRUE, aes(group = result, color = result)) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
grid.arrange(plot1, plot2, plot4, plot5, plot6, plot7, nrow = 3, ncol = 3)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'
# plotly_plot1 <- ggplotly(plot1)
# plotly_plot2 <- ggplotly(plot2)
# plotly_plot3 <- ggplotly(plot3)
# plotly_plot4 <- ggplotly(plot4)
# plotly_plot5 <- ggplotly(plot5)
# plotly_plot6 <- ggplotly(plot6)
# plotly_plot7 <- ggplotly(plot7)
#
# subplot(
# plotly_plot1, plotly_plot2, plotly_plot3,
# plotly_plot4, plotly_plot5, plotly_plot6,
# plotly_plot7, nrows = 3, margin = 0.05 # Adjust layout as needed
# )
What does it take to win! Identification of the mean value for each KPI
winning_teams <- RelScores %>% filter(result == "win") %>%
ungroup()
mean_values <- winning_teams %>%
summarise(
eFG_pct = round(mean(eFG_pct_Rel), 1),
field_goals_attempted = round(mean(field_goals_attempted_Rel), 1),
fouls = round(mean(fouls_Rel), 1),
free_throws_attempted = round(mean(free_throws_attempted_Rel), 1),
three_pointers_percentage = round(mean(three_pointers_percentage_Rel), 1),
rebounds_defensive = round(mean(rebounds_defensive_Rel), 1)
) %>%
gather(variable, mean_values)
# Create an interactive table using DT
# datatable(mean_values,
# options = list(pageLength = 10),
# rownames = FALSE,
# class = 'cell-border stripe',
# extensions = c('Buttons', 'Scroller'))
# Create a kable table
kable_table <- kable(mean_values, "html") %>%
kable_styling("striped", full_width = F) %>%
column_spec(1, bold = TRUE)
kable_table
| variable | mean_values |
|---|---|
| eFG_pct | 6.5 |
| field_goals_attempted | -0.4 |
| fouls | -1.7 |
| free_throws_attempted | 3.6 |
| three_pointers_percentage | 6.6 |
| rebounds_defensive | 4.2 |
FURTHER ANALYSES THAT COULD BE DEVELOPED AND USED
Standard procedures would dictate that the distribution of each variable is assessed on each group (win and loss), prior to conducting any paramentric tests.
Testing distribution for each variable in the data
options(scipen = 999)
v_names<- colnames(RelScores)
for(c in 27:65){
print(v_names[c])
print(ks.test(RelScores[,c], "pnorm"))
} # check normality for each variable using a loop
## [1] "field_goals_made_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.36198, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "field_goals_attempted_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.40795, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "field_goals_percentage_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.40227, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "three_pointers_made_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.34853, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "three_pointers_attempted_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.4053, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "three_pointers_percentage_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.43612, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "two_pointers_made_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.36113, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "two_pointers_attempted_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.40947, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "two_pointers_percentage_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.42498, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "free_throws_made_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.39741, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "free_throws_attempted_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41249, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "free_throws_percentage_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.43955, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "rebounds_defensive_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.39067, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "rebounds_offensive_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.36939, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "rebounds_total_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41142, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "assists_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.37593, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "turnovers_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.35724, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "steals_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.32375, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "blocks_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.27938, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "blocks_received_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.27512, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "fouls_personal_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.36011, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "fouls_on_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.35731, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "fouls_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.30001, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "timeouts_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.40189, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "points_from_turnovers_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.40709, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "points_second_chance_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.39261, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "points_fast_break_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.35987, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "bench_points_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.43984, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "points_in_the_paint_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.43178, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "eFG_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.39822, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "TS_pct_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.4019, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "FTr_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.4139, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "DREB_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41197, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "OREB_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41197, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "TREB_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41873, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "AST_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.43841, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "BLK_pct_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.28264, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
##
## [1] "AST_TOV_ratio_Rel"
## Warning in ks.test.default(RelScores[, c], "pnorm"): ties should not be present
## for the Kolmogorov-Smirnov test
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.056735, p-value = 0.0000005962
## alternative hypothesis: two-sided
##
## [1] "golden_hands_Rel"
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: RelScores[, c]
## D = 0.41708, p-value < 0.00000000000000022
## alternative hypothesis: two-sided
From this we can see that most variables are not normally distributed.
Therefore, if we wanted to conduct some more formal comparisons of means between winning and losing outcome, we would proceed with non-parametric testing.
Comparison of means
#Comparison of absolute team total me(dia)ns values for top 6 KPI's
test_results <- lapply(df[c("eFG_pct", "field_goals_attempted", "fouls",
"free_throws_attempted", "three_pointers_percentage",
"rebounds_defensive")], function(x) {
wilcox.test(x ~ result, data = df)
})
# View the test results
test_results
## $eFG_pct
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 318327, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $field_goals_attempted
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 700595, p-value = 0.2269
## alternative hypothesis: true location shift is not equal to 0
##
##
## $fouls
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 900520, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $free_throws_attempted
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 511527, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $three_pointers_percentage
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 400254, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $rebounds_defensive
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 418164, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
# Extract p-values
p_values <- sapply(test_results, function(x) x$p.value)
p_values
## eFG_pct
## 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006337767
## field_goals_attempted
## 0.22693043529560794069332985145592829212546348571777343750000000000000000000000000000000000000000000000000000000000000
## fouls
## 0.00000000000000000000000000000000000000000364936077973619625992689197158114211561041884124279022216796875000000000000
## free_throws_attempted
## 0.00000000000000000000000020530750922825317418551893666744945221580564975738525390625000000000000000000000000000000000
## three_pointers_percentage
## 0.00000000000000000000000000000000000000000000000000000000000000000105866082461897009834965455166155834376695565879345
## rebounds_defensive
## 0.00000000000000000000000000000000000000000000000000000000007812048798068688315512225761594322648306842893362045288086
#All significantly different, with the exception of field goal attempts
#Comparison of relative value me(dia)ns for top 6 KPI's
test_results <- lapply(RelScores[c("eFG_pct_Rel", "field_goals_attempted_Rel", "fouls_Rel",
"free_throws_attempted_Rel", "three_pointers_percentage_Rel",
"rebounds_defensive_Rel")], function(x) {
wilcox.test(x ~ result, data = RelScores)
})
# View the test results
test_results
## $eFG_pct_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 154335, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $field_goals_attempted_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 706177, p-value = 0.1212
## alternative hypothesis: true location shift is not equal to 0
##
##
## $fouls_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 1013475, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $free_throws_attempted_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 433202, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $three_pointers_percentage_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 291411, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
##
##
## $rebounds_defensive_Rel
##
## Wilcoxon rank sum test with continuity correction
##
## data: x by result
## W = 273822, p-value < 0.00000000000000022
## alternative hypothesis: true location shift is not equal to 0
# Extract p-values
p_values <- sapply(test_results, function(x) x$p.value)
p_values
## eFG_pct_Rel
## 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001418624
## field_goals_attempted_Rel
## 0.1211520558530003732755631062900647521018981933593750000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
## fouls_Rel
## 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096058598869168600143872427388203050213633105158805847167968750000000000000000000000000000000000000000000000000000000000000000000000000000000000
## free_throws_attempted_Rel
## 0.0000000000000000000000000000000000000000000000000002659753171395416371106459885176320767641300335526466369628906250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
## three_pointers_percentage_Rel
## 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013248825416223264704682766046417441430094186216592788696289062500000000000000000000000000000000000000000000000
## rebounds_defensive_Rel
## 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000048439092742376734252590653184711300127673894166946411132812500000000000000000000000000000000000000
#All significantly different
We could then do some more modelling, including considering some contextual factors (home_away) and possibly regular season and play-offs.
Prior to running these models, it is necessary to check and remove collinear variables.
Running VIF on per 100 df
dd <- df_norm.hund %>%
dplyr::select(score_margin, field_goals_made:golden_hands)
de <- dd %>%
dplyr::select(-c(two_pointers_made, two_pointers_attempted, rebounds_total, turnovers, golden_hands, TS_pct, eFG_pct, field_goals_made,free_throws_made, blocks, timeouts, assists, field_goals_percentage, OREB_pct, FTr, three_pointers_made, rebounds_offensive))
model <- lm(score_margin ~ .,
data = de)
#timeouts removed as they are a coaching decision, not a performance indicator
# remove the following variables due to singularities (perfect correlations).
# two_pointers_made
# two_pointers attempted
# rebounds total
# turnovers
# golden_hands
# removed due to VIF
# TS_pct
# eFG_pct
# field_goals_made
# free_throws_made
# blocks
# assists
# field_goals_percentage
# OREB_pct
# FTr
# three_pointers_made
# rebounds_offensive
summary(model, rsq = TRUE, fit.measures = TRUE, standardized = TRUE)
##
## Call:
## lm(formula = score_margin ~ ., data = de)
##
## Residuals:
## Min 1Q Median 3Q Max
## -24.6276 -4.9510 -0.0305 4.5895 27.7971
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -165.41706 6.28165 -26.333 < 0.0000000000000002
## field_goals_attempted 0.43874 0.05505 7.970 0.00000000000000246
## three_pointers_attempted -0.01587 0.03335 -0.476 0.634300
## three_pointers_percentage 0.55823 0.02103 26.544 < 0.0000000000000002
## two_pointers_percentage 0.61343 0.03991 15.369 < 0.0000000000000002
## free_throws_attempted 0.25344 0.03738 6.781 0.00000000001512539
## free_throws_percentage 0.12654 0.01372 9.221 < 0.0000000000000002
## rebounds_defensive 0.96962 0.03559 27.247 < 0.0000000000000002
## steals 0.84236 0.05765 14.612 < 0.0000000000000002
## blocks_received -0.14840 0.06549 -2.266 0.023535
## fouls_personal -0.13196 0.03924 -3.363 0.000785
## fouls_on 0.33717 0.05222 6.456 0.00000000013038788
## fouls -0.12882 0.06493 -1.984 0.047398
## points_from_turnovers 0.39991 0.02691 14.863 < 0.0000000000000002
## points_second_chance -0.14318 0.03252 -4.403 0.00001115574013994
## points_fast_break -0.11783 0.02472 -4.767 0.00000198622370116
## bench_points -0.01411 0.01255 -1.124 0.261028
## points_in_the_paint -0.08459 0.02734 -3.094 0.001999
## DREB_pct -0.15096 0.03088 -4.889 0.00000108514811583
## TREB_pct 0.53447 0.06176 8.655 < 0.0000000000000002
## AST_pct -0.04962 0.01814 -2.736 0.006265
## BLK_pct 0.32346 0.05590 5.786 0.00000000818820936
## AST_TOV_ratio 2.23378 0.41229 5.418 0.00000006650043651
##
## (Intercept) ***
## field_goals_attempted ***
## three_pointers_attempted
## three_pointers_percentage ***
## two_pointers_percentage ***
## free_throws_attempted ***
## free_throws_percentage ***
## rebounds_defensive ***
## steals ***
## blocks_received *
## fouls_personal ***
## fouls_on ***
## fouls *
## points_from_turnovers ***
## points_second_chance ***
## points_fast_break ***
## bench_points
## points_in_the_paint **
## DREB_pct ***
## TREB_pct ***
## AST_pct **
## BLK_pct ***
## AST_TOV_ratio ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.4 on 2311 degrees of freedom
## Multiple R-squared: 0.7204, Adjusted R-squared: 0.7177
## F-statistic: 270.6 on 22 and 2311 DF, p-value: < 0.00000000000000022
ols_vif_tol(model)
## Variables Tolerance VIF
## 1 field_goals_attempted 0.1334239 7.494910
## 2 three_pointers_attempted 0.3615611 2.765784
## 3 three_pointers_percentage 0.6179453 1.618266
## 4 two_pointers_percentage 0.2420014 4.132208
## 5 free_throws_attempted 0.2412416 4.145222
## 6 free_throws_percentage 0.9391191 1.064828
## 7 rebounds_defensive 0.5147227 1.942794
## 8 steals 0.6502807 1.537797
## 9 blocks_received 0.8545311 1.170232
## 10 fouls_personal 0.6023463 1.660175
## 11 fouls_on 0.3439185 2.907665
## 12 fouls 0.6618753 1.510859
## 13 points_from_turnovers 0.6435962 1.553769
## 14 points_second_chance 0.5047867 1.981035
## 15 points_fast_break 0.8100178 1.234541
## 16 bench_points 0.8694090 1.150207
## 17 points_in_the_paint 0.2774809 3.603851
## 18 DREB_pct 0.3696059 2.705584
## 19 TREB_pct 0.2050522 4.876807
## 20 AST_pct 0.5077794 1.969359
## 21 BLK_pct 0.9386979 1.065306
## 22 AST_TOV_ratio 0.2606990 3.835841
vif <- as.data.frame(vif(model))
check_model(model)
df_norm.hund
## # A tibble: 2,334 × 61
## match_id season match_type match_name name code short_name opp_name
## <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 2266972 2023-2024 REGULAR "" Tasmania … TAS JackJumpe… Cairns …
## 2 2266972 2023-2024 REGULAR "" Cairns Ta… CNS Taipans Tasmani…
## 3 2266970 2023-2024 REGULAR "" Illawarra… ILL Hawks South E…
## 4 2266970 2023-2024 REGULAR "" South Eas… SEM Phoenix Illawar…
## 5 2266968 2023-2024 REGULAR "" Sydney Ki… SYD Kings Melbour…
## 6 2266968 2023-2024 REGULAR "" Melbourne… MEL United Sydney …
## 7 2266966 2023-2024 REGULAR "" Tasmania … TAS JackJumpe… New Zea…
## 8 2266966 2023-2024 REGULAR "" New Zeala… NZL Breakers Tasmani…
## 9 2266965 2023-2024 REGULAR "" Cairns Ta… CNS Taipans Perth W…
## 10 2266965 2023-2024 REGULAR "" Perth Wil… PER Wildcats Cairns …
## # ℹ 2,324 more rows
## # ℹ 53 more variables: opp_short_name <chr>, venue_name <chr>, home_away <chr>,
## # result <chr>, full_score <int>, opp_full_score <int>, score_margin <int>,
## # p1_score <int>, p2_score <int>, p3_score <int>, p4_score <int>,
## # extra_periods_used <int>, possessions <dbl>, game_pace <dbl>,
## # field_goals_made <dbl>, field_goals_attempted <dbl>,
## # field_goals_percentage <int>, three_pointers_made <dbl>, …
corre <- cor(dd)
Multiple linear regressions
#FINAL MODEL, based on iteratively removing variables until the VIF of all remaining variables < 10 from the ols_vif_tol(model_refined) output.
model_refined <- lm(score_margin ~
##blocks_Rel + #VIF = 400
##blocks_received_Rel + #VIF = 76
##fouls_on_Rel + #VIF = 20
##FTr_Rel + #VIF = 31
#rebounds_total_Rel + #VIF = Inf
##eFG_pct_Rel + #VIF = 807
##DREB_pct_Rel +
##field_goals_made_Rel + #VIF = Inf
##field_goals_attempted_Rel + #VIF = Inf
##field_goals_percentage_Rel + #VIF = 35
##three_pointers_made_Rel + #VIF = 26
##three_pointers_attempted_Rel + #VIF = Inf
three_pointers_percentage_Rel +
##two_pointers_made_Rel + ## VIF = 82
two_pointers_attempted_Rel +
two_pointers_percentage_Rel +
##free_throws_made_Rel + # VIF = 129
free_throws_attempted_Rel +
free_throws_percentage_Rel +
##rebounds_defensive_Rel + #VIF = Inf
##rebounds_offensive_Rel + #VIF = Inf
##rebounds_total_Rel + #VIF = Inf
##assists_Rel + #VIF = Inf
##turnovers_Rel + #VIF = Inf
##steals_Rel + #VIF = Inf
fouls_personal_Rel +
fouls_Rel +
points_from_turnovers_Rel +
points_second_chance_Rel +
points_fast_break_Rel +
bench_points_Rel +
points_in_the_paint_Rel +
##TS_pct_Rel + # VIF = 255
##OREB_pct_Rel + #VIF = 557
DREB_pct_Rel +
AST_pct_Rel +
BLK_pct_Rel +
AST_TOV_ratio_Rel +
golden_hands_Rel,
data = RelScores)
summary(model_refined)
##
## Call:
## lm(formula = score_margin ~ three_pointers_percentage_Rel + two_pointers_attempted_Rel +
## two_pointers_percentage_Rel + free_throws_attempted_Rel +
## free_throws_percentage_Rel + fouls_personal_Rel + fouls_Rel +
## points_from_turnovers_Rel + points_second_chance_Rel + points_fast_break_Rel +
## bench_points_Rel + points_in_the_paint_Rel + DREB_pct_Rel +
## AST_pct_Rel + BLK_pct_Rel + AST_TOV_ratio_Rel + golden_hands_Rel,
## data = RelScores)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.706 -2.241 0.000 2.241 12.706
##
## Coefficients:
## Estimate Std. Error
## (Intercept) -0.000000000000000006204 0.072548871253415719851
## three_pointers_percentage_Rel 0.548643657054037059417 0.005966040170245815420
## two_pointers_attempted_Rel -0.084619563912763637314 0.012150490275685857725
## two_pointers_percentage_Rel 0.552029107555530806906 0.012480177822672879756
## free_throws_attempted_Rel 0.063489000286179986254 0.010563244702682692694
## free_throws_percentage_Rel 0.142850374133831670154 0.004729959343456284840
## fouls_personal_Rel -0.294337696733058207244 0.019530633226208560932
## fouls_Rel 0.024885840255823243883 0.023727410207936724257
## points_from_turnovers_Rel 0.044303507006279656266 0.010553930500025475447
## points_second_chance_Rel 0.050727958078860775626 0.011490615185989467781
## points_fast_break_Rel 0.018762590544821849958 0.010147272583689227743
## bench_points_Rel 0.004106531206439104596 0.004440084339974802174
## points_in_the_paint_Rel 0.003994287824652439228 0.010198847811230406235
## DREB_pct_Rel 0.371018850152194656911 0.009673781703230901682
## AST_pct_Rel -0.175686970221000104964 0.006481130949323681174
## BLK_pct_Rel 0.010461837358287684116 0.020072541760013214857
## AST_TOV_ratio_Rel 1.188627145195858103577 0.146026685393744110053
## golden_hands_Rel 0.401426330867150293891 0.013218243379095476545
## t value Pr(>|t|)
## (Intercept) 0.000 1.0000
## three_pointers_percentage_Rel 91.961 < 0.0000000000000002 ***
## two_pointers_attempted_Rel -6.964 0.000000000004283663 ***
## two_pointers_percentage_Rel 44.232 < 0.0000000000000002 ***
## free_throws_attempted_Rel 6.010 0.000000002144275626 ***
## free_throws_percentage_Rel 30.201 < 0.0000000000000002 ***
## fouls_personal_Rel -15.071 < 0.0000000000000002 ***
## fouls_Rel 1.049 0.2944
## points_from_turnovers_Rel 4.198 0.000027967276038220 ***
## points_second_chance_Rel 4.415 0.000010577967671763 ***
## points_fast_break_Rel 1.849 0.0646 .
## bench_points_Rel 0.925 0.3551
## points_in_the_paint_Rel 0.392 0.6954
## DREB_pct_Rel 38.353 < 0.0000000000000002 ***
## AST_pct_Rel -27.107 < 0.0000000000000002 ***
## BLK_pct_Rel 0.521 0.6023
## AST_TOV_ratio_Rel 8.140 0.000000000000000639 ***
## golden_hands_Rel 30.369 < 0.0000000000000002 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.505 on 2316 degrees of freedom
## Multiple R-squared: 0.9371, Adjusted R-squared: 0.9367
## F-statistic: 2031 on 17 and 2316 DF, p-value: < 0.00000000000000022
check_model(model_refined)
#Model appears to meet the assumptions and has an R-squared = 0.938
#Model checks
vif_result <- ols_vif_tol(model_refined)
vif(model_refined)
## three_pointers_percentage_Rel two_pointers_attempted_Rel
## 1.208458 3.921262
## two_pointers_percentage_Rel free_throws_attempted_Rel
## 3.552914 2.780200
## free_throws_percentage_Rel fouls_personal_Rel
## 1.050055 3.065534
## fouls_Rel points_from_turnovers_Rel
## 1.682577 2.312696
## points_second_chance_Rel points_fast_break_Rel
## 1.970939 1.261107
## bench_points_Rel points_in_the_paint_Rel
## 1.119344 4.226327
## DREB_pct_Rel AST_pct_Rel
## 2.229756 2.136990
## BLK_pct_Rel AST_TOV_ratio_Rel
## 1.167940 4.065715
## golden_hands_Rel
## 5.852320
diag_col <- ols_coll_diag(model_refined)
ress <- summary(model_refined)$coefficient
#Method of standardising coefficients (rather than scaling whole dataset). This enables us to interpret the relative importance of each variable when ranking.
coef_lmbeta <- lm.beta(model_refined)
standard_coef <- coef_lmbeta %>%
as.data.frame()
standard_coef$variable <- standard_coef$.
standard_coef <- standard_coef %>%
dplyr::select(variable)
We can then look at more Machine learning approaches.
theme_set(theme_classic())
set.seed(123)
training.samples <- RelScores$result2 %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- RelScores[training.samples, ]
test.data <- RelScores[-training.samples, ]
# Estimate preprocessing parameters
preproc.param <- train.data %>%
preProcess(method = c("center", "scale"))
## Warning in preProcess.default(., method = c("center", "scale")): These
## variables have zero variances: game_pace_Rel
# Transform the data using the estimated parameters
train.transformed <- preproc.param %>% predict(train.data)
test.transformed <- preproc.param %>% predict(test.data)
# Fit the model
#discriminant analysis for score margin
model.sm <- lda(score_margin ~ field_goals_percentage_Rel + three_pointers_percentage_Rel + two_pointers_percentage_Rel + free_throws_percentage_Rel +
rebounds_defensive_Rel + rebounds_offensive_Rel +
assists_Rel + turnovers_Rel + steals_Rel +
blocks_received_Rel + fouls_personal_Rel + fouls_on_Rel +
points_from_turnovers_Rel + points_second_chance_Rel + points_fast_break_Rel + bench_points_Rel + points_in_the_paint_Rel + TS_pct_Rel +
DREB_pct_Rel + AST_pct_Rel + BLK_pct_Rel + golden_hands_Rel + home_away + possessions_Rel, data = train.transformed)
## Warning in lda.default(x, grouping, ...): variables are collinear
# Make predictions
predictions <- model.sm %>% predict(test.transformed)
# Model accuracy
mean(predictions$class==test.transformed$score_margin)
## [1] 0.3283262
predictions.sm <- model.sm %>% predict(test.transformed)
names(predictions.sm)
## [1] "class" "posterior" "x"
lda.data.sm <- cbind(train.transformed, predict(model.sm)$x)
# ggplot(lda.data.sm, aes(LD1, LD2)) +
# geom_point(aes(color = score_margin))
mean(predictions.sm$class==test.transformed$score_margin)
## [1] 0.3283262
sum(predictions.sm$posterior[ ,1] >=.5)
## [1] 0
#Distriminant analysis for WIN-LOSS
model.wl <- lda(result ~ field_goals_percentage_Rel + three_pointers_percentage_Rel + two_pointers_percentage_Rel + free_throws_percentage_Rel +
# rebounds_defensive_Rel + rebounds_offensive_Rel +
# assists_Rel + turnovers_Rel + steals_Rel +
# blocks_received_Rel + fouls_personal_Rel + fouls_on_Rel +
# points_from_turnovers_Rel + points_second_chance_Rel + points_fast_break_Rel + bench_points_Rel + points_in_the_paint_Rel + TS_pct_Rel +
DREB_pct_Rel + TREB_pct_Rel + AST_pct_Rel + BLK_pct_Rel + golden_hands_Rel + home_away + possessions_Rel, data = train.data)
model.wl
## Call:
## lda(result ~ field_goals_percentage_Rel + three_pointers_percentage_Rel +
## two_pointers_percentage_Rel + free_throws_percentage_Rel +
## DREB_pct_Rel + TREB_pct_Rel + AST_pct_Rel + BLK_pct_Rel +
## golden_hands_Rel + home_away + possessions_Rel, data = train.data)
##
## Prior probabilities of groups:
## loss win
## 0.5 0.5
##
## Group means:
## field_goals_percentage_Rel three_pointers_percentage_Rel
## loss -5.222698 -6.460385
## win 5.365096 6.465739
## two_pointers_percentage_Rel free_throws_percentage_Rel DREB_pct_Rel
## loss -4.531049 -2.813704 -2.513910
## win 4.711991 2.622056 2.329711
## TREB_pct_Rel AST_pct_Rel BLK_pct_Rel golden_hands_Rel home_awayhome
## loss -4.255036 -2.089664 -0.6078259 -5.699239 0.4154176
## win 4.104940 2.279260 0.7201790 5.883851 0.5952891
## possessions_Rel
## loss 0.066423983
## win -0.001777302
##
## Coefficients of linear discriminants:
## LD1
## field_goals_percentage_Rel 0.0781545864
## three_pointers_percentage_Rel 0.0435090272
## two_pointers_percentage_Rel 0.0238518342
## free_throws_percentage_Rel 0.0211255125
## DREB_pct_Rel 0.0426820517
## TREB_pct_Rel -0.0007322615
## AST_pct_Rel -0.0164867848
## BLK_pct_Rel 0.0012065875
## golden_hands_Rel 0.0515148696
## home_awayhome 0.1442862065
## possessions_Rel 0.0233797042
plot(model.wl)
predictions.wl <- model.wl %>% predict(test.data)
names(predictions.wl)
## [1] "class" "posterior" "x"
# head(predictions$class, 6)
# # Predicted probabilities of class memebership.
# head(predictions$posterior, 6)
# # Linear discriminants
# head(predictions$x, 3)
lda.data.wl <- cbind(train.data, predict(model.wl)$x)
# ggplot(lda.data.wl, aes(LD1, LD2)) +
# geom_point(aes(color = result))
mean(predictions.wl$class==test.transformed$result)
## [1] 0.9248927
qda_mistakes_all <- which(predictions.wl$class != (test.data$result))
qda_error_all <- length(qda_mistakes_all) / nrow(test.data)
qda_error_all
## [1] 0.0751073
model.wl
## Call:
## lda(result ~ field_goals_percentage_Rel + three_pointers_percentage_Rel +
## two_pointers_percentage_Rel + free_throws_percentage_Rel +
## DREB_pct_Rel + TREB_pct_Rel + AST_pct_Rel + BLK_pct_Rel +
## golden_hands_Rel + home_away + possessions_Rel, data = train.data)
##
## Prior probabilities of groups:
## loss win
## 0.5 0.5
##
## Group means:
## field_goals_percentage_Rel three_pointers_percentage_Rel
## loss -5.222698 -6.460385
## win 5.365096 6.465739
## two_pointers_percentage_Rel free_throws_percentage_Rel DREB_pct_Rel
## loss -4.531049 -2.813704 -2.513910
## win 4.711991 2.622056 2.329711
## TREB_pct_Rel AST_pct_Rel BLK_pct_Rel golden_hands_Rel home_awayhome
## loss -4.255036 -2.089664 -0.6078259 -5.699239 0.4154176
## win 4.104940 2.279260 0.7201790 5.883851 0.5952891
## possessions_Rel
## loss 0.066423983
## win -0.001777302
##
## Coefficients of linear discriminants:
## LD1
## field_goals_percentage_Rel 0.0781545864
## three_pointers_percentage_Rel 0.0435090272
## two_pointers_percentage_Rel 0.0238518342
## free_throws_percentage_Rel 0.0211255125
## DREB_pct_Rel 0.0426820517
## TREB_pct_Rel -0.0007322615
## AST_pct_Rel -0.0164867848
## BLK_pct_Rel 0.0012065875
## golden_hands_Rel 0.0515148696
## home_awayhome 0.1442862065
## possessions_Rel 0.0233797042
sum(predictions.wl$posterior[ ,1] >=.5)
## [1] 232