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:
# Import
import pandas as pd
import seaborn as sns
import pingouin
from scipy.stats import mannwhitneyu
# Import data (first column of the CSV file is the index)
men_results_df = pd.read_csv('men_results.csv', index_col=0, parse_dates = ['date'])
women_results_df = pd.read_csv('women_results.csv', index_col=0, parse_dates = ['date'])
# Add columns 'Gender' in both df
men_results_df['gender'] = 'men'
women_results_df['gender'] = 'women'
# Concatenate the two df
all_results_df = pd.concat([men_results_df, women_results_df])
# Verify there are no null values
assert(~all_results_df.isna().any()).all()
# Add column total_score (sum of home and away score) and verify the sum
all_results_df['total_score'] = all_results_df['home_score'] + all_results_df['away_score']
assert (all_results_df['total_score'] == all_results_df['home_score'] + all_results_df['away_score']).all()
# Filter data
results_df = all_results_df[(all_results_df['tournament']=='FIFA World Cup') & (all_results_df['date']>'2002-01-01')]
# Corrected assertions
assert (results_df['tournament']=='FIFA World Cup').all()
assert (pd.to_datetime(results_df['date'])>'2002-01-01').all()
results_df.head()# Visualize the distribution of total score for each gender
sns.displot(x='total_score',data = results_df, hue='gender', kind = 'kde')
# Data is right-skewed (not symmetric) => not normally distributed
# Check sample size and summary stats
print(results_df.groupby('gender')['total_score'].describe())# Choice of hypothesis test :
# comparing means of 2 independents variables
# not normally distributed (non-parametrics)
# => wilcoxon
women_score = results_df[results_df['gender']=='women']['total_score']
men_score = results_df[results_df['gender']=='men']['total_score']
# Perform mannwhitneyu test with alternative='greater' as we suspect women nb of goals is higher
test_result = mannwhitneyu(x = women_score, y = men_score, alternative='greater')
print(test_result)
# Set significance level (alpha) to 10% and determine result
alpha = 0.1
result = 'reject' if test_result.pvalue <= alpha else 'fail to reject'
# Store p-value and result of the test in a dictionary
result_dict = {"p_val": test_result.pvalue, "result": result}
print(result_dict)