正如其他人所指出的,包Hmisc
具有功能wt.var()
。
请注意,您需要了解是否需要频率权重或可靠性权重。在您的情况下,我相信您对可靠性权重感兴趣,因此需要明确设置normwt=TRUE
. 在这种情况下,您可以以任何格式(总和为 1,总和为 N 等)给出您的权重。如果要使用频率权重,则需要注意如何指定权重。
library(Hmisc)
n <- 3
x <- seq_len(n)
w <- c(0.1, 0.2, 0.6)
w2 <- w / min(w)
w3 <- w / sum(w)
## reliability weights?
wtd.var(x = x, weights = w, normwt=TRUE)
#> [1] 0.95
wtd.var(x = x, weights = w2, normwt=TRUE)
#> [1] 0.95
wtd.var(x = x, weights = w3, normwt=TRUE)
#> [1] 0.95
## frequency weights?
wtd.var(x = x, weights = w)
#> Warning in wtd.var(x = x, weights = w): only one effective observation; variance
#> estimate undefined
#> [1] -4.222222
wtd.var(x = x, weights = w2)
#> [1] 0.5277778
wtd.var(x = x, weights = w3)
#> Warning in wtd.var(x = x, weights = w3): only one effective observation;
#> variance estimate undefined
#> [1] Inf
由reprex 包(v0.3.0)于 2020 年 8 月 26 日创建