最近いろいろとデータベースをいじることが多くなったのを機に、少しデータマイニングについて調べています。そこでRandomForestというものを初めて聞き、興味を持ったので忘れないように書こうと思います。
install.packages("randomForest")でインストールして、library(randomForest)で読み込むだけです。
Usageは
randomForest(formula, data=NULL, ..., subset, na.action=na.fail)
ネット上ではirisやtitanicを使った例がたくさんあったので非常にわかりやすく勉強になりました。ただregressionを使った例があまりなかったので、その辺を今後もうちょい調べて行こうかと思います。
ひとつ気になったことがあったので忘れないようにメモしておきます。
試しにclassification modelをやってみたところこんな結果に。
Call:
randomForest(x = IV, y = DV, ntree = 10000, mtry = 7, do.trace = 100)
Type of random forest: classification
Number of trees: 10000
No. of variables tried at each split: 7
OOB estimate of error rate: 16.24%
Confusion matrix:
No Risk Risk class.error
No Risk 91 5 0.05208333
Risk 14 7 0.66666667
ちなみにtuneRFを使ったら7がいいということでmtryは7にしてありますが、よく理解していません。ntreeもdefaultでは500らしいですが、試しに10000にしてみただけです。
まぁデータ数も少ないし、OBBも16%でこんなものかと思っていたのですが、Confusion MatrixのRisk のエラーが66%と高すぎ!!と思い調べてみました。stat.stack.exchangeというところでほぼ同じ質問が出ていたので参考にさせていただきます。
コメントで、Your set is sharply unbalanced -- RF usually fails in this scenario (i.e. predicts well only the bigger class). You should try balancing your set either by sampling the "0" class only to have about the same size as "1" class or by playing with classwt parameter.っていうのと、Based on your confusion matrix, you've got 5,908 data points and the vast, vast majority of them are of type 0 ('employee stayed'). The classifier can therefore get away with being "lazy" and picking the majority class unless it's absolutely certain that an example belongs to the other class. Note that your overall error rate is ~7%, which is quite close to the percent of Class1 examples!って言うのがありました
要はrandomForestは数が大きいグループをもとに分類してしまうようです。分類しやすい方向へ流れて行ってしまうということでしょうか。lazyとはわかりやすい表現です。。。
ということで選択肢としては数が多い方のサンプル数(自分のデータではNo Risk)を減らす、などなどらしいです。
randomForestの他にも関連してrpartやctreeなどのパッケージを使ったDecision Treeなどもあるので、まだまだ調べることはてんこ盛りのようです。(むしろrpartやctreeを先に学ぶべきなんでしょうが。。。)数学なんて高校以来まともにやっていないので数式を理解するのに異常なほど時間がかかりますが、基礎を理解せずに使うと後々苦労しそうなので、がんばろうかと思っている今日この頃です。
install.packages("randomForest")でインストールして、library(randomForest)で読み込むだけです。
Usageは
randomForest(formula, data=NULL, ..., subset, na.action=na.fail)
ネット上ではirisやtitanicを使った例がたくさんあったので非常にわかりやすく勉強になりました。ただregressionを使った例があまりなかったので、その辺を今後もうちょい調べて行こうかと思います。
ひとつ気になったことがあったので忘れないようにメモしておきます。
試しにclassification modelをやってみたところこんな結果に。
Call:
randomForest(x = IV, y = DV, ntree = 10000, mtry = 7, do.trace = 100)
Type of random forest: classification
Number of trees: 10000
No. of variables tried at each split: 7
OOB estimate of error rate: 16.24%
Confusion matrix:
No Risk Risk class.error
No Risk 91 5 0.05208333
Risk 14 7 0.66666667
ちなみにtuneRFを使ったら7がいいということでmtryは7にしてありますが、よく理解していません。ntreeもdefaultでは500らしいですが、試しに10000にしてみただけです。
まぁデータ数も少ないし、OBBも16%でこんなものかと思っていたのですが、Confusion MatrixのRisk のエラーが66%と高すぎ!!と思い調べてみました。stat.stack.exchangeというところでほぼ同じ質問が出ていたので参考にさせていただきます。
コメントで、Your set is sharply unbalanced -- RF usually fails in this scenario (i.e. predicts well only the bigger class). You should try balancing your set either by sampling the "0" class only to have about the same size as "1" class or by playing with classwt parameter.っていうのと、Based on your confusion matrix, you've got 5,908 data points and the vast, vast majority of them are of type 0 ('employee stayed'). The classifier can therefore get away with being "lazy" and picking the majority class unless it's absolutely certain that an example belongs to the other class. Note that your overall error rate is ~7%, which is quite close to the percent of Class1 examples!って言うのがありました
要はrandomForestは数が大きいグループをもとに分類してしまうようです。分類しやすい方向へ流れて行ってしまうということでしょうか。lazyとはわかりやすい表現です。。。
ということで選択肢としては数が多い方のサンプル数(自分のデータではNo Risk)を減らす、などなどらしいです。
randomForestの他にも関連してrpartやctreeなどのパッケージを使ったDecision Treeなどもあるので、まだまだ調べることはてんこ盛りのようです。(むしろrpartやctreeを先に学ぶべきなんでしょうが。。。)数学なんて高校以来まともにやっていないので数式を理解するのに異常なほど時間がかかりますが、基礎を理解せずに使うと後々苦労しそうなので、がんばろうかと思っている今日この頃です。