[R] Different value between R variance and definition ofvariance

Stefano Calza stef at biostatistics.it
Wed Nov 28 10:23:56 CET 2007


Hi!


maybe because sample variance has N-1 at the denominator (say degrees of freedom)?

so

all.equal((sum(x^2) - 100*mean(x)^2)/99, var(x)) ## TRUE 

but

(sum(x^2) - 100*mean(x)^2)/100 # == your value


Stef

On Wed, Nov 28, 2007 at 09:56:58AM +0100, Tine wrote:
<Tine>Hi!
<Tine>
<Tine>Let us define random variable:
<Tine> > x = seq(0,1,length=100)
<Tine>
<Tine>If we calculate variance following definition E[(x-E(x))^2] we get:
<Tine> > mean( (x - mean(x))^2 ) # == mean(x^2) - mean(x)^2
<Tine>0.08501684
<Tine>
<Tine>And if we use internal R function var:
<Tine> > var(x)
<Tine>0.08587559
<Tine>
<Tine>Can anyone tells me why the difference?
<Tine>
<Tine>______________________________________________
<Tine>R-help a r-project.org mailing list
<Tine>https://stat.ethz.ch/mailman/listinfo/r-help
<Tine>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
<Tine>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list