2016-10-07 39 views
2

所以,我正在使用R來嘗試在我使用phytools包中的phyl.pca函數的數據集上進行系統發生PCA。但是,我遇到的問題是,該函數將以接受的方式組織我的數據!這還不是全部:我做了一些實驗,我知道還有更多問題在後面,我將進入...使用配對數據創建數據集並將其轉換爲矩陣

直接問題,這裏是數據框(帶有虛擬數據)我使用:

>all 
        Taxa Tibia Feather 
1   Microraptor 138 101 
2   Microraptor 139 114 
3   Microraptor 145 141 
4   Anchiornis 160 81 
5   Anchiornis 14 NA 
6  Archaeopteryx 134 82 
7  Archaeopteryx 136 71 
8  Archaeopteryx 132 NA 
9  Archaeopteryx 14 NA 
10 Scansoriopterygidae 120 85 
11 Scansoriopterygidae 116 NA 
12 Scansoriopterygidae 123 NA 
13   Sapeornis 108 NA 
14   Sapeornis 112 86 
15   Sapeornis 118 NA 
16   Sapeornis 103 NA 
17  Confuciusornis 96 NA 
18  Confuciusornis 107 30 
19  Confuciusornis 148 33 
20  Confuciusornis 128 61 

中的物種被安排成一樹(稱爲「樹」)與小盜龍是最基礎,然後通過對孔子進展依次是:

>summary(tree) 

Phylogenetic tree: tree 

    Number of tips: 6 
    Number of nodes: 5 
    Branch lengths: 
    mean: 1 
    variance: 0 
    distribution summary: 
    Min. 1st Qu. Median 3rd Qu. Max. 
     1  1  1  1  1 
    No root edge. 
    Tip labels: Confuciusornis 
       Sapeornis 
       Scansoriopterygidae 
       Archaeopteryx 
       Anchiornis 
       Microraptor 
    No node labels. 

而且功能:

>phyl.pca(tree, all, method="BM", mode="corr") 

這是即將到來的錯誤:

Error in phyl.pca(tree, all, method = "BM", mode = "corr") : 
number of rows in Y cannot be greater than number of taxa in your tree 

Y可以是 「所有」 數據幀。所以我在我的樹中有6個分類羣(匹配數據框中的6個分類羣),但在我的數據框中有20行。所以我用這個功能:

> all_agg <- aggregate(all[,-1],by=list(all$Taxa),mean,na.rm=TRUE) 

,並得到這樣的:

   Group.1 Tibia Feather 
1   Anchiornis 153  81 
2  Archaeopteryx 136  77 
3  Confuciusornis 120  41 
4   Microraptor 141  119 
5   Sapeornis 110  86 
6 Scansoriopterygidae 120  85 

這是一個有點奇怪的類羣的順序發生了變化......這是好嗎?

在任何情況下,我將它轉換成一個矩陣:

> all_agg_matrix <- as.matrix(all_agg) 
> all_agg_matrix 
       Group.1 Tibia Feather 
[1,]   "Anchiornis" "153" "81" 
[2,]  "Archaeopteryx" "136" "77" 
[3,]  "Confuciusornis" "120" "41" 
[4,]   "Microraptor" "141" "119" 
[5,]   "Sapeornis" "110" "86" 
[6,] "Scansoriopterygidae" "120" "85" 

然後使用phyl.pca功能:

> phyl.pca(tree, all_agg_matrix, method = "BM", mode = "corr") 
[1] "Y has no names. function will assume that the row order of Y matches tree$tip.label" 
Error in invC %*% X : requires numeric/complex matrix/vector arguments 

所以,現在該函數考慮類羣的順序都是錯誤的(但我可以相對容易地修復)。問題是phyl.pca似乎並不認爲我的矩陣實際上是一個矩陣。任何想法爲什麼?

+0

我認爲第一個錯誤信息是相當清楚的......你用什麼樹,你可以發佈它?代碼是[here](https://github.com/liamrevell/phytools/blob/master/R/phyl.pca.R),它只是檢查Y行與Ntip(樹)的行數。至於你的問題的第二部分,你的矩陣應該只有數字是,而不是像一對數字。 '138,101'。 NA和NULL可能是一個額外的問題,但您需要提供矩陣1st。 – lrnzcig

+0

嗨,我已經發布了樹的細節。從Microraptor到Confuciusornis,這是一個非常簡單的過程。至於矩陣,我如何確保每個「脛骨」號碼與相應的「羽毛」號碼對應,如果我不成對的話呢?有其他解決方案嗎? –

+0

看到我的編輯我的答案。不要在整個數據框中使用'as.matrix';它將您的數字列轉換爲字符串... –

回答

0

我認爲你可能有更大的問題。大多數系統發育方法,我懷疑包括phyl.pca,假設性狀固定在物種水平(即,他們不考慮物種內變異)。因此,如果您想使用phyl.pca,則可能需要將數據摺疊爲每個物種的單個值,例如,通過

dd_agg <- aggregate(dd[,-1],by=list(dd$Taxa),mean,na.rm=TRUE) 

提取數字列和正確標註該行,以便phyl.pca可以提示正確地匹配起來:

dd_mat <- dd_agg[,-1] 
rownames(dd_mat) <- dd_agg[,1] 

使用這些彙總的數據,我可以上樹(因爲你並沒有給我們一個),然後運行phyl.pca ......

library(phytools) 
tt <- rcoal(nrow(dd_agg),tip.label=dd_agg[,1]) 
phyl.pca(tt,dd_mat) 

如果你需要做一個分析,認爲需要種內變異考慮你可能東東d詢問更專業化的地方,例如[email protected]郵件列表...

+0

感謝您的評論!請根據您的建議和新問題查看原始帖子的變化... –

+0

謝謝!這似乎解決了我的問題! –

0

Ben Bolker發佈的答案似乎適用於將數據(稱爲「全部」)在創建矩陣和運行函數之前摺疊爲每個物種的單個值。根據如下:

> all_agg <- aggregate(all[,-1],by=list(all$Taxa),mean,na.rm=TRUE) 
> all_mat <- all_agg[,-1] 
> rownames(all_mat) <- all_agg[,1] 
> phyl.pca(tree,all_mat, method= "lambda", mode = "corr") 

感謝大家誰貢獻了答案,特別是本! :)

相關問題