Re: [R] computing the variance

From: Romain Francois <francoisromain_at_free.fr>
Date: Mon 05 Dec 2005 - 20:23:20 EST

Le 05.12.2005 09:53, Wang Tian Hua a écrit :

>hi,
>when i was computing the variance of a simple vector, i found unexpect
>result. not sure whether it is a bug.
> > var(c(1,2,3))
>[1] 1 #which should be 2/3.
> > var(c(1,2,3,4,5))
>[1] 2.5 #which should be 10/5=2
>
>it seems to me that the program uses (sample size -1) instead of sample
>size at the denominator. how can i rectify this?

>
>regards,
>tianhua
>
>
These results are expected, it is so not a bug.  From details section in ?var

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

If you really want biased variance, work around :

biasedVar <- function(x, ...){
  n <- length(x)
  (n-1) / n * var(x,...)
}

Romain

-- 
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques
mixmod 1.7 is released : http://www-math.univ-fcomte.fr/mixmod/index.php
+---------------------------------------------------------------+
| Romain FRANCOIS - http://francoisromain.free.fr               |
| Doctorant INRIA Futurs / EDF                                  |
+---------------------------------------------------------------+

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Dec 05 20:34:41 2005

This archive was generated by hypermail 2.1.8 : Mon 05 Dec 2005 - 23:30:51 EST