Untitled4.ipynb - Colab
Untitled4.ipynb - Colab
ipynb - Colab
import pandas as pd
from scipy.stats import chi2_contingency, ttest_ind
import numpy as np
# Convert to DataFrame
df = pd.DataFrame(data)
# 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
# Simulate the ages using the given mean and standard deviation
np.random.seed(0) # For reproducibility
d fi i t d l( d fi i t d d fi i t t t l d fi i t)
https://colab.research.google.com/drive/124NGmd9yE6l1HjKmR5rjNtB1v6Z53GVW#scrollTo=u37HLwrn0INd&printMode=true 1/3
09/07/2024, 14:57 Untitled4.ipynb - Colab
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)
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)}")
# 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)
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)}")
https://colab.research.google.com/drive/124NGmd9yE6l1HjKmR5rjNtB1v6Z53GVW#scrollTo=u37HLwrn0INd&printMode=true 2/3
09/07/2024, 14:57 Untitled4.ipynb - Colab
41-50,3 (9.4%),7 (21.9%)
51-60,3 (9.4%),7 (21.9%)
61+,0 (0.0%),2 (6.2%)
https://colab.research.google.com/drive/124NGmd9yE6l1HjKmR5rjNtB1v6Z53GVW#scrollTo=u37HLwrn0INd&printMode=true 3/3