# Re: [Rd] [R] computing the variance

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon 05 Dec 2005 - 18:10:08 GMT

{from R-help, diverted to R-devel}:

UweL> Wang Tian Hua wrote:

```    UweL> hi, when i was computing the variance of a simple
UweL> vector, i found unexpect result. not sure whether it
UweL> is a bug.

```

UweL> Not a bug! ?var:

```    UweL> "The denominator n - 1 is used which gives an unbiased
UweL>  estimator of the (co)variance for
UweL>  i.i.d. observations."

UweL> > var(c(1,2,3))
UweL> [1] 1  #which should be 2/3.
UweL> > var(c(1,2,3,4,5))
UweL> [1] 2.5 #which should be 10/5=2
UweL>
```

UweL> it seems to me that the program uses (sample size -1) instead of sample     UweL> size at the denominator. how can i rectify this?

UweL> Simply change it by:

```    UweL> x <- c(1,2,3,4,5)
UweL> n <- length(x)
UweL> var(x)*(n-1)/n

```

UweL> if you really want it.

It seems Insightful at some point in time have given in to this user request, and S-plus nowadays has an argument "unbiased = TRUE"
where the user can choose {to shoot (him/her)self in the leg and} require 'unbiased = FALSE'.
{and there's also 'SumSquraes = FALSE' which allows to not
require any division (by N or N-1)}

VAR <- function(x,....) .. N/(N-1)*var(x,...) Should we?

Martin

R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Dec 06 05:17:16 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:34 GMT