r中的weighted.mean命令很奇怪

时间:2018-01-27 07:29:15

标签: r average weighted-average

假设我有这些载体:

q1<-c(9,8,10,9,3,2,1,2,4,5)
q2<-c(9,7,8,6,5,4,8,7,8,9)
q3<-c(0,0,0,5,9,5,9,5,0,5)

我想根据像corvector

这样的权重向量来计算加权平均值
MMean<-colMeans(rbind(q1,q2,q3))
corvector<-c(cor(q1,MMean),cor(q2,MMean),cor(q3,MMean))

所以我使用以下命令

weighted.mean(c(mean(q1),mean(q2),mean(q3)), corvector, na.rm = TRUE)
#5.709562

但结果不等于下面的直接计算

sum(c(mean(q1),mean(q2),mean(q3))*corvector)
#6.248393

我怎样才能恰当地使用weighted.mean?你能解释一下这个命令如何计算这个值吗?

1 个答案:

答案 0 :(得分:2)

我们检查函数weighted.mean

methods('weighted.mean')
#[1] weighted.mean.Date*     weighted.mean.default*  
#[3] weighted.mean.difftime* weighted.mean.POSIXct* 
#[5] weighted.mean.POSIXlt* 

getAnywhere('weighted.mean.default')
function (x, w, ..., na.rm = FALSE) 
{
    if (missing(w)) {
        if (na.rm) 
            x <- x[!is.na(x)]
        return(sum(x)/length(x))
    }
    if (length(w) != length(x)) 
        stop("'x' and 'w' must have the same length")
    w <- as.double(w)
    if (na.rm) {
        i <- !is.na(x)
        w <- w[i]
        x <- x[i]
    }
    sum((x * w)[w != 0])/sum(w) ### here
}

基于那个

out1 <- sum(c(mean(q1),mean(q2),mean(q3))*corvector)/sum(corvector)
out2 <- weighted.mean(c(mean(q1),mean(q2),mean(q3)), corvector, na.rm = TRUE)
identical(out1, out2)
#[1] TRUE