GotelliLab / EcoSimR

Repository for EcoSimR, by Gotelli, N.J. , Hart E. M. and A.M. Ellison. 2014. EcoSimR 0.1.0
http://ecosimr.org
Other
27 stars 10 forks source link

min_ratio question #37

Closed emhart closed 9 years ago

emhart commented 9 years ago

@ngotelli I was working on tests, and I'm not sure I understand the logic behind min_ratio().

Here's a sequence:

m <- c(2,3,4,5,6)

So on it's face I'd expect the minimum ratio to be 2/3 ~= 0.66667

But min_ratio() gives 0.1823 Which is strange because looking at the code it seems that you would want to exponentiate to get the ratio in non-log terms. When I do that, I get 1.2 (which is 6/5). So the reason we get that is because when diff is working on a sorted vector of a,b,c, it takes b - a and c - b. Is that what you want? e.g.

> diff(c(4,2,4))
[1] -2  2

Just ask because based on the description you wrote of the function I would write it as:

min_ratio2 <- function(m=runif(20)) {
  m <- sort(log(m), decreasing=T)
  mr <- min(exp(diff(m)))
  return(mr)
}

m <- c(2,3,4,5,6)
min_ratio2(m)
[1] 0.6666667

This gives me what I think of as: "the minimum size ratio difference between adjacent, ordered values".

I just came across this while I was writing tests and need to get back the expected values from a known vector.

ngotelli commented 9 years ago

Hi @emhart:

Good catch! As you note, the difference should be exponentiated to get it back into ratio form. However, we do not want to use the reverse sort. In this literature, the ratio is always calculated as

size_ratio(ab) = larger(a)/(next larger(b))

For your example, the smallest ratio is 6/5, and the correct answer is 1.2, not 0.1823. So the correct code should be:

min_ratio3 <- function(m=runif(20)) {
m <- sort(log(m))
mr <- exp(min(diff)))
return(mr)
}

Thus, it is the exponentiated value of my original log difference that we want, based on an increasing order for the sort. I guess the wording is a bit ambiguous about how the ratio should be calculated for adjacent ordered values. I will change the code and adjust the wording to say "minimum size ratio difference between adjacent, ordered values (larger/next larger)".