0

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

Rgeek
  • 419
  • 1
  • 9
  • 23
  • I confess that I haven't used rpy2 myself, but it looks suspicious having the R `c()` function in your code. Have you tried just `binom=R.r['binom.test'](count_a,count_b)` Exactly which line causes the error (ie, if you comment them all out and add them back in one-by-one which triggers the error?) – MrFlick May 06 '14 at 21:50
  • import rpy2.robjects as R, causes the error. – Rgeek May 06 '14 at 21:56
  • Which version R are you using? And which version of rpy2 for that matter? – MrFlick May 06 '14 at 21:58
  • R versions is 3.0.2 and rpy version is 2.3.0 – Rgeek May 06 '14 at 22:04
  • Latest rpy2 in the 2.3.x series is 2.3.10. – lgautier May 06 '14 at 22:06
  • I installed using sudo pip install rpy2.It was successfully installed.But,when I run python -m 'rpy2.tests' rpy2 version: 2.3.0 built against R version: 3-0.2--63987 Error: 'rho' must be an environment not NULL: detected in C-level eval During startup - Warning message: Abort trap: 6 – Rgeek May 06 '14 at 22:07

2 Answers2

1

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • The latest version was installed,and the error was resolved.But I am not able to extract the p-value from the "binom" object.I am getting this error"TypeError: 'str' object cannot be interpreted as an index" – Rgeek May 07 '14 at 17:16
0
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

Rgeek
  • 419
  • 1
  • 9
  • 23