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
  ))
