<jupyter_start><jupyter_text>Compare Sample Means (parametric)- Student’s t-test- Paired Student’s t-test- Analysis of Variance Test (ANOVA)- Repeated Measures ANOVA Test  t-test- It compares **mean** of **two groups**- It is a parametric statistical test.- It's used to study if there is **statistical difference** between **two groups**  Types of t-test- One sample t-test- Paired t-test(Dependent)- Unpaired t-test(Independent) Unpaired t-test also have 2 categories - Student's t-test  - Equal variance  - Two sample t-test- Welch t-test  - Unequal variance  - Unequal variance t-test  Selection of t-test- One sample t-test(for one sample)- Paired t-test(for dependent samples)- Student t-test(When sample size and variance are equal)- Welch t-test(When sample size and variance are different)<jupyter_code>import statsmodels.api as sm
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns 
sns.set(font_scale=2, palette= "viridis")
from sklearn.preprocessing import scale
import researchpy as rp
from scipy import stats
data = pd.read_csv('../data/pulse_data.csv')
data.shape
data.head()<jupyter_output><empty_output><jupyter_text>One Sample t-testIt compares the mean of one sample - Known(from previous study) mean ($\mu$)- Hypothetical mean($\mu$) Interpretation__Question: Is the average height different from a established height?____Hypothesis__ - H0: The average age is $\mu$ = 20- Ha: The average age is $\mu$ $\neq$ 20<jupyter_code>data['Height'].describe() 
stats.skew(data['Height'])
stats.kurtosis(data['Height'])
sns.histplot(data['Height'], kde=True)
plt.show() 
stats.ttest_1samp(data['Height'], 20)
stat, p,  = stats.ttest_1samp(data['Age'], 20)
print(f'stat={stat}, p-value={p}') 
alpha = 0.05 
if p > alpha:
    print('The average age is 20(fail to reject H0, result is not significant)')
else:
    print('The average age is not 20(reject H0, result is significant)')<jupyter_output>stat=1.5578736387208711, p-value=0.12221613857427135
The average age is 20(fail to reject H0, result is not significant)<jupyter_text>Student's t-test- The independent t-test is also called the two sample t-test, student’s t-test, or unpaired t-test. - It’s an univariate test that tests for a significant difference between the mean of two unrelated groups.- It compares the mean of two independent samples. AssumptionsThe assumptions that the data must meet in order for the test results to be valid are:- The independent variable (IV) is categorical with at least two levels (groups)- The dependent variable (DV) is continuous which is measured on an interval or ratio scale- The distribution of the two groups should follow the normal distribution- The variances between the two groups are equal    - This can be tested using statistical tests including Levene’s test, F-test, and Bartlett’s test.If any of these assumptions are violated then another test should be used. Interpretation__Question: Is there a difference in the height between men and women?____Hypothesis__- H0: the means of the samples are equal.- Ha: the means of the samples are unequal.__References__https://pythonfordatascienceorg.wordpress.com/independent-t-test-python/<jupyter_code>data['Gender'].unique()
data['Height'].describe()
data.shape
data.groupby('Gender')['Height'].describe()
plt.figure(figsize=(10,8))
sns.boxplot(data=data, x='Height', y="Gender")
plt.show()
# Subsets of data 
sample_01 = data[(data['Gender'] == 'Male')]

sample_02 = data[(data['Gender'] == 'Female')]
sample_01.shape, sample_02.shape
# sample size should be equal 
sample_01 = sample_01.sample(50)
sample_01.shape, sample_02.shape<jupyter_output><empty_output><jupyter_text>The Hypothesis Being Tested* Null Hypothesis (H0): u1 = u2, which translates to the mean of `sample_01` is equal to the mean of `sample 02`* Alternative Hypothesis (H1): u1 ? u2, which translates to the means of `sample01` is not equal to `sample 02`  Homogeneity of varianceOf these tests, the most common assessment for homogeneity of variance is Levene's test. The Levene's test uses an F-test to test the null hypothesis that the variance is equal across groups. A p value less than .05 indicates a violation of the assumption.https://en.wikipedia.org/wiki/Levene%27s_testhttps://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.levene.htmlTo know, [Click here](https://en.wikipedia.org/wiki/Levene%27s_test) why we test for levene's test?  Levene's test  Levene's test is an inferential statistic used to assess the equality of variances for a variable calculated for two or more groups   Interpretation - H0: The variances are equal between two groups  - Ha: The variances are not equal between two groups<jupyter_code>stats.levene(sample_01['Height'], sample_02['Height'])
stat, p,  = stats.levene(sample_01['Height'], sample_02['Height'])
print(f'stat={stat}, p-value={p}')
alpha = 0.05 
if p > alpha:
    print('The variances are equal between two groups(fail to reject H0, not significant)')
