Skip to content
Back to formatted view

Raw Message

Message-ID: <49C604AC.3040305@idi.ntnu.no>
Date: 2009-03-22T09:28:12Z
From: Wacek Kusnierczyk
Subject: variance/mean
In-Reply-To: <49C601BD.3080506@idi.ntnu.no>

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