0% found this document useful (0 votes)
10 views

Untitled4.ipynb - Colab

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)
10 views

Untitled4.ipynb - Colab

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/ 3

09/07/2024, 14:57 Untitled4.

ipynb - Colab

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, 2
"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
"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

# 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)

# 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_norm

# 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)}")

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%)

Chi-square statistic: 1 329

https://colab.research.google.com/drive/124NGmd9yE6l1HjKmR5rjNtB1v6Z53GVW#scrollTo=u37HLwrn0INd&printMode=true 3/3

You might also like