統計ER

R, EZR, SPSS, KH Coder を使ったデータ分析方法を紹介するブログ。ニッチな内容が多め

バギング アンサンブル学習を R で

バギングというアンサンブル学習を R でやってみる。

↑1万人以上の医療従事者が購読中

 

 

 

ランダムフォレストとバギングの違い

ランダムフォレストとバギングの違いは、以下の記事を参照。

toukeier.hatenablog.com

  

バギングのための R パッケージの準備

adabagパッケージをインストールする。

install.packages("adabag")

 

インストール後、呼び出しておく。

library(adabag)

 

バギングためのデータの準備

例題のデータを準備する。

データはirisを使う。

irisは150行のデータ。一部を学習データにする。

150を50ずつ3つに分けて、それぞれ50個から35個をランダムに取りだして、105個のsubという数列を作る。subに一致する行番号のデータを学習データにする。

いつも同じ結果ができるようにseedを決めておく。

set.seed(17)

sub <- c(sample(1:50, 35), sample(51:100, 35), sample(101:150, 35))

 

↑1万人以上の医療従事者が購読中

 

バギングの解析例

irisのSpeciesを分類する識別器をbaggingで求める。mfinalは分類木の数。デフォルトは100。

set.seed(17)
iris.bagging<-bagging(Species ~ ., data=iris[sub,], mfinal=10)
iris.bagging

 

分類木は以下の10個が作られた。

> iris.bagging$trees
1
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 66 versicolor (0.3238095 0.3714286 0.3047619)  
  2) Petal.Length< 2.5 34  0 setosa (1.0000000 0.0000000 0.0000000) *
  3) Petal.Length>=2.5 71 32 versicolor (0.0000000 0.5492958 0.4507042)  
    6) Petal.Width< 1.7 40  1 versicolor (0.0000000 0.9750000 0.0250000) *
    7) Petal.Width>=1.7 31  0 virginica (0.0000000 0.0000000 1.0000000) *

2
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 67 setosa (0.36190476 0.29523810 0.34285714)  
  2) Petal.Length< 2.5 38  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.5 67 31 virginica (0.00000000 0.46268657 0.53731343)  
    6) Petal.Width< 1.75 34  3 versicolor (0.00000000 0.91176471 0.08823529) *
    7) Petal.Width>=1.75 33  0 virginica (0.00000000 0.00000000 1.00000000) *

3
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 59 versicolor (0.20000000 0.43809524 0.36190476)  
  2) Petal.Width< 1.7 71 25 versicolor (0.29577465 0.64788732 0.05633803)  
    4) Petal.Length< 2.5 21  0 setosa (1.00000000 0.00000000 0.00000000) *
    5) Petal.Length>=2.5 50  4 versicolor (0.00000000 0.92000000 0.08000000) *
  3) Petal.Width>=1.7 34  0 virginica (0.00000000 0.00000000 1.00000000) *

4
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 105 63 setosa (0.4000000 0.2761905 0.3238095)  
   2) Petal.Length< 2.6 42  0 setosa (1.0000000 0.0000000 0.0000000) *
   3) Petal.Length>=2.6 63 29 virginica (0.0000000 0.4603175 0.5396825)  
     6) Petal.Width< 1.75 33  4 versicolor (0.0000000 0.8787879 0.1212121)  
      12) Petal.Length< 4.95 26  0 versicolor (0.0000000 1.0000000 0.0000000) *
      13) Petal.Length>=4.95 7  3 virginica (0.0000000 0.4285714 0.5714286) *
     7) Petal.Width>=1.75 30  0 virginica (0.0000000 0.0000000 1.0000000) *

5
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 68 setosa (0.35238095 0.31428571 0.33333333)  
  2) Petal.Length< 2.5 37  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.5 68 33 virginica (0.00000000 0.48529412 0.51470588)  
    6) Petal.Length< 4.8 31  0 versicolor (0.00000000 1.00000000 0.00000000) *
    7) Petal.Length>=4.8 37  2 virginica (0.00000000 0.05405405 0.94594595) *

6
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 70 setosa (0.33333333 0.33333333 0.33333333)  
  2) Petal.Length< 2.6 35  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.6 70 35 versicolor (0.00000000 0.50000000 0.50000000)  
    6) Petal.Length< 4.75 34  0 versicolor (0.00000000 1.00000000 0.00000000) *
    7) Petal.Length>=4.75 36  1 virginica (0.00000000 0.02777778 0.97222222) *

7
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 60 versicolor (0.25714286 0.42857143 0.31428571)  
  2) Petal.Length< 2.6 27  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.6 78 33 versicolor (0.00000000 0.57692308 0.42307692)  
    6) Petal.Length< 4.75 42  0 versicolor (0.00000000 1.00000000 0.00000000) *
    7) Petal.Length>=4.75 36  3 virginica (0.00000000 0.08333333 0.91666667) *

8
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 66 setosa (0.37142857 0.30476190 0.32380952)  
  2) Petal.Length< 2.6 39  0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.6 66 32 virginica (0.00000000 0.48484848 0.51515152)  
    6) Petal.Length< 4.75 31  0 versicolor (0.00000000 1.00000000 0.00000000) *
    7) Petal.Length>=4.75 35  1 virginica (0.00000000 0.02857143 0.97142857) *

9
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 59 versicolor (0.22857143 0.43809524 0.33333333)  
  2) Petal.Width< 1.7 71 25 versicolor (0.33802817 0.64788732 0.01408451)  
    4) Petal.Length< 2.6 24  0 setosa (1.00000000 0.00000000 0.00000000) *
    5) Petal.Length>=2.6 47  1 versicolor (0.00000000 0.97872340 0.02127660) *
  3) Petal.Width>=1.7 34  0 virginica (0.00000000 0.00000000 1.00000000) *

10
n= 105 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 105 67 setosa (0.3619048 0.3333333 0.3047619)  
  2) Petal.Length< 2.6 38  0 setosa (1.0000000 0.0000000 0.0000000) *
  3) Petal.Length>=2.6 67 32 versicolor (0.0000000 0.5223881 0.4776119)  
    6) Petal.Width< 1.7 35  0 versicolor (0.0000000 1.0000000 0.0000000) *
    7) Petal.Width>=1.7 32  0 virginica (0.0000000 0.0000000 1.0000000) *

 

分類木の決定は Plurality Vote(多数代表制)で決める。

The majority vote (the most often predicted class) で併合する。

adabag: An R Package for Classification with Boosting and Bagging (PDF)

 

テストデータ(-subで行番号を指定)で予測性能を確認する。

predict.bagging()でテストデータでの予測が行える。

iris.predbagging<-predict.bagging(iris.bagging, newdata=iris[-sub,])
iris.predbagging

 

Confusion tableで誤分類を確認する。

versicolorをvirginicaに1件、virginicaをversicolorに2件、誤分類した。

> iris.predbagging$confusion
               Observed Class
Predicted Class setosa versicolor virginica
     setosa         15          0         0
     versicolor      0         14         2
     virginica       0          1        13

 

誤分類率は0.067(6.7%)と計算された。

> iris.predbagging$error
[1] 0.06666667

 

まとめ

バギングは、学習セットからブートストラップ法で何度もサンプリングして、分類器をいくつも作り、それを併合してよりよい分類器を作る方法。

Rでは、adabagパッケージのbagging()で実行可能。