Test Hybrid-RFE

source('hybrid_RFE.R')
## Loading required package: randomForest
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
## Loading required package: e1071
## Loading required package: caret
## Loading required package: lattice
## Loading required package: ggplot2
##
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
##
##     margin
## Loading required package: gbm
## Loaded gbm 2.1.5

dataset : iris

ds = iris[,c(5,1,2,3,4)] head(ds)

ranking = hybridRFE.kfold(ds,k=5,halve.above=500,max.row=1000, method=‘s.sum’)

show.rank.dataset(ds, ranking)

show.rank.imp(ds, ranking)

ds = iris[,c(5,1,2,3,4)]
head(ds)
##   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1  setosa          5.1         3.5          1.4         0.2
## 2  setosa          4.9         3.0          1.4         0.2
## 3  setosa          4.7         3.2          1.3         0.2
## 4  setosa          4.6         3.1          1.5         0.2
## 5  setosa          5.0         3.6          1.4         0.2
## 6  setosa          5.4         3.9          1.7         0.4
ranking = hybridRFE.kfold(ds,k=5,halve.above=500,max.row=1000, method='s.sum') 
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=======================                                               |  33%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=======================                                               |  33%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=======================                                               |  33%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=======================                                               |  33%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=======================                                               |  33%
  |
  |======================================================================| 100%
show.rank.dataset(ds, ranking)
## ** feature ranking by dataset order
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width
##            3            4            2            1
show.rank.imp(ds, ranking) 
## ** feature ranking by importance order
## [1] "Petal.Width"  "Petal.Length" "Sepal.Length" "Sepal.Width"

dataset : Ionosphere (from mlbench)

