python的tableone包可以自定义检验方法,而R语言的包好像不可以,其链接地址为https://github.com/tompollard/tableone/blob/main/tableone.ipynb,复习一下其包的使用。数据集用它自己的。
安装并导入包
pip install tableone
# Import numerical libraries
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
%matplotlib inline
# Import tableone
try:
from tableone import TableOne, load_dataset
except (ModuleNotFoundError, ImportError):
# install on Colab
!pip install tableone
from tableone import TableOne, load_dataset
查看一下tableone的各种设置可以使用
# View the tableone docstring
TableOne??
导入数据并查看
# Load PhysioNet 2012 sample data
data = pd.read_csv('../tableone-main/data/pn2012_demo.csv')
data.head()
探究哈蒂根浸出试验提出的警告
哈蒂根浸出试验是一种多模态试验。该试验提示年龄、SysABP和身高分布可能是多模态的。我们在这里画出分布。
data[['Age','SysABP','Height']].dropna().plot.kde(figsize=[10,6])
plt.legend(['Age (years)', 'SysABP (mmHg)', 'Height (cm)'])
plt.xlim([-30,300])
探究图基规则提出的警告
Tukey的规则已经在Height中找到了异常值,所以我们将在箱线图中看到这一点
data[['Age','Height','SysABP']].boxplot(whis=3)
plt.show()
分层tableone, 并含有不同统计值
# columns to summarize
columns = ['Age', 'SysABP', 'Height', 'Weight', 'ICU', 'death']
# columns containing categorical variables
categorical = ['ICU']
# non-normal variables
nonnormal = ['Age']
# limit the binary variable "death" to a single row
limit = {"death": 1}
# set the order of the categorical variables
order = {"ICU": ["MICU", "SICU", "CSRU", "CCU"]}
# alternative labels
labels={'death': 'Mortality'}
# set decimal places for age to 0
decimals = {"Age": 0}
# optionally, a categorical variable for stratification
groupby = ['death']
# rename the death column
labels={'death': 'Mortality'}
# display minimum and maximum for listed variables
min_max = ['Height']
table2 = TableOne(data, columns=columns, categorical=categorical, groupby=groupby,
nonnormal=nonnormal, rename=labels, label_suffix=True,
decimals=decimals, limit=limit, min_max=min_max,htest_name=True,pval = True
,smd=True)
table2
打印或保存
print(table2.tabulate(tablefmt = "github"))
# Save to Excel
fn1 = 'tableone.xlsx'
table5.to_excel(fn1)
原创文章(本站视频密码:66668888),作者:xujunzju,如若转载,请注明出处:https://zyicu.cn/?p=16126