[R] computing the variance

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Dec 5 10:23:43 CET 2005


Wang Tian Hua wrote:

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

Not a bug! ?var:
"The denominator n - 1 is used which gives an unbiased estimator of the 
(co)variance for i.i.d. observations."


>  > 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?

Simply change it by:

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

if you really want it.


Uwe Ligges



> regards,
> tianhua
> 
> ______________________________________________
> R-help at 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




More information about the R-help mailing list