library(mlbench)
data(Sonar)
ds = Sonar[,c(61,1:60)]
head(ds)
##   Class     V1     V2     V3     V4     V5     V6     V7     V8     V9    V10
## 1     R 0.0200 0.0371 0.0428 0.0207 0.0954 0.0986 0.1539 0.1601 0.3109 0.2111
## 2     R 0.0453 0.0523 0.0843 0.0689 0.1183 0.2583 0.2156 0.3481 0.3337 0.2872
## 3     R 0.0262 0.0582 0.1099 0.1083 0.0974 0.2280 0.2431 0.3771 0.5598 0.6194
## 4     R 0.0100 0.0171 0.0623 0.0205 0.0205 0.0368 0.1098 0.1276 0.0598 0.1264
## 5     R 0.0762 0.0666 0.0481 0.0394 0.0590 0.0649 0.1209 0.2467 0.3564 0.4459
## 6     R 0.0286 0.0453 0.0277 0.0174 0.0384 0.0990 0.1201 0.1833 0.2105 0.3039
##      V11    V12    V13    V14    V15    V16    V17    V18    V19    V20    V21
## 1 0.1609 0.1582 0.2238 0.0645 0.0660 0.2273 0.3100 0.2999 0.5078 0.4797 0.5783
## 2 0.4918 0.6552 0.6919 0.7797 0.7464 0.9444 1.0000 0.8874 0.8024 0.7818 0.5212
## 3 0.6333 0.7060 0.5544 0.5320 0.6479 0.6931 0.6759 0.7551 0.8929 0.8619 0.7974
## 4 0.0881 0.1992 0.0184 0.2261 0.1729 0.2131 0.0693 0.2281 0.4060 0.3973 0.2741
## 5 0.4152 0.3952 0.4256 0.4135 0.4528 0.5326 0.7306 0.6193 0.2032 0.4636 0.4148
## 6 0.2988 0.4250 0.6343 0.8198 1.0000 0.9988 0.9508 0.9025 0.7234 0.5122 0.2074
##      V22    V23    V24    V25    V26    V27    V28    V29    V30    V31    V32
## 1 0.5071 0.4328 0.5550 0.6711 0.6415 0.7104 0.8080 0.6791 0.3857 0.1307 0.2604
## 2 0.4052 0.3957 0.3914 0.3250 0.3200 0.3271 0.2767 0.4423 0.2028 0.3788 0.2947
## 3 0.6737 0.4293 0.3648 0.5331 0.2413 0.5070 0.8533 0.6036 0.8514 0.8512 0.5045
## 4 0.3690 0.5556 0.4846 0.3140 0.5334 0.5256 0.2520 0.2090 0.3559 0.6260 0.7340
## 5 0.4292 0.5730 0.5399 0.3161 0.2285 0.6995 1.0000 0.7262 0.4724 0.5103 0.5459
## 6 0.3985 0.5890 0.2872 0.2043 0.5782 0.5389 0.3750 0.3411 0.5067 0.5580 0.4778
##      V33    V34    V35    V36    V37    V38    V39    V40    V41    V42    V43
## 1 0.5121 0.7547 0.8537 0.8507 0.6692 0.6097 0.4943 0.2744 0.0510 0.2834 0.2825
## 2 0.1984 0.2341 0.1306 0.4182 0.3835 0.1057 0.1840 0.1970 0.1674 0.0583 0.1401
## 3 0.1862 0.2709 0.4232 0.3043 0.6116 0.6756 0.5375 0.4719 0.4647 0.2587 0.2129
## 4 0.6120 0.3497 0.3953 0.3012 0.5408 0.8814 0.9857 0.9167 0.6121 0.5006 0.3210
## 5 0.2881 0.0981 0.1951 0.4181 0.4604 0.3217 0.2828 0.2430 0.1979 0.2444 0.1847
## 6 0.3299 0.2198 0.1407 0.2856 0.3807 0.4158 0.4054 0.3296 0.2707 0.2650 0.0723
##      V44    V45    V46    V47    V48    V49    V50    V51    V52    V53    V54
## 1 0.4256 0.2641 0.1386 0.1051 0.1343 0.0383 0.0324 0.0232 0.0027 0.0065 0.0159
## 2 0.1628 0.0621 0.0203 0.0530 0.0742 0.0409 0.0061 0.0125 0.0084 0.0089 0.0048
## 3 0.2222 0.2111 0.0176 0.1348 0.0744 0.0130 0.0106 0.0033 0.0232 0.0166 0.0095
## 4 0.3202 0.4295 0.3654 0.2655 0.1576 0.0681 0.0294 0.0241 0.0121 0.0036 0.0150
## 5 0.0841 0.0692 0.0528 0.0357 0.0085 0.0230 0.0046 0.0156 0.0031 0.0054 0.0105
## 6 0.1238 0.1192 0.1089 0.0623 0.0494 0.0264 0.0081 0.0104 0.0045 0.0014 0.0038
##      V55    V56    V57    V58    V59    V60
## 1 0.0072 0.0167 0.0180 0.0084 0.0090 0.0032
## 2 0.0094 0.0191 0.0140 0.0049 0.0052 0.0044
## 3 0.0180 0.0244 0.0316 0.0164 0.0095 0.0078
## 4 0.0085 0.0073 0.0050 0.0044 0.0040 0.0117
## 5 0.0110 0.0015 0.0072 0.0048 0.0107 0.0094
## 6 0.0013 0.0089 0.0057 0.0027 0.0051 0.0062
ranking = hybridRFE.kfold(ds,k=5,halve.above=500,max.row=1000, method='s.sum') 
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=                                                                     |   2%
  |
  |==                                                                    |   3%
  |
  |====                                                                  |   5%
  |
  |=====                                                                 |   7%
  |
  |======                                                                |   8%
  |
  |=======                                                               |  10%
  |
  |========                                                              |  12%
  |
  |=========                                                             |  14%
  |
  |===========                                                           |  15%
  |
  |============                                                          |  17%
  |
  |=============                                                         |  19%
  |
  |==============                                                        |  20%
  |
  |===============                                                       |  22%
  |
  |=================                                                     |  24%
  |
  |==================                                                    |  25%
  |
  |===================                                                   |  27%
  |
  |====================                                                  |  29%
  |
  |=====================                                                 |  31%
  |
  |=======================                                               |  32%
  |
  |========================                                              |  34%
  |
  |=========================                                             |  36%
  |
  |==========================                                            |  37%
  |
  |===========================                                           |  39%
  |
  |============================                                          |  41%
  |
  |==============================                                        |  42%
  |
  |===============================                                       |  44%
  |
  |================================                                      |  46%
  |
  |=================================                                     |  47%
  |
  |==================================                                    |  49%
  |
  |====================================                                  |  51%
  |
  |=====================================                                 |  53%
  |
  |======================================                                |  54%
  |
  |=======================================                               |  56%
  |
  |========================================                              |  58%
  |
  |==========================================                            |  59%
  |
  |===========================================                           |  61%
  |
  |============================================                          |  63%
  |
  |=============================================                         |  64%
  |
  |==============================================                        |  66%
  |
  |===============================================                       |  68%
  |
  |=================================================                     |  69%
  |
  |==================================================                    |  71%
  |
  |===================================================                   |  73%
  |
  |====================================================                  |  75%
  |
  |=====================================================                 |  76%
  |
  |=======================================================               |  78%
  |
  |========================================================              |  80%
  |
  |=========================================================             |  81%
  |
  |==========================================================            |  83%
  |
  |===========================================================           |  85%
  |
  |=============================================================         |  86%
  |
  |==============================================================        |  88%
  |
  |===============================================================       |  90%
  |
  |================================================================      |  92%
  |
  |=================================================================     |  93%
  |
  |==================================================================    |  95%
  |
  |====================================================================  |  97%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=                                                                     |   2%
  |
  |==                                                                    |   3%
  |
  |====                                                                  |   5%
  |
  |=====                                                                 |   7%
  |
  |======                                                                |   8%
  |
  |=======                                                               |  10%
  |
  |========                                                              |  12%
  |
  |=========                                                             |  14%
  |
  |===========                                                           |  15%
  |
  |============                                                          |  17%
  |
  |=============                                                         |  19%
  |
  |==============                                                        |  20%
  |
  |===============                                                       |  22%
  |
  |=================                                                     |  24%
  |
  |==================                                                    |  25%
  |
  |===================                                                   |  27%
  |
  |====================                                                  |  29%
  |
  |=====================                                                 |  31%
  |
  |=======================                                               |  32%
  |
  |========================                                              |  34%
  |
  |=========================                                             |  36%
  |
  |==========================                                            |  37%
  |
  |===========================                                           |  39%
  |
  |============================                                          |  41%
  |
  |==============================                                        |  42%
  |
  |===============================                                       |  44%
  |
  |================================                                      |  46%
  |
  |=================================                                     |  47%
  |
  |==================================                                    |  49%
  |
  |====================================                                  |  51%
  |
  |=====================================                                 |  53%
  |
  |======================================                                |  54%
  |
  |=======================================                               |  56%
  |
  |========================================                              |  58%
  |
  |==========================================                            |  59%
  |
  |===========================================                           |  61%
  |
  |============================================                          |  63%
  |
  |=============================================                         |  64%
  |
  |==============================================                        |  66%
  |
  |===============================================                       |  68%
  |
  |=================================================                     |  69%
  |
  |==================================================                    |  71%
  |
  |===================================================                   |  73%
  |
  |====================================================                  |  75%
  |
  |=====================================================                 |  76%
  |
  |=======================================================               |  78%
  |
  |========================================================              |  80%
  |
  |=========================================================             |  81%
  |
  |==========================================================            |  83%
  |
  |===========================================================           |  85%
  |
  |=============================================================         |  86%
  |
  |==============================================================        |  88%
  |
  |===============================================================       |  90%
  |
  |================================================================      |  92%
  |
  |=================================================================     |  93%
  |
  |==================================================================    |  95%
  |
  |====================================================================  |  97%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=                                                                     |   2%
  |
  |==                                                                    |   3%
  |
  |====                                                                  |   5%
  |
  |=====                                                                 |   7%
  |
  |======                                                                |   8%
  |
  |=======                                                               |  10%
  |
  |========                                                              |  12%
  |
  |=========                                                             |  14%
  |
  |===========                                                           |  15%
  |
  |============                                                          |  17%
  |
  |=============                                                         |  19%
  |
  |==============                                                        |  20%
  |
  |===============                                                       |  22%
  |
  |=================                                                     |  24%
  |
  |==================                                                    |  25%
  |
  |===================                                                   |  27%
  |
  |====================                                                  |  29%
  |
  |=====================                                                 |  31%
  |
  |=======================                                               |  32%
  |
  |========================                                              |  34%
  |
  |=========================                                             |  36%
  |
  |==========================                                            |  37%
  |
  |===========================                                           |  39%
  |
  |============================                                          |  41%
  |
  |==============================                                        |  42%
  |
  |===============================                                       |  44%
  |
  |================================                                      |  46%
  |
  |=================================                                     |  47%
  |
  |==================================                                    |  49%
  |
  |====================================                                  |  51%
  |
  |=====================================                                 |  53%
  |
  |======================================                                |  54%
  |
  |=======================================                               |  56%
  |
  |========================================                              |  58%
  |
  |==========================================                            |  59%
  |
  |===========================================                           |  61%
  |
  |============================================                          |  63%
  |
  |=============================================                         |  64%
  |
  |==============================================                        |  66%
  |
  |===============================================                       |  68%
  |
  |=================================================                     |  69%
  |
  |==================================================                    |  71%
  |
  |===================================================                   |  73%
  |
  |====================================================                  |  75%
  |
  |=====================================================                 |  76%
  |
  |=======================================================               |  78%
  |
  |========================================================              |  80%
  |
  |=========================================================             |  81%
  |
  |==========================================================            |  83%
  |
  |===========================================================           |  85%
  |
  |=============================================================         |  86%
  |
  |==============================================================        |  88%
  |
  |===============================================================       |  90%
  |
  |================================================================      |  92%
  |
  |=================================================================     |  93%
  |
  |==================================================================    |  95%
  |
  |====================================================================  |  97%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=                                                                     |   2%
  |
  |==                                                                    |   3%
  |
  |====                                                                  |   5%
  |
  |=====                                                                 |   7%
  |
  |======                                                                |   8%
  |
  |=======                                                               |  10%
  |
  |========                                                              |  12%
  |
  |=========                                                             |  14%
  |
  |===========                                                           |  15%
  |
  |============                                                          |  17%
  |
  |=============                                                         |  19%
  |
  |==============                                                        |  20%
  |
  |===============                                                       |  22%
  |
  |=================                                                     |  24%
  |
  |==================                                                    |  25%
  |
  |===================                                                   |  27%
  |
  |====================                                                  |  29%
  |
  |=====================                                                 |  31%
  |
  |=======================                                               |  32%
  |
  |========================                                              |  34%
  |
  |=========================                                             |  36%
  |
  |==========================                                            |  37%
  |
  |===========================                                           |  39%
  |
  |============================                                          |  41%
  |
  |==============================                                        |  42%
  |
  |===============================                                       |  44%
  |
  |================================                                      |  46%
  |
  |=================================                                     |  47%
  |
  |==================================                                    |  49%
  |
  |====================================                                  |  51%
  |
  |=====================================                                 |  53%
  |
  |======================================                                |  54%
  |
  |=======================================                               |  56%
  |
  |========================================                              |  58%
  |
  |==========================================                            |  59%
  |
  |===========================================                           |  61%
  |
  |============================================                          |  63%
  |
  |=============================================                         |  64%
  |
  |==============================================                        |  66%
  |
  |===============================================                       |  68%
  |
  |=================================================                     |  69%
  |
  |==================================================                    |  71%
  |
  |===================================================                   |  73%
  |
  |====================================================                  |  75%
  |
  |=====================================================                 |  76%
  |
  |=======================================================               |  78%
  |
  |========================================================              |  80%
  |
  |=========================================================             |  81%
  |
  |==========================================================            |  83%
  |
  |===========================================================           |  85%
  |
  |=============================================================         |  86%
  |
  |==============================================================        |  88%
  |
  |===============================================================       |  90%
  |
  |================================================================      |  92%
  |
  |=================================================================     |  93%
  |
  |==================================================================    |  95%
  |
  |====================================================================  |  97%
  |
  |======================================================================| 100%
