2016-11-04 29 views
1

我有一個corr矩陣列表[1:1505, 1:1505]。 我在想如何調整他們的Eigen在R. 的初始線程How to adjust this data structure for smaller segments in R?僅由一個測試功能cor這是不是到底充足,所以這裏的第二個測試功能 - 在COR矩陣Eigen如何調整R中的特徵數據結構?

# https://stackoverflow.com/q/40429343/54964 
set.seed(24) 
A=541650 
m1 <- matrix(1:A, ncol=4, nrow=A) 

a=360; b=1505; c=4; 
m2 <- array(`length<-`(m1, a*b*c), dim = c(a,b,c)) 

res <- lapply(seq(dim(m2)[3]), function(i) cor(m2[,,i])) 

res2 <- lapply(res, function(x) eigen(replace(x, is.na(x), 0))$vectors[,1:2]) 

str(res2) 

e1 <- res[,1] 
e2 <- res[,2] 

輸出

List of 4 
$ : num [1:1505, 1:2] -0.0144 0.0512 0.0157 -0.0232 0.0248 ... 
$ : num [1:1505, 1:2] 0.02233 0.00977 -0.02361 0.01597 0.00115 ... 
$ : num [1:1505, 1:2] -0.005876 0.000417 0.008206 0.006237 0.01514 ... 
$ : num [1:1505, 1:2] 0.00844 0.04382 0.0203 0.0348 0.02553 ... 
Error in res2[, 1] : incorrect number of dimensions 
Execution halted 

R:3.3.1
OS:Debian的8.5

+1

喔,沒關係,你應用'eigen'。它應該是'lapply(res2,function(x)eigen(x)$ vectors [,1:2])'左右 – akrun

+1

我認爲這是因爲NA元素,'lapply(res2,function(x)eigen替換(x,is.na(x),0))$ vectors [,1:2])'我不認爲用0代替NA是有意義的,但是這個工作沒有任何錯誤。您可能需要閱讀有關特徵的部分。 – akrun

+1

在這種情況下,最好在交叉驗證後發佈問題(即http://stats.stackexchange.com/)而不是stackoverflow – akrun

回答

1

我認爲,完整的功能代碼是在`list`以下立足akrun的初步回答,他的評論和一些迭代

# http://stackoverflow.com/q/40429343/54964 
set.seed(24) 
A=541650 
m1 <- matrix(1:A, ncol=4, nrow=A) 

a=360; b=1505; c=4; 
# http://stackoverflow.com/a/40430229/54964 
m2 <- array(`length<-`(m1, a*b*c), dim = c(a,b,c)) 

res <- lapply(seq(dim(m2)[3]), function(i) cor(m2[,,i])) 

res2 <- lapply(res, function(x) eigen(replace(x, is.na(x), 0))$vectors[,1:2]) 

str(res2) 

e1 <- res[1] 
e2 <- res[2] 

str(e1) 

str(e2) 

輸出

List of 4 
$ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... 
$ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... 
$ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... 
$ : num [1:1505, 1:2] -0.0258 -0.0258 -0.0258 -0.0258 -0.0258 ... 
List of 1 
$ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ... 
List of 1 
$ : num [1:1505, 1:1505] 1 1 1 1 1 1 1 1 1 1 ...