我试图用ade4包的s.class函数来构建一个pca图。 我有一个数据集,其中包含不同样本(列)中细菌种类(行)的丰度。我需要执行一些统计测试,并获得我的样本的一些集群,以在PCA中表示它们。 我跑了这一点,下面就一纸公布的脚本:添加样品名到PCA绘制s.class
>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',但你应该编辑你的问题。 – jlhoward