else:
    print('The variances are not equal between two groups(reject H0, significant)')<jupyter_output>stat=0.003971792169895755, p-value=0.9498772253776951
The variances are equal between two groups(fail to reject H0, not significant)<jupyter_text>__If the test were to be significant, a viable alternative would be to conduct a Welch’s t-test__  Normal Distribution  of Residuals<jupyter_code>plt.figure(figsize=(12,8))
sns.histplot(sample_01['Height'], kde=True)
plt.show() 
# Checking for normality by Q-Q plot graph
plt.figure(figsize=(12, 8))
stats.probplot(sample_01['Height'], plot=plt, dist='norm')
plt.show()<jupyter_output><empty_output><jupyter_text>__the data should be on the red line. If there are data points that are far off of it, it’s an indication that there are some deviations from normality.__  Checking normal distribution by `shapiro method`- https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.shapiro.html- https://stats.stackexchange.com/questions/15696/interpretation-of-shapiro-wilk-test<jupyter_code>stat, p_value = stats.shapiro(sample_01['Height'])
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample does not have a normal distribution(Reject the null hypothesis, the result is significant)")
stat, p_value = stats.shapiro(sample_02['Height'])
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample does not have a normal distribution(Reject the null hypothesis, the result is significant)")<jupyter_output>statistic = 0.9614583253860474, p-value = 0.10232792049646378
The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)<jupyter_text>__Note:-__[See here](https://stats.stackexchange.com/questions/15696/interpretation-of-shapiro-wilk-test)W test statistic and the second value is the p-value. Since the test statistic does not produce a significant p-value, the data is indicated to be normally distributedThe data met all the assumptions for the t-test which indicates the results can be trusted and the t-test is an appropriate test to be used.  Independent t-test by using `scipy.stats`<jupyter_code>stats.ttest_ind(sample_01['Height'], sample_02['Height'])
stat, p,  = stats.ttest_ind(sample_01['Height'], sample_02['Height'])
print(f'stat={stat}, p-value={p}')
alpha =0.05 
if p > alpha:
    print('Accept null hypothesis that the means are equal between two groups')
else:
    print('Reject the null hypothesis that the means are not equal between two groups.')<jupyter_output>stat=9.423826887131927, p-value=2.156064530440312e-15
Reject the null hypothesis that the means are not equal between two groups.<jupyter_text>Independent t-test using `researchpy`https://researchpy.readthedocs.io/en/latest/ttest_documentation.html<jupyter_code>rp.ttest(sample_01['Height'], sample_02['Height'])
descriptives, results = rp.ttest(sample_01['Height'], sample_02['Height'])
descriptives
results<jupyter_output><empty_output><jupyter_text>Paired t-test- It compares the mean between two related samples.(each subject is measured twice)<jupyter_code>bp_reading = pd.read_csv('../data/blood_pressure.csv')
bp_reading.head() 
bp_reading.shape
bp_reading.describe().T
bp_reading[['bp_before', 'bp_after']].boxplot(figsize=(12, 8))
plt.show()<jupyter_output><empty_output><jupyter_text>The Hypothesis Being Tested* Null Hypothesis (H0): u1 = u2, which translates to the mean of sample 01 is equal to the mean of sample 02* Alternative hypothesis (Ha): u1 ? u2, which translates to the means of sample 01 is not equal to sample 02  Assumption check * The samples are independently and randomly drawn* The distribution of the residuals between the two groups should follow the normal distribution* The variances between the two groups are equal<jupyter_code>stat, p,  = stats.levene(bp_reading['bp_after'], bp_reading['bp_before'])
print(f'stat={stat}, p-value={p}')
alpha = 0.05 
if p > alpha:
    print('Accept null hypothesis that the variances are equal between two groups')
else:
    print('Reject the null hypothesis that the variances are not equal between two groups.')
bp_reading['bp_diff'] = scale(bp_reading['bp_after'] - bp_reading['bp_before'])
bp_reading.head() 
bp_reading[['bp_diff']].head()
bp_reading[['bp_diff']].hist(figsize=(12, 8))
plt.show() 
plt.figure(figsize=(15, 8))
stats.probplot(bp_reading['bp_diff'], plot=plt)

plt.title('Blood pressure difference Q-Q plot')
plt.show()<jupyter_output><empty_output><jupyter_text>**Note:-** The corresponding points are lies very close to line that means are our sample data sets are normally distributed<jupyter_code>stat, p_value = stats.shapiro(bp_reading['bp_diff'])
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample does not have a normal distribution(Reject the null hypothesis, the result is significant)")<jupyter_output><empty_output><jupyter_text>Using Researchpy- https://researchpy.readthedocs.io/en/latest/ttest_documentation.html<jupyter_code>rp.ttest(bp_reading['bp_after'], bp_reading['bp_before'], 
         paired = True, equal_variances=False)<jupyter_output><empty_output><jupyter_text>Welch's t-test- It compares the mean of two independent samples.- It assumes:  - Samples don't have equal variance  - Sample size is not equal.   Welch's t-test AssumptionsLike every test, this inferential statistic test has assumptions. The assumptions that the data must meet in order for the test results to be valid are:- The independent variable (IV) is categorical with at least two levels (groups)- The dependent variable (DV) is continuous which is measured on an interval or ratio scale- The distribution of the two groups should follow the normal distributionIf any of these assumptions are violated then another test should be used. Interpretation- **Null hypothesis (H0):** u1 = u2, which translates to the mean of sample 1 is equal to the mean of sample 2- **Alternative hypothesis (HA):** u1 ≠ u2, which translates to the mean of sample 1 is not equal to the mean of sample 2<jupyter_code>us_mortality = pd.read_csv('../data/USRegionalMortality.csv')
