# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Wilcoxon and Chi Squared

# +
import numpy as np
import pandas as pd

df = pd.read_csv("prepared_neuror2_data.csv")


# +
def stats_for_neuror2_range(lo, hi):
    admissions = df[df.NR2_Score.between(lo, hi)]
    total_patients = admissions.shape[0]
    readmits = admissions[admissions.UnplannedReadmission]
    total_readmits = readmits.shape[0]
    return (total_readmits, total_patients, "%.1f" % (total_readmits/total_patients*100,))

mayo_davis = []
for (expected, (lo, hi)) in [(1.4, (0, 0)),
                             (4, (1, 4)),
                             (5.6, (5, 8)),
                             (14.2, (9, 13)),
                             (33.0, (14, 19)),
                             (0.0, (20, 22))]:
    (total_readmits, total_patients, readmit_percent) = stats_for_neuror2_range(lo, hi)
    mayo_davis.append([lo, hi, expected, readmit_percent, total_readmits, total_patients])
    
title="Davis and Mayo Populations by NeuroR2 Score"
print(title)
print("-" * len(title))
print(pd.DataFrame(mayo_davis, columns=["Low", "High", "Mayo %", "Davis %", 
                                  "Readmits", "Total"]).to_string(index=False))

# +
# Continuous variables were compared using wilcoxon
from scipy.stats import ranksums as wilcoxon

def create_samples(col_name):
    unplanned = df[df.UnplannedReadmission][col_name].values
    planned = df[~df.UnplannedReadmission][col_name].values
    return (unplanned, planned)

continous_vars = ["AdmissionAgeYears", "LengthOfStay", "NR2_Score"]#, "MsDrgWeight"]
for var in continous_vars:
    (unplanned, planned) = create_samples(var)
    (stat, p) = wilcoxon(unplanned, planned)
    
    print ("%30s" % (var,), "p-value %f" % (p,))
# -

unplanned, planned = create_samples("LengthOfStay")
print(pd.DataFrame(unplanned, columns=["Unplanned Readmission"]).describe())
print(pd.DataFrame(planned, columns=[" Index Only Admission"]).describe())

# +
# Categorical variables were compared using chi squared
from scipy.stats import chi2, chi2_contingency
from IPython.core.display import display, HTML

# Collect all the categorical features
cols = sorted([col for col in df.columns if "_" in col])
for var in continous_vars:
    try:
        cols.remove(var)
    except:
        pass

index_only = df[~df.UnplannedReadmission].shape[0]
unplanned_readmit = df[df.UnplannedReadmission].shape[0]

html = "<table><tr>"
for th in ["Characteristic", "Index admission only</br>(n=%d)" % (index_only,), 
           "Unplanned readmission</br>(n = %d)" % (unplanned_readmit,),"<i>p</i> Value"]:
    html += "<th>%s</th>" % (th,)
html += "</tr>"

start_row = "<tr><td>%s</td>"
end_row = "<td>%d (%.1f)</td><td>%d (%.1f)</td><td></td></tr>"

pval_str = lambda p: "<0.001" if p<0.001 else "%.3f" % p
col_str = lambda col, p: "<b><i>%s</i></b>" % (col,) if p < 0.05 else col

for col in sorted(cols):
    table = pd.crosstab(df[col], df.UnplannedReadmission)
    stat, p, dof, expected = chi2_contingency(table)
    html += "<tr><td>%s</td><td></td><td></td><td>%s</td></tr>" % (col_str(col,p), pval_str(p))
    html += start_row % ("No",)
    html += end_row % (table.values[0][0], expected[0][0],
                       table.values[0][1], expected[0][1])
    try:
        html += start_row % ("Yes",)
        html += end_row % (table.values[1][0], expected[1,0],
                          table.values[1][1], expected[1][1])
    except IndexError:
        html += "<td>-</td><td>-</td><td></td></tr>"
        
html += "</table>"
display(HTML(html))

StarcoderdataJupyterScriptsDedupFiltered