16
そそそそそそそそそそそそそそそ Imputation of Missing Values using Random ForestmissForest package そそそそそそ (DJ Stekhoven, P Bühlmann (2011), Bioinformatics 28 (1), 112-118) そ 53 そ R そそそ そそそ @( #TokyoR random Forest そそそそそそそそそrfImpute そそそそ missForest そそそ rfImpute そそそ missForest そそそそそそ

Imputation of Missing Values using Random Forest

Embed Size (px)

Citation preview

Page 1: Imputation of Missing Values using  Random Forest

それを捨てるなんてとんでもない“ Imputation of Missing Values using Random Forest”

missForest package を紹介します(DJ Stekhoven, P Bühlmann (2011), Bioinformatics 28 (1), 112-118)

第 53回 R勉強会@東京(#TokyoR)random Forest による欠損値の補完( rfImpute )の概要missForest による rfImpute の改良missForest を使ってみる

Page 2: Imputation of Missing Values using  Random Forest

欠測のある観測データ、どうしてますか?

https://www.iwanami.co.jp/cgi-bin/isearch?isbn=ISBN978-4-00-029847-6

目次1. はじめに:欠測の Do's and Don'ts とガイドライン2. 欠測データに対する最尤法3. EM アルゴリズム4. 単一代入と多重代入5. 回帰分析モデルにおける欠測データ解析6. 脱落を伴う経時測定データの解析7. 欠測データメカニズムの検討

Page 3: Imputation of Missing Values using  Random Forest

ランダムフォレスト

学習データのランダムサブセットで構築した様々な決定木の集合(=森)の予測結果 を統合する 分類 → 多数決 回帰 → 平均 特定の説明変数への依存が少ないため、クエリデータの説明変数が欠損していても良い出力を与える

ALL DATA

Random subset Random subset Random subset

Page 4: Imputation of Missing Values using  Random Forest

データ①,②データ③

弱学習器= (決定木 )

欠損値を初期値で埋める 連続変数 → 中央値 カテゴリカル変数 → 最頻値

それぞれの木でデータ同士の近似度計算 目的変数が同じ Leaf に落ちたら+1 そうでなければ+0森全体でデータ間の近似度を集計・正規化 近似度の和 ÷ 木の数欠損していない他のデータから補完連続変数 → 近似度による重み付け平均値カテゴリカル変数 → 近似度による重みづけ最頻値

データ①と②の近似度は+1データ①と③の近似度は+0

randomForest で学習

ノンパラメトリックな補完 連続・カテゴリカルな変数の混在を許す

random Forest による欠損値の補完{randomForest} パッケージには、 rfImpute 関数が用意されている

Page 5: Imputation of Missing Values using  Random Forest

目的変数が同じ Leaf に落ちるかどうかで重みを計算する↓目的変数も欠損しているデータには適用できない

既知のデータセットに含まれる欠損値の推定には役立つが、予測をしたいデータの前処理に用いたいときに困る

random Forest による欠損値の補完{randomForest} パッケージには、 rfImpute 関数が用意されているが…

Page 6: Imputation of Missing Values using  Random Forest

random Forest による欠損値の補完missForest の改良点

補完したい変数を目的変数に設定する

変数ごとに予測値で補完

目的変数の欠損の有無でデータ分割 補完したい変数が非欠損 → 教師データ 補完したい変数が欠損値 → テストデータ 

テストデータ=欠損値の予測教師データで randomForest の学習

補完開始

Page 7: Imputation of Missing Values using  Random Forest

missForest を使ってみるデータの準備データの準備 とりあえず動かす (検証)

エラー計測とチューニング#install.packages(missForest, dependencies = TRUE)require(dplyr)require(missForest)

data(diamonds, package = "ggplot2")dia.sample <- sample_n(diamonds, size=2000)dia.sample <- as.data.frame(dia.sample)summary(dia.sample)dia.sample %>% head

par(mfrow=c(3,4))for(i in c(1,5:10)){ hist(unlist(dia.sample[,i]) , main=colnames(dia.sample[,i]), xlab="")}plot.new()for(i in 2:4){ dia.sample[,i] %>% table %>% barplot}par(mfrow=c(1,1))

連続変数    × 7 カテゴリ変数  × 3

ggplot2 の diamond データを間引いて使う

Page 8: Imputation of Missing Values using  Random Forest

missForest を使ってみるデータの準備データの準備 とりあえず動かす (検証)

エラー計測とチューニング# create sample data with NA ---------------------------------------------dia.mis <- prodNA(dia.sample, noNA=0.05)summary(dia.mis)

5%欠損データを人工的につくる

Page 9: Imputation of Missing Values using  Random Forest

missForest を使ってみるとりあえず動かすデータの準備 とりあえず動かす (検証)

エラー計測とチューニングdia.imp <- missForest(dia.mis, verbose=TRUE)dia.imp %>% str(max.level=1)

dia.imp$ximp

返ってくる結果の構造に注意!補完後のデータは $ximp ※ よく間違える

Iteration の打ち切り条件 defference(s) が十分に小さくなったら収束

Page 10: Imputation of Missing Values using  Random Forest

missForest を使ってみるエラー計測: OOB error※を算出し、補完精度を推定するデータの準備 とりあえず動かす (検証)

エラー計測とチューニングdia.imp$OOBerrordia.imp <- missForest(dia.mis, verbose=TRUE, variablewise=TRUE)

※ out-of-bag (OOB) imputation error estimate

連続変数全体の評価= NRMSE ( normalized root mean squared error )カテゴリ変数全体の評価= PFC ( proportion of falsely classified )

TRUE とセットすると変数の数だけOOB error を返す(並びはカラム順)

Page 11: Imputation of Missing Values using  Random Forest

missForest を使ってみるチューニング: OOB error を見ながら変えてみる。基本は RF と同じ。データの準備 とりあえず動かす (検証)

エラー計測とチューニングdia.imp <- missForest(dia.mis, verbose=TRUE, maxiter=4)

補完の繰り返し回数※ 収束するまで繰り返しがデフォルト推奨dia.imp <- missForest(dia.mis, verbose=TRUE, mtree=4000)

決定木の数※ 多いほうがよさそうだが、  増やした分だけ計算量は線形に増加する

dia.imp <- missForest(dia.mis, verbose=TRUE, mtry=4)

決定木あたりに使う変数の数※ デフォルトは(変数の数) 1/2 だが、  推奨値の提示は難しい(データ次第)とのこと

Page 12: Imputation of Missing Values using  Random Forest

missForest を使ってみる変数ごとに補完プロセスを並列化データの準備

エラー計測とチューニング補完プロセスの並列化

require(doParallel)

cl <- makeCluster(NCOL(dia.mis))registerDoParallel(cl)

dia.imp <- missForest( xmis = dia.mis, variablewise = TRUE,   ntree = 1200, parallelize = "variables", verbose = TRUE)

stopCluster(cl)

(検証)

Page 13: Imputation of Missing Values using  Random Forest

missForest を使ってみるrandomForest の学習を並列化データの準備

エラー計測とチューニング補完プロセスの並列化

require(doParallel)

cl <- makeCluster(NCOL(dia.mis))registerDoParallel(cl)

dia.imp <- missForest( xmis = dia.mis, variablewise = TRUE,   ntree = 1200, parallelize = "forests", verbose = TRUE)

stopCluster(cl)

※ 注意  ソースコードを見ると指定できるコア数に制約あり   makeCluster で指定するコア数≦カラムの数  ( variablewise な並列化を推奨?)

(検証)

Page 14: Imputation of Missing Values using  Random Forest

missForest を使ってみる検証: 欠損のない完全データがあるときには補完精度の評価ができるデータの準備 補完プロセス (検証)

エラー計測とチューニングmixError(ximp = dia.imp$ximp, xmis = dia.mis, xtrue= dia.sample)

dia.imp <- missForest(dia.mis, verbose=TRUE, xtrue=dia.sample)

ximp : 補完後のデータxmis : 人工的に作った欠損データxtrue : 欠損のない完全データ

補完中の精度評価も可能

Page 15: Imputation of Missing Values using  Random Forest

missForest を使ってみる検証:  mixError() は OOB Error を基準に、別の補完法との比較もできる

require(mi)

dia.mdf <- missing_data.frame(dia.mis)summary(dia.mdf)

n.chains <- 20options(mc.cores=n.chains)

dia.MI <- mi(dia.mdf, n.chains = n.chains)str(dia.MI,max.level = 2)dia.dfs <- complete(dia.MI)str(dia.dfs)dia.imp_itr <- foreach(i=1:n.chains) %do% { this <- dia.dfs[[i]]%>%select(-contains("missing"))}

dia.err_mi <- foreach(i=1:n.chains, .combine=rbind) %do% { mixError(ximp = dia.imp_itr[[i]], xmis = dia.mis, xtrue= dia.sample) } dia.err_micolMeans(dia.err_mi)

データの準備 補完プロセス (検証)エラー計測とチューニング

Page 16: Imputation of Missing Values using  Random Forest

参考文献• randomForest {randomForest}

• Breiman, L. (2001). Random forests. Machine learning, 45(1), 5–32.

• cForest {party}• "Party on! A New, Conditional Variable Importance Measure for Random Forests Available in the party

Package", Strobl et al. 2009. • http://epub.ub.uni-muenchen.de/9387/1/techreport.pdf

• rfImputeによる欠損値の補完• “Random Forest を用いた欠測データの補完とその応用”

• http://www.rd.dnc.ac.jp/~tunenori/doc/jjasRf2010slide.pdf

• missForest • Package missForest

• https://cran.r-project.org/web/packages/missForest/• Vignette: “Using the missForest Package”

• https://stat.ethz.ch/education/semesters/ss2013/ams/paper/missForest_1.2.pdf

• “MissForest - nonparametric missing value imputation for mixed-type data”• http://bioinformatics.oxfordjournals.org/content/early/2011/10/28/bioinformatics.btr597.full.pdf