繁体   English   中英

使用R计算python中的p值

[英]Calculate p-value in python using R

我想在RI中使用RI使用python rpy2来计算python中的p值。我正在动态生成count_a和count_b,并与之一起计算p值。 当我运行脚本时,python意外关闭,并显示以下错误消息:

“错误:'rho'必须是非NULL的环境:在启动期间在C级eval中检测到-警告消息:

中止陷阱:6英寸

数据如下:

 count_a  count_b

 94       107
 109      92
 90       89
 18       13

下面是我的代码:

import rpy2.robjects as R
out= open(args.outfile, 'w')
binom=R.r['binom.test'](c(count_a,count_b))
P_val=binom['p.value'][0][0]
out.write(str(count_a) + '\t' + str(count_b) + '\t' + str(P_val)
out.close()

有什么建议或选项可以在python中计算一对值的p值吗?

Binom对象的计算:

Exact binomial test

数据:c(94L,107L)成功次数= 94,试验次数= 201,p值= 0.3974
替代假设:成功的真实概率不等于0.5 95%置信区间:0.3971286 0.5391627
样本估算:
成功的可能性
0.4676617

但是在提取p值时,出现此错误:

getitem res = super(Vector,self)中的文件“ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2/robjects/vectors.py”的第233行。 getitem (i)TypeError:“ str”对象无法解释为索引

该线程看来,早期版本的rpy2和R 3.0.2可能存在问题。 看起来R 3.0.2的推荐版本至少是rpy2-2.3.8。

The problem was binom.names is a  StrVector, and does not support index, however it can be     converted to a Python list easily enough,and then extract those values.

    my_vec = R.IntVector([count_a,count_b])
    binom=R.r['binom.test'](my_vec)
    names= binom.names
    names = list(names)
    P_val= binom[names.index('p.value')][0]

有关更多说明,请访问此博客http://telliott99.blogspot.com/2010/11/rpy-r-from-python-2.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM