2017-06-09 5 views
1

は質問1: 私はdocumentationhttps://cran.r-project.org/web/packages/apcluster/apcluster.pdf)でAggExResultオブジェクトのプロット()関数およびクラスタで動作するように仕事しようとしています予想通り。- 着色Dendogramsときれいにフォーマット出力が

私自身のデータでは、分類の目的で事前定義された "ターゲット"を提供する入力欄が追加されています。また、色で強調表示された樹状突起ラベルを持つ方法があるかどうか疑問です=クラス0、青色=クラス1)であり、ターゲットのクラスは因子(または文字)である。私は最終的に、「純粋な」対「混在」クラスを含むクラスタの数を視覚的に表示しようとしています。ここでは、オンラインドキュメントは、私の入力データがどのように見えるかを大まかに示すために、いくつか少し変更したコードは次のとおりです。

cl1Targ <- matrix(nrow=50,ncol=1) 
for(c1t in 1:nrow(cl1Targ)){ cl1Targ[c1t] <- as.factor(0) } 
cl2Targ <- matrix(nrow=50,ncol=1) 
for(c2t in 1:nrow(cl2Targ)){ cl2Targ[c2t] <- as.factor(1) } 

## create two Gaussian clouds 
#cl1 <- cbind(rnorm(50,0.2,0.05),rnorm(50,0.8,0.06)) 
#cl2 <- cbind(rnorm(50,0.7,0.08),rnorm(50,0.3,0.05)) 
cl1 <- cbind(rnorm(50,0.2,0.05),rnorm(50,0.8,0.06),cl1Targ) 
cl2 <- cbind(rnorm(50,0.7,0.08),rnorm(50,0.3,0.05),cl2Targ) 
x <- rbind(cl1,cl2) 
colnames(x) <- c('Column 1','Column 2','Class_ID') 

## compute similarity matrix (negative squared Euclidean) 
sim <- negDistMat(x, r=2) 
## run affinity propagation 
apres <- apcluster(sim, q=0.7) 
## compute agglomerative clustering from scratch 
aggres1 <- aggExCluster(sim) 
## plot dendrogram 
plot(aggres1, main='aggres1 w/ target') # 

どのように入力で定義されたターゲットによる系統樹に色でしょうか?

質問2: 私は(見せる)例えば、データのAPResult、私は以下を参照してください。

show(apres)  
APResult object 

Number of samples  = 100 
Number of iterations = 165 
Input preference  = -0.01281384 
Sum of similarities = -0.1222309 
Sum of preferences = -0.1409522 
Net similarity  = -0.2631832 
Number of clusters = 11 

Exemplars: 
    8 17 24 37 43 52 58 68 92 95 99 
Clusters: 
    Cluster 1, exemplar 8: 
     7 8 9 25 31 36 39 42 47 48 
    Cluster 2, exemplar 17: 
     6 11 13 15 17 18 19 23 32 35 
    Cluster 3, exemplar 24: 
     2 5 10 24 45 

私は自分のデータを使用する場合、私はこれ、row.names(以下を参照してください。私は内容的に期待されるが、私は報告目的のためにこれをフォーマットしたいと思い何も

show(apclr2q05_mean) 

APResult object 

Number of samples  = 1045 
Number of iterations = 429 
Input preference  = -390.0822 
Sum of similarities = -89326.99 
Sum of preferences = -83477.58 
Net similarity  = -172804.6 
Number of clusters = 214 

Exemplars: 
    amantadine_58mg6h_fc amiodarone_147mg3d_fc clarithromycin_56mg1d_fc fluconazole_394mg5d_fc ketoconazole_114mg5d_fc ketoconazole_2274mg1d_fc 
    pantoprazole_1100mg1d_fc pantoprazole_1100mg3d_fc quetiapine_500mg5d_fc roxithromycin_312mg5d_fc torsemide_3mg3d_fc acetazolamide_250mg3d_fc 
Clusters: 
    Cluster 1, exemplar amantadine_58mg6h_fc: 
     amantadine_58mg6h_fc promazine_100mg1d_fc cyproteroneAcetate_2500mg6h_fc danazol_2g5d_fc ivermectin_7500ug1d_fc letrozole_250mg6h_fc 
     mefenamicAcid_93mg3d_fc olanzapine_23mg1d_fc secobarbital_20mg6h_fc zaleplon_100mg3d_fc 
    Cluster 2, exemplar amiodarone_147mg3d_fc: 
     amiodarone_147mg3d_fc amiodarone_147mg5d_fc aspirin_375mg5d_fc betaNapthoflavone_80mg5d_fc clofibrate_130mg3d_fc finasteride_800mg5d_fc 
    Cluster 3, exemplar clarithromycin_56mg1d_fc: 
     ciprofloxacin_72mg5d_fc ciprofloxacin_450mg6h_fc clarithromycin_56mg1d_fc clarithromycin_56mg3d_fc clarithromycin_56mg5d_fc 
    Cluster 4, exemplar fluconazole_394mg5d_fc: 
     fluconazole_394mg5d_fc 

)遺伝子発現によって、クラスタ化された薬は、変更値を折る意味しています。私はdput()を使ってこれをエクスポートしようとしましたが、出力ファイルに余計な不必要な情報がたくさんあります。私は、上で述べたオブジェクト名とターゲット分類子と共に、上から同じタイプの情報を、次のような表にエクスポートすることができるかもしれないと思っています(

これらの質問に電子メールで迅速に応答してくれて大変感謝しています。コミュニティと話し合いたいと思っています。 )


