简体   繁体   English

使用rpy2将R软件包安装/导入到python中,导入/忽略有问题的数据包

[英]Install/importing R packages into python with rpy2, imports/ignores the packet in question

Here is what I am trying to do: 这是我正在尝试做的事情:

  1. I want to use the discrete Kolmogorov-Smirov Goodness-of-fit test , which is currently only available in R. Further, R has the normal KS test as well -- I do not want to use this test. 我想使用离散的Kolmogorov-Smirov拟合优度检验 ,该检验目前仅在R中可用。此外, R也具有正常的KS检验 -我不想使用此检验。
  2. I am a python user, so need to port the discrete KS test to python, to do this I am trying to use rpy2 . 我是python用户,因此需要将离散的KS测试移植到python,为此我尝试使用 rpy2

The problem I am facing , as detailed in more statistical detail here , is that rpy2 seems to replace the imported discrete test with the standard version. 我现在面临的问题 ,在更详细的统计详这里 ,是rpy2似乎取代了标准导入的离散测试。 I know this because it does not produce the right answer when tested. 我知道这一点,因为它在测试时无法产生正确的答案。

Attempts so far 到目前为止的尝试

import rpy2.robjects.packages as r
utils = r.importr("utils")
package_name = "dgof"
utils.install_packages(package_name)

results in 结果是

/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: 

  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: 
  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: The downloaded source packages are in
    ‘/tmp/RtmpTBas6a/downloaded_packages’
  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Updating HTML index of packages in '.Library'

  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Making 'packages.html' ...
  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning:  done

  warnings.warn(x, RRuntimeWarning)
rpy2.rinterface.NULL

Ok, so far so good, that should have installed it. 好,到目前为止,一切都很好,应该已经安装了它。 So lets import it: 因此,让我们导入它:

# Import Discrete goodness-of-fit package which includes KS and CVM tests.
dgof = rpackages.importr('dgof')

Has it really imported it? 它真的导入了吗? Lets see: 让我们来看看:

env = r.wherefrom('dgof')

returns 退货

/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: object 'dgof' not found

  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: In addition: 
  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Warning message:

  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: In (function (x, y, ..., alternative = c("two.sided", "less", "greater"),  :
  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: 

  warnings.warn(x, RRuntimeWarning)
/home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning:  cannot compute correct p-values with ties

  warnings.warn(x, RRuntimeWarning)

  warnings.warn(x, RRuntimeWarning)

Ok that's weird, but maybe it works anyway, lets see ( this is exactly the same example as used on the R side and should return D = 0.66667, p-value = 0.07407 ) : 好的,这很奇怪,但是无论如何它还是可以工作的,让我们看看( 这与R端使用的示例完全相同 ,应该返回D = 0.66667, p-value = 0.07407 ):

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
import numpy as np
a = np.array([1,1,1])
b = np.arange(1,3)
dgof.ks_test(a,b)

returns 退货

D = 0.5, p-value = 0.925086

If this doesn't mean anything to you that's fine, what you need to know is that it is wrong. 如果这对您并不意味着任何好处,那么您需要知道这是错误的。 It seems to be wrong because, somehow, the standard ks_test is being loaded in place of the discrete one (the one we talk about in item 2 in the above list). 这似乎是错误的,因为以某种方式加载了标准的ks_test而不是离散的ks_test (在上面列表的第2项中讨论的那个)。 Lets verify, by loading the standard library and the KS test: 让我们通过加载标准库和KS测试来验证:

from rpy2.robjects.packages import importr
base     = importr('base')
stats    = importr('stats')
import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()
import numpy as np

a = np.array([1,1,1])
b = np.arange(1,3)
stats.ks_test(a,b)

returns 退货

D = 0.5, p-value = 0.925086

So that's cool -- does anyone know why this may be happening? 所以这很酷-有人知道为什么会这样吗?

NOTE: this question is related to my other question , but with lots more detail on the python side of things. 注意: 这个问题与我的其他问题有关 ,但是在python方面有更多详细信息。

Has it really imported it? 它真的导入了吗? Lets see: 让我们来看看:

 env = r.wherefrom('dgof') 

returns 退货

 /home/usr/anaconda3/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: object 'dgof' not found 

The RRuntimeWarning comes from R itself, and is what one would expect. RRuntimeWarning来自R本身,这是人们所期望的。 There is no object dgof because R package namespaces are not objects. 没有对象dgof因为R包名称空间不是对象。

What you want is likely wherefrom('ks.test') (see https://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html#finding-where-an-r-symbol-is-coming-from ). 您想要的可能来自wherefrom('ks.test') (参见https://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html#finding-where-an-r-symbol-is -来自- )。

Many things can be happening here between, depending on what the package 'dgof' is doing (if you are coming from Python, R can let package developers do really strange things). 在这之间可能会发生许多事情,具体取决于软件包dgof的功能(如果您来自Python,R可以让软件包开发人员做一些真正奇怪的事情)。

Did you try relying on R's dispatch and function overloading mechanisms ? 您是否尝试过依赖R的调度和函数重载机制? After loading the R package dgof , call ks.test without specifying a namespace. 加载R包dgof ,调用ks.test而不指定名称空间。

dgof = rpackages.importr('dgof')
import rpy2.robjects
# "generic" function ks.test
ks_test = rpy2.robjects.r('ks.test')
# Use it
ks_test(a, b)

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

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