一种计算大熊猫奇数比的更好方法

Ace*_*.py 5 python pandas

我有一个数据帧计数1,看起来像:

Factor            w-statin  wo-statin
AgeGroups Cancer                     
0-5       No           108       6575
          Yes            0        223
11-15     No             5       3669
          Yes            1        143
16-20     No            28       6174
          Yes            1        395
21-25     No            80       8173
          Yes            2        624
26-30     No           110       9143
          Yes            2        968
30-35     No           171       9046
          Yes            5       1225
35-40     No           338       8883
          Yes           21       1475
Run Code Online (Sandbox Code Playgroud)

我想计算oddsratio(w-statin/wo-statin).我做了旧样式,就像我用纸做的那样:

counts1['sumwwoStatin']= counts1['w-statin']+counts1['wo-statin']

counts1['oddRatio']=((counts1['w-statin']/counts1['sumwwoStatin'])/(counts1['wo-statin']/counts1['sumwwoStatin']))
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法来计算Pandas中的优势比,相对风险,Contigency表和卡方检验,就像在R?任何建议表示赞赏.哦顺便说一下,我忘了提一下我的csv是怎样的:

    Frequency Cancer     Factor AgeGroups
0         223    Yes  wo-statin       0-5
1         112    Yes  wo-statin      6-10
2         143    Yes  wo-statin     11-15
3         395    Yes  wo-statin     16-20
4         624    Yes  wo-statin     21-25
5         968    Yes  wo-statin     26-30
6        1225    Yes  wo-statin     30-35
7        1475    Yes  wo-statin     35-40
8        2533    Yes  wo-statin     41-45
9        4268    Yes  wo-statin     46-50
10       5631    Yes  wo-statin     52-55
11       6656    Yes  wo-statin     56-60
12       7166    Yes  wo-statin     61-65
13       8573    Yes  wo-statin     66-70
14       8218    Yes  wo-statin     71-75
15       4614    Yes  wo-statin     76-80
16       1869    Yes  wo-statin     81-85
17        699    Yes  wo-statin     86-90
18        157    Yes  wo-statin     91-95
19         31    Yes  wo-statin    96-100
20          5    Yes  wo-statin      >100
21        108     No   w-statin       0-5
22          6     No   w-statin      6-10
23          5     No   w-statin     11-15
24         28     No   w-statin     16-20
25         80     No   w-statin     21-25
26        110     No   w-statin     26-30
27        171     No   w-statin     30-35
28        338     No   w-statin     35-40
29        782     No   w-statin     41-45
..
Run Code Online (Sandbox Code Playgroud)

pan*_*sen 6

AFAIK熊猫不提供统计计算和测试,除了基本时刻,如均值,方差,相关性等...

但是,您可以依赖scipy此要求.你会发现你需要的大部分内容.例如,要计算比值比:

import scipy.stats as stats

table = df.groupby(level="Cancer").sum().values
print(table)

>>> array([[  840, 51663],
           [   32,  5053]])

oddsratio, pvalue = stats.fisher_exact(table)
print("OddsR: ", oddsratio, "p-Value:", pvalue)

>>> OddsR:  2.56743220487 p-Value: 2.72418938361e-09
Run Code Online (Sandbox Code Playgroud)

herehere更多.