Independent samples t-test, multiple populations¶
Here we expand on the example with independent samples t-test and incorporate itertool.combinations()to test several populations against eachother in an effective way. We use the same dataset as in the previous example on independent samples t-test but slice the dataset by gaming platform instead of gender.
In [ ]:
import pandas as pd
from itertools import combinations
from scipy.stats import ttest_ind
# Import data
df = pd.read_csv('Gaming and Mental Health.csv')
# Extract unique entries in relevant column
populations = df['gaming_platform'].unique()
# Create a list of tuples with all possible combinations of 2 elements
possible_combinations = list(combinations(populations, 2))
# Create empty lists, we append results from each iteration here in order to be able to create a summary in the end
pop_1 = []
pop_2 = []
t_stat = []
p_value = []
# Iterate over the list of combinations
for combination in possible_combinations:
# Perform t-test
t, p = ttest_ind(df[df['gaming_platform']==combination[0]]['daily_gaming_hours'], df[df['gaming_platform']==combination[1]]['daily_gaming_hours'], equal_var=False)
# Append data
pop_1.append(combination[0])
pop_2.append(combination[1])
t_stat.append(t)
p_value.append(p)
# Create a df from the lists with the results
df_summary = pd.DataFrame({'Population 1': pop_1,
'Population 2': pop_2,
't-stat': t_stat,
'p-value': p_value})
# Take a peek a the result, round to two digits for readability
print(df_summary.round(2))
Some more reading: https://www.geeksforgeeks.org/maths/t-test-in-statistics/
This tutorial uses the “Gaming and Mental Health” licensed under the Apache License 2.0. © Shaista Shahid. License: http://www.apache.org/licenses/LICENSE-2.0 source: https://www.kaggle.com/datasets/shaistashahid/gaming-and-mental-health?resource=download