varW: Weighted Variance

Description Usage Arguments Details Value See Also Examples

View source: R/stdEff-fun.R

Description

Calculate the weighted variance of x.

Usage

1
varW(x, w = NULL, na.rm = FALSE)

Arguments

x

A numeric vector.

w

A numeric vector of weights of the same length as x.

na.rm

Logical, whether NAs in x should be removed.

Details

Calculate the weighted variance of x via the weighted covariance matrix (cov.wt()). If no weights are supplied, the simple variance is returned instead. As in weighted.mean(), NAs in w are not handled specially and will return NA as result.

Value

A numeric value, the weighted variance of x.

See Also

var()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Weighted variance
x <- rnorm(30)
w <- runif(30, 0, 1)
varW(x, w)

# Simple variance
varW(x)
stopifnot(varW(x) == var(x))

# NA handling
varW(c(x[1:29], NA), w, na.rm = TRUE)  # NA in x (removed)
varW(c(x[1:29], NA), w, na.rm = FALSE)  # NA in x (NA returned)
varW(x[1:29], w = c(w[1:29], NA))  # NA in w (NA returned)

semEff documentation built on Oct. 12, 2021, 5:06 p.m.