矢量的对数平均值

时间:2013-03-01 15:09:14

标签: r mean

为了遵循文章中提出的确切方法,我想计算数据向量的对数平均值。我在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)

UPDATE用计算去掉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

2 个答案:

答案 0 :(得分:4)

其次是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

答案 1 :(得分: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
相关问题