2011-06-18 44 views
-3

我有動物園的物體看起來像這樣的一些文件(每個文件開始,並在不同的日期結束):不同的動物園之間的相關性矩陣對象

  code pp 
1942-06-01 4016 0 
1942-06-02 4016 NA 
1942-06-03 4016 0 
1942-06-04 4016 0 
1942-06-05 NA 0 
1942-06-06 NA 0 

我想要做的PP之間的相關性矩陣9月,10月和11月的所有文件(顯示代碼,以便我可以識別誰是誰)。由於代碼列中存在NAs,因此無法使用list.files函數(代碼由Correlation matrix between different files中的Joran提供)。所以,我想出瞭如下因素代碼:

files <- list.files(pattern=".csv") 
xx<-read.zoo(files[1],sep=",", header=TRUE,index.column=1) 
name<- as.name(xx$code[[1]]) 
colnames(xx) <- c("code", name) 
x<-xx[months(time(xx), TRUE) %in% c("Sep", "Oct", "Nov")] 
yy<-read.zoo(files[2],sep=",", header=TRUE,index.column=1) 
name<- as.name(yy$code[1]) 
colnames(yy) <- c("code", name) 
y<-yy[months(time(yy), TRUE) %in% c("Sep", "Oct", "Nov")] 
CET<-merge(x, y, all = TRUE, fill = NA, check.names=FALSE) 
for (i in 3:length(files)) 
{ 
    z<-read.zoo(files[i],sep=",", header=TRUE,index.column=1) 
    name<- as.name(z$code[1]) 
    colnames(z) <- c("code", name) 
    CET<-merge(CET, z, all = TRUE, fill = NA, check.names=FALSE) 
} 
a<-1:(dim(CET)[2]) 
even <- a[ a%%2 == 0 ]  
# saves the precipitation column (even numbers) and discards the code ones 
dat<-CET[,even] 
c.mat<-cor(dat,use="pairwise.complete.obs") 

但什麼是錯的:在相關矩陣的一些列/行的名字有一個額外的「.Z」或「.CET」,而最重要的相關性係數不正確!我無法找到問題,因此有助於找到問題或提出更簡單的代碼來做到這一點將非常感激!

+0

-1不可重複的,「不正確」是不是有什麼不妥足夠的解釋。 –

+0

@Grothendieck - 「不正確」,因爲這些值與手動計算/使用minitab時不同。對不起,但真的不知道如何使這個問題可以重現... – sbg

回答

0

我不知道爲什麼,但如果不是從每個文件中提取我想要的月份,然後將它們合併到一個文件中,我合併這些文件,然後提取我想要的相關值的月份正確!我的意思是:

files <- list.files(pattern=".csv") x<-read.zoo(files[1],sep=",", header=TRUE,index.column=1)
y<-read.zoo(files[2],sep=",", header=TRUE,index.column=1)
CET<-merge(x, y, all = TRUE, fill = NA, check.names=FALSE)
for (i in 3:length(files))
{
z<-read.zoo(files[i],sep=",", header=TRUE,index.column=1)
CET<-merge(CET, z, all = TRUE, fill = NA, check.names=FALSE)
}
a<-1:(dim(CET)[2])
even <- a[ a%%2 == 0 ]
dat<-CET[,even]
dat.aut<-dat[months(time(dat), TRUE) %in% c("Sep", "Oct", "Nov")]
c.mat<-cor(dat.aut,use="pairwise.complete.obs")