如果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
比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"
然而,这是不是什么的文档状态。进一步看,为什么发生这种情况,让我们来看看相关的代码片段从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例子中的必要部分)是: 从target
和current
删除任何NA和相等值(的target
和current
)。然后计算xy
作为target
和current
的平均绝对差值。但是决定是否将要relative
或absolute
取决于部分what
。这里xy
没有检查任何条件。它取决于只有在xn
这是mean(abs(target))
。
所以,最后,由OP粘贴部分(粘贴在这里为方便起见):
如果这(意思,平均绝对差)是小于公差或不是有限的,绝对的使用差异,否则通过平均绝对差异缩放相对差异。
似乎是错误/误导。
+1打算发布几乎相同的东西。 – juba 2013-03-11 09:20:04
这意味着文档不正确或我阅读帮助文件错误? – Roland 2013-03-11 09:34:02
@Roland,无论是“相对”还是“绝对”差异计算似乎都只依赖*目标,至少从代码来看。纠正我,如果我错了。我认为这些文件是误导性的。我会添加更多详细的代码以进一步阐明。 – Arun 2013-03-11 09:48:34