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).

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)

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.

Value

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

all(abs(r) <= 1).

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

Var(1:10)  # 9.166667
#> [1] 9.17

Var(1:5, 1:5) # 2.5
#> [1] 1.67

# weighted Variance
set.seed(45)
(z <- as.numeric(names(w <- table(x <- sample(-10:20, size=50, replace=TRUE)))))
#>  [1] -9 -8 -7 -6 -5 -4 -3 -2  0  3  4  5  6  7  8  9 10 12 13 15 16 17 18 19 20
Var(z, w=w)
#> [1] 86.5

# check!
all.equal(Var(x), Var(z, w=w))
#> [1] TRUE


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