简体   繁体   中英

How to find covariance matrix by category in data.table in R

Supposed that my data contain 3 categories. I want to find the covariance matrix of each category, and expect the return to be an array.

set.seed(7)    
x <- rbind(cbind(rnorm(120,-1,0.1), rnorm(120,-0.5,0.1)),
               cbind(rnorm(120,-0.4,0.1), rnorm(120,0,0.1)),
               cbind(rnorm(120,.2,0.1), rnorm(120,0,0.1)))
lab <- c(rep(1, 120), rep(2, 120), rep(3, 120))
newx <- cbind(x, lab = lab)
s <- sapply(1:3, function(k){ var(newx[lab == k, -3]) })
dim(s) <- c(2,2,3)

return,

, , 1

             [,1]         [,2]
[1,]  0.008880447 -0.001116058
[2,] -0.001116058  0.009229061

, , 2

             [,1]         [,2]
[1,]  0.012193536 -0.001217923
[2,] -0.001217923  0.009391710

, , 3

            [,1]        [,2]
[1,] 0.010752319 0.001231336
[2,] 0.001231336 0.008226595

I know how to do it by using sapply , but if I want to use data.table , how can I do it ?

I have tried:

library(data.table)
dt <- data.table(newx)
dt[,lapply(.SD, var), by = lab]

but it does not provide the return as I expected.

If you need the result as a 3-dimensional matrix, then obviously that is not a data.table. However, you can use data.table for the calculation.

library(data.table)
DT <- as.data.table(newx)
result <- DT[,var(.SD),by=lab]
result <- as.matrix(result$V1)
dim(result) <- c(2,2,3)
result
# , , 1
# 
#              [,1]         [,2]
# [1,]  0.008880447 -0.001116058
# [2,] -0.001116058  0.009229061
# 
# , , 2
# 
#              [,1]         [,2]
# [1,]  0.012193536 -0.001217923
# [2,] -0.001217923  0.009391710
# 
# , , 3
#
#             [,1]        [,2]
# [1,] 0.010752319 0.001231336
# [2,] 0.001231336 0.008226595

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM