gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:47 29 4
gpt4 key购买 nike

这是我正在尝试做的事情:

  1. 我想使用discrete Kolmogorov-Smirov Goodness-of-fit test ,目前仅在 R 中可用。此外,R has the normal KS test as well -- 我不想使用这个测试。
  2. 我是 python 用户,因此需要将离散 KS 测试移植到 python,以执行此操作 I am trying to use rpy2

我面临的问题,详见更多统计详细信息 here ,是rpy2似乎用标准版本替换了导入的离散测试。我知道这一点是因为它在测试时不会产生正确的答案。

迄今为止的尝试

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

结果

/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

好的,到目前为止一切顺利,应该已经安装了。因此,让我们导入它:

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

真的是进口的吗?让我们看看:

env = r.wherefrom('dgof')

返回

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

好吧,这很奇怪,但也许它仍然有效,让我们看看(this is exactly the same example as used on the R side 并且应该返回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)

返回

D = 0.5, p-value = 0.925086

如果这对你来说没有任何意义,那没关系,你需要知道的是,这是错误的。这似乎是错误的,因为不知何故,正在加载标准 ks_test 来代替离散测试(我们在上面列表中的第 2 项中讨论的测试)。让我们通过加载标准库和 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)

返回

D = 0.5, p-value = 0.925086

这很酷——有人知道为什么会发生这种情况吗?

注意: this question is related to my other question ,但在 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

RRuntimeWarning 来自 R 本身,并且是人们所期望的。没有对象 dgof 因为 R 包命名空间不是对象。

您想要的可能是 wherefrom('ks.test') (请参阅 https://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html#finding-where-an-r-symbol-is-coming-from )。

这之间可能会发生很多事情,具体取决于包“dgof”正在做什么(如果您来自 Python,R 可以让包开发人员做一些非常奇怪的事情)。

您是否尝试过依赖 R 的调度和函数重载机制?加载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)

关于python - 使用 rpy2 将 R 包安装/导入到 python 中,导入/忽略有问题的包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54112992/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com