Calculating weighted mean and standard deviation
The Hmisc package contains the functions you need.
Thus:
x <- c(3.7,3.3,3.5,2.8)
wt <- c(5, 5, 4, 1)/15
xm <- wtd.mean(x, wt)
var <- wtd.var(x, wt)
sd <- sqrt(var)
Unfortunately the author of the Hmisc package did not include an explicit wtd.sd
function. You have to square root wtd.var.
Charles Kangai
Package Hmisc
has function wt.var()
, as noted by others.
Note that you need to understand whether you want frequency or reliability weights. In your case, I believe you are interested in reliability weights, so will need to set explicitely normwt=TRUE
. In that case you can give your weights in any format (sum to 1, sum to N, etc). If you were to use frequency weights, you would need to be careful about how you specify weights.
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
Created on 2020-08-26 by the reprex package (v0.3.0)
R provides weighted mean. In fact, ?weighted.mean shows this example:
## GPA from Siegel 1994
wt <- c(5, 5, 4, 1)/15
x <- c(3.7,3.3,3.5,2.8)
xm <- weighted.mean(x, wt)
One more step:
v <- sum(wt * (x - xm)^2)
I too get errors from Hmisc
when using the wtd.var()
function. Fortunately, SDMTools
has comparable functionality, and even calculates SD directly for you, without needing to take sqrt of variance.
library(SDMTools)
x <- c(3.7,3.3,3.5,2.8)
wt <- c(5, 5, 4, 1)/15 ## Note: no actual need to normalize weights to sum to 1, this will be done automatically.
wt.mean(x, wt)
wt.sd(x,wt)
wt.var(x, wt)