通过 rpy2 在 Python 中运行测试时出现问题
鉴于我两周前开始编码,我有一种感觉,这将是一个快速解决方案。我尝试在 Python 中运行统计测试 - Mantel,寻找两个距离矩阵之间的相关性,使用已经通过 Rpy2 用 R 编写的函数(?)。 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 ' '
编辑:现在可以了! “这会返回错误:“AttributeError:'R'对象没有属性'mantel'”这使我相信此处调用的对象在“.”处被截断(即“mantel”与完整的“mantel.rtest”)。我尝试将“mantel.rtest”重新分配为不带“.”的对象)。 rmantel =“mantel.rtest” 并替换为 结果 = robjects.r.rmantel (M1, M2, nrepet = 9999) 仅收到错误:“AttributeError:'R'对象没有属性'rmantel'” - 所以这不起作用。关于如何解决这个问题有什么想法吗?”
新问题: Mantel 测试需要“dist”中的数据格式,因此当我运行编辑后的代码时,我收到以下错误“RRuntimeError:函数错误(m1,m2,nrepet = 99): 因此
,我尝试将文件转换为该格式,当我打印结果时,它是正确大小的矩阵的下半部分,但所有字段都填充为“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 ' '
我得到“
1 2 3 4 5 6 7 8 9 10 11 12 13
2 NA
3 NA NA
4 NA NA NA
5 NA NA NA NA
6 NA NA NA NA
7 NA NA NA NA NA NA
8 NA NA NA NA NA NA NA
9 NA NA NA NA NA NA NA NA
10 NA NA NA NA NA NA NA NA NA
11 NA NA NA NA NA NA NA NA NA NA
12 NA NA NA NA NA NA NA NA NA NA NA
13 NA NA NA NA NA娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜娜
娜娜
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. The R module is "ade4" and it contains "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 "." (i.e. "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?"
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"
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"
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
2 NA
3 NA NA
4 NA NA NA
5 NA NA NA NA
6 NA NA NA NA NA
7 NA NA NA NA NA NA
8 NA NA NA NA NA NA NA
9 NA NA NA NA NA NA NA NA
10 NA NA NA NA NA NA NA NA NA
11 NA NA NA NA NA NA NA NA NA NA
12 NA NA NA NA NA NA NA NA NA NA NA
13 NA NA NA NA NA NA NA NA NA NA NA NA
14 NA NA NA NA NA NA NA NA NA NA NA NA NA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
robjects.r['mantel.rtest']
:这也有效:
编辑(针对新问题):
既然你说
mantel.rtest
需要dist
格式的数据,我想M1
和M2
应该是>dist
格式。但 M1 和 M2 似乎是 numpy 数组。另一方面,MOne
和MTwo
看起来可能是dist
格式。所以也许尝试一下
Try
robjects.r['mantel.rtest']
:This also works:
Edit (for the New Issue):
Since you say
mantel.rtest
requires data indist
format, I supposeM1
andM2
should be indist
format. ButM1
andM2
appear to be numpy arrays. On the other hand,MOne
andMTwo
look like they might be indist
format.So perhaps try
从 rpy2-2.1.x 开始,推荐的简单方法是:
From rpy2-2.1.x, the recommended simple way to do it is: