Hi Alex,
cov() uses the formula for a sample covariance. The denominator is N
- 1 instead of N. However, the formula you used from Wikipedia is for
a population covariance. To move between the two in this case:
cov(sr, sr) * (4/5) # should equal E[XX] - E[x] * E[x]
Cheers,
Josh
On Wed, Jan 19, 2011 at 6:29 AM, Alaios <alaios at yahoo.com>
wrote:> Hello everyone,
> I am trying to understand how covariance work. So I created a vector called
sr<-c(2,5,7,5,2)
>
> so according to wikipedia Cov(X,X)=E[XX]-E[x]*E[x] which in R is
>
>
> mean(sr*sr)-mean(sr)*mean(sr)
> [1] 3.76
>
> but also
>
> cov(sr,sr)
> [1] 4.7
>
> why is this difference between these two approaches? Where I am wrong?
>
> I would like to thank you in advance for your help
>
> Best Regards
> Alex
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/