简体   繁体   English

如何在R的data.table中按类别查找协方差矩阵

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

Supposed that my data contain 3 categories. 假设我的数据包含3个类别。 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 ? 我知道如何使用sapply来做到这sapply ,但是如果我想使用data.table ,该怎么办?

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. 如果需要将结果作为3维矩阵,则显然不是data.table。 However, you can use data.table for the calculation. 但是,您可以使用data.table进行计算。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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