colWeightedSds {DelayedMatrixStats}R Documentation

Weighted variance and weighted standard deviation

Description

Computes a weighted variance / standard deviation of a numeric vector or across rows or columns of a matrix.

Usage

colWeightedSds(x, w = NULL, rows = NULL, cols = NULL,
  na.rm = FALSE, ...)

colWeightedVars(x, w = NULL, rows = NULL, cols = NULL,
  na.rm = FALSE, ...)

rowWeightedSds(x, w = NULL, rows = NULL, cols = NULL,
  na.rm = FALSE, ...)

rowWeightedVars(x, w = NULL, rows = NULL, cols = NULL,
  na.rm = FALSE, ...)

## S4 method for signature 'DelayedMatrix'
colWeightedSds(x, w = NULL, rows = NULL,
  cols = NULL, na.rm = FALSE, force_block_processing = FALSE, ...)

## S4 method for signature 'DelayedMatrix'
colWeightedVars(x, w = NULL, rows = NULL,
  cols = NULL, na.rm = FALSE, force_block_processing = FALSE, ...)

## S4 method for signature 'DelayedMatrix'
rowWeightedSds(x, w = NULL, rows = NULL,
  cols = NULL, na.rm = FALSE, force_block_processing = FALSE, ...)

## S4 method for signature 'DelayedMatrix'
rowWeightedVars(x, w = NULL, rows = NULL,
  cols = NULL, na.rm = FALSE, force_block_processing = FALSE, ...)

Arguments

x

A NxK DelayedMatrix.

w

a vector of weights the same length as x giving the weights to use for each element of x. Negative weights are treated as zero weights. Default value is equal weight to all values.

rows

A vector indicating subset of elements (or rows and/or columns) to operate over. If NULL, no subsetting is done.

cols

A vector indicating subset of elements (or rows and/or columns) to operate over. If NULL, no subsetting is done.

na.rm

a logical value indicating whether NA values in x should be stripped before the computation proceeds, or not. If NA, no check at all for NAs is done. Default value is NA (for efficiency).

...

Additional arguments passed to specific methods.

force_block_processing

FALSE (the default) means that a seed-aware, optimised method is used (if available). This can be overridden to use the general block-processing strategy by setting this to TRUE (typically not advised). The block-processing strategy loads one or more (depending on getAutoBlockSize()) columns (colFoo()) or rows (rowFoo()) into memory as an ordinary base::array.

Details

The estimator used here is the same as the one used by the "unbiased" estimator of the Hmisc package. More specifically, weightedVar(x, w = w) == Hmisc::wtd.var(x, weights = w),

Value

Returns a numeric scalar.

Missing values

Missing values are dropped at the very beginning, if argument na.rm is TRUE, otherwise not.

See Also

For the non-weighted variance, see var.

Examples

# A DelayedMatrix with a 'SolidRleArraySeed' seed
dm_Rle <- RleArray(Rle(c(rep(1L, 5),
                         as.integer((0:4) ^ 2),
                         seq(-5L, -1L, 1L))),
                   dim = c(5, 3))

colWeightedSds(dm_Rle, w = 1 / rowMeans2(dm_Rle))

# Specifying weights inversely proportional to rowwise means
colWeightedVars(dm_Rle, w = 1 / rowMeans2(dm_Rle))

# Specifying weights inversely proportional to columnwise means
rowWeightedSds(dm_Rle, w = 1 / colMeans2(dm_Rle))

# Specifying weights inversely proportional to columnwise means
rowWeightedVars(dm_Rle, w = 1 / colMeans2(dm_Rle))

[Package DelayedMatrixStats version 1.6.0 Index]