我想用以下代码计算加权平均值
factor <- factor(cut(var1, quantile(var1, seq(0,1,0.1))))
var2_split = split(vat2, factor)
weight_split = split(weight, factor)
sapply(var2_split, weighted.mean, weight_split)
我收到以下错误
Error in FUN(X[[1L]], ...) : 'x' and 'w' must have the same length
如何格式化我的矢量和权重?
作为一个例子
假设我有一个矩阵m,其中包含3列x,y,z,其中x是一组目标值,y是一组权重,z是一组值,我想要将其加权.mean( X,Y)。具体来说,我想要由z的四分位数表示的weighted.mean(x,y)。
# Code that doesn't work
x <- c(1,2,3,4,5,6)
y <- c(6,3,4,2,3,4)
z <- c(1,1,2,3,3,4)
m <- as.matrix(c(x,y,z),nrow=6,ncol=3))
# bucket z by quartile.
z.factor <- cut(z, quantile(z, seq(0,1,0.25)), include.lowest=TRUE)
x.split = split(x, z.factor)
y.split = split(y, z.factor)
# want to bucket weighted.mean(x,y) on quartiles of z
sapply(x.split, weighted.mean, y.split)
答案 0 :(得分:0)
使用您的特定样本,尝试
#first, note the include.lowest=TRUE to get all values
z.factor <- factor(cut(z, quantile(z, seq(0,1,0.25)), include.lowest=TRUE))
#same
x.split = split(x, z.factor)
y.split = split(y, z.factor)
# here we use mapply
mapply(weighted.mean, x.split, y.split)
这给出了
[1,1.25] (1.25,2.5] (2.5,3] (3,4]
1.333333 3.000000 4.600000 6.000000
考虑到您的样本输入,这似乎是正确的。