【发布时间】:2013-02-02 11:12:32
【问题描述】:
我有一个给定矩阵的列之间所有可能组合的列表。我想计算每个组合的 cov,最后计算每个协方差矩阵的行列式。
问题是我需要在计算行列式之前计算一个平方矩阵,我尝试将 do.call 与 cbind 和 sapply 一起使用,但不起作用:
matrices.sq=do.call("cbind",lapply(list.of.matrices,get))
代码如下:
myarray=matrix(rexp(200),50,5)
list.of.matrices <- apply(expand.grid(rep(list(c(FALSE, TRUE)), ncol(myarray))),
1, function(j)myarray[, j, drop = FALSE])
list.of.cov.matrices=sapply(list.of.matrices, cov)
list.of.cov.matrices.2=list.of.cov.matrices[-(1:ncol(myarray))] #so get those with more than one column
list.of.det<- sapply(list.of.cov.matrices.2, det)
【问题讨论】:
-
不应该是
sapply(list.of.cov.matrices.2, det)吗? -
@plannapus 谢谢,但不幸的是列表不是平方矩阵,我认为这是问题所在。
-
当我使用您的代码但将最后一行替换为
list.of.det<-sapply(list.of.cov.matrices.2,det)时,它可以工作。list.of.matrices不是由方阵组成,但list.of.cov.matrices.2是。
标签: r