R 패키지 설치하고 불러오기

if (! ("caret" %in% rownames(installed.packages()))) 
  { install.packages("caret") }
base::require("caret")
if (! ("rpart.plot" %in% rownames(installed.packages()))) 
  { install.packages("rpart.plot") }
base::require("rpart.plot")
if (! ("adabag" %in% rownames(installed.packages()))) 
  { install.packages("adabag") }
base::require("adabag")
if (! ("randomForest" %in% rownames(installed.packages()))) 
  { install.packages("randomForest") }
base::require("randomForest")

iris data 살펴보기

data("iris")
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
table(iris$Species)
## 
##     setosa versicolor  virginica 
##         50         50         50
ggplot(data=iris,aes(x=Petal.Width,y=Sepal.Width,col=Species)) + geom_point()

데이터 분할하기

70% 데이터 랜덤추출

set.seed(123)
rdata <- createDataPartition(y=iris$Species,p=0.7,list=F)
training <- iris[rdata,]
testing <- iris[-rdata,]

iris data 사용한 부스팅 모형

전체 데이터 사용

boosting.adabag <- boosting(Species~.,data=iris, boos = T,mfinal=10)
boosting.adabag$importance
## Petal.Length  Petal.Width Sepal.Length  Sepal.Width 
##    65.782868    18.874461     6.518862     8.823809
plot(boosting.adabag$trees[[10]])
text(boosting.adabag$trees[[10]])

pred <- predict(boosting.adabag,newdata=iris)
table(pred$class,iris[,5])
##             
##              setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         50         0
##   virginica       0          0        50

iris data 부스팅 모형

training 데이터 사용

boos는 가중치사용, mfinal는 부스팅 반복횟수

boosting.adabag <- boosting(Species~.,data=training, boos = T,mfinal=10)
boosting.adabag$importance
## Petal.Length  Petal.Width Sepal.Length  Sepal.Width 
##    69.385293    21.975015     2.121939     6.517752
plot(boosting.adabag$trees[[10]])
text(boosting.adabag$trees[[10]])

pred <- predict(boosting.adabag,newdata=testing)
table(pred$class,testing[,5])
##             
##              setosa versicolor virginica
##   setosa         15          0         0
##   versicolor      0         14         2
##   virginica       0          1        13
confusionMatrix(as.factor(pred$class),testing[,5])
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   setosa versicolor virginica
##   setosa         15          0         0
##   versicolor      0         14         2
##   virginica       0          1        13
## 
## Overall Statistics
##                                          
##                Accuracy : 0.9333         
##                  95% CI : (0.8173, 0.986)
##     No Information Rate : 0.3333         
##     P-Value [Acc > NIR] : < 2.2e-16      
##                                          
##                   Kappa : 0.9            
##                                          
##  Mcnemar's Test P-Value : NA             
## 
## Statistics by Class:
## 
##                      Class: setosa Class: versicolor Class: virginica
## Sensitivity                 1.0000            0.9333           0.8667
## Specificity                 1.0000            0.9333           0.9667
## Pos Pred Value              1.0000            0.8750           0.9286
## Neg Pred Value              1.0000            0.9655           0.9355
## Prevalence                  0.3333            0.3333           0.3333
## Detection Rate              0.3333            0.3111           0.2889
## Detection Prevalence        0.3333            0.3556           0.3111
## Balanced Accuracy           1.0000            0.9333           0.9167

iris data CART 모형

5-fold cross validation

rdata <- trainControl(method="cv",number = 5)
bagging.rf.kfold <- train(Species~.,data=iris,medhod="treebag",trControl=rdata)
bagging.rf.kfold
## Random Forest 
## 
## 150 samples
##   4 predictor
##   3 classes: 'setosa', 'versicolor', 'virginica' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 120, 120, 120, 120, 120 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy  Kappa
##   2     0.96      0.94 
##   3     0.96      0.94 
##   4     0.96      0.94 
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
pred <- predict(bagging.rf.kfold,newdata=testing)
table(testing$Species,pred)
##             pred
##              setosa versicolor virginica
##   setosa         15          0         0
##   versicolor      0         15         0
##   virginica       0          0        15
confusionMatrix(data=pred,reference=testing$Species)
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   setosa versicolor virginica
##   setosa         15          0         0
##   versicolor      0         15         0
##   virginica       0          0        15
## 
## Overall Statistics
##                                      
##                Accuracy : 1          
##                  95% CI : (0.9213, 1)
##     No Information Rate : 0.3333     
##     P-Value [Acc > NIR] : < 2.2e-16  
##                                      
##                   Kappa : 1          
##                                      
##  Mcnemar's Test P-Value : NA         
## 
## Statistics by Class:
## 
##                      Class: setosa Class: versicolor Class: virginica
## Sensitivity                 1.0000            1.0000           1.0000
## Specificity                 1.0000            1.0000           1.0000
## Pos Pred Value              1.0000            1.0000           1.0000
## Neg Pred Value              1.0000            1.0000           1.0000
## Prevalence                  0.3333            0.3333           0.3333
## Detection Rate              0.3333            0.3333           0.3333
## Detection Prevalence        0.3333            0.3333           0.3333
## Balanced Accuracy           1.0000            1.0000           1.0000

iris data randomforest 모형

mtry는 tree에서 사용할 변수 갯수

ntree는 tree 총 갯수

rdata <- sample(1:150,120)
training <- iris[rdata,]
testing <- iris[-rdata,]
bagging.rf <- randomForest(Species~.,data=training,mtry=2,ntree=15)
bagging.rf
## 
## Call:
##  randomForest(formula = Species ~ ., data = training, mtry = 2,      ntree = 15) 
##                Type of random forest: classification
##                      Number of trees: 15
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 6.67%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         41          0         0  0.00000000
## versicolor      0         31         3  0.08823529
## virginica       0          5        40  0.11111111
pred <- predict(bagging.rf,newdata=testing)
table(testing$Species,pred)
##             pred
##              setosa versicolor virginica
##   setosa          9          0         0
##   versicolor      0         15         1
##   virginica       0          0         5
confusionMatrix(data=pred,reference=testing$Species)
## Confusion Matrix and Statistics
## 
##             Reference
## Prediction   setosa versicolor virginica
##   setosa          9          0         0
##   versicolor      0         15         0
##   virginica       0          1         5
## 
## Overall Statistics
##                                           
##                Accuracy : 0.9667          
##                  95% CI : (0.8278, 0.9992)
##     No Information Rate : 0.5333          
##     P-Value [Acc > NIR] : 1.759e-07       
##                                           
##                   Kappa : 0.9454          
##                                           
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: setosa Class: versicolor Class: virginica
## Sensitivity                    1.0            0.9375           1.0000
## Specificity                    1.0            1.0000           0.9600
## Pos Pred Value                 1.0            1.0000           0.8333
## Neg Pred Value                 1.0            0.9333           1.0000
## Prevalence                     0.3            0.5333           0.1667
## Detection Rate                 0.3            0.5000           0.1667
## Detection Prevalence           0.3            0.5000           0.2000
## Balanced Accuracy              1.0            0.9688           0.9800