簡體   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在 R 測試中對發生不匹配的觀察的差異(絕對/相對)進行測試,然后確保它在公差范圍內。 理想情況下,它應該通過容差量測試發生不匹配的所有觀察結果,然后報告差異......為什么行為如此?

例如,在以下情況下,我希望 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

如果目標參數和當前參數幾乎等於指定容差(默認值接近 1.5e-8),則all.equal函數返回 TRUE。 該函數返回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

如果您閱讀 all.equal 的幫助頁面,您會看到原因:

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

編輯:基於 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