Geometric Mean: is there a built-in?

前端 未结 9 1747
走了就别回头了
走了就别回头了 2020-11-28 20:45

I tried to find a built-in for geometric mean but couldn\'t.

(Obviously a built-in isn\'t going to save me any time while working in the shell, nor do I suspect ther

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 21:06

    Here is a vectorized, zero- and NA-tolerant function for calculating geometric mean in R. The verbose mean calculation involving length(x) is necessary for the cases where x contains non-positive values.

    gm_mean = function(x, na.rm=TRUE){
      exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
    }
    

    Thanks to @ben-bolker for noting the na.rm pass-through and @Gregor for making sure it works correctly.

    I think some of the comments are related to a false-equivalency of NA values in the data and zeros. In the application I had in mind they are the same, but of course this is not generally true. Thus, if you want to include optional propagation of zeros, and treat the length(x) differently in the case of NA removal, the following is a slightly longer alternative to the function above.

    gm_mean = function(x, na.rm=TRUE, zero.propagate = FALSE){
      if(any(x < 0, na.rm = TRUE)){
        return(NaN)
      }
      if(zero.propagate){
        if(any(x == 0, na.rm = TRUE)){
          return(0)
        }
        exp(mean(log(x), na.rm = na.rm))
      } else {
        exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
      }
    }
    

    Note that it also checks for any negative values, and returns a more informative and appropriate NaN respecting that geometric mean is not defined for negative values (but is for zeros). Thanks to commenters who stayed on my case about this.

提交回复
热议问题