简体   繁体   English

为什么 R 中的 all.equal 不测试每个观察值的差异,其中通过容差量而不是聚合平均值发生不匹配?

[英]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 in R tests for differences (absolute / relative) for observations where mismatch happen, and then makes sure that it is within tolerance amount. all.equal在 R 测试中对发生不匹配的观察的差异(绝对/相对)进行测试,然后确保它在公差范围内。 Ideally, it should test all observations where mismatch happens by tolerance amount, and then report the differences... Why is the behavior so ?理想情况下,它应该通过容差量测试发生不匹配的所有观察结果,然后报告差异......为什么行为如此?

eg In the following case, I would expect the result of all.equal to be FALSE, as the first observation in x is not equal to y例如,在以下情况下,我希望 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

The all.equal function returns TRUE if the the target and current arguments are nearly equal up to a specified tolerance, which has a default value close to 1.5e-8.如果目标参数和当前参数几乎等于指定容差(默认值接近 1.5e-8),则all.equal函数返回 TRUE。 The function does not return FALSE otherwise, rather it returns the mean relative ( scale=NULL ) or absolute ( scale=1 ) difference.该函数返回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

If you read the help page for all.equal, you would see the reason:如果您阅读 all.equal 的帮助页面,您会看到原因:

tolerance   numeric ≥ 0. Differences smaller than tolerance are not reported.

Edit : Based on Aaron's assessment of the OP's source of confusion, perhaps the any function was required:编辑:基于 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.

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