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.

# Start your code here!
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pingouin
from scipy.stats import t

alpha = 0.1

Perform right-tailed (greater than) t-test (comparison of two means) on the data with alpha = 0.1 for a 10% significance level

mean of women's goals - mean of men's goals is sample stat for hyp test

  1. Calc mean goals, length of df's, and stddev of both data sets
  2. Calc t stat
  3. Get p-value
  4. Compare p-value to alpha
# Women data set values
# Load data set with Pandas
women_goals_full = pd.read_csv('women_results.csv', parse_dates=['date'])

# Keep dates since 2002-01-01 and in FIFA World Cup tournament
women_goals = women_goals_full[(women_goals_full['date'] >= '2002-01-01') & (women_goals_full['tournament'] == 'FIFA World Cup')] 

# Make new column for total goals, women group and calculate average total goals for sample statistic
women_goals['total_goals'] = women_goals['home_score'] + women_goals['away_score']
women_goals['group'] = 'women'
x_bar_women = women_goals['total_goals'].mean()

# Calculate standard deviation of total goals scored
s_women = women_goals['total_goals'].std()

# Calculate length of data set for n
n_women = len(women_goals)

print(women_goals.head())

# Check if data is normally distributed
sns.histplot(data = women_goals, x = 'total_goals')
plt.show()
# Men data set values
# Load data set with Pandas
men_goals_full = pd.read_csv('men_results.csv', parse_dates=['date'])

# Keep dates since 2002-01-01 and in FIFA World Cup tournament
men_goals = men_goals_full[(men_goals_full['date'] >= '2002-01-01') & (men_goals_full['tournament'] == 'FIFA World Cup')]

men_goals.head()

# Make new column for total goals, men group and calculate average total goals for sample statistic
men_goals['total_goals'] = men_goals['home_score'] + men_goals['away_score']
men_goals['group'] = 'men'
x_bar_men = men_goals['total_goals'].mean()

# Calculate standard deviation of total goals scored
s_men = men_goals['total_goals'].std()

# Calculate length of data set for n
n_men = len(men_goals)

men_goals.head()

# Check if data is normally distributed
sns.histplot(data = men_goals, x = 'total_goals')
plt.show()
Hidden code
Hidden code
# Use Wilcoxon-Mann-Whitney test for non-normal population distribution
# Combine both data sets for analysis
combined = pd.concat([women_goals, men_goals], axis = 0, ignore_index = True)

#Take subset of goals and groups to convert to wide format
combined_subset = combined[['total_goals', 'group']]

print(combined_subset.head())
# Convert total_goals into wide format
combined_wide = combined.pivot(columns='group', values='total_goals')
print(combined_wide.head())

# Run a two-sided Wilcoxon-Mann-Whitney test on women's and men's goals
wmw_test = pingouin.mwu(x = combined_wide['women'], y = combined_wide['men'], alternative = 'greater')

# Print the test results
print(wmw_test)
p_val = wmw_test['p-val'].to_numpy(dtype = float)
print(p_val)
# If statement for results
if p_val < alpha:
    result = 'reject'
else: 
    result = 'fail to reject'

result_dict = {'p_val': p_val, 'result': result}
print(result_dict)