title: Can I use random forest to do feature selection caret
body: <p>I am using the caret R package do model training, I am totally new to machine learning.</p>
  <p>I don't know if I can use the idea shows below to do feature selection and train a model</p>
  <p>My code is shown as below:</p>
  <p>The idea is that first I will run random forest in train function, then I will select the top20 important features(based on varImp function),and re-train my data based on these top20 features.</p>
  <p>I am not sure if this method works?
  1.first I will train a random forest model with all features</p>
  <pre><code>ctrl &lt;- trainControl(method=&quot;cv&quot;, 
                       number = 5,
                       summaryFunction=twoClassSummary, 
                       classProbs=T,
                       savePredictions = T)
  
  ##################################### train model 
  set.seed(1234)
  model &lt;- 
    train(Bgroup ~ ., 
          data=all_data, 
          method=&quot;rf&quot;, preProc=c(&quot;center&quot;, &quot;scale&quot;,&quot;nzv&quot;), 
          trControl=ctrl,
          tuneLength = 10,
          metric = &quot;ROC&quot;
    )
  
  #####################################  based on this model, I can get a plot of feature importance 
  
  feature_importance &lt;- varImp(model)
  
  
  ##################################### I only selected top20 important features 
  importance_df &lt;- data.frame(feature_importance$importance,feature = rownames(feature_importance$importance))
  top20 &lt;- head(importance_df[order(importance_df[,1],decreasing = T),],n=20) %&gt;%
    .$feature %&gt;%
    gsub(&quot;`&quot;, '', .)  
  
  
  ##################################### top20 data selection
  data_top20 &lt;- all_data[,top20] 
  data_top20$group &lt;- all_data$Bgroup
  
  ##################################### re-train model again based on these 20 features 
  set.seed(1234)
  model_top20 &lt;- train(group ~ ., 
                                    data=data_top20, 
                                    method=&quot;rf&quot;, preProc=c(&quot;center&quot;, &quot;scale&quot;,&quot;nzv&quot;), 
                                    trControl=ctrl,
                                    tuneLength = 10,
                                    metric = &quot;ROC&quot;
  )
  
  ### calculate performance 
  a &lt;- filter(data_top20$pred, mtry ==4)
  confusionMatrix(a$pred,a$obs,positive = &quot;positive&quot;)
  </code></pre>
  <p>Is the idea okay? I am very appreciated if anyone can give me some suggestions</p>