[R] var on a vector

Matthias Kohl Matthias.Kohl at uni-bayreuth.de
Thu Oct 2 14:55:07 CEST 2003


landau at poleia.lip6.fr schrieb:

>Hello
>
>I am an "R" newbie. I have a problem with computing a variance on a vector.
>
>  
>
>>data(cars)
>>variance <- function (x) mean(x^2)-mean(x)^2;
>>variance(cars[,1])
>>    
>>
>[1] 27.4
>  
>
>>var(cars[,1])
>>    
>>
>[1] 27.95918
>
>What did I assume/understand wrong ?
>
>TIA
>
>  
>
Hello,

help(var) says:

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

Try:
n <- length(cars[,1])
var(cars[,1])*(n-1)/n

Matthias Kohl




More information about the R-help mailing list