IPYNB Converter
IPYNB Converter
July 9, 2024
"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,␣
# Convert to DataFrame
df = pd.DataFrame(data)
1
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}--------------------------------------------")
# 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')]
2
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])
# SURGERY
contingency_table_surgery = create_contingency_table(df['surgery'],␣
↪df[status])
# Display results
print(f"n (Deficient,Normal): ,n =␣
↪{total_deficient}({percent_deficient}%),n =␣
↪{total_normal}({percent_normal}%)")
# 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)
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)
3
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)}")
4
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%)
5
T-statistic: 0.204
P-value: ,0.84
6
P-value: ,0.978
7
Chi-square statistic: 0.242
P-value: ,0.623