简体   繁体   English

通过rpy2在Python中运行测试的问题

[英]issue running a test in Python, via rpy2

I have a feeling this will be a quick fix, given that I started coding two weeks ago. 鉴于我是在两周前开始编码的,所以我觉得这将是一个快速修复。 I am try to run a statistical test - a Mantel, looking for a correlation between two distance matrices - in Python, by using a function(?) that has already been written in R, via Rpy2. 我尝试通过使用已经通过Rpy2在R中编写的function(?)在Python中运行统计测试-Mantel,以查找两个距离矩阵之间的相关性。 The R module is "ade4" and it contains "mantel.rtest" R模块是“ ade4”,其中包含“ mantel.rtest”

from rpy2 import robjects

import rpy2.robjects as robjects

robjects.r('library(ade4)')
**EDIT** rmantel = robjects.r("mantel.rtest")

for i in windownA:

    M1 = asmatrix(identityA[i]).reshape(14,14)

    for j in windownB:

        M2 = asmatrix(identityB[j]).reshape(14,14)

       **EDIT** result = rmantel (M1, M2, nrepet = 9999)
        print result
        print ' '

EDIT: this now works! 编辑:这现在有效! "This returns the error: "AttributeError: 'R' object has no attribute 'mantel'" which leads me to believe that the object being called here is truncated at the "." (ie "mantel" versus the full "mantel.rtest"). I tried reassigning the "mantel.rtest" as an object without a "." ex) rmantel = "mantel.rtest" and substituting that result = robjects.r.rmantel (M1, M2, nrepet = 9999) only to receive the error: "AttributeError: 'R' object has no attribute 'rmantel'" - so that did not work. Any thoughts as to how I can get around this issue?" “这将返回错误: “ AttributeError:'R'对象没有属性'mantel'” ,这使我相信此处调用的对象在“。”(即“ mantel”)与完整的“ mantel.rtest”处被截断了。我尝试将“ mantel.rtest”重新分配为不带“。”的对象,例如rmantel =“ mantel.rtest”,然后将结果= robjects.r.rmantel(M1,M2,nrepet = 9999)替换为收到错误: “ AttributeError:'R'对象没有属性'rmantel'” -从而无法正常工作。关于如何解决此问题的任何想法?

New Issue : The Mantel test require data in "dist" format, so when I run the edited code, I get the following error "RRuntimeError: Error in function (m1, m2, nrepet = 99) : Object of class 'dist' expected" 新问题 Mantel测试需要“ dist”格式的数据,因此,当我运行编辑后的代码时,会收到以下错误“ RRuntimeError:函数错误(m1,m2,nrepet = 99):预期为'dist'类的对象”

So I tried to convert the file to that format and when I print the results, it's the bottom half of a matrix of the correct size, but all fields are filled with "NA" 因此,我尝试将文件转换为该格式,然后在打印结果时,它是正确大小的矩阵的下半部分,但是所有字段都填充有“ NA”

robjects.r('library(ade4)')
rmantel = robjects.r("mantel.rtest")

distify = robjects.r("dist")

for i in windownA:

    M1 = asmatrix(identityA[i]).reshape(14,14)
    print distify(M1)
    MOne = distify(M1, 14)

    for j in windownB:

        M2 = asmatrix(identityB[j]).reshape(14,14)
        print distify(M2)
        MTwo = distify(M2, 14)

        result = rmantel(M1, M2, nrepet = 9999)
        print result
        print ' '

i get" 我明白了

1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13

2 NA 2不适用

3 NA NA 3不适用

4 NA NA NA 4不适用不适用

5 NA NA NA NA 5不适用不适用不适用

6 NA NA NA NA NA 6不适用不适用不适用不适用

7 NA NA NA NA NA NA 7不适用不适用不适用不适用

8 NA NA NA NA NA NA NA 8不适用不适用不适用不适用不适用

9 NA NA NA NA NA NA NA NA 9不适用不适用不适用不适用不适用

10 NA NA NA NA NA NA NA NA NA 10不适用不适用不适用不适用不适用不适用

11 NA NA NA NA NA NA NA NA NA NA 11不适用不适用不适用不适用不适用不适用不适用

12 NA NA NA NA NA NA NA NA NA NA NA 12不适用不适用不适用不适用不适用不适用不适用不适用

13 NA NA NA NA NA NA NA NA NA NA NA NA 13不适用不适用不适用不适用不适用不适用不适用不适用

14 NA NA NA NA NA NA NA NA NA NA NA NA NA 14不适用不适用不适用不适用不适用不适用不适用不适用不适用

Try robjects.r['mantel.rtest'] : 尝试robjects.r['mantel.rtest']

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:from rpy2 import robjects
import rpy2.robjects as robjects
robjects.r('library(ade4)')
::::::--

In [3]: robjects.r['mantel.rtest']
Out[5]: <RFunction - Python:0xa2aac0c / R:0xac9ec04>

This also works: 这也适用:

In [8]: robjects.r('mantel.rtest')
Out[8]: <RFunction - Python:0xaf7042c / R:0xac9ec04>

Edit (for the New Issue): Since you say mantel.rtest requires data in dist format, I suppose M1 and M2 should be in dist format. 编辑(针对新问题):由于您说mantel.rtest需要dist格式的数据,因此我认为M1M2应该为dist格式。 But M1 and M2 appear to be numpy arrays. 但是M1M2似乎是numpy数组。 On the other hand, MOne and MTwo look like they might be in dist format. 另一方面, MOneMTwo看起来可能是dist格式。

So perhaps try 所以也许尝试

result = rmantel(MOne, MTwo, nrepet = 9999)

From rpy2-2.1.x, the recommended simple way to do it is: 从rpy2-2.1.x开始,推荐的简单方法是:

from rpy2.robjects.packages import importr
stats = importr('stats')
ade4 = importr('ade4')

result = ade4.mantel_rtest(stats.dist(M1),
                           stats.dist(M2),
                           nrepet)

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

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