## Scaling data...Done!
##
  |
  |                                                                      |   0%
  |
  |=                                                                     |   2%
  |
  |==                                                                    |   3%
  |
  |====                                                                  |   5%
  |
  |=====                                                                 |   7%
  |
  |======                                                                |   8%
  |
  |=======                                                               |  10%
  |
  |========                                                              |  12%
  |
  |=========                                                             |  14%
  |
  |===========                                                           |  15%
  |
  |============                                                          |  17%
  |
  |=============                                                         |  19%
  |
  |==============                                                        |  20%
  |
  |===============                                                       |  22%
  |
  |=================                                                     |  24%
  |
  |==================                                                    |  25%
  |
  |===================                                                   |  27%
  |
  |====================                                                  |  29%
  |
  |=====================                                                 |  31%
  |
  |=======================                                               |  32%
  |
  |========================                                              |  34%
  |
  |=========================                                             |  36%
  |
  |==========================                                            |  37%
  |
  |===========================                                           |  39%
  |
  |============================                                          |  41%
  |
  |==============================                                        |  42%
  |
  |===============================                                       |  44%
  |
  |================================                                      |  46%
  |
  |=================================                                     |  47%
  |
  |==================================                                    |  49%
  |
  |====================================                                  |  51%
  |
  |=====================================                                 |  53%
  |
  |======================================                                |  54%
  |
  |=======================================                               |  56%
  |
  |========================================                              |  58%
  |
  |==========================================                            |  59%
  |
  |===========================================                           |  61%
  |
  |============================================                          |  63%
  |
  |=============================================                         |  64%
  |
  |==============================================                        |  66%
  |
  |===============================================                       |  68%
  |
  |=================================================                     |  69%
  |
  |==================================================                    |  71%
  |
  |===================================================                   |  73%
  |
  |====================================================                  |  75%
  |
  |=====================================================                 |  76%
  |
  |=======================================================               |  78%
  |
  |========================================================              |  80%
  |
  |=========================================================             |  81%
  |
  |==========================================================            |  83%
  |
  |===========================================================           |  85%
  |
  |=============================================================         |  86%
  |
  |==============================================================        |  88%
  |
  |===============================================================       |  90%
  |
  |================================================================      |  92%
  |
  |=================================================================     |  93%
  |
  |==================================================================    |  95%
  |
  |====================================================================  |  97%
  |
  |======================================================================| 100%
