I am a researcher trying to use python to translate a formula for scoring liver cirrhosis called MELD and MELDNa. Would someone please double check my implementation of the formula into python?
So this is the formula:
MELD = 1.33 (if female) + [4.56 x loge(bilirubin)] + [0.82 x (137 – sodium)] – [0.24 x (137 – sodium) x loge(bilirubin)] + [9.09 x loge(INR)] + [11.14 x loge(creatinine)] + [1.85 x (3.5 – albumin)] – [1.83 x (3.5 – albumin) x loge(creatinine)] + 6
MELD-Na = MELD + 1.32 x (137-Na) – [0.033 x MELD x (137-Na)]
import pandas as pd
import math
def calculate_meld(bilirubin, sodium, INR, creatinine, albumin, Sex):
if bilirubin <= 0 or sodium <= 0 or INR <= 0 or creatinine <= 0:
return None # Return None if any of the required parameters are non-positive
if Sex.lower() == 'female':
meld_score = (1.33 +
4.56 * math.log(bilirubin) +
0.82 * (137 - sodium) -
0.24 * (137 - sodium) * math.log(bilirubin) +
9.09 * math.log(INR) +
11.14 * math.log(creatinine) +
1.85 * (3.5 - albumin) -
1.83 * (3.5 - albumin) -
1.83 * (3.5 - albumin) * math.log(creatinine) +
6)
else:
meld_score = (4.56 * math.log(bilirubin) +
0.82 * (137 - sodium) -
0.24 * (137 - sodium) * math.log(bilirubin) +
9.09 * math.log(INR) +
11.14 * math.log(creatinine) +
1.85 * (3.5 - albumin) -
1.83 * (3.5 - albumin) -
1.83 * (3.5 - albumin) * math.log(creatinine) +
6)
meld_score = round(meld_score)
meld_score = max(6, min(meld_score, 40)) # Limiting the MELD score between 6 and 40
return meld_score
def calculate_meld_na(meld, sodium):
if meld is None:
return None # Return None if MELD score is None
meld_na_score = meld + 1.32 * (137 - sodium) - 0.033 * meld * (137 - sodium)
meld_na_score = round(meld_na_score)
meld_na_score = max(6, min(meld_na_score, 40)) # Limiting the MELD-Na score between 6 and 40
return meld_na_score
# Load data from Excel file
data = pd.read_excel('ICC NSQIP Data.xlsx')
# Drop rows with missing data
data.dropna(subset=['Total Bilirubin', 'Serum Sodium', 'INR', 'Serum Creatinine', 'Albumin', 'Sex'], inplace=True)
# Extract relevant columns
bilirubin = data['Total Bilirubin']
sodium = data['Serum Sodium']
INR = data['INR']
creatinine = data['Serum Creatinine']
albumin = data['Albumin']
Sex = data['Sex']
# Calculate MELD and MELD-Na scores
meld_scores = [calculate_meld(b, s, i, c, a, g) for b, s, i, c, a, g in zip(bilirubin, sodium, INR, creatinine, albumin, Sex)]
meld_scores = [score if score is not None else 0 for score in meld_scores] # Replace None with 0
meld_na_scores = [calculate_meld_na(m, s) for m, s in zip(meld_scores, sodium)]
# Add MELD and MELD-Na scores to the DataFrame
data['MELD Score'] = meld_scores
data['MELD-Na Score'] = meld_na_scores
# Save the updated DataFrame to a new Excel file
data.to_excel('liver_data_updated_2.xlsx', index=False)
It runs and gives us data, but it’s off for several rows of data versus when we manually calculate