美文网首页
ks.test报错

ks.test报错

作者: 坐看云起时zym | 来源:发表于2019-09-24 21:26 被阅读0次

作者在利用R中的ks.test函数进行Kolmogorov-Smirnov检验,验证x是否服从均匀分布时,发现编译器报错

ks.test(x,"punif")
Warning message:
In ks.test(x, "punif") :
ties should not be present for the Kolmogorov-Smirnov test

出现这样的错误是因为我们输入的向量中有重复值。如上图,我们检验x是否服从均匀分布。因而x首先必须是连续分布。而对于连续分布来说,出现两个相同的值的概率为0,所以会报错。

如果从代码层面来看,ks.test函数在实现的时候就考虑了这种情况

if (length(unique(x)) < n) {
    warning("ties should not be present for the Kolmogorov-Smirnov test")
    TIES <- TRUE
}

如果我们通过help(ks.test)查看官方文档,发现官方文档对这类错误的介绍如下:
The presence of ties always generates a warning, since continuous distributions do not generate them. If the ties arose from rounding the tests may be approximately valid, but even modest amounts of rounding can have a significant effect on the calculated statistic.
具体内容和我们上文所说的大致相同。

出现这种问题的解决办法:
1、更换检验方法
2、给x增加一些小的扰动,读者也可根据自己的需要,自行更改amount参数的值

jitter(x,0.000001)
output.png

Reference:
R document

相关文章

网友评论

      本文标题:ks.test报错

      本文链接:https://www.haomeiwen.com/subject/myubuctx.html