[R] variance/mean
Wacek Kusnierczyk
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Mar 22 10:15:41 CET 2009
rkevinburton at charter.net wrote:
> At the risk of appearing ignorant why is the folowing true?
>
> o <- cbind(rep(1,3),rep(2,3),rep(3,3))
> var(o)
> [,1] [,2] [,3]
> [1,] 0 0 0
> [2,] 0 0 0
> [3,] 0 0 0
>
> and
>
> mean(o)
> [1] 2
>
> How do I get mean to return an array similar to var? I would expect in the above example a vector of length 3 {1,2,3}.
>
you may well be ignorant about how var works with matrices, but this
does not mean it's your fault. the documentation is typically cryptical.
when you apply var to a single matrix, it will compute covariances
between its columns rather than the overall variance:
set.seed(0)
x = matrix(rnorm(4), 2, 2)
var(x)
# [,1] [,2]
# [1,] 1.2629543 1.329799
# [2,] -0.3262334 1.272429
matrix(nrow=2, ncol=2, byrow=TRUE, c(
cov(x[,1], x[,1]), cov(x[,1], x[,2]),
cov(x[,2], x[,1]), cov(x[,2], x[,2])))
vQ
More information about the R-help
mailing list