2
我有列表中給定矩陣的列之間的所有可能的組合。我想計算每個組合的cov並最終計算每個協方差矩陣的行列式。do.call cbind和sapply在R
的問題是,我需要計算行列式之前計算平方矩陣,我試圖用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
@plannapus謝謝,但不幸的是一個列表不是一個平方矩陣我認爲這是問題。 – nopeva
當我使用你的代碼,但用'list.of.det <-sapply(list.of.cov.matrices.2,det)'替換最後一行時,它可以工作。 'list.of.matrices'不是由平方矩陣組成,而是'list.of.cov.matrices.2'。 – plannapus