Вопрос

I want to calculate p-value in python using R.I am using this package rpy2.I am generating count_a and count_b on the fly,and calculate p-values along with it. When I run my script,python closes unexpectedly,and get this error message:

"Error: 'rho' must be an environment not NULL: detected in C-level eval During startup - Warning message:

Abort trap: 6"

The data is below:

 count_a  count_b

 94       107
 109      92
 90       89
 18       13

Below is my code:

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()

Any suggestions,or options to calculate p-value in python on a pair of values?

binom object is calculated:

Exact binomial test

data: c(94L, 107L) number of successes = 94, number of trials = 201, p-value = 0.3974
alternative hypothesis: true probability of success is not equal to 0.5 95 percent confidence interval: 0.3971286 0.5391627
sample estimates:
probability of success
0.4676617

However while extracting the p-value,I am getting this error:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rpy2/robjects/vectors.py", line 233, in getitem res = super(Vector, self).getitem(i) TypeError: 'str' object cannot be interpreted as an index

Это было полезно?

Решение 2

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]

For more clarifications,visit this blog http://telliott99.blogspot.com/2010/11/rpy-r-from-python-2.html

Другие советы

It appears from this thread that there may have been a problem with earlier version of rpy2 and R 3.0.2. Looks like the recommended version for R 3.0.2 is at least rpy2-2.3.8.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top