[HEAD]: initial commit

This commit is contained in:
John Gatward
2026-08-04 19:19:24 +01:00
commit 2d9bfafbe6
39 changed files with 8905 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
{
"path": "~/football_competitiveness",
"sortOrder": [
{
"columnIndex": 2,
"ascending": true
}
]
}
@@ -0,0 +1,3 @@
{
"activeTab": 0
}
@@ -0,0 +1,14 @@
{
"left": {
"splitterpos": 312,
"topwindowstate": "NORMAL",
"panelheight": 744,
"windowheight": 782
},
"right": {
"splitterpos": 469,
"topwindowstate": "NORMAL",
"panelheight": 744,
"windowheight": 782
}
}
@@ -0,0 +1,5 @@
{
"TabSet1": 0,
"TabSet2": 0,
"TabZoom": {}
}
@@ -0,0 +1,4 @@
{
"source_window_id": "",
"Source": "Source"
}
+1
View File
@@ -0,0 +1 @@
~%2Ffootball_competitiveness%2Fdata_manipulation.R="9DEA2FB2"
@@ -0,0 +1,24 @@
{
"id": "2E827BA4",
"path": "~/football_competitiveness/data_manipulation.R",
"project_path": "data_manipulation.R",
"type": "r_source",
"hash": "0",
"contents": "",
"dirty": false,
"created": 1785865647905.0,
"source_on_save": false,
"relative_order": 1,
"properties": {
"source_window_id": "",
"Source": "Source"
},
"folds": "",
"lastKnownWriteTime": 1785865410,
"encoding": "UTF-8",
"collab_server": "",
"source_window": "",
"last_content_update": 1785865410,
"read_only": false,
"read_only_alternatives": []
}
@@ -0,0 +1,126 @@
European_performance_data = read_csv('Data/European performance historical data.csv')
Points_difference_data <- read_csv("Data/Points difference.csv")
Players_used_data <- read_csv("Data/European clubs number of players used.csv")
Top_goalscorers_data <- read_csv("Data/Average top scorers.csv")
Market_value_data <- read_csv("Data/Market values.csv")
gini_coeffecient_data <- Market_value_data %>%
select(-`Average market value of clubs`)
all_seasons <- unique(European_performance_data$Season)
overall_country_performance <- European_performance_data %>%
group_by(Country) %>%
summarise(total_performance = sum(European_performance, na.rm = TRUE)) %>%
arrange(desc(total_performance))
#season by season performance
country_performance_by_season <- European_performance_data %>%
filter(Country %in% c("Germany", "Spain", "England", "Portugal", "Italy", "France")) %>%
group_by(Country, Season) %>%
summarise(
European_performance = sum(European_performance, na.rm = TRUE),
.groups = "drop"
) %>%
complete(Country, Season = all_seasons, fill = list(European_performance = 0))
#season by season performance by competition
country_performance_by_season_comp_breakdown <- European_performance_data %>%
filter(Country %in% c("Germany", "Spain", "England", "Portugal", "Italy", "France")) %>%
group_by(Country, Season) %>%
summarise(
CL = sum(European_performance[Competition == "CL"], na.rm = TRUE),
EL = sum(European_performance[Competition == "EL"], na.rm = TRUE),
.groups = "drop"
) %>%
complete(Country, Season = all_seasons, fill = list(CL = 0, EL = 0)) %>%
pivot_longer(cols = c("CL", "EL"), names_to = "competition", values_to = "European_performance") %>%
mutate(European_performance = ifelse(competition == "CL", European_performance/2, European_performance)) #halving score for CL as had double weighting
#join all metrics data together
#impute missing Gini coefficient with average for league - assumed the same over time
season_league_metrics_dataset <- Points_difference_data %>%
left_join(Players_used_data, by = c("Country", "Season")) %>%
left_join(Top_goalscorers_data, by = c("Country", "Season")) %>%
left_join(gini_coeffecient_data, by = c("Country", "Season")) %>%
mutate(season_start = as.integer(str_extract(Season, "^\\d{4}"))) %>%
group_by(Country) %>%
mutate(Gini_coefficient = coalesce(Gini_coefficient, mean(Gini_coefficient, na.rm = TRUE))) %>%
ungroup()
#average country performance
average_country_metrics <- season_league_metrics_dataset %>%
group_by(Country) %>%
summarise(across(!contains("eason"), mean, na.rm = TRUE))
#scaling to min-max of variables
average_country_metrics_scaled <- average_country_metrics
average_country_metrics_scaled[,-1] <- lapply(average_country_metrics[,-1], function(x) {
(x - min(x)) / (max(x) - min(x))
})
#scaling metrics normally
scaled_metrics_data <- season_league_metrics_dataset %>%
mutate(across(
c(First_and_second, First_and_CL, First_and_relegated, CL_and_relegated,
Winners_GD, Top_4_total_GD, Average_number_of_players,
Average_goals_by_top_3_players, Gini_coefficient),
~ as.numeric(scale(.))
))
#join metrics data to European performance data
#calculate European performance in next season as lead
season_league_full_dataset <- season_league_metrics_dataset %>%
left_join(country_performance_by_season, by = c("Country", "Season")) %>%
arrange(Season, Country) %>%
group_by(Country) %>%
mutate(lead_european_performance = dplyr::lead(European_performance)) %>%
ungroup()
#join metrics data to European performance data by competition
#calculate European performance in next season as lead
season_league_full_dataset_comp_breakdown <- season_league_metrics_dataset %>%
left_join(country_performance_by_season_comp_breakdown, by = c("Country", "Season")) %>%
arrange(Season, Country) %>%
group_by(Country, competition) %>%
mutate(lead_european_performance = dplyr::lead(European_performance)) %>%
ungroup()
#full data scaled
scaled_full_data <- scaled_metrics_data %>%
left_join(country_performance_by_season, by = c("Country", "Season")) %>%
arrange(Season, Country) %>%
group_by(Country) %>%
mutate(lead_european_performance = dplyr::lead(European_performance)) %>%
ungroup()
#data by competition scaled
scaled_full_data_comp_breakdown <- scaled_metrics_data %>%
left_join(country_performance_by_season_comp_breakdown, by = c("Country", "Season")) %>%
arrange(Season, Country) %>%
group_by(Country, competition) %>%
mutate(lead_european_performance = dplyr::lead(European_performance)) %>%
ungroup()
#full dataset with competition breakdown and full
pivoted_scaled_full_data_comp_breakdown <- scaled_full_data_comp_breakdown %>%
pivot_wider(names_from = competition, values_from = c(European_performance, lead_european_performance)) %>%
mutate(European_performance = 2*European_performance_CL + European_performance_EL,
lead_european_performance = 2*lead_european_performance_CL + lead_european_performance_EL)
#basic profiling based on European performance
quantile_splits <- scaled_full_data %>%
mutate(
performance_group = ntile(European_performance, 3)
)
european_performance_groups_characteristics <- quantile_splits %>%
group_by(performance_group) %>%
summarise(across(where(is.numeric), mean, na.rm = TRUE)) %>%
select(-season_start) %>%
mutate(label = case_when(
performance_group == 1 ~ "Worst performing",
performance_group == 2 ~ "Middle performing",
performance_group == 3 ~ "Best performing"
)) %>%
mutate(performance_group = case_when(
label == "Worst performing" ~ 1,
label == "Middle performing" ~ 3,
label == "Best performing" ~ 2
))