3

I am using the caret package to run a gbm model. Once the model is run, I use the varImp function to extract the list of important predictors (displays top 20). However I would like to capture the names of the predictors in a character list. How do I do that? The object returned from the varImp does not seem to have the attribute that lists the predictor name - only the variable importance. Here is a sample :

gbmModel= train(target ~. , data = trainData, ....other params )
varimp = varImp(gbmModel, scale=TRUE)
str(varimp)
List of 3
 $ importance:'data.frame': 77 obs. of  1 variable:
..$ Overall: num [1:77] 6.63 0 5.35 2.01 0 ...
$ model     : chr "gbm"
$ calledFrom: chr "varImp"
- attr(*, "class")= chr "varImp.train"
## Display the important variables

varimp

gbm variable importance

 only 20 most important variables shown (out of 77)

           Overall
Var126     100.000
Var189      99.647
Var113      41.994
... And so on

I would like a list like ("Var126", "Var189", "Var113" ...)

Thanks in advance,

chittip
  • 31
  • 1
  • 2
  • What about `rownames(varimp$importance)`? Otherwise, please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with test data so we can run the same code as you to test possible solutions. – MrFlick Jun 25 '14 at 15:47
  • where is `varImp()` coming from? (Not from **gbm**, but perhaps you are using [unstated] the **caret** package?) – Gavin Simpson Jun 25 '14 at 15:56
  • varImp is the function from the caret package. – chittip Jun 25 '14 at 17:28
  • rownames(varimp$importance seems to do the trick. Thanks – chittip Jun 25 '14 at 17:28

2 Answers2

6

From the structure of the output you provided, it appears that

rownames(varimp$importance)

will return the values you are after.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
1
varImp(gbmModel, scale = TRUE)$importance

Return all variables

Dharman
  • 30,962
  • 25
  • 85
  • 135
forever
  • 139
  • 1
  • 2
  • 8