0% found this document useful (0 votes)
16 views8 pages

Methodolgy

Uploaded by

sakshamsehrawet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Methodolgy

Uploaded by

sakshamsehrawet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

import pandas as pd

from scipy.stats import chi2_contingency, ttest_ind


import numpy as np

# Data for the analysis


data = {
"age": [51, 39, 28, 60, 52, 48, 77, 55, 43, 25, 55, 55, 49, 48,
46, 56, 31, 50, 57, 35, 45, 23, 31, 53, 53, 44, 71, 48, 24, 43, 35,
22],
"sex": [0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0],
"BMI": [52.1, 44.5, 60, 38.2, 42.9, 35.8, 42.19, 49.3, 51.1, 46.2,
51.5, 46.8, 38.1, 62, 46.7, 60, 50, 48, 46.2, 40, 41.5, 43.6, 51.1,
53.4, 46.6, 40, 35.7, 68, 36, 46.4, 46.5, 45.4],
"surgery": [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0],
"haemoglobin_status": [0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
"B12_status": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1],
"Ionized_Ca_status": [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0],
"Vit_D3_status": [1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1]
}

# Convert to DataFrame
df = pd.DataFrame(data)

# Replace status values


statuses = ["haemoglobin_status", "B12_status", "Ionized_Ca_status",
"Vit_D3_status"]
for status in statuses:
df[status] = df[status].replace({1: 'deficient', 0: 'normal'})

# Helper function to create contingency tables with percentages


