# [R] what's wrong with my "cov"?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Mar 10 09:32:12 CET 2006

```Michael <comtech.usa at gmail.com> writes:

> Hi all,
>
> Why cov(y, y) only gives one value, and cov(t(y), t(y)) gives 3x3 NA matrix?
>
> Here my y is listed below and it is a 3x1 matrix.
>
> I am expecting that if I have a random vector y=[y1 y2 y3]', here " ' "
> denotes a transposition so that
>
> y is a column vector, where y1, y2, y3 are independent random variables...
>
> then cov(y, y) should be
>
> E[ y * y' ] - E[y] * E[y] ',
>
> and it should generate an 3x3 identity matrix.
>
> In fact, as you can see, my y below is generated by
>
> y=[f(1)+e1, f(2)+e2, f(3)+e3]',
>   =[ 1^2+e1, 2^2+e2, 3^2+e3]',
>   =[ 1 + e1,  4 + e1,  9 + e1].
> where f(x)=x^2, e1, e2, e3 are three independent normal(0, 1) sample
> values...
>
> so I expect the cov(y, y) will be a 3x3 identity matrix.
>
> What's wrong here?

Nothing.

cov(X,X) ::= crossprod(scale(X,scale=FALSE))/(nrow(X)-1)

if X is a matrix and same thing with  .../(length(X)-1) if X is a
vector. Try calculating the constituent parts.

> > cov(y, y)
>          [,1]
> [1,] 13.78024
> > cov(t(y), t(y))
>      [,1] [,2] [,3]
> [1,]   NA   NA   NA
> [2,]   NA   NA   NA
> [3,]   NA   NA   NA
> > y
>          [,1]
> [1,] 1.146766
> [2,] 5.412608
> [3,] 8.542067
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help