us_mortality.head() 
sample_01 = us_mortality[(us_mortality['Cause'] == "Heart disease") & (us_mortality['Sex'] == 'Male')]

sample_02 = us_mortality[(us_mortality['Cause'] == "Heart disease") & (us_mortality['Sex'] == 'Female')]
sample_01.shape, sample_02.shape
stat, p_value =stats.shapiro(sample_01['Rate'])
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample does not have a normal distribution(Reject the null hypothesis, the result is significant)")
stat, p_value =stats.shapiro(sample_02['Rate'])
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample has normal distribution(Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample does not have a normal distribution(Reject the null hypothesis, the result is significant)")
stats.ttest_ind(sample_01['Rate'], sample_02['Rate'], equal_var = False)
stat, p_value =stats.ttest_ind(sample_01['Rate'], sample_02['Rate'], equal_var = False)
print(f'statistic = {stat}, p-value = {p_value}')
alpha = 0.05 
if p_value > alpha: 
    print("The sample means are equal (Fail to reject the null hypothesis, the result is not significant)")
else: 
    print("The sample means are not equal (Reject the null hypothesis, the result is significant)")<jupyter_output><empty_output><jupyter_text>Using Researchpy- https://researchpy.readthedocs.io/en/latest/ttest_documentation.html<jupyter_code>des, res = rp.ttest(sample_01['Rate'], sample_01['Rate'],
                            equal_variances= False)
des
res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10694423568315047, 'fraction_numerical': 0.025928152713061776, 'mean_word_length': 5.594152626362735, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 63, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '533636', 'license': 'CC0-1.0', 'n_tokens_mistral': 4375, 'n_tokens_neox': 3978, 'n_words': 1419, 'path': '07 - Inferential Stats with Python/notebooks/04_CompareSampleMeans(parametric).ipynb', 'repo_name': 'hossainlab/PY4R', 'size': '13306'}