Rでクラスター分析

(1)R起動
$ R

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

(2)データ入力
> seiseki<-matrix 40="" 55="" 57="" byrow="TRUE) </div" c="">
> colnames(seiseki)<-c div="">
> rownames(seiseki)<-c div="">
> seiseki.d<-dist div="" seiseki="">
> round(seiseki.d)
     田中 佐藤 鈴木 本田 川端 吉野
佐藤   69                         
鈴木   34   81                    
本田   60   64   53               
川端   28   61   21   47          
吉野   63   12   76   54   56     
斉藤   68   38   88   92   68   46

(3)クラスター分析
 クラスターの方法
■ 最近隣法
 最近隣法(nearest neighbor method)は、最短距離法、単連結法(single linkagr)法とも呼ばれる。最近隣法は、2つのクラスターのそれぞれの中から1個ずつ個体を選んで個体間の距離を求め、それらの中で、最も近い個体間の距離をこの2つのクラスター間の距離とする方法である。

■ 最遠隣法
 最遠隣法(furthest neighbor method)は、最遠距離法、完全連結(complete linkage)法とも呼ばれる。最遠隣法は、最近隣法とは逆に、2つのクラスターの中のそれぞれの中から1個ずつ個体を選んで個体間の距離を求め、それらの中で、最も遠い個体間の距離をこの2つのクラスター間の距離とする方法である。

■ 群平均法
 群平均法(group average method)は、最近隣法と最遠隣法を折衷した方法で、2つのクラスターのそれぞれの中から1個ずつ個体を選んで個体間の距離を求め、それらの距離の平均値を2つのクラスター間の距離とする。

■ 重心法
 重心法(centroid method)は、クラスターのそれぞれの重心(例えば、平均ベクトル)を求め、その重心間の距離をクラスターの間の距離とする。重心を求める際には、クラスターに含まれる個体数が反映されるように、個体数を重みとして用いる。

■ メディアン法
 メディアン(median method)法は、重心法の変形で、2つのクラスターの重心の間の重み付きの距離を求めるとき、重みを等しくして求めた距離の値を、2つのクラスター間の距離とする。

■ ウォード法
 ウォード法(Ward's method)は、2つのクラスターを融合した際に、群内の分散と群間の分散の比を最大化する基準でクラスターを形成していく方法である。ウォード法は最小分散法(minimum variance method)とも呼ばれている。

ユークリッド距離

> sei.d<-dist div="" seiseki="">
>  (sei.hc<-hclust div="" sei.d="">

Call:
hclust(d = sei.d)

Cluster method   : complete 
Distance         : euclidean 
Number of objects: 7 

>  summary(sei.hc)
            Length Class  Mode     
merge       12     -none- numeric  
height       6     -none- numeric  
order        7     -none- numeric  
labels       7     -none- character
method       1     -none- character
call         2     -none- call     
dist.method  1     -none- character
> sei.hc$merge
     [,1] [,2]
[1,]   -2   -6
[2,]   -3   -5
[3,]   -1    2
[4,]   -7    1
[5,]   -4    3
[6,]    4    5


(4)樹形図  関数plotあるいはplclustを用いてhclustの結果の樹形図を作成
> par(mfrow=c(2,2))
> plot(sei.hc,main="最遠隣法") 
> plot(sei.hc,hang=-1,main="最遠隣法") 
> s.hc2<-hclust method="centroid" sei.d="" span="">
>  plot(s.hc2,hang=-1,main="重心法")
> s.hc3<-hclust method="ward" sei.d="" span="">
The "ward" method has been renamed to "ward.D"; note new "ward.D2"
> plot(s.hc3,hang=-1,main="ウォード法")




コメント

人気の投稿