Learn R Programming

DescTools (version 0.99.57)

Var: Variance

Description

Var() computes the variance of x. If x is a matrix variances of the columns of x are computed. Varn returns the uncorrected sample variance (which is biased estimator for the sample variance).

Usage

Var(x, ...)

# S3 method for Freq Var(x, breaks, ...)

# S3 method for default Var(x, weights = NULL, na.rm = FALSE, method = c("unbiased", "ML"), ...)

VarN(x, na.rm = FALSE)

Value

For r <- Cor(*, use = "all.obs"), it is now guaranteed that

all(abs(r) <= 1).

Arguments

x

a numeric vector, matrix or data frame.

weights

a numerical vector of weights the same length as x giving the weights to use for elements of x.

na.rm

logical. Should missing values be removed?

method

determines the estimator type; if "unbiased" (the default) then the usual unbiased estimate (using Bessel's correction) is returned, if "ML" then it is the maximum likelihood estimate for a Gaussian distribution. Uses stats:cov.wt for both methods.

breaks

breaks for calculating the variance for classified data as composed by Freq.

...

further arguments passed to or from other methods.

Details

Var is just another interface to Cov.

The denominator \(n - 1\) is used which gives an unbiased estimator of the (co)variance for i.i.d. observations. These functions return NA when there is only one observation (whereas S-PLUS has been returning NaN), and fail if x has length zero.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

cor, cov for covariance and correlation matrices

cor.test for confidence intervals (and tests).

cov.wt for weighted covariance computation.

sd for standard deviation (vectors).

Examples

Run this code
Var(1:10)  # 9.166667

Var(1:5, 1:5) # 2.5

# weighted Variance
set.seed(45)
(z <- as.numeric(names(w <- table(x <- sample(-10:20, size=50, replace=TRUE)))))
Var(z, w=w)

# check!
all.equal(Var(x), Var(z, w=w))


# Variance for frequency tables
Var(Freq(as.table(c(6,16,24,25,17))),
     breaks=c(0, 10, 20, 30, 40, 50))
     

Run the code above in your browser using DataLab