def create_contingency_table(variable, status):
contingency_table = pd.crosstab(variable, status)
contingency_table_percentage = contingency_table.apply(lambda r: r
/ len(variable) * 100, axis=1)
contingency_table_combined = contingency_table.astype(str) + " ("
+ contingency_table_percentage.round(1).astype(str) + "%)"
return contingency_table_combined
# Helper function to print tables in CSV format
def print_csv_format(contingency_table, table_name):
print(f"\n{table_name}")
print(contingency_table.to_csv(index=True))
# Analyze each status
for status in statuses:
print(f"\n\nAnalysis for
{status}--------------------------------------------")

# Total and percentage of patients


total_deficient = df[df[status] == 'deficient'].shape[0]
total_normal = df[df[status] == 'normal'].shape[0]
percent_deficient = round((total_deficient / len(df)) * 100, 1)
percent_normal = round((total_normal / len(df)) * 100, 1)

# Calculate mean and SD for age


mean_age_normal = df[df[status] == 'normal']['age'].mean()
sd_age_normal = df[df[status] == 'normal']['age'].std()
mean_age_deficient = df[df[status] == 'deficient']['age'].mean()
sd_age_deficient = df[df[status] == 'deficient']['age'].std()
# Calculate mean and SD for BMI
mean_bmi_normal = df[df[status] == 'normal']['BMI'].mean()
sd_bmi_normal = df[df[status] == 'normal']['BMI'].std()
mean_bmi_deficient = df[df[status] == 'deficient']['BMI'].mean()
sd_bmi_deficient = df[df[status] == 'deficient']['BMI'].std()
# AGE
bins_age = [0, 30, 40, 50, 60, float('inf')]
df['Age Category'] = pd.cut(df['age'], bins=bins_age,
labels=["<=30", "31-40", "41-50", "51-60", "61+"])
contingency_table_age = create_contingency_table(df['Age
Category'], df[status])
chi2_stat_age, p_val_age, dof_age, ex_age =
chi2_contingency(pd.crosstab(df['Age Category'], df[status]))

# SEX
contingency_table_sex = create_contingency_table(df['sex'],
df[status])
chi2_stat_sex, p_val_sex, dof_sex, ex_sex =
chi2_contingency(pd.crosstab(df['sex'], df[status]))

# BMI
bins_bmi = [0, 30, 40, 50, float('inf')]
df['BMI Category'] = pd.cut(df['BMI'], bins=bins_bmi,
labels=["<=30", "31-40", "41-50", "51+"])
contingency_table_bmi = create_contingency_table(df['BMI
Category'], df[status])
chi2_stat_bmi, p_val_bmi, dof_bmi, ex_bmi =
chi2_contingency(pd.crosstab(df['BMI Category'], df[status]))

# SURGERY
contingency_table_surgery =
create_contingency_table(df['surgery'], df[status])
chi2_stat_surgery, p_val_surgery, dof_surgery, ex_surgery =
chi2_contingency(pd.crosstab(df['surgery'], df[status]))

# Display results
print(f"n (Deficient,Normal): ,n = {total_deficient}
({percent_deficient}%),n = {total_normal}({percent_normal}%)")
print(f"Age (Deficient,Normal): ,{mean_age_deficient:.2f} ±
{sd_age_deficient:.2f},{mean_age_normal:.2f} ± {sd_age_normal:.2f}")

# Simulate the ages using the given mean and standard deviation
np.random.seed(0) # For reproducibility
ages_deficient = np.random.normal(mean_age_deficient,
sd_age_deficient, total_deficient)
ages_normal = np.random.normal(mean_age_normal, sd_age_normal,
total_normal)

# Perform the t-test


t_stat, p_value = ttest_ind(ages_deficient, ages_normal,
equal_var=False)

print(f"T-statistic: {t_stat.round(3)}")
print(f"P-value: ,{p_value.round(3)}")
print_csv_format(contingency_table_age, "Contingency Table for
Age")
print(f"\nChi-square statistic: {chi2_stat_age.round(3)}")
print(f"P-value: ,{p_val_age.round(3)}")
print_csv_format(contingency_table_sex, "Contingency Table for
Sex")
print(f"\nChi-square statistic: {chi2_stat_sex.round(3)}")
print(f"P-value: ,{p_val_sex.round(3)}")

print(f"\nBMI (Deficient,Normal): ,{mean_bmi_deficient:.2f} ±


{sd_bmi_deficient:.2f},{mean_bmi_normal:.2f} ± {sd_bmi_normal:.2f}")

# Simulate the ages using the given mean and standard deviation
np.random.seed(0) # For reproducibility
bmi_deficient = np.random.normal(mean_bmi_deficient,
sd_bmi_deficient, total_deficient)
bmi_normal = np.random.normal(mean_bmi_normal, sd_bmi_normal,
total_normal)

# Perform the t-test


t_stat, p_value = ttest_ind(bmi_deficient,bmi_normal,
equal_var=False)

print(f"T-statistic: {t_stat.round(3)}")
print(f"P-value: ,{p_value.round(3)}")
print_csv_format(contingency_table_bmi, "Contingency Table for
BMI")
print(f"\nChi-square statistic: {chi2_stat_bmi.round(3)}")
print(f"P-value: ,{p_val_bmi.round(3)}")
print_csv_format(contingency_table_surgery, "Contingency Table for
Surgery")
print(f"\nChi-square statistic: {chi2_stat_surgery.round(3)}")
print(f"P-value: ,{p_val_surgery.round(3)}")

Analysis for
haemoglobin_status--------------------------------------------
n (Deficient,Normal): ,n = 11(34.4%),n = 21(65.6%)
Age (Deficient,Normal): ,40.09 ± 10.79,48.14 ± 13.99
T-statistic: -1.045
P-value: ,0.305

Contingency Table for Age


Age Category,deficient,normal
<=30,3 (9.4%),2 (6.2%)
31-40,1 (3.1%),4 (12.5%)
41-50,6 (18.8%),4 (12.5%)
51-60,1 (3.1%),9 (28.1%)
61+,0 (0.0%),2 (6.2%)

Chi-square statistic: 8.506


P-value: ,0.075

Contingency Table for Sex


sex,deficient,normal
0,9 (28.1%),14 (43.8%)
1,2 (6.2%),7 (21.9%)

Chi-square statistic: 0.242


P-value: ,0.623

BMI (Deficient,Normal): ,46.95 ± 8.08,47.11 ± 7.77


T-statistic: 1.0
P-value: ,0.328

Contingency Table for BMI


BMI Category,deficient,normal
31-40,2 (6.2%),5 (15.6%)
41-50,6 (18.8%),10 (31.2%)
51+,3 (9.4%),6 (18.8%)

Chi-square statistic: 0.178


P-value: ,0.915

Contingency Table for Surgery


surgery,deficient,normal
0,9 (28.1%),12 (37.5%)
1,2 (6.2%),9 (28.1%)
Chi-square statistic: 1.008
P-value: ,0.315

Analysis for B12_status--------------------------------------------


n (Deficient,Normal): ,n = 9(28.1%),n = 23(71.9%)
Age (Deficient,Normal): ,35.78 ± 12.95,49.13 ± 11.78
T-statistic: -1.286
P-value: ,0.22

Contingency Table for Age


Age Category,deficient,normal
<=30,3 (9.4%),2 (6.2%)
31-40,3 (9.4%),2 (6.2%)
41-50,1 (3.1%),9 (28.1%)
51-60,2 (6.2%),8 (25.0%)
61+,0 (0.0%),2 (6.2%)

Chi-square statistic: 7.76


P-value: ,0.101

Contingency Table for Sex


sex,deficient,normal
0,7 (21.9%),16 (50.0%)
1,2 (6.2%),7 (21.9%)

Chi-square statistic: 0.001


P-value: ,0.978

BMI (Deficient,Normal): ,46.61 ± 4.83,47.23 ± 8.72


T-statistic: 0.204
P-value: ,0.84

Contingency Table for BMI


BMI Category,deficient,normal
31-40,1 (3.1%),6 (18.8%)
41-50,6 (18.8%),10 (31.2%)
51+,2 (6.2%),7 (21.9%)

Chi-square statistic: 1.514


P-value: ,0.469

Contingency Table for Surgery


surgery,deficient,normal
0,5 (15.6%),16 (50.0%)
1,4 (12.5%),7 (21.9%)
Chi-square statistic: 0.113
P-value: ,0.737

Analysis for
Ionized_Ca_status--------------------------------------------
n (Deficient,Normal): ,n = 9(28.1%),n = 23(71.9%)
Age (Deficient,Normal): ,41.78 ± 12.54,46.78 ± 13.70
T-statistic: 0.116
P-value: ,0.909

Contingency Table for Age


Age Category,deficient,normal
<=30,2 (6.2%),3 (9.4%)
31-40,1 (3.1%),4 (12.5%)
41-50,3 (9.4%),7 (21.9%)
51-60,3 (9.4%),7 (21.9%)
61+,0 (0.0%),2 (6.2%)

Chi-square statistic: 1.329


P-value: ,0.856

Contingency Table for Sex


sex,deficient,normal
0,7 (21.9%),16 (50.0%)
1,2 (6.2%),7 (21.9%)

Chi-square statistic: 0.001


P-value: ,0.978

BMI (Deficient,Normal): ,47.17 ± 4.98,47.01 ± 8.69


T-statistic: 0.544
P-value: ,0.591

Contingency Table for BMI


BMI Category,deficient,normal
31-40,1 (3.1%),6 (18.8%)
41-50,5 (15.6%),11 (34.4%)
51+,3 (9.4%),6 (18.8%)

Chi-square statistic: 0.861


P-value: ,0.65

Contingency Table for Surgery


surgery,deficient,normal
0,6 (18.8%),15 (46.9%)
1,3 (9.4%),8 (25.0%)

Chi-square statistic: 0.0


P-value: ,1.0

Analysis for Vit_D3_status--------------------------------------------


n (Deficient,Normal): ,n = 21(65.6%),n = 11(34.4%)
Age (Deficient,Normal): ,44.43 ± 12.52,47.18 ± 15.36
T-statistic: -0.77
P-value: ,0.452

Contingency Table for Age


Age Category,deficient,normal
<=30,3 (9.4%),2 (6.2%)
31-40,4 (12.5%),1 (3.1%)
41-50,7 (21.9%),3 (9.4%)
51-60,6 (18.8%),4 (12.5%)
61+,1 (3.1%),1 (3.1%)

Chi-square statistic: 0.97


P-value: ,0.914

Contingency Table for Sex


sex,deficient,normal
0,14 (43.8%),9 (28.1%)
1,7 (21.9%),2 (6.2%)

Chi-square statistic: 0.242


P-value: ,0.623

BMI (Deficient,Normal): ,47.20 ± 7.86,46.78 ± 7.90


T-statistic: 0.046
P-value: ,0.964

Contingency Table for BMI


BMI Category,deficient,normal
31-40,4 (12.5%),3 (9.4%)
41-50,11 (34.4%),5 (15.6%)
51+,6 (18.8%),3 (9.4%)

Chi-square statistic: 0.297


P-value: ,0.862

Contingency Table for Surgery


surgery,deficient,normal
0,14 (43.8%),7 (21.9%)
1,7 (21.9%),4 (12.5%)

Chi-square statistic: 0.0


P-value: ,1.0

You might also like