show.rank.dataset(ds, ranking)
## ** feature ranking by dataset order
##  V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
##  11  12  45  48   9  49  36   4  10  16  31  37  51  52  21  23  28  27  46  17
## V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36 V37 V38 V39 V40
##  47   5  20  44   8   7  15  43  30  40  13  39  19   1  32  24  26  22  18   6
## V41 V42 V43 V44 V45 V46 V47 V48 V49 V50 V51 V52 V53 V54 V55 V56 V57 V58 V59 V60
##  35  55   2  29  34  25  42  14  38  41  33  54  59   3  50  58  57  60  53  56
show.rank.imp(ds, ranking) 
## ** feature ranking by importance order
##  [1] "V34" "V43" "V54" "V8"  "V22" "V40" "V26" "V25" "V5"  "V9"  "V1"  "V2"
## [13] "V31" "V48" "V27" "V10" "V20" "V39" "V33" "V23" "V15" "V38" "V16" "V36"
## [25] "V46" "V37" "V18" "V17" "V44" "V29" "V11" "V35" "V51" "V45" "V41" "V7"
## [37] "V12" "V49" "V32" "V30" "V50" "V47" "V28" "V24" "V3"  "V19" "V21" "V4"
## [49] "V6"  "V55" "V13" "V14" "V59" "V52" "V42" "V60" "V57" "V56" "V53" "V58"

SVM Test & graph

library(e1071)
features <- c(5,10,15,20,25,30,35,40,45,50,55,60)

X <- ds[,-1]
Y <- ds[,1]

acc = c()
i <- 1
for (no in features) {
  model <- svm(X[,1:no],Y)
  pred <- predict(model,X[,1:no])
  acc[i] <- mean(Y==pred)
  i=i+1
}
plot(features, acc, main='Training Accuracy', xlab='# of features', ylab='accuracy',
     type='b', axes=T)