DecTree_JPH {lazy.tree}R Documentation

決定木の作成

Description

Greedy algorithm with Depth-First Search (DFS) を用いて 個体の集合を2分割することを繰り返し決定木を求める。
English help file: DecTree

Usage

DecTree( y, X, impfuncname="gini", majestic=0, method=4, app=0
, maxapp=12, maxncut=50, switch=0
, eps=0, minnobs=1,maxdepth=50, maxdivide=500, minp = 1e-04
, nrepM=50, psampleM=100, rf=1
, nxvarM=if(!is.null(y) && !is.factor(y))
         max(floor(ncol(X)/3), 1) else floor(sqrt(ncol(X)))
, .R.sM=NULL
, minLeftM=0.5, maxRightM=0.5, minnobsM=10
, title=NULL, print=1, plot=0, debug=0 )

Arguments

y

基準変数ベクトル

X

説明変数行列からなる data frame

inpfuncname

不純度指標を計算する関数名

majestic

= 1, 2, 3, 4, or 5 to create a in-node resampling tree
= -1 to create simple in-node resampling tree. その場合には、結果の再現性のために、DecTree を呼び出す前に乱数の seed を 設定することが望ましい。

method

カテゴリカルな説明変数の数量化の方法の指定
= 0 説明変数のダミー展開,
= 1 第1正準変数のみを利用,
= 2 第2正準変数が存在する場合には, それに加えてと、 第1変数との和と差を利用
= 3 method=2 の結果に加えて、基準変数の各水準を one-versus-rest で2値化し そレらの説明変数の各水準ごとの条件付き平均を用いる。
= 4 基準変数の水準の二つのサブセットへの分割をもとに 基準変数を2値化し、説明変数の水準ごとの条件付き平均を用いる。

app

app (all possible partition) の利用の指定

maxapp

app を利用するための説明変数の最大水準数 また、method=4 の場合の基準変数の最大水準数

maxncut

連続的説明変数の閾値の候補の最大数

eps

不純度の改善の最小値

minnobs

ノードに含まれる個体数の最大値

maxdepth

木の最大深度

maxdivide

最大分割数

minp

minimum value of multinomial probability

nrepM

# of divisions to try for majestic tree

psampleM

percentage of the sample for majestic tree

rf

= 0 not to resample regressors

nxvarM

# of X variables to sample for majestic tree (Random Forest)

.R.sM

random number seed for majestic tree obtained as:
.R.sM=get(".Random.seed", envir=globalenv() )
in the previous run of this module.

minLeftM

minimum proportion of x values to be classified as Left

maxRightM

maximum proportion of x values to be classified as Right

minnobsM

minimum # of obs in a node to use majestic

title

タイトルとして使われる文字列

print

= 1 で結果を表示、 = 2 で分割履歴を表示、 = 2 で詳細を表示

plot

= 1 で決定木を印刷 ( igraph パッケージが必要)

debug

= 1 to print intermediate result

Details

入力変数
基準変数 y は numeric または factor であること。 説明変数行列 X は data frame で有ること。 カテゴリカルな変数は factor とすること。

分割の詳細
数値的な説明変数は、その全てのユニークな値が分割の閾値として試される。

もしも基準変数が数値的か2水準のカテゴリカルな場合には 説明変数の各水準ごとに基準変数の条件付き平均を計算し、その値を 数値化された説明変数の値とする。(以下の全ての method の指定でも同様)

method=0: 基準変数の水準数が 3 以上の場合には、基準変数と 説明変数を one-hot なダミー変数に展開したものを数値化された説明変数とする。
(数値化された説明変数の数 = 説明変数の水準の数)

method=1: もしも基準変数が数値的か2水準のカテゴリカルな場合には 説明変数の各水準ごとに基準変数の条件付き平均を計算し、その値を 数値化された説明変数の値とする。(以下の全ての method の指定でも同様)
基準変数の水準数が 3 以上の場合には、基準変数と 説明変数を one-hot なダミー変数に展開したものとの間で正準判別分析を行い 計算された第1正準変数 cv1 を数値化された説明変数の値とする。
(数値化された説明変数の数 = 1)

method=2: もしも第2正準変数 cv2 が計算可能な場合には上記の cv1 に 加えて cv2 と cv1+cv2, cv1-cv2 の全部で 4 つの値を 数値化された説明変数の値とする。 cv2 が計算できない場合には method=1 と同じ。
(数値化された説明変数の数 = 4 or 1)

