简体   繁体   English

R:使用带有两个以上对象的all.equal

[英]R: Using all.equal with more than two objects

When I compare more than two objects with identical the results make sense, but I do not understand what all.equal is doing. 当我比较两个以上identical的对象时,结果是有意义的,但我不明白all.equal正在做什么。 What is the logic behind the results that all.equal is outputting? all.equal输出的结果背后的逻辑是什么?

identical(25 + 2, 27)  #TRUE -- makes sense
identical(25, 2, 27)   #FALSE -- makes sense
identical(27, 22, 27)  #FALSE -- makes sense

all.equal(25 + 2, 27)  #TRUE -- makes sense
all.equal(25, 2, 27)   #TRUE -- why?
all.equal(27, 22, 27)  #TRUE -- why?

If either one of the last two all.equal() tests were false I would understand what R is doing -- either testing whether any sum of objects is equal (25+2==27) or testing whether any two objects are equal (27==27). 如果最后两个all.equal()测试中的任何一个是假的,我会理解R正在做什么 - 测试任何对象的总和是否相等(25 + 2 == 27)或测试任何两个对象是否相等( 27 == 27)。

What is all.equal testing such that both of the last two equations are true? 什么是all.equal测试,以便最后两个方程式都是正确的?

The comments above appear to be correct. 上面的评论似乎是正确的。 The arguments are taken in order, and the third argument is designated as the tolerance. 参数按顺序排列,第三个参数指定为公差。 However, the following results appear even stranger. 但是,以下结果显得更加陌生。

all.equal(target=25, current=2, 5) 
[1] TRUE

and

all.equal(target=25, current=2, tolerance=5) 
[1] TRUE

Here, the tolerance is clearly smaller than the difference, but TRUE is still returned. 这里,公差明显小于差值,但仍然返回TRUE。 If we reduce tolerance to be even smaller, we get 如果我们将公差降低到更小,我们就会得到

all.equal(target=25, current=2, tolerance=.05) 
[1] "Mean relative difference: 0.92"

What is going on is that relative differences are calculated based on the mean value of the target argument. 发生的事情是相对差异是根据目标参数的平均值计算的。 From the help file, 从帮助文件中,

Numerical comparisons for scale = NULL (the default) are typically on relative difference scale unless the target values are close to zero: First, the mean absolute difference of the two numerical vectors is computed. 除非目标值接近于零,否则比例= NULL(默认值)的数值比较通常在相对差异比例上:首先,计算两个数值向量的平均绝对差值。 If this is smaller than tolerance or not finite, absolute differences are used, otherwise relative differences scaled by the mean absolute target value . 如果这小于容差或不是有限的,则使用绝对差异 ,否则相对差异按平均绝对目标值缩放 (Note that these comparisons are computed only for those vector elements where target is not NA and differs from current.) (请注意,这些比较仅针对目标不是NA并且与当前不同的那些矢量元素计算。)

So, reversing target and current arguments, we get 因此,我们得到了逆转目标和当前的论点

all.equal(target=2, current=25, tolerance=5) 
[1] "Mean absolute difference: 23"

which is what we might have expected. 这是我们可能期望的。

Note that we could also get this by setting the scale argument to 1: 请注意,我们也可以通过将scale参数设置为1来实现此目的:

all.equal(target=25, current=2, tolerance=5, scale=1) 
[1] "Mean absolute difference: 23"

or only naming scale, 或仅限命名,

all.equal(25, 2, 5, scale=1) 
[1] "Mean absolute difference: 23"

which re-affirms the comments in the OP. 这再次肯定了OP中的评论。

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

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