アップデートとして、質問1の回答を実装しようとしましたが、サンプルコードは期待通りに機能しますが、これを自分のデータで動作させるのに問題があります。入力データには2つの部分があります。最初は、列と行のラベルを含む数値の測定データとのマトリクスである:

> fci[1:3,1:3] 
         M30596_PROBE1 AI231309_PROBE1 NM_012489_PROBE1 
amantadine_58mg1d_fc  0.05630744  -0.10441722  0.41873201 
amantadine_58mg6h_fc -0.42780274  -0.26222322  0.02703001 
amantadine_220mg1d_fc 0.35260779  -0.09902214  0.04067055 

第二は、上記FCIの同じ行に対応し、それぞれが倍形式で「目標」値である:

> targs[1:3] 
amantadine_58mg1d_fc amantadine_58mg6h_fc amantadine_220mg1d_fc 
        0      0      0 
Levels: 0 1 
ここから

は、ツリーは以下のように構築されました:

# build the AggExResult: 
aglomr1 <- aggExCluster(negDistMat(r=2), fci) 

# convert the data 
tree <- as.dendrogram(aglomr1) 

# assign the color codes 
colorCodes <- c("0"="red", "1"="green") 
names(targs) <- rownames(fci) 
xColor <- colorCodes[as.character(targs)] 
names(xColor) <- rownames(fci) 

# plot the colored tree 
labels_colors(tree) <- xColor[order.dendrogram(tree)] 
plot(tree, main="Colored Tree") 

ツリーが生成されましたが、葉が着色されませんでした。いくつかの掘削を行う:ラインlabels_colors(木)<一部が割り当てられた正しい色を有するターゲットの面で期待どおりに動作するようだが、rownamesははxcolorではありません、その

> head(xColor) 
    0  0  0  0  0  0 
"red" "red" "red" "red" "red" "red" 

- はxcolor [オーダー。系統樹(木)]は、同様のラベルを返すのではなく、行番号、またはNASであることをどのように表示されていません:私はラインlabels_colors(木)を取得する方法を

> head(order.dendrogram(tree)) 
[1] "295" "929" "488" "493" "233" "235" 

> head(labels_colors(tree)) 
295 929 488 493 233 235 

> head(xColor[order.dendrogram(tree)]) 
<NA> <NA> <NA> <NA> <NA> <NA> 
NA NA NA NA NA NA 

< - はxcolor [order.dendrogram(ツリー)]は、提供されている例と同じように動作しますか?具体的には、amantadine_58mg1d_fcなどの葉書は、ターゲット(0/1)に対応する色で強調表示されています。ここで

+0

あなたは類似性を計算する方法について少しコメント:あなたのデータは、数値ラベル列、negDistMatを(持っている場合)はこの列を使用します。これはおそらくあなたが欲しい/意図しているものではありません。したがって、クラスタリングする前にラベル列をデータから削除するか、データをデータフレームに配置してください。これを行い、ラベル列が要素の場合、 'apcluster'パッケージに実装されている類似性測度は自動的に無視されます。 – UBod

+0

1.4.4より前のバージョンのas.dendrogram()メソッドにバグがありました。そのため、xColor [order.dendrogram(tree)]による色の割り当てが機能しませんでした。古いバージョンでは、バージョン1.4.4(CRANの2017-07-04以降)にアップグレードするか、回避策xColor [as.numeric(order.dendrogram(tree))]を使用することをお勧めします。 – UBod

答えて

0

はあなた質問1に私の答えは:「AggExResult」のオブジェクトのplot()方法は、内部plot.dendrogram()メソッドを使用しています。この方法は樹状図の葉を着色することができないため、これは機能しません。しかし、そのような機能を提供する 'dendextend'パッケージがあります。 [apcluster]は 'hclust'オブジェクトと 'dendrogram'オブジェクトにいくつかのキャストを提供しているので、このパッケージの機能は多かれ少なかれ直接使用できます(別のスレッドでこの解決策を見つけました:Label and color leaf dendrogram in r)。

ので、ここでいくつかのサンプルコードは次のとおりです。ここで

