[英]Why does all.equal in R not test for differences per observations where mismatch happen by tolerance amount, rather than the aggregate mean?
all.equal
在 R 测试中对发生不匹配的观察的差异(绝对/相对)进行测试,然后确保它在公差范围内。 理想情况下,它应该通过容差量测试发生不匹配的所有观察结果,然后报告差异......为什么行为如此?
例如,在以下情况下,我希望 all.equal 的结果为 FALSE,因为 x 中的第一个观察结果不等于 y
> x = rep(1, 1000)
> y = rep(1, 1000)
> x = x + 0.001
> y[1] = 2
> print(all.equal(x, y, scale = 1, tolerance=0.01))
[1] TRUE
如果目标参数和当前参数几乎等于指定容差(默认值接近 1.5e-8),则all.equal
函数返回 TRUE。 该函数不返回FALSE否则,而是返回的平均相对( scale=NULL
)或绝对的( scale=1
)的差异。
x = rep(1, 1000)
y = rep(1, 1000)
x = x + 0.001
y[1] = 2
print(all.equal(x, y, scale = 1)) # Omit the tolerance
[1] "Mean absolute difference: 0.001998"
mean(abs(x-y))
[1] 0.001998
如果您阅读 all.equal 的帮助页面,您会看到原因:
tolerance numeric ≥ 0. Differences smaller than tolerance are not reported.
编辑:基于 Aaron 对 OP 混淆源的评估,也许需要any
功能:
y[1] = 1
any(abs(x-y) > 0.01) # Element-wise comparison
[1] FALSE
y[1] = 2
any(abs(x-y) > 0.01)
[1] TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.