Skip to content
Project: Hypothesis Testing with Men's and Women's Soccer Matches
  • AI Chat
  • Code
  • Report
  • You're working as a sports journalist at a major online sports media company, specializing in soccer analysis and reporting. You've been watching both men's and women's international soccer matches for a number of years, and your gut instinct tells you that more goals are scored in women's international football matches than men's. This would make an interesting investigative article that your subscribers are bound to love, but you'll need to perform a valid statistical hypothesis test to be sure!

    While scoping this project, you acknowledge that the sport has changed a lot over the years, and performances likely vary a lot depending on the tournament, so you decide to limit the data used in the analysis to only official FIFA World Cup matches (not including qualifiers) since 2002-01-01.

    You create two datasets containing the results of every official men's and women's international football match since the 19th century, which you scraped from a reliable online source. This data is stored in two CSV files: women_results.csv and men_results.csv.

    The question you are trying to determine the answer to is:

    Are more goals scored in women's international soccer matches than men's?

    You assume a 10% significance level, and use the following null and alternative hypotheses:

    : The mean number of goals scored in women's international soccer matches is the same as men's.

    : The mean number of goals scored in women's international soccer matches is greater than men's.

    # Start your code here!
    import pandas as pd
    import matplotlib.pyplot as plt
    import pingouin
    from scipy.stats import mannwhitneyu
    
    #Load men's and women's datasets
    men_results = pd.read_csv('men_results.csv')
    women_results = pd.read_csv('women_results.csv')
    
    #Adjust dates format
    men_results['date'] = pd.to_datetime(men_results['date'])
    women_results['date'] = pd.to_datetime(women_results['date'])
    
    #Filtering the data for the time range and tournament
    men_subset = men_results[(men_results['date'] >= "2002-01-01") & (men_results['tournament'].isin(["FIFA World Cup"]))]
    women_subset = women_results[(women_results['date'] >= "2002-01-01") & (women_results['tournament'].isin(["FIFA World Cup"]))]
    
    #Adding goals in each group
    men_subset["Total_goals"] = men_subset["home_score"] + men_subset["away_score"]
    women_subset["Total_goals"] = women_subset["home_score"] + women_subset["away_score"]
    
    #creating groups columns
    women_subset["type"] = "women"
    men_subset["type"] = "men"
    
    #Cheking histogram for distributions
    fig, axs = plt.subplots(ncols=2)
    axs[0].hist(men_subset["Total_goals"])
    axs[0].set_xlabel("Men's goals")
    axs[1].hist(women_subset["Total_goals"])
    axs[1].set_xlabel("Women's goals")
    plt.show()
    
    #concat both of the data sets
    both = pd.concat([women_subset,men_subset],axis=0, ignore_index=True)
    
    #Transfor the data for the pongouin tests
    both_subset = both[["Total_goals","type"]]
    both_subset_w = both_subset.pivot(columns="type",values="Total_goals")
    
    #Wilcoxon-Mann-Whitney test of two groups (Histograms are not normally distributed)
    results_pg = pingouin.mwu(x=both_subset_w["women"],y=both_subset_w["men"],alternative="greater")
    results_scipy = mannwhitneyu(x=women_subset["Total_goals"],
                                 y=men_subset["Total_goals"],
                                 alternative="greater")
    # Extract p-value as a float
    p_val = results_pg["p-val"].values[0]
    
    # Determine hypothesis test result using sig. level
    if p_val <= 0.01:
        result = "reject"
    else:
        result = "fail to reject"
    
    result_dict = {"p_val": p_val, "result": result}