这是我正在尝试做的事情:
- 我想使用离散的 Kolmogorov-Smirov Goodness-of-fit test,目前仅在 R 中可用。此外,R 也有正常的 KS 测试——我不想使用这个测试。
- 我是 python 用户,所以需要将离散 KS 测试移植到 python,为此我正在尝试使用
rpy2
.
我面临的问题,正如此处更详细的统计信息所详述的那样,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)
好吧,这很奇怪,但也许它仍然有效,让我们看看(这与 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)
返回
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
所以这很酷 - 有谁知道为什么会发生这种情况?
注意: 这个问题与我的另一个问题有关,但在 python 方面有更多细节。