tompollard / tableone

Create "Table 1" for research papers in Python
https://pypi.python.org/pypi/tableone/
MIT License
161 stars 38 forks source link

Range for nonnormal variables #95

Closed glegaspi711 closed 4 years ago

glegaspi711 commented 4 years ago

Is it possible to get the range for non-normal variables instead of the first and third quartile? How would I be able to do this?

tompollard commented 4 years ago

There's no straightforward way to swap out the quartiles for range that I can think of, but it's something that we could implement without too much effort.

The main question for adding this feature is how it should be handled in the arguments. If you have a suggestion, please add to this thread...

glegaspi711 commented 4 years ago

Its possible that one could just add: if nonnormal = r, or nonnormal = q, then carry out the appropriate arguments. like an if, elif, else statement.

tompollard commented 4 years ago

Vincent Lequertier kindly contributed a patch to add a min_max argument, which I think addresses this issue. For example, to display age ranges, set the min_max argument to ['Age']:

from tableone import TableOne, load_dataset
data = load_dataset('pn2012')

columns = ['Age', 'SysABP', 'Height', 'Weight', 'ICU', 'death']

# columns containing categorical variables
categorical = ['ICU']

# set decimal places for age to 0
decimals = {"Age": 0}

# non-normal variables
nonnormal = ['Age']

# optionally, a categorical variable for stratification
groupby = ['death']

t1 = TableOne(data, columns=columns, categorical=categorical,
              groupby=groupby, nonnormal=nonnormal, decimals=decimals,
              min_max=['Age'])

print(t1.tabulate(tablefmt="github"))
Missing Overall 0 1
n 1000 864 136
Age, median [min,max] 0 68 [16,90] 66 [16,90] 75 [26,90]
SysABP, mean (SD) 291 114.3 (40.2) 115.4 (38.3) 107.6 (49.4)
Height, mean (SD) 475 170.1 (22.1) 170.3 (23.2) 168.5 (11.3)
Weight, mean (SD) 302 82.9 (23.8) 83.0 (23.6) 82.3 (25.4)
ICU, n (%) CCU 0 162 (16.2) 137 (15.9) 25 (18.4)
CSRU 202 (20.2) 194 (22.5) 8 (5.9)
MICU 380 (38.0) 318 (36.8) 62 (45.6)
SICU 256 (25.6) 215 (24.9) 41 (30.1)
tompollard commented 4 years ago

@glegaspi711 this argument is available in version 0.7.6.

tompollard commented 4 years ago

@glegaspi711 I think your request has been addressed, but please reopen the issue if not.