-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodds_ratio_functions.py
72 lines (60 loc) · 2.95 KB
/
odds_ratio_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Dict, Tuple
import numpy as np
import pandas as pd
import config_odds_ratio
from Odds import Odds
def create_df_from_dict() -> pd.DataFrame:
d: Dict = config_odds_ratio.analyses_b_estimates
df = pd.DataFrame(d).transpose()
df['estimate_lower_ci'] = df['estimate'] - 2 * df['se']
df['estimate_upper_ci'] = df['estimate'] + 2 * df['se']
df['odds_ratio'] = np.exp(df['estimate'])
df['odds_ratio_lower_ci'] = np.exp(df['estimate_lower_ci'])
df['odds_ratio_upper_ci'] = np.exp(df['estimate_upper_ci'])
return df
def calc_odds_ratio(age: int, heart: bool, sofa: int, lymphocyte: float, d_dimer: str, add_intercept: bool) -> Odds:
df: pd.DataFrame = create_df_from_dict()
if d_dimer == '0':
df_dimer_middle = False
df_dimer_high = False
elif d_dimer == '1':
df_dimer_middle = True
df_dimer_high = False
elif d_dimer == '2':
df_dimer_middle = False
df_dimer_high = True
else:
df_dimer_middle = False
df_dimer_high = False
estimate = (df.loc['intercept', 'estimate'] * add_intercept +
age * df.loc['age', 'estimate'] +
heart * df.loc['heart', 'estimate'] +
sofa * df.loc['sofa', 'estimate'] +
lymphocyte * df.loc['lymphocyte', 'estimate'] +
df_dimer_middle * df.loc['d_dimer_middle', 'estimate'] +
df_dimer_high * df.loc['d_dimer_high', 'estimate']
)
estimate_lower_ci = (df.loc['intercept', 'estimate_lower_ci'] * add_intercept +
age * df.loc['age', 'estimate_lower_ci'] +
heart * df.loc['heart', 'estimate_lower_ci'] +
sofa * df.loc['sofa', 'estimate_lower_ci'] +
lymphocyte * df.loc['lymphocyte', 'estimate_lower_ci'] +
df_dimer_middle * df.loc['d_dimer_middle', 'estimate_lower_ci'] +
df_dimer_high * df.loc['d_dimer_high', 'estimate_lower_ci']
)
estimate_upper_ci = (df.loc['intercept', 'estimate_upper_ci'] * add_intercept +
age * df.loc['age', 'estimate_upper_ci'] +
heart * df.loc['heart', 'estimate_upper_ci'] +
sofa * df.loc['sofa', 'estimate_upper_ci'] +
lymphocyte * df.loc['lymphocyte', 'estimate_upper_ci'] +
df_dimer_middle * df.loc['d_dimer_middle', 'estimate_upper_ci'] +
df_dimer_high * df.loc['d_dimer_high', 'estimate_upper_ci']
)
odds = Odds(estimate=estimate,
estimate_lower_ci=estimate_lower_ci,
estimate_upper_ci=estimate_upper_ci,
odds=np.exp(estimate),
odds_lower_ci=np.exp(estimate_lower_ci),
odds_upper_ci=np.exp(estimate_upper_ci),
p=1 / (1 + np.exp(-estimate)))
return odds