【问题标题】:Logarithmic mean of vector向量的对数平均值
【发布时间】:2013-03-04 01:10:48
【问题描述】:

为了遵循文章中介绍的确切方法,我想计算数据向量的对数平均值。我在 R 中或之前的任何讨论中都没有找到任何功能。 2 个数字的情况很清楚,但我无法找出最有效的方法来计算大量数字的对数均值。有什么建议吗?

# Calculating different types of means

# some data
dat <- c(0.008845299, 0.040554701)

# arithmetic mean
arith.m <- mean(dat)

# logarithmic mean
# http://en.wikipedia.org/wiki/Logarithmic_mean
log.m <- (dat[1] - dat[2])/(log(dat[1])-log(dat[2]))

# geometric mean
# http://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in
geo_mean <- function(data) {
  log_data <- log(data)
  gm <- exp(mean(log_data[is.finite(log_data)]))
  return(gm)
}

geo.m <- geo_mean(dat)

# show arithmetic > logarithmic > geometric
arith.m; log.m; geo.m

# how to calculate logarithmic mean for a vector?
dat.n <- c(0.008845299, 0.040554701, 0.047645299, 0.036654701, 0.017345299, 0.018754701, 0.032954701, 0.043145299, 0.026845299, 0.033054701, 0.025554701)

使用去除 0 值的计算进行更新(但是,如下所述,这是有效的吗?):

# add a very low number (generally considered zero in R)
nzero <- 1.940656e-324
dat.n <- c(dat.n, nzero)

# arithmetic mean
arith.m <- mean(dat.n)

geo_mean <- function(data) {
  log_data <- log(data)
  gm <- exp(mean(log_data[is.finite(log_data)]))
  return(gm)
}

geo.m <- geo_mean(dat.n)

lmv <- function(x){
  ddlog <- function(x){
    d <- rep(0, length(x))
    for (i in 1:length(x)){
      d[i] <- prod(x[i] - x[-i])
    }
    sum(log(x)[is.finite(log(x))]/d[is.finite(log(x))])
  }
  n <- length(x[which(x>0)]) - 1
  ((-1)^(n+1)*n*ddlog(x))^(-1/n)
}

log.m <- lmv(dat.n)

# show arithmetic > logarithmic > geometric
arith.m; log.m; geo.m

【问题讨论】:

  • 它没有任何功能,因为它很简单:diff(dat.n)/diff(log(dat.n)).
  • @JoshuaUlrich:这不是只有两个数字的情况吗?
  • @EdwardP.Morris:当你有两个以上的数字时,这是如何定义的?您链接到的维基百科页面仅讨论该案例。您是否在考虑与几何平均值相同的对数平均值? en.wikipedia.org/wiki/Geometric_mean#Log-average
  • 只漏掉零或接近零的值是非常有问题的。例如,c(1e-1000,1,1) 的几何平均值是多少?应该是10^(-998/3)
  • @BenBolker 有趣!

标签: r mean


【解决方案1】:

后跟 wiki(泛化为 (n+1) 个值):

http://en.wikipedia.org/wiki/Divided_difference#Expanded_form

http://en.wikipedia.org/wiki/Logarithmic_mean#Mean_value_theorem_of_differential_calculus_2

ddlog <- function(x){
  d <- rep(0, length(x))
  for (i in 1:length(x)){
    d[i] <- prod(x[i] - x[-i])
  }
  sum(log(x)/d)
}

# ddlog is to get divided difference of the logarithm.

lmv <- function(x){
  n <- length(x) - 1
  ((-1)^(n+1)*n*ddlog(x))^(-1/n)
}

R > a <- c(0.008845299, 0.040554701, 0.047645299, 0.036654701, 0.017345299, 0.018754701, 0.032954701, 0.043145299, 0.026845299, 0.033054701, 0.025554701)
R > 
R > lmv(a)
[1] 0.0277

【讨论】:

  • 太好了,谢谢,这似乎可以完成这项工作。我的数学有点生疏了,我会研究你提供的链接。
  • 好的,所以上面的例子适用于给定的例子,但是当有一个“零”或数字 R 认为在要平均的数字向量中为零时,它会返回 NaN。
  • @EdwardP.Morris,我猜对数平均值只适用于非负数。从 wiki 复制两个数字案例:“在数学中,对数平均值是两个非负数的函数,等于它们的差除以它们的商的对数。”
  • 是的,这似乎是合理的。我认为现在的问题是我必须删除向量中所有小到可以被视为“零”的数字,根据rwiki.sciviews.org/doku.php?id=misc:r_accuracy,它大约是 4.940656e-324。
  • is.finite 处理日志转换中的Inf 问题,但是该函数在((-1)^(n+1)*n*ddlog(x))^(-1/n) 处返回NaN。这是-1.111856e+19 ^-0.09090909
【解决方案2】:

试试这个:

> -diff(dat.n)/-diff(log(dat.n))
 [1] 0.02082356 0.04400483 0.04191009 0.02580711 0.01804083 0.02519117 0.03782146 0.03435320 0.02984241
[10] 0.02914404

【讨论】:

  • 为什么有两个负号/一元减号?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2021-08-13
相关资源
最近更新 更多