所以,我正在使用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似乎並不認爲我的矩陣實際上是一個矩陣。任何想法爲什麼?
我認爲第一個錯誤信息是相當清楚的......你用什麼樹,你可以發佈它?代碼是[here](https://github.com/liamrevell/phytools/blob/master/R/phyl.pca.R),它只是檢查Y行與Ntip(樹)的行數。至於你的問題的第二部分,你的矩陣應該只有數字是,而不是像一對數字。 '138,101'。 NA和NULL可能是一個額外的問題,但您需要提供矩陣1st。 – lrnzcig
嗨,我已經發布了樹的細節。從Microraptor到Confuciusornis,這是一個非常簡單的過程。至於矩陣,我如何確保每個「脛骨」號碼與相應的「羽毛」號碼對應,如果我不成對的話呢?有其他解決方案嗎? –
看到我的編輯我的答案。不要在整個數據框中使用'as.matrix';它將您的數字列轉換爲字符串... –