简体   繁体   English

all.equal()的公差参数如何工作?

[英]How does the tolerance parameter of all.equal() work?

Can someone please explain to me the tolerance parameter of all.equal ? 有人可以向我解释all.equal的公差参数吗?

The manual says ( ?all.equal ) : 手册说?all.equal

tolerance : numeric ≥ 0. Differences smaller than tolerance are not considered. tolerance :数字≥0。不考虑小于公差的差异。

Numerical comparisons for scale = NULL (the default) are done by first computing the mean absolute difference of the two numerical vectors. 通过首先计算两个数值向量的平均绝对差来完成scale = NULL(默认值)的数值比较。 If this is smaller than tolerance or not finite, absolute differences are used, otherwise relative differences scaled by the mean absolute difference. 如果此值小于公差或不是有限的,则使用绝对差,否则使用平均绝对差来缩放相对差。

Example: 例:

all.equal(0.3, 0.26, tolerance=0.1)

returns Mean relative difference: 0.1333333 返回Mean relative difference: 0.1333333

Why is the mean relative difference returned here? 为什么在这里返回平均相对差? Isn't the mean absolute difference of the two numerical vectors smaller than tolerance? 两个数值向量的平均绝对差不小于公差吗?

0.3 - 0.26 = 0.04 < 0.1

Thank you! 谢谢!

If target is greater than tolerance , it seems to check for relative error <= tolerance . 如果target大于tolerance ,则似乎要检查relative error <= tolerance That is, abs(current-target)/target <= tolerance in: 也就是说, abs(current-target)/target <= tolerance

all.equal(target, current, tolerance)

For ex: 例如:

all.equal(3, 6, tolerance = 1)
# TRUE --> abs(6-3)/3 <= 1

Instead, if target is smaller than tolerance , all.equal uses mean absolute difference . 相反,如果target小于tolerance ,则all.equal使用mean absolute difference

all.equal(0.01, 4, tolerance = 0.01)
# [1] "Mean absolute difference: 3.99"

all.equal(0.01, 4, tolerance = 0.00999)
# [1] "Mean relative difference: 399"

all.equal(4, 0.01, tolerance = 0.01)
# [1] "Mean relative difference: 0.9975"

However, this is not what the documentation states. 但是,这不是文档说明的内容。 To look further as to why this is happening, let's look at the relevant snippet from all.equal.numeric : 为了进一步了解为什么会发生这种情况,让我们从all.equal.numeric查看相关片段:

# take the example: all.equal(target=0.01, current=4, tolerance=0.01)
cplx <- is.complex(target) # FALSE
out <- is.na(target) # FALSE
out <- out | target == current # FALSE

target <- target[!out] # = target (0.01)
current <- current[!out] # = current (4)

xy <- mean((if(cplx) Mod else abs)(target - current)) # else part is run = 3.99

# scale is by default NULL
what <- if (is.null(scale)) {
    xn <- mean(abs(target)) # 0.01
    if (is.finite(xn) && xn > tolerance) { # No, xn = tolerance
        xy <- xy/xn
        "relative"
    }
    else "absolute" # this is computed for this example
}
else {
    xy <- xy/scale
    "scaled"
}

All that is being checked in the code above (shown only the necessary parts for the example from OP) is: to remove any NA and equal values (of target and current ) from target and current . 上面的代码(在OP中仅显示了示例所需的部分)中正在检查的所有内容是:从targetcurrent 删除所有NA以及相等的值targetcurrent )。 Then compute xy as the mean absolute difference of target and current . 然后将xy计算为targetcurrent的平均绝对差。 But deciding if it is going to be relative or absolute depends on the part what . 但如果将是决定relativeabsolute取决于部件what And here xy is not checked for any conditions. 此处没有检查xy的任何条件。 It depends only on xn which is mean(abs(target)) . 取决于xn ,它是mean(abs(target))

So, in conclusion, the part pasted by the OP (pasted here for convenience): 因此,总而言之,OP粘贴的部分(为方便起见粘贴在此处):

If this (meaning, mean absolute difference ) is smaller than tolerance or not finite, absolute differences are used, otherwise relative differences scaled by the mean absolute difference. 如果此值平均绝对差 )小于公差或不为公差,则使用绝对差,否则,相对差按平均绝对差缩放。

seems wrong/misleading. 似乎是错误的/误导的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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