[R] variance/mean

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Mar 22 10:28:12 CET 2009


Wacek Kusnierczyk wrote:
>
> 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
>   

except for that i seem to have pasted wrong output.

    set.seed(0)
    x = matrix(rnorm(4), 2, 2)

    var(x)
    #           [,1]        [,2]
    # [1,] 1.2627587 0.045585801
    # [2,] 0.0455858 0.001645655

    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])))
    #           [,1]        [,2]
    # [1,] 1.2627587 0.045585801
    # [2,] 0.0455858 0.001645655

vQ




More information about the R-help mailing list