问题 all.equal()的容差参数如何工作?


有人可以向我解释一下公差参数 all.equal

手册说 (?all.equal

tolerance:numeric≥0。小于容差的差异不是   考虑。

scale = NULL(默认值)的数字比较首先完成   计算两个数值向量的平均绝对差值。   如果这小于公差或不是有限的绝对差异   使用,否则相对差异按平均绝对值缩放   区别。

例:

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

回报 Mean relative difference: 0.1333333

为什么这里返回平均相对差异?两个数值向量的平均绝对差值是否小于容差?

0.3 - 0.26 = 0.04 < 0.1

谢谢!


12039
2018-03-11 09:08


起源



答案:


如果 target 大于 tolerance,它似乎检查 relative error <= tolerance。那是, abs(current-target)/target <= tolerance 在:

all.equal(target, current, tolerance)

例如:

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

相反,如果 target 小于 toleranceall.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"

但是,这是  文件说明了什么。为了进一步了解为什么会发生这种情况,让我们看一下相关的代码片段 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"
}

在上面的代码中检查的所有内容(仅显示来自OP的示例的必要部分)是:to 删除任何NA和相等的值 (的 target 和 current)来自 target 和 current。然后计算 xy 作为平均绝对差异 target 和 current。但决定它是否会成功 relative 要么 absolute 取决于部分 what。和这里 xy 没有检查任何条件。这取决于 只要 上 xn 是的 mean(abs(target))

因此,总之,OP粘贴的部分(为方便起见粘贴在这里):

如果 这个 (含义, 平均绝对差异)小于容差或不是有限的,使用绝对差异,否则相对差异由平均绝对差异缩放。

似乎错误/误导。


12
2018-03-11 09:11



+1即将发布几乎相同的内容。 - juba
这意味着文档不正确或者我读错了帮助文件? - Roland
@Roland,是否 relative 要么 absolute 差异计算似乎依赖 只要 上 target,至少从代码中。如果我错了,请纠正我。我认为这些文件具有误导性。我将添加更详细的代码,以进一步明确。 - Arun


答案:


如果 target 大于 tolerance,它似乎检查 relative error <= tolerance。那是, abs(current-target)/target <= tolerance 在:

all.equal(target, current, tolerance)

例如:

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

相反,如果 target 小于 toleranceall.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"

但是,这是  文件说明了什么。为了进一步了解为什么会发生这种情况,让我们看一下相关的代码片段 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"
}

在上面的代码中检查的所有内容(仅显示来自OP的示例的必要部分)是:to 删除任何NA和相等的值 (的 target 和 current)来自 target 和 current。然后计算 xy 作为平均绝对差异 target 和 current。但决定它是否会成功 relative 要么 absolute 取决于部分 what。和这里 xy 没有检查任何条件。这取决于 只要 上 xn 是的 mean(abs(target))

因此,总之,OP粘贴的部分(为方便起见粘贴在这里):

如果 这个 (含义, 平均绝对差异)小于容差或不是有限的,使用绝对差异,否则相对差异由平均绝对差异缩放。

似乎错误/误导。


12
2018-03-11 09:11



+1即将发布几乎相同的内容。 - juba
这意味着文档不正确或者我读错了帮助文件? - Roland
@Roland,是否 relative 要么 absolute 差异计算似乎依赖 只要 上 target,至少从代码中。如果我错了,请纠正我。我认为这些文件具有误导性。我将添加更详细的代码,以进一步明确。 - Arun