Skip to content

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.

# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import mannwhitneyu
import pingouin as pg

# Load the datasets
men = pd.read_csv("men_results.csv")
women = pd.read_csv("women_results.csv")

# Convert the date columns to datetime format
men['date'] = pd.to_datetime(men['date'])
women['date'] = pd.to_datetime(women['date'])

# Filter for FIFA World Cup matches after January 1, 2002
men_wc = men[(men['date'] > '2002-01-01') & (men['tournament'] == 'FIFA World Cup')]
women_wc = women[(women['date'] > '2002-01-01') & (women['tournament'] == 'FIFA World Cup')]

# Create columns to track the groups and calculate total goals per match
men_wc['group'] = 'men'
women_wc['group'] = 'women'
men_wc['goals_scored'] = men_wc['home_score'] + men_wc['away_score']
women_wc['goals_scored'] = women_wc['home_score'] + women_wc['away_score']

# Visualize the goal distributions for normality checks
plt.figure()
men_wc['goals_scored'].hist(bins=15, alpha=0.7, label='Men')
women_wc['goals_scored'].hist(bins=15, alpha=0.7, label='Women')
plt.legend()
plt.title('Goals Scored Distribution')
plt.show()

# Combine both datasets for the Wilcoxon-Mann-Whitney test
combined_goals = pd.concat([men_wc[['goals_scored', 'group']], 
                             women_wc[['goals_scored', 'group']]], 
                             axis=0, ignore_index=True)

# Pivot to create a wide format for the test
combined_wide = combined_goals.pivot(columns='group', values='goals_scored')

# Perform the Wilcoxon-Mann-Whitney test (right-tailed) using pingouin
test_results = pg.mwu(x=combined_wide['women'], y=combined_wide['men'], alternative='greater')

# Alternative approach using scipy
scipy_results = mannwhitneyu(women_wc['goals_scored'], men_wc['goals_scored'], alternative='greater')

# Extract the p-value from the pingouin test results
p_val = test_results['p-val'].values[0]

# Determine the result based on a significance level of 0.01
if p_val <= 0.01:
    result = "reject"
else:
    result = "fail to reject"

# Store the p-value and hypothesis test result in a dictionary
result_dict = {"p_val": p_val, "result": result}

# Print the result
print(result_dict)