如何在多个coliumn上执行测试

2024-05-18 04:26:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据框在下面

patid age gender    tg0 tg1 tg2 tg3 tg4 wgt0 wgt1 wgt2 wgt3 wgt4
0   1   45  Male    180 148 106 113 100 198 196 193 188 192
1   2   56  Male    139 94  119 75  92  237 233 232 228 225
2   3   50  Male    152 185 86  149 118 233 231 229 228 226
3   4   46  Female  112 145 136 149 82  179 181 177 174 172
4   5   64  Male    156 104 157 79  97  219 217 215 213 214

如果我计算tg0 tg1 tg2 tg3 tg4wgt0 wgt1 wgt2 wgt3 wgt4的平均值,得到两列a和b,然后进行测试,这是正确的方法吗

同时也复制了案例研究

A physician is evaluating a new diet for her patients with a family history of heart disease. To test the effectiveness of this diet, 16 patients are placed on the diet for 6 months. Their weights and triglyceride levels are measured before and after the study, and the physician wants to know if either set of measurements has changed

无效假设:使用新饮食6个月后,个体的甘油三酯水平和体重没有差异

Alt假说:使用新饮食6个月后,个体的甘油三酯水平和体重存在显著差异

对于2个变量,我们可以执行以下代码

from scipy import stats
#Data of group 1
a = np.array([42.1, 80.0, 30.0, 45.8, 57.7, 80.0, 82.4, 66.2, 66.9, 79.0])
#Data of group 2
b = np.array([80.7, 85.1, 88.6, 81.7, 69.8, 79.5, 107.2, 69.3, 80.9, 63.0])
t2, p2 = stats.ttest_ind(a,b)

Tags: andofthetg2malediettg1wgt1
2条回答

我不知道为什么每个病人有四个甘油三酯和体重的测量

假设测量间隔一个月(节食时和开始节食时tg0、wgt0),那么你可以做两件事中的一件:

  1. 取第一个和最后一个值(tg0、tg4),并将它们作为两组(a、b)。对wgt0和wgt4执行相同的操作
  2. 为了获得更好的准确性,我们可以通过拟合每个患者甘油三酯水平的最佳拟合线来合并其他值。然后对每个患者使用该最佳拟合线的第一个值作为a,最后一个值作为b。对砝码也要这样做

Is it the right way If I do the average of tg0 tg1 tg2 tg3 tg4 and wgt0 wgt1 wgt2 wgt3 wgt4 so that i will get 2 columns a and b and do the ttest

如果(tg0 tg1 tg2 tg3 tg4)是饮食前的测量,而(wgt0 wgt1 wgt2 wgt3 wgt4)是饮食后的测量,并且它们测量的是相同的东西(例如体重),那么您可以按照您的建议进行

看起来您希望找到每种度量类型在6个月期间前后的差异。基于此,您似乎希望执行两个单独的测试:

  1. 最终甘油三酯测量值是否与初始甘油三酯测量值显著不同
  2. 最终重量测量值是否与初始重量测量值显著不同

注意:我假设每列代表一段时间内的度量,从0开始,以4结束。这意味着,tg0wgt0分别是最初的甘油三酯和体重测量值,tg4wgt4是最终测量值

对于每个测试,您将比较最终测量值与初始测量值,因此您希望按照以下方式构建测试:

t_tg, p_tg = stats.ttest_ind(tg4,tg0)
t_wgt, p_wgt = stats.ttest_ind(wgt4,wgt0)

然后使用p_tgp_wgt对甘油三酯和重量进行唯一测定

相关问题 更多 >