method=3: 上記の 4 つの値に加えて、基準変数の各水準に関して 基準変数を one-versus-rest という形で2値化したものを用いて 説明変数の各水準ごとに基準変数の条件付き平均を計算したものを用いる。 ただし、分散が 0 になたっり他の変数と同一の値になる場合は除く。
(数値化された説明変数の数 = 4 or 1 + 基準変数の水準数)

If method=4:基準変数の水準を二つのサブセットへの分割をもとに 基準変数を2値化し、説明変数の水準ごとにそれらの条件付き平均を 計算したものを用いる。 ただし、分散が 0 になたっり他の変数と同一の値になる場合は除く。
(数値化された説明変数の数 = 2^nc-1-1、ただし、 nc は基準変数の 水準数。)
基準変数の水準が少なめで、説明変数の水準数が多い場合には最適。

app=1 の場合:
基準変数の水準数が 3 以上で説明変数の水準数も 3 以上の場合には 説明変数の全ての水準の組み合わせからなる2分割を用いて個体の分割を行う。

app=9 の場合:
説明変数がカテゴリカルな場合には常に 説明変数の全ての水準の組み合わせからなる2分割を用いて個体の分割を行う。

分割の停止のルール:
1) 分割による不純度指標の改善の割合が eps より小さいとき
2) 一つのノードに含まれる個体数が minnobs 以下になったとき
3) 分割によりどちらかのサブセットの個体数が 0 となったとき
4) 最大分割数が maxdivide を超えたとき
5) 最大深度が maxdepth になったとき.

決定木のグラフを出力するためには igraph パッケージが必要である。

Value

DecTree クラスのオブジェクトで、以下の要素からなるリスト

n: データ数
ncaty: 基準変数の水準数 ( 0 の場合は連続量)
yname: 基準変数の名前
yname2: y 引数の名前
nx: 説明変数の数
ncatx: 説明変数の水準数 ( 0 の場合は連続量
xname: 説明変数の名前
xname2; X 引数の名前
impfuncname: 不純度指標の名前
nleaf: 葉の数
MaxDepth: The maximum depth of the tree
RNGkindM, .R.sM: random number seed info

node: 各ノードの情報を含むリストで node[[r]] には以下のものが含まれる。
 node[[r]]$parent   親ノードの番号
 node[[r]]$set      このノードに含まれる個体番号
 node[[r]]$nobs     このノードに含まれる個体数
 node[[r]]$j        このノードの分割に使われる変数番号
 node[[r]]$k        数値化された説明変数の番号、または 0 または NA
 node[[r]]$th       j が数値変数の場合にその閾値(数値化された場合も含む)
 node[[r]]$valxj    j がカテゴリカルな場合
                    left ノードに分類される水準名
 node[[r]]$valxjR   j がカテゴリカルな場合
                    right ノードに分類される水準名
 node[[r]]$score    このノードを分割することにより得られる不純度
 node[[r]]$ImpMeasure  score / nobs
 node[[r]]$scoreimp このノードを分割することにより得られる不純度の改善度
 node[[r]]$impfuncname 不純度の指標名
 node[[r]]$depth    このノードの深度
 node[[r]]$Left     left として得られる子ノードの番号
 node[[r]]$Right    right として得られる子ノードの番号

 node[[r]]$switched = 1 when left and right were swiched.

 node[[r]]$yhat  ** 終端ノードにおける予測値
 node[[r]]$prob  ** 終端ノードにおける予測確率

fimport: 説明変数の重要度ベクトル

Tab: カテゴリカルな基準変数の場合の実測値と予測値とのクロス表
margin: クロス表の周辺度数
cor: 数値的な基準変数の場合の実測値と予測値の間の相関係数
Gini: gini 係数
mllh: 多項分布を仮定したときの対数尤度の符号を反転したもの
nparam, aic, bic:
tprecision: total precision
macroF1: macro F1
SCT: 各水準ごとの accuracy, precision, recall, specificity, and F1-score

args: DecTree への引数のリスト
title: DecTree が生成したタイトル文字列

y:  input
yhat: predicted value of y
prob: probability vector
Node: node number

RNGkind: The name of randon mumber generator
.R.sM: The random number seed used for majestic.

References

鈴木護 (2020) S統計的機械学習の数理 100 問 with R. 共立出版.

# @keywords internal

Examples

# univariate regression tree;
# numerical y and numerical X
X <- data.frame(x=univar$x)
plot( univar$x, univar$y1, type="p" )
res01 <- DecTree( univar$y1,X, minnobs=1, eps=0.001
, print=3, plot=0, app=0, title="y1" )
# library(igraph)
# plot_dt(res01$node)

# categorical y and continuous X
res02 <- DecTree( as.factor(univar$y1),X, minnobs=1, eps=0.001, maxncut=100
                 , print=3, plot=0,, app=0, title="y1, method=3" )

# categorical y and categorical X: (method 1 and 2 are not available)
Xc <- data.frame(x=as.factor(univar$x))
res03 <- DecTree( as.factor(univar$y1),Xc, minnobs=1, eps=0.001, method=3
                 , print=3, plot=0,, app=0, title="y1, method=3" )
res04 <- DecTree( as.factor(univar$y1),Xc, minnobs=1, eps=0.001, method=4
                 , print=3, plot=0,, app=0, title="y1, method=4" )




# bivariate classification tree
# categorical y, categorical X
SQ2=matrix(square$class4, 4,, byrow=1)
heatmap( SQ2, Rowv=NA, Colv=NA, reorderfun=NA, symm=1
       , labRow=1:4, labCol=1:4 )
Xc <- data.frame(x1=as.factor(square$x1),x2=as.factor(square$x2))
res1 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, method=1, print=3
            , plot=0, title="square: categorical y, categorical X" )

