Hypothesis Testing with Men's and Women's Soccer Matches
  • AI Chat
  • Code
  • Report
  • Beta
    Spinner

    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, 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 numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    men = pd.read_csv('men_results.csv')
    men_copy = men.copy()
    men_copy['date'] = pd.to_datetime(men_copy['date'])
    men_clean = men_copy.loc[
        (men_copy['tournament']=='FIFA World Cup') &
        (men_copy['date']>= '2002-01-01')
    ]
    men_clean.drop(['Unnamed: 0'], axis=1, inplace=True)
    men_clean['goals'] = men_clean['home_score'] + men_clean['away_score']
    men_clean.head(10)
    women = pd.read_csv('women_results.csv')
    women_copy = women.copy()
    women_copy['date'] = pd.to_datetime(women_copy['date'])
    women_clean = women_copy.loc[
        (women_copy['tournament']=='FIFA World Cup') &
        (women_copy['date']>= '2002-01-01')
    ]
    women_clean.drop(['Unnamed: 0'], axis=1, inplace=True)
    women_clean['goals'] = women_clean['home_score'] + women_clean['away_score']
    women_clean.head(10)
    men_clean.info()
    def bootstrap_replicate_1d(data, func):
        """Generate bootstrap replicate of 1D data."""
        bs_sample = np.random.choice(data, len(data))
    
        return func(bs_sample)
    
    def draw_bs_reps(data, func, size=1):
        """Draw `size` bootstrap replicates."""
        # Initialize array of replicates
        bs_replicates = np.empty(size)
    
        # Generate the replicates
        for i in range(size):
          bs_replicates[i] = bootstrap_replicate_1d(data, func)
    
        return bs_replicates
    men_clean_goals = draw_bs_reps(men_clean['goals'], np.mean, 100_000)
    women_clean_goals = draw_bs_reps(women_clean['goals'], np.mean, 100_000)
    sns.histplot(men_clean_goals, kde=True)
    sns.histplot(women_clean_goals, kde=True)
    plt.title('Distribution of the Means')
    plt.xlabel('Mean Score Per Match')
    plt.ylabel('Frequency')
    plt.legend(['Men', 'Women'])
    plt.show()
    p = np.mean(women_clean_goals > men_clean_goals)
    res = {'p_val': round(1-p,4), 'result': 'reject'}
    print(res)
    # Imports
    import pandas as pd
    import pingouin
    from scipy.stats import mannwhitneyu
    
    # Load men's and women's datasets
    men = pd.read_csv("men_results.csv")
    women = pd.read_csv("women_results.csv")
    
    # Filter the data for the time range and tournament
    men["date"] = pd.to_datetime(men["date"])
    men_subset = men[(men["date"] > "2002-01-01") & (men["tournament"].isin(["FIFA World Cup"]))]
    women["date"] = pd.to_datetime(women["date"])
    women_subset = women[(women["date"] > "2002-01-01") & (women["tournament"].isin(["FIFA World Cup"]))]
    
    # Create group and goals_scored columns
    men_subset["group"] = "men"
    women_subset["group"] = "women"
    men_subset["goals_scored"] = men_subset["home_score"] + men_subset["away_score"]
    women_subset["goals_scored"] = women_subset["home_score"] + women_subset["away_score"]
    
    # Combine women's and men's data and calculate goals scored in each match
    both = pd.concat([women_subset, men_subset], axis=0, ignore_index=True)
    
    # Transform the data for the pingouin Mann-Whitney U t-test/Wilcoxon-Mann-Whitney test
    both_subset = both[["goals_scored", "group"]]
    both_subset_wide = both_subset.pivot(columns="group", values="goals_scored")
    
    # Perform right-tailed Wilcoxon-Mann-Whitney test with pingouin
    results_pg = pingouin.mwu(x=both_subset_wide["women"],
                              y=both_subset_wide["men"],
                              alternative="greater")
    
    # Alternative SciPy solution: Perform right-tailed Wilcoxon-Mann-Whitney test with scipy
    results_scipy = mannwhitneyu(x=women_subset["goals_scored"],
                                 y=men_subset["goals_scored"],
                                 alternative="greater")
    
    # Extract p-value as a float and round
    p_val = round(results_pg["p-val"].values[0], 4)
    
    # 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}
    print(result_dict)
    results_pg
    results_scipy
    sns.histplot(data=both, x='goals_scored', hue='group', kde=True)
    plt.title('Distribution of the Means')
    plt.xlabel('Scores Per Match')
    plt.ylabel('Frequency')
    # plt.legend(['Men', 'Women'])
    plt.show()
    sns.ecdfplot(
        data=both, x="goals_scored", hue="group",
        hue_order=["women", "men"]
    )
    plt.show()