データサイエンティスト上がりのDX参謀・起業家

データサイエンティスト上がりのDX参謀・起業家のブログ。データ分析や事業について。自身はアーティスト、経営者、事業家。

決定木のRパッケージについて

教科書

The Elements of Statistical Learning: Data Mining, Inference, and Prediction, Second Edition (Springer Series in Statistics)

The Elements of Statistical Learning: Data Mining, Inference, and Prediction, Second Edition (Springer Series in Statistics)

Classification and Regression Trees (Wadsworth Statistics/Probability)

Classification and Regression Trees (Wadsworth Statistics/Probability)

Rによる統計解析ハンドブック

Rによる統計解析ハンドブック

  • 作者: Brian S.Everitt,Torsten Hothorn,大門貴志,吉川俊博,手良向聡
  • 出版社/メーカー: メディカル・パブリケーションズ
  • 発売日: 2010/04
  • メディア: 単行本
  • 購入: 20人 クリック: 744回
  • この商品を含むブログ (6件) を見る



決定木のアルゴリズムはこれらが代表的です。

  • CART(かーと、classification and regression tree、ほとんどこれ)
  • ID3→C4.5→C5.0(ID3からバージョンアップ)
  • J4.8


Rのどのパッケージで何が出来るかを紹介します。

  • partykit
  • mvpart
  • rpart
    • 『Classification and Regression Trees』に説明されているアルゴリズム
    • CART、Gini係数がデフォルト
  • party
    • 条件推測木(conditional inference tree、前のノードで条件付けてる?)
    • 結果変数による説明変数の有意性検定を行い、多重調整したP値が有意水準を下回ると分岐する
    • 枝が少なめになりやすい。
  • randomForest
    • CARTでのバッギングアンサンブル
  • C5.0
  • CAHID
    • https://r-forge.r-project.org/R/?group_id=343
    • 枝を複数に分けれる
    • Both response and all covariates are assumed to be categorical(説明変数も結果変数も全てカテゴリにする必要がある、計算時間が長い)
  • RWeka
    • J4.8、M5’、LMT
  • knnTree
    • k近傍木
  • LogicReg
    • 論理回帰
  • BayesTree
  • TWIX
    • extra tree、Response must be a factor!!(結果変数は因子のみ)
  • Cubist
    • cubist
  • randomSurvivalForest
    • 生存時間ランダムフォレスト
  • gbm
    • 一般化ブースティング木
  • mboost
    • モデル勾配ブースティング、勾配ブースティング木(blackboosting、回帰ラインの解釈が難しいのでブラックボックスのblackを付けている)


それぞれのパッケージの実行例です。

library(mboost)	# bodyfatデータ用

#------rpart
library(rpart)
bodyfat_rpart <- rpart(DEXfat ~ age + waistcirc + hipcirc + elbowbreadth + 
kneebreadth, data = bodyfat, control = rpart.control(minsplit = 10))
#---普通のプロット
plot(bodyfat_rpart)
text(bodyfat_rpart, use.n=T)

#---partykitのプロット
library(partykit)
plot(as.party(bodyfat_rpart), tp_args = list(id = FALSE))	#tp_argsでnode idの表示

#------ランダムフォレスト、caretで並列計算
(vignettも良い http://cran.r-project.org/web/packages/caret/index.html)
library(randomForest)
bodyfat_rf <-randomForest(DEXfat ~ age + waistcirc + hipcirc + elbowbreadth + 
kneebreadth, data = bodyfat, importance=TRUE)
varImpPlot(bodyfat_rf)

#------mvpart
library(mvpart)


#------partyの条件付き分割
bodyfat_ctree <- ctree(DEXfat ~ age + waistcirc + hipcirc + elbowbreadth + kneebreadth, data = bodyfat)
plot(bodyfat_ctree)


#------mboost
bodyfat.gb <- blackboost(DEXfat ~ age + waistcirc + hipcirc + elbowbreadth + 
kneebreadth, data = bodyfat, control = boost_control(mstop = 50))
plot(bodyfat.gb)


#------CHAID(結構時間がかかる、699obs 11varで30秒程度、カイD)
library(CHAID)
library(mlbench)
data("BreastCancer", package = "mlbench")
b_chaid <- chaid(Class ~ Cl.thickness + Cell.size + Cell.shape + Marg.adhesion + 
Epith.c.size + Bare.nuclei + Bl.cromatin + Normal.nucleoli + Mitoses, data = 
BreastCancer)
plot(b_chaid)


#------TWIX
library(TWIX)
TM <- TWIX(Class ~ Cl.thickness + Cell.size + Cell.shape + Marg.adhesion + 
Epith.c.size + Bare.nuclei + Bl.cromatin + Normal.nucleoli + Mitoses, data = 
BreastCancer, topN=c(9,9), method="local")
plot(TM)