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:
# Imports
import pandas as pd
from scipy.stats import mannwhitneyu
# Load the datasets
men = pd.read_csv("men_results.csv")
women = pd.read_csv("women_results.csv")
# Convert date columns to datetime
men["date"] = pd.to_datetime(men["date"])
women["date"] = pd.to_datetime(women["date"])
# Filter data for World Cup matches from 2002 onwards
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")]
# Calculate total goals scored in each match
men_wc["goals_scored"] = men_wc["home_score"] + men_wc["away_score"]
women_wc["goals_scored"] = women_wc["home_score"] + women_wc["away_score"]
# Perform the Wilcoxon-Mann-Whitney test (one-tailed)
stat, p_val = mannwhitneyu(women_wc["goals_scored"], men_wc["goals_scored"], alternative="greater")
# Determine the result based on the p-value and the significance level
alpha = 0.10
result = "reject" if p_val <= alpha else "fail to reject"
# Store the results in a dictionary
result_dict = {"p_val": p_val, "result": result}
print(result_dict)A brief explanation of each part of code
# Imports import pandas as pd from scipy.stats import mannwhitneyu
import pandas as pd: Imports thepandaslibrary, which is used for data manipulation and analysis.from scipy.stats import mannwhitneyu: Imports themannwhitneyufunction fromscipy.stats, used to perform the Wilcoxon-Mann-Whitney test.
# Load the datasets men = pd.read_csv("men_results.csv") women = pd.read_csv("women_results.csv")
pd.read_csv("men_results.csv"): Loads the CSV file containing men's match results into a DataFrame namedmen.pd.read_csv("women_results.csv"): Loads the CSV file containing women's match results into a DataFrame namedwomen.
# Convert date columns to datetime men["date"] = pd.to_datetime(men["date"]) women["date"] = pd.to_datetime(women["date"])
pd.to_datetime(men["date"]): Converts thedatecolumn in themenDataFrame from a string to a datetime format.pd.to_datetime(women["date"]): Converts thedatecolumn in thewomenDataFrame from a string to a datetime format.
# Filter data for World Cup matches from 2002 onwards 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")]
men["date"] > "2002-01-01": Filters themenDataFrame to include only rows where the date is after January 1, 2002.men["tournament"] == "FIFA World Cup": Further filters to include only rows where the tournament is the FIFA World Cup.women["date"] > "2002-01-01": Filters thewomenDataFrame to include only rows where the date is after January 1, 2002.women["tournament"] == "FIFA World Cup": Further filters to include only rows where the tournament is the FIFA World Cup.men_wc: DataFrame containing filtered data for men's World Cup matches.women_wc: DataFrame containing filtered data for women's World Cup matches.
# Calculate total goals scored in each match men_wc["goals_scored"] = men_wc["home_score"] + men_wc["away_score"] women_wc["goals_scored"] = women_wc["home_score"] + women_wc["away_score"]
men_wc["home_score"] + men_wc["away_score"]: Calculates the total goals scored in each match by summing the home and away scores for men's matches.women_wc["home_score"] + women_wc["away_score"]: Calculates the total goals scored in each match by summing the home and away scores for women's matches.men_wc["goals_scored"]: Adds a new column to themen_wcDataFrame for the total goals scored.women_wc["goals_scored"]: Adds a new column to thewomen_wcDataFrame for the total goals scored.
# Perform the Wilcoxon-Mann-Whitney test (one-tailed) stat, p_val = mannwhitneyu(women_wc["goals_scored"], men_wc["goals_scored"], alternative="greater")
mannwhitneyu(women_wc["goals_scored"], men_wc["goals_scored"], alternative="greater"): Performs the Wilcoxon-Mann-Whitney test to compare the distribution of goals scored in women's matches to that in men's matches. Thealternative="greater"parameter specifies a one-tailed test to check if the mean number of goals scored in women's matches is greater than in men's matches.stat: The test statistic returned by themannwhitneyufunction.p_val: The p-value returned by themannwhitneyufunction.
# Determine the result based on the p-value and the significance level alpha = 0.10 result = "reject" if p_val <= alpha else "fail to reject"
alpha = 0.10: Sets the significance level (alpha) to 0.10 (10%).result = "reject" if p_val <= alpha else "fail to reject": Compares the p-value to the significance level. If the p-value is less than or equal to the significance level, it indicates sufficient evidence to reject the null hypothesis; otherwise, it indicates insufficient evidence to reject the null hypothesis.
# Store the results in a dictionary result_dict = {"p_val": p_val, "result": result}
result_dict = {"p_val": p_val, "result": result}: Creates a dictionary to store the p-value and the result of the hypothesis test.