library(apcluster) 

## create two Gaussian clouds along with class labels 0/1 
cl1 <- cbind(rnorm(50, 0.2, 0.05), rnorm(50, 0.8, 0.06)) 
cl2 <- cbind(rnorm(50, 0.7, 0.08), rnorm(50, 0.3, 0.05)) 
x <- cbind(Columns=data.frame(rbind(cl1, cl2)), 
      "Class_ID"=factor(as.character(c(rep(0, 50), rep(1, 50))))) 

## compute similarity matrix (negative squared Euclidean) 
sim <- negDistMat(x[, 1:2], r=2) 

## compute agglomerative clustering from scratch 
aggres1 <- aggExCluster(sim) 

## load 'dendextend' package 
## install.packages("dendextend") ## if not yet installed 
library(dendextend) 

## convert object 
tree <- as.dendrogram(aggres1) 

## assign color codes 
colorCodes <- c("0"="red", "1"="green") 
xColor <- colorCodes[x$Class_ID] 
names(xColor) <- rownames(x) 

## plot color-labeled tree 
labels_colors(tree) <- xColor[order.dendrogram(tree)] 
plot(tree) 
0

はあなた質問2に私の答えです。申し訳ありませんが、このような機能は、「apcluster」パッケージに実装されていません。これはかなり特殊な要求なので、パッケージに含めることには消極的です(show()メソッドは追加の引数を持つことはできません)。だから、その代わりに、私が手本とサンプルをグループ化/標識のためにできるカスタム機能を提供します:

library(apcluster) 

## create two Gaussian clouds along with class labels 0/1 
cl1 <- cbind(rnorm(50, 0.2, 0.05), rnorm(50, 0.8, 0.06)) 
cl2 <- cbind(rnorm(50, 0.7, 0.08), rnorm(50, 0.3, 0.05)) 
x <- cbind(Columns=data.frame(rbind(cl1, cl2)), 
      "Class_ID"=factor(as.character(c(rep(0, 50), rep(1, 50))))) 

## compute similarity matrix (negative squared Euclidean) 
sim <- negDistMat(x[, 1:2], r=2) 

## special show() function with labeled data 
show.ExClust.labeled <- function(object, labels=NULL) 
{ 
    if (!is(object, "ExClust")) 
     stop("'object' is not of class 'ExClust'") 

    if (is.null(labels)) 
    { 
     show(object) 
     return(invisible(NULL)) 
    } 

    cat("\n", class(object), " object\n", sep="") 

    if (!is.finite([email protected]) || !is.finite([email protected])) 
     stop("object is not result of an affinity propagation run; ", 
      "it is pointless to create 'APResult' objects yourself.") 

    cat("\nNumber of samples  = ", [email protected], "\n") 
    if (length([email protected]) > 0) 
    { 
     cat("Number of sel samples = ", length([email protected]), 
      paste(" (", round(100*length([email protected])/[email protected],1), 
        "%)\n", sep="")) 
     cat("Number of sweeps  = ", [email protected], "\n") 
    } 
    cat("Number of iterations = ", [email protected], "\n") 
    cat("Input preference  = ", [email protected], "\n") 
    cat("Sum of similarities = ", [email protected], "\n") 
    cat("Sum of preferences = ", [email protected], "\n") 
    cat("Net similarity  = ", [email protected], "\n") 
    cat("Number of clusters = ", length([email protected]), "\n\n") 

    if (length([email protected]) > 0) 
    { 
     if (length(names([email protected])) == 0) 
     { 
      cat("Exemplars:\n") 
      df <- data.frame("Sample"[email protected], 
          Label=labels[[email protected]]) 
      print(df, row.names=FALSE) 

      for (i in 1:length([email protected])) 
      { 
       cat("\nCluster ", i, ", exemplar ", 
        [email protected][i], ":\n", sep="") 

       df <- data.frame([email protected][[i]], 
           Label=labels[[email protected][[i]]]) 
       print(df, row.names=FALSE) 
      } 
     } 
     else 
     { 
      df <- data.frame("Exemplars"=names([email protected]), 
          Label=labels[names([email protected])]) 
      print(df, row.names=FALSE) 

      for (i in 1:length([email protected])) 
      { 
       cat("\nCluster ", i, ", exemplar ", 
        names([email protected])[i], ":\n", sep="") 

       df <- data.frame(Sample=names([email protected][[i]]), 
           Label=labels[names([email protected][[i]])]) 
       print(df, row.names=FALSE) 
      } 
     } 
    } 
    else 
    { 
     cat("No clusters identified.\n") 
    } 
} 


## create label vector (with proper names) 
label <- x$Class_ID 
names(label) <- rownames(x) 

## run apcluster() 
apres <- apcluster(sim, q=0.3) 

## show with labels 
show.ExClust.labeled(apres, label) 
関連する問題