weighted.mean r 中的命令运行异常
weighted.mean command in r works strange
假设我有这些向量:
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?你能解释一下这个命令是如何计算这个值的吗?
我们检查函数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
假设我有这些向量:
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?你能解释一下这个命令是如何计算这个值的吗?
我们检查函数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