table <- fb_season_team_stats(country = "ENG",
gender = "M",
season_end_year = 1992:2023,
tier = "1st",
stat_type = "league_table")
model <- lm(Pts ~ GF, data = table)
# Coefficient is 0.96330
# R-squared is 0.7794
###Step 3: Identifying Critical Attributes
#RFE ANALYSIS
#Creating the dummy for G+A
merged_data$exPts <- merged_data$`G+A` * 0.96330 #CONVERTS G+A INTO POINTS
merged_data$exPts_dummy <- ifelse(merged_data$exPts >= 12, 1, 0)
#Manually pick which predictors make sense to include for testing relevance
predictor_columns <- unique(c(
"Att 3rd_Touches", "Succ_Take", "PrgDist_Carries", "CPA_Carries",
"PrgR_Receiving", "SoT_per_90_Standard", "Dist_Standard",
"Sh_per_90_Standard", "SoT_percent_Standard",
"Att Pen_Touches", "Succ_percent_Take", "Final_Third_Carries",
"Live_Touches", "Min_Playing"
))
#Create new dataframe with only the selected predictors and add the exPts_dummy column
merged_data_short <- merged_data %>% select(all_of(predictor_columns), exPts_dummy) %>% na.omit()
# Set up Recursive Feature Elimination (RFE) with cross-validation
set.seed(123)
control <- rfeControl(functions = rfFuncs, method = "cv", number = 10) # 10-fold cross-validation
# Convert exPts_dummy to factor (if not already done) to ensure classification is recognized
merged_data_short$exPts_dummy <- as.factor(merged_data_short$exPts_dummy)
# Run RFE to select top predictors for classification
set.seed(12)
rfe_results <- rfe(
merged_data_short[, predictor_columns],
merged_data_short$exPts_dummy,
sizes = c(5, 6, 7, 8, 10), # Specify range of subset sizes
rfeControl = control
)
#Relevant predictors:
#Att Pen_Touches, Min_Playing, SoT_per_90_Standard, Att 3rd_Touches, Live_Touches
#Using the relevant predictors to create a logistic regression model for validation
#VALIDATION OF RFE USING LOGISTIC REGRESSION
#Split the merged dataset into a training and validation set
set.seed(123)
train.index <- sample(row.names(merged_data), 0.8 * nrow(merged_data))
valid.index <- setdiff(row.names(merged_data), train.index)
# Create training and validation datasets
train.df <- merged_data[train.index, ]
valid.df <- merged_data[valid.index, ]
logistic_model <- glm(
exPts_dummy ~ `Att 3rd_Touches` + Live_Touches + Min_Playing + SoT_per_90_Standard + Min_Playing + `Att Pen_Touches`,
data = train.df,
family = "binomial"
)
#Check accuracy of logistics model of the relevant predictors
predicted_probs <- predict(logistic_model, newdata = valid.df, type = "response")
predicted_classes <- ifelse(predicted_probs > 0.5, 1, 0)
# Actual classes
actual_classes <- valid.df$exPts_dummy
# Confusion matrix
conf_matrix <- confusionMatrix(
factor(predicted_classes, levels = c(0, 1)), # Predicted values as factor
factor(actual_classes, levels = c(0, 1)) # Actual values as factor
)
#remove observations if G+A is smaller than 13
merged_data <- merged_data %>% filter(`G+A` >= 13)
# Proceed with the pre-processing and k-NN steps
preprocess_model <- preProcess(merged_data %>%
select(`Att Pen_Touches`, Live_Touches,`Att 3rd_Touches`,
SoT_per_90_Standard, Min_Playing),
method = c("center", "scale"))
# Scale the Full Dataset
big5_scaled <- predict(preprocess_model, newdata = merged_data)
# Create Optimal Player Profile Using 80th Percentile Values
optimal_player <- data.frame(
`Att 3rd_Touches` = quantile(merged_data$`Att 3rd_Touches`, 0.80, na.rm = TRUE),
Live_Touches = quantile(merged_data$Live_Touches, 0.80, na.rm = TRUE),
SoT_per_90_Standard = quantile(merged_data$SoT_per_90_Standard, 0.80, na.rm = TRUE),
Min_Playing = quantile(merged_data$Min_Playing, 0.80, na.rm = TRUE),
`Att Pen_Touches` = quantile(merged_data$`Att Pen_Touches`, 0.80, na.rm = TRUE)
)
# Rename columns in optimal_player to match preprocess_model exactly
colnames(optimal_player) <- colnames(preprocess_model$mean)
# Apply the preprocessing model to scale the optimal player profile
optimal_player_scaled <- predict(preprocess_model, newdata = optimal_player)
library(FNN)
## Warning: package 'FNN' was built under R version 4.3.3
# SCALE data for NN
big5_player_knn_scaled <- big5_scaled %>% select(`Att Pen_Touches`, Live_Touches,`Att 3rd_Touches`,
SoT_per_90_Standard, Min_Playing)
optimal_player_knn_scaled <- optimal_player_scaled %>% select(`Att Pen_Touches`, Live_Touches,`Att 3rd_Touches`,
SoT_per_90_Standard, Min_Playing)
k <- 30
big5_player_knn_scaled <- na.omit(big5_player_knn_scaled)
optimal_player_knn_scaled <- na.omit(optimal_player_knn_scaled)
# Run the k-NN function
nearest_neighbors <- get.knnx(data = big5_player_knn_scaled, query = optimal_player_knn_scaled, k = k)
closest_players_indices <- nearest_neighbors$nn.index
# Retrieve closest players from merged_data, which has the G+A filter applied
closest_players <- merged_data[closest_players_indices, ]
# Only view Player, exPts_dummy and G+A and squad
closest_players <- closest_players %>% select(Player, exPts_dummy, `G+A`, Squad, Comp, Age)
#remove players over 26
closest_players <- closest_players %>% filter(Age <= 26)
# Match-specific parameters
match_minutes <- 90 # Match Minutes for Loïs Openda
goal_rate_per_90 <- 0.8 # Openda's goals per 90 minutes in Bundesliga
assist_rate_per_90 <- 0.23 # Openda's assists per 90 minutes in Bundesliga
# Convert to per-minute rates
goal_rate_per_minute <- goal_rate_per_90 / 90
assist_rate_per_minute <- assist_rate_per_90 / 90
# Simulation parameters
num_simulations <- 10000
# Simulate goals and assists
set.seed(42) # For reproducibility
simulated_goals <- rbinom(n = num_simulations, size = match_minutes, prob = goal_rate_per_minute)
simulated_assists <- rbinom(n = num_simulations, size = match_minutes, prob = assist_rate_per_minute)
# Calculate total contributions (Goals + Assists)
simulated_contributions <- simulated_goals + simulated_assists
# Calculate summary statistics
mean_contributions <- mean(simulated_contributions)
prob_at_least_one_contribution <- mean(simulated_contributions >= 1)
prob_one_contribution<- mean(simulated_contributions == 1)
prob_at_least_two_contribution <- mean(simulated_contributions >= 2)
# Display results
cat("Probability of at least one contribution (Goal or Assist):", prob_at_least_one_contribution, "\n")
## Probability of at least one contribution (Goal or Assist): 0.6445
cat("Probability of exactly one contribution (Goal or Assist):", prob_one_contribution, "\n")
## Probability of exactly one contribution (Goal or Assist): 0.3706
cat("Probability of at least two contributions (Goals + Assists):", prob_at_least_two_contribution, "\n")
## Probability of at least two contributions (Goals + Assists): 0.2739
# Set parameters
p_at_least_one <- 0.6445 # Probability of scoring at least one goal
p_exactly_one <- 0.3706 # Probability of scoring exactly one goal
p_at_least_two <- 0.2739 # Probability of scoring at least two goals
drawn_games <- 6 # Number of drawn games
lost_games <- 6 # Number of games lost by one goal
required_points <- 12 # Target additional points
num_simulations <- 10000 # Number of Monte Carlo simulations
# Simulation function
simulate_points <- function(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations) {
total_points <- numeric(num_simulations) # Vector to store total points for each simulation
for (i in 1:num_simulations) {
points <- 0
# Simulate drawn games
for (game in 1:drawn_games) {
if (runif(1) < p_at_least_one) { # Scoring at least one goal gives 2 additional points
points <- points + 2
}
}
# Simulate lost games
for (game in 1:lost_games) {
outcome <- runif(1)
if (outcome < p_at_least_two) { # Scoring at least two goals gives 2 additional points
points <- points + 3
} else if (outcome < p_at_least_two + (p_exactly_one - p_at_least_two)) { # Scoring exactly one goal gives 1 additional point
points <- points + 1
}
}
total_points[i] <- points
}
# Calculate probability of gaining at least the required points
mean(total_points >= required_points)
}
# Run the simulation
set.seed(42) # For reproducibility
probability_12_points <- simulate_points(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations)
# Print the result
cat("Probability of gaining at least 12 points:", probability_12_points, "\n")
## Probability of gaining at least 12 points: 0.6625
#----------------------------------------------
#Repeat for Nunez
# Match-specific parameters
match_minutes <- 90 # Match Minutes for Darwin Nunez
goal_rate_per_90 <- 0.48 # Openda's goals per 90 minutes in Premier League
assist_rate_per_90 <- 0.35 # Openda's assists per 90 minutes in Premier League
# Convert to per-minute rates
goal_rate_per_minute <- goal_rate_per_90 / 90
assist_rate_per_minute <- assist_rate_per_90 / 90
# Simulation parameters
num_simulations <- 10000
# Simulate goals and assists
set.seed(42) # For reproducibility
simulated_goals <- rbinom(n = num_simulations, size = match_minutes, prob = goal_rate_per_minute)
simulated_assists <- rbinom(n = num_simulations, size = match_minutes, prob = assist_rate_per_minute)
# Calculate total contributions (Goals + Assists)
simulated_contributions <- simulated_goals + simulated_assists
# Calculate summary statistics
mean_contributions <- mean(simulated_contributions)
prob_at_least_one_contribution <- mean(simulated_contributions >= 1)
prob_one_contribution<- mean(simulated_contributions == 1)
prob_at_least_two_contribution <- mean(simulated_contributions >= 2)
# Display results
cat("Probability of at least one contribution (Goal or Assist):", prob_at_least_one_contribution, "\n")
## Probability of at least one contribution (Goal or Assist): 0.5634
cat("Probability of exactly one contribution (Goal or Assist):", prob_one_contribution, "\n")
## Probability of exactly one contribution (Goal or Assist): 0.3592
cat("Probability of at least two contributions (Goals + Assists):", prob_at_least_two_contribution, "\n")
## Probability of at least two contributions (Goals + Assists): 0.2042
# Set parameters
p_at_least_one <- 0.5634 # Probability of scoring at least one goal
p_exactly_one <- 0.3592 # Probability of scoring exactly one goal
p_at_least_two <- 0.2042 # Probability of scoring at least two goals
drawn_games <- 6 # Number of drawn games
lost_games <- 6 # Number of games lost by one goal
required_points <- 12 # Target additional points
num_simulations <- 10000 # Number of Monte Carlo simulations
# Simulation function
simulate_points <- function(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations) {
total_points <- numeric(num_simulations) # Vector to store total points for each simulation
for (i in 1:num_simulations) {
points <- 0
# Simulate drawn games
for (game in 1:drawn_games) {
if (runif(1) < p_at_least_one) { # Scoring at least one goal gives 2 additional points
points <- points + 2
}
}
# Simulate lost games
for (game in 1:lost_games) {
outcome <- runif(1)
if (outcome < p_at_least_two) { # Scoring at least two goals gives 2 additional points
points <- points + 3
} else if (outcome < p_at_least_two + (p_exactly_one - p_at_least_two)) { # Scoring exactly one goal gives 1 additional point
points <- points + 1
}
}
total_points[i] <- points
}
# Calculate probability of gaining at least the required points
mean(total_points >= required_points)
}
# Run the simulation
set.seed(42) # For reproducibility
probability_12_points <- simulate_points(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations)
# Print the result
cat("Probability of gaining at least 12 points:", probability_12_points, "\n")
## Probability of gaining at least 12 points: 0.4765
#----------------------------------------------
#repeat for Undav
# Match-specific parameters
match_minutes <- 90 # Match Minutes for Denis Undav
goal_rate_per_90 <- 0.78 # Openda's goals per 90 minutes in Bundesliga
assist_rate_per_90 <- 0.43 # Openda's assists per 90 minutes in Bundesliga
# Convert to per-minute rates
goal_rate_per_minute <- goal_rate_per_90 / 90
assist_rate_per_minute <- assist_rate_per_90 / 90
# Simulation parameters
num_simulations <- 10000
# Simulate goals and assists
set.seed(42) # For reproducibility
simulated_goals <- rbinom(n = num_simulations, size = match_minutes, prob = goal_rate_per_minute)
simulated_assists <- rbinom(n = num_simulations, size = match_minutes, prob = assist_rate_per_minute)
# Calculate total contributions (Goals + Assists)
simulated_contributions <- simulated_goals + simulated_assists
# Calculate summary statistics
mean_contributions <- mean(simulated_contributions)
prob_at_least_one_contribution <- mean(simulated_contributions >= 1)
prob_one_contribution<- mean(simulated_contributions == 1)
prob_at_least_two_contribution <- mean(simulated_contributions >= 2)
# Display results
cat("Probability of at least one contribution (Goal or Assist):", prob_at_least_one_contribution, "\n")
## Probability of at least one contribution (Goal or Assist): 0.7009
cat("Probability of exactly one contribution (Goal or Assist):", prob_one_contribution, "\n")
## Probability of exactly one contribution (Goal or Assist): 0.3638
cat("Probability of at least two contributions (Goals + Assists):", prob_at_least_two_contribution, "\n")
## Probability of at least two contributions (Goals + Assists): 0.3371
# Set parameters
p_at_least_one <- 0.7009 # Probability of scoring at least one goal
p_exactly_one <- 0.3638 # Probability of scoring exactly one goal
p_at_least_two <- 0.3371 # Probability of scoring at least two goals
drawn_games <- 6 # Number of drawn games
lost_games <- 6 # Number of games lost by one goal
required_points <- 12 # Target additional points
num_simulations <- 10000 # Number of Monte Carlo simulations
# Simulation function
simulate_points <- function(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations) {
total_points <- numeric(num_simulations) # Vector to store total points for each simulation
for (i in 1:num_simulations) {
points <- 0
# Simulate drawn games
for (game in 1:drawn_games) {
if (runif(1) < p_at_least_one) { # Scoring at least one goal gives 2 additional points
points <- points + 2
}
}
# Simulate lost games
for (game in 1:lost_games) {
outcome <- runif(1)
if (outcome < p_at_least_two) { # Scoring at least two goals gives 2 additional points
points <- points + 3
} else if (outcome < p_at_least_two + (p_exactly_one - p_at_least_two)) { # Scoring exactly one goal gives 1 additional point
points <- points + 1
}
}
total_points[i] <- points
}
# Calculate probability of gaining at least the required points
mean(total_points >= required_points)
}
# Run the simulation
set.seed(42) # For reproducibility
probability_12_points <- simulate_points(drawn_games, lost_games, p_at_least_one, p_exactly_one, p_at_least_two, required_points, num_simulations)