【发布时间】:2011-02-01 18:29:00
【问题描述】:
我能够成功地在因子水平内获得给定向量的简单平均值,但是在尝试将其用于加权观察的下一步时,我无法让它发挥作用。这有效:
> tapply(exp.f,part.f.p.d,mean)
1 2 3 4 5 6 7 8 9 10
0.8535996 1.1256058 0.6968142 1.4346451 0.8136110 1.2006801 1.6112160 1.9168835 1.5135006 3.0312460
但这不是:
> tapply(exp.f,part.f.p.d,weighted.mean,b.pct)
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
>
在下面的代码中,我试图在因子 part.f.p.d 的级别内找到 exp.f 的加权平均值,由每个级别中 b.pct 内的观察加权。
b.exp <- tapply(exp.f,part.f.p.d,weighted.mean,b.pct)
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
我认为我必须提供不正确的语法,因为所有 3 个向量的长度都相同:
> length(b.pct)
[1] 978
> length(exp.f)
[1] 978
> length(part.f.p.d)
[1] 978
这样做的正确方法是什么?提前谢谢你。
【问题讨论】:
-
您好 jonw- exp.f 是股票预期收益的数值向量,part.f.p.d 是一个有 10 个级别的因子,b.pct 是指数中每只股票的百分比(前 1000 只股票) )
标签: r