What's the tolerance of the which.max function in R?

流过昼夜 提交于 2019-12-13 17:05:41

问题


Based on a problem I discussed here: https://stackoverflow.com/a/57364028/2725773 I'm wondering what's the tolerance/precision of the which.max function in R.

Specifically, the alternative max.col function has a tolerance of 1e-5, which means that 0.12345 is the same for it as 0.12346.

The help page for max.col suggests an alternative, namely using unname(apply(m, 1, which.max)), which brings me to the questions what's the tolerance of which.max?


回答1:


Fascinating question. I do not know the precise answer. But it's possible to test some very small numbers to see what happens..

# the fourth element is the max
c(1,2,3,4,2) %>% which.max
# [1] 4

vec <- c(1,2,3,4,2)

# how tiny can the numbers become before which.max cannot tell the difference between them?
for(i in 1:30) {

  vec <- vec / (10 ^ i)
  max_num <- vec %>% which.max 
  print(vec)
  print(max_num)

}

It looks like the smallest these numbers can get to is 1e-300 2e-300 3e-300 4e-300 2e-300 (on the next iteration, which.max cannot tell the difference)



来源:https://stackoverflow.com/questions/57393262/whats-the-tolerance-of-the-which-max-function-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!