# categorical y, categorical X
res2 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, method=2, print=3
          , plot=0, title="square: categorical y, categorical X method=2" )

# categorical y, categorical X
res3 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, method=3, print=3
           , plot=0, title="square: categorical y, categorical X method=3" )

# categorical y, categorical X
res4 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, method=4, print=3
           , plot=0, title="square: categorical y, categorical X method=4" )


# categorical y, categorical X
res11 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, app=1, print=3
                 , title="square: categorical y, categorical X app=1" )

# categorical y, categorical X
res12 <- DecTree( square$Class4, Xc, minnobs=1, eps=0, app=9, print=3
                 , title="square: categorical y, categorical X app=9" )



# categorical y, numerical X
X <- data.frame(x1=square$x1,x2=square$x2)
res21 <- DecTree( square$Class4, X, minnobs = 2, eps=0, print=3
                   , title="square: categorical y, numerical X" )
plot2d_dt( square$Class4, X, res21$node, ngrid=50, title="Border Lines" )


# numerical y, numerical X
res22 <- DecTree( square$class4, X, minnobs=2, eps=0, print=3
                 , title="square: numerical y, numerical X" )



# slightly larger data
# categorical y, categorical X
Xc <- data.frame(x1=as.factor(square2$x1),x2=as.factor(square2$x2))
res31 <- DecTree( as.factor(square2$y), Xc, minnobs=50, eps=0.0001
                 , print=3, maxdepth=50, plot=0, app=9
                 , title="square: categorical y, categorical X, app=9" )

# categorical y, categorical X
Xc <- data.frame(x1=as.factor(square2$x1),x2=as.factor(square2$x2))
res32 <- DecTree( as.factor(square2$y), Xc, minnobs=50, eps=0.0001
                 , print=3, maxdepth=50, plot=0, app=0, method=4
                 , title="square: categorical y, categorical X, app=0" )




# iris data
Species <- iris$Species
levels(Species) <- c("set","ver","vir")
ncaty <- 3; ncatX <- rep(0,4)
resI1 <- DecTree( Species, iris[,1:4], eps=1e-5
                   , title="rect: categorical y, mixed X", plot=0  )


# iris: categorical y, mixed X
Species <- iris$Species
levels(Species) <- c("set","ver","vir")
Xc <- iris[,-5]
Xc[,3] <- cut(Xc[,3],9, labels=1:9)
Xc[,4] <- cut(Xc[,4],9, labels=1:9)
ncaty <- 3; ncatX <- c(0,0,8,8)
resI2 <- DecTree( Species, Xc, eps=1e-5, app=1
                  , title="iris: categorical y, mixed X", plot=0  )
resI3 <- DecTree( Species, Xc, eps=1e-5, app=0, method=1
                  , title="iris: categorical y, mixed X", plot=0  )
resI4 <- DecTree( Species, Xc, eps=1e-5, app=0, method=3
                  , title="iris: categorical y, mixed X", plot=0  )
resI5 <- DecTree( Species, Xc, eps=1e-5, app=0, method=4
                  , title="iris: categorical y, mixed X", plot=0  )





[Package lazy.tree version 0.1.5 ]