几何均值:是否有内置的? [英] Geometric Mean: is there a built-in?

查看:98
本文介绍了几何均值:是否有内置的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为几何平均值找到一个内置函数,但是找不到。



(显然,内置函数不会在任何时候节省我时间在外壳程序中工作,我也不怀疑准确性是否存在任何差异;对于脚本,我尝试尽可能多地使用内置功能,在这些位置(累积)性能通常会显着提高。


$ b

  gm_mean = function $ b 

如果没有一个(我怀疑是这种情况) (a){prod(a)^(1 / length(a))}


解决方案

这是一个矢量化的,零容忍和无NA的函数,用于计算R中的几何平均值。详细的平均值计算涉及如果 x 包含非正值,则必须使用length(x)

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

感谢@ ben-bolker注意 na .rm 传递和@Gregor以确保其正确运行。



我认为某些评论与错误的对等有关数据中的 NA 值和零。在我想到的应用程序中,它们是相同的,但是当然,通常情况并非如此。因此,如果要包括零的可选传播,并且在 NA length(x) >删除,以下是上述功能的替代品。

  gm_mean = function(x,na.rm = TRUE ,zero.propagate = FALSE){
if(any(x< 0,na.rm = TRUE)){
return(NaN)
}
if(zero。传播){
if(any(x == 0,na.rm = TRUE)){
return(0)
}
exp(平均值(log(x), na.rm = na.rm))
}否则{
exp(sum(log(x [x> 0]),na.rm = na.rm)/ length(x))
}
}

请注意,它还会检查任何负值并返回更加有意义和恰当的 NaN ,因为没有为负值定义几何平均值(但为零)。感谢评论者坚持我的观点。


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 there's any difference in accuracy; for scripts I try to use built-ins as often as possible, where the (cumulative) performance gain is often noticeable.

In case there isn't one (which I doubt is the case) here's mine.

gm_mean = function(a){prod(a)^(1/length(a))}

解决方案

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.

这篇关于几何均值:是否有内置的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