【发布时间】:2014-01-02 06:56:42
【问题描述】:
我正在尝试使用 ade4 包的 s.class 函数构建 pca 图。 我有一个数据集,其中包含不同样本(列)中细菌种类(行)的丰度。我需要执行一些统计测试并获得一些样本集群,以在 PCA 中表示它们。 我按照论文上发表的脚本运行了这个:
>data=read.table("L4.txt",header=T,row.names=1,dec=".",sep="\t")
>data=data[-1,]
>library(cluster)
> JSD <- function(x,y) sqrt(0.5 * KLD(x, (x+y)/2) + 0.5 * KLD(y, (x+y)/2))
> KLD <- function(x,y) sum(x * log(x/y))
> dist.JSD <- function(inMatrix, pseudocount=0.000001, ...) {
KLD <- function(x,y) sum(x *log(x/y))
JSD<- function(x,y) sqrt(0.5 * KLD(x, (x+y)/2) + 0.5 * KLD(y, (x+y)/2))
matrixColSize <- length(colnames(inMatrix))
matrixRowSize <- length(rownames(inMatrix))
colnames <- colnames(inMatrix)
resultsMatrix <- matrix(0, matrixColSize, matrixColSize)
inMatrix = apply(inMatrix,1:2,function(x) ifelse (x==0,pseudocount,x))
for(i in 1:matrixColSize) {
for(j in 1:matrixColSize) {
resultsMatrix[i,j]=JSD(as.vector(inMatrix[,i]),
as.vector(inMatrix[,j]))
}
}
colnames -> colnames(resultsMatrix) -> rownames(resultsMatrix)
as.dist(resultsMatrix)->resultsMatrix
attr(resultsMatrix, "method") <- "dist"
return(resultsMatrix)
}
>data.dist=dist.JSD(data)
>pam.clustering=function(x,k) { # x is a distance matrix and k the number of clusters
require(cluster)
cluster = as.vector(pam(as.dist(x), k, diss=TRUE)$clustering)
return(cluster)
}
>data.cluster=pam.clustering(data.dist,k=3)
>library(ade4)
>obs.pca=dudi.pca(data.frame(t(data)), scannf=F, nf=10)
>obs.bet=bca(obs.pca, fac=as.factor(data.cluster), scannf=F, nf=k-1)
>s.class(obs.bet$ls, fac=as.factor(data.cluster), grid=F)
我的数据如下:
06.TO.VG 21.TO.V 02.TO.VG 41.TO.VG 30.TO.V
Actinomycetaceae 0.000000000 0.00000000 0.000000000 0.000000000 0.000000000
Bifidobacteriaceae 0.019108280 0.00000000 0.000000000 0.000787092 0.000000000
Coriobacteriaceae 0.060006705 0.01490985 0.002632445 0.003148367 0.008313539
Propionibacteriaceae 0.000000000 0.00000000 0.000000000 0.000000000 0.000000000
Bacteroidetes 0.000000000 0.00000000 0.000000000 0.000000000 0.000000000
Bacteroidia 0.157224271 0.02288488 0.010529780 0.002361276 0.005938242
Bacteroidaceae 0.072745558 0.01178918 0.028956894 0.059031877 0.097387173
Porphyromonadaceae 0.004022796 0.01005548 0.147745969 0.026367572 0.038004751
Prevotellaceae 0.083808247 0.30374480 0.586048042 0.487603306 0.290973872
Rikenellaceae 0.025477707 0.07836338 0.011187891 0.003935458 0.004750594
我有我正在寻找的情节,但没有样本名称,所以我不知道哪些样本聚集在一起。 我还尝试构建一个包含我的数据的列名(sampleID)的向量,然后将其传递给 s.class 中的标签选项
> s.class(obs.bet$ls, fac=as.factor(data.cluster), grid=F, label=labs)
但我还是没有……
有没有办法用 s.class 函数来获得这个?
谢谢 弗朗西斯卡
【问题讨论】:
-
请提供一个可重现的例子。
-
在代码的倒数第二行,您有
nf=k-1,但您从未定义k,因此您的代码无法运行。我从上下文中假设k=3,但您可能应该编辑您的问题。