caret:trainに慣れる
caret:trainに慣れる
kanosuke
2015年10月19日
パッケージ caret
色々なアルゴリズムを個別のパッケージで対応してきた。
でも、それぞれの使い方を調べながら対応するのが面倒。
caretは多くのアルゴリズムを一つのパッケージにまとめてくれている。
また、モデル構築で必要なツールを提供してくれている。
caret:train
caret:trainでモデルを構築する。
オプション:method
method = でアルゴリズムを指定。多くのアルゴリズムを指定できる。
今回は“lm”, “rf”, “gbm”を使用する。
シンプルなモデル構築
library(caret)
library(dplyr)
iris <- tbl_df(iris)
model.lm <- train(
data = iris,
Sepal.Length ~ .,
method = "lm"
)
model.rf <- train(
data = iris,
Sepal.Length ~ .,
method = "rf"
)
model.gbm <- train(
data = iris,
Sepal.Length ~ .,
method = "gbm"
)
model.lm
## Linear Regression
##
## 150 samples
## 4 predictor
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results
##
## RMSE Rsquared RMSE SD Rsquared SD
## 0.3248528 0.8457439 0.01828276 0.02130072
##
##
summary(model.lm)
##
## Call:
## lm(formula = .outcome ~ ., data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.79424 -0.21874 0.00899 0.20255 0.73103
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.17127 0.27979 7.760 1.43e-12 ***
## Sepal.Width 0.49589 0.08607 5.761 4.87e-08 ***
## Petal.Length 0.82924 0.06853 12.101 < 2e-16 ***
## Petal.Width -0.31516 0.15120 -2.084 0.03889 *
## Speciesversicolor -0.72356 0.24017 -3.013 0.00306 **
## Speciesvirginica -1.02350 0.33373 -3.067 0.00258 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3068 on 144 degrees of freedom
## Multiple R-squared: 0.8673, Adjusted R-squared: 0.8627
## F-statistic: 188.3 on 5 and 144 DF, p-value: < 2.2e-16
model.rf
## Random Forest
##
## 150 samples
## 4 predictor
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results across tuning parameters:
##
## mtry RMSE Rsquared RMSE SD Rsquared SD
## 2 0.3749241 0.8155266 0.03152655 0.03701402
## 3 0.3629939 0.8235616 0.02747483 0.03561889
## 5 0.3627847 0.8207195 0.02842011 0.03995335
##
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 5.
summary(model.rf)
## Length Class Mode
## call 4 -none- call
## type 1 -none- character
## predicted 150 -none- numeric
## mse 500 -none- numeric
## rsq 500 -none- numeric
## oob.times 150 -none- numeric
## importance 5 -none- numeric
## importanceSD 0 -none- NULL
## localImportance 0 -none- NULL
## proximity 0 -none- NULL
## ntree 1 -none- numeric
## mtry 1 -none- numeric
## forest 11 -none- list
## coefs 0 -none- NULL
## y 150 -none- numeric
## test 0 -none- NULL
## inbag 0 -none- NULL
## xNames 5 -none- character
## problemType 1 -none- character
## tuneValue 1 data.frame list
## obsLevels 1 -none- logical
model.gbm
## Stochastic Gradient Boosting
##
## 150 samples
## 4 predictor
##
## No pre-processing
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results across tuning parameters:
##
## interaction.depth n.trees RMSE Rsquared RMSE SD
## 1 50 0.3913974 0.7822964 0.03051559
## 1 100 0.3737524 0.7994068 0.02906737
## 1 150 0.3706075 0.8027339 0.02712242
## 2 50 0.3741219 0.7996173 0.02745996
## 2 100 0.3688091 0.8048558 0.02672018
## 2 150 0.3675264 0.8055333 0.02610464
## 3 50 0.3712947 0.8009253 0.02792836
## 3 100 0.3684993 0.8043281 0.02672187
## 3 150 0.3710270 0.8023887 0.02685090
## Rsquared SD
## 0.03417764
## 0.03301104
## 0.03097777
## 0.02871627
## 0.02685198
## 0.02633916
## 0.02546331
## 0.02405717
## 0.02456531
##
## Tuning parameter 'shrinkage' was held constant at a value of 0.1
##
## Tuning parameter 'n.minobsinnode' was held constant at a value of 10
## RMSE was used to select the optimal model using the smallest value.
## The final values used for the model were n.trees = 150,
## interaction.depth = 2, shrinkage = 0.1 and n.minobsinnode = 10.
summary(model.gbm)
## var rel.inf
## Petal.Length Petal.Length 79.9216869
## Sepal.Width Sepal.Width 9.0176394
## Petal.Width Petal.Width 5.4888793
## Speciesversicolor Speciesversicolor 5.3919231
## Speciesvirginica Speciesvirginica 0.1798713
rfとgbmはパラメータチューニングのため複数モデルを構築し性能が良いモデルのパラメータを採用している。
rfはmtryを、gbmはinteraction.depthとn.treesをチューニングしている。
モデルの評価はRMSEを採用。
rfはmtry=5のモデル、gbmはinteraction.depth = 2, n.trees = 150 のモデルを採用。
最終結果として
- lm : RMSE = 0.324
- rf : RMSE = 0.362
- gbm : RMSE = 0.367
lmのモデルが最も性能が良い。
後でパラーメータチューニング、リサンプリングの指定方法を整理したいが今日はここまで。