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:
# Start your code here!
import pandas as pd
import seaborn as sns
men_results_df = pd.read_csv('men_results.csv', parse_dates=['date'])
women_results_df = pd.read_csv('women_results.csv', parse_dates=['date'])
men_results_df = men_results_df[men_results_df.date >= '2002-01-01']
women_results_df = women_results_df[women_results_df.date >= '2002-01-01']
men_results_df = men_results_df[men_results_df.tournament == 'FIFA World Cup']
women_results_df = women_results_df[women_results_df.tournament == 'FIFA World Cup']print(men_results_df.shape, women_results_df.shape)men_results_df['total_goals'] = men_results_df.home_score + men_results_df.away_score
women_results_df['total_goals'] = women_results_df.home_score + women_results_df.away_scoreimport scipy.stats as stats
# Null hypothesis: men_results_df.total_goals is normally distributed
# Alternative hypothesis: men_results_df.total_goals is not normally distributed
alpha = 0.05
# Shapiro-Wilk test for normality
stat, p = stats.shapiro(men_results_df.total_goals)
if p > alpha:
print("p-value = ", p)
print("Fail to reject the null hypothesis. men_results_df.total_goals is normally distributed.")
else:
print("p-value = ", p)
print("Reject the null hypothesis. men_results_df.total_goals is not normally distributed.")
# Shapiro-Wilk test for normality
stat, p = stats.shapiro(women_results_df.total_goals)
if p > alpha:
print("p-value = ", p)
print("Fail to reject the null hypothesis. women_results_df.total_goals is normally distributed.")
else:
print("p-value = ", p)
print("Reject the null hypothesis. women_results_df.total_goals is not normally distributed.")sns.histplot(data=men_results_df, x='total_goals', binwidth=1)sns.histplot(data=women_results_df, x='total_goals', binwidth=1)men_results_df['group'] = 'men'
women_results_df['group'] = 'women'both_df = pd.concat([men_results_df, women_results_df], ignore_index=True, axis=0)
pivot_df = both_df[['total_goals', 'group']].pivot(columns='group', values='total_goals')import pingouin
results_pg = pingouin.mwu(x=pivot_df["women"],
y=pivot_df["men"],
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)