<jupyter_start><jupyter_text>Using t-test for testing significance of Glucose Treatment in Diabetes<jupyter_code>import pandas as pd
import researchpy as rp
import scipy.stats as stats
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv("diabetes_treatment.csv")
df.head()
df.info()
# calculate the change in HbA1c for treatment and no glucose treatment

df['HbA1c.Change'] = df['HbA1c.Followup'] - df['HbA1c.Baseline'] 
df.head()<jupyter_output><empty_output><jupyter_text>Test to see if the change in HbA1c is significantly different between patients that received the glucose treatment and the mean of the population.<jupyter_code># create a new dataframe for patients that have had the glucose treatment

df_gt = df[df['GlucoseTreatment'] == True]
df_gt.head()
df_gt['HbA1c.Change'].describe()<jupyter_output><empty_output><jupyter_text>https://seaborn.pydata.org/generated/seaborn.histplot.html<jupyter_code># create a histogram to look at the type of distribution as we need to know if it is normal distribution
# this is just the histogram for the change in HbA1c change for just patients who had the glucose treatment

sns.distplot(df_gt['HbA1c.Change'], kde=False, color='red', bins=10)
plt.title('HbA1c Change  v Frequency [Glucose Treatment]', fontsize=20)
plt.xlabel('HbA1c Change', fontsize=18)
plt.ylabel('Frequency', fontsize=18)
# we can test for normality of the distribution as well

stats.shapiro(df_gt['HbA1c.Change'])<jupyter_output><empty_output><jupyter_text>https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_1samp.html<jupyter_code># now we can do a one sample t-test on this data to see if the mean of the distribution is significantly different to zero

stats.ttest_1samp(df_gt['HbA1c.Change'], popmean=0)<jupyter_output><empty_output><jupyter_text>Test to see if the change in HbA1c is significantly different between patients that received the glucose treatment and those that did not receive it.<jupyter_code># create a new dataframe for those patients that did NOT receive the Glucose Treatment

df_Nogt = df[df['GlucoseTreatment'] == False]
df_Nogt.head()
df_Nogt['HbA1c.Change'].describe()
sns.distplot(df_Nogt['HbA1c.Change'], kde=False, color='red', bins=10)
plt.title('HbA1c Change  v Frequency [NO Glucose Treatment]', fontsize=20)
plt.xlabel('HbA1c Change', fontsize=18)
plt.ylabel('Frequency', fontsize=18)
df_gt['HbA1c.Change'].describe()
df_Nogt['HbA1c.Change'].describe()<jupyter_output><empty_output><jupyter_text>Get a visualalization of the difference in change between Treatment and No Treatment  https://seaborn.pydata.org/generated/seaborn.boxplot.html<jupyter_code># NOTE: noticet hat we are using the original dataframe df for doing this! as we are compating both Treatment/no treatment

ax = sns.boxplot(x='GlucoseTreatment', y='HbA1c.Change', data=df, order=[True, False])<jupyter_output><empty_output><jupyter_text>https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html<jupyter_code># we are going to do a 2 sample t-test for independence here instead of paired t-test as looking at change in each group

stats.ttest_ind(df_gt['HbA1c.Change'], df_Nogt['HbA1c.Change'])<jupyter_output><empty_output><jupyter_text>Alternative way to do this using Researchpy  https://researchpy.readthedocs.io/en/latest/ttest_documentation.html<jupyter_code>rp.ttest(df_gt['HbA1c.Change'], df_Nogt['HbA1c.Change'])
# we can return two python objects des = description, res = results

des, res = rp.ttest(df_gt['HbA1c.Change'], df_Nogt['HbA1c.Change'])

des
res<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1086775713889659, 'fraction_numerical': 0.01275298031605212, 'mean_word_length': 6.792656587473002, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 5, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '421957', 'license': 'MIT', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1223, 'n_words': 332, 'path': 'Linear Regression in HealthCare/DC-Stat-T-test-DiabetesTreatmentPython-Jamieson.ipynb', 'repo_name': 'Aboubacar2012/Darthmouth_DataScience_HealthCare', 'size': '3607'}