简体   繁体   中英

Sorting output from variable importance (caret package)

I am building a few logistic regression models and find myself using the varImp('model name') function from the caret package. This function has been useful, but I would prefer that the variable importance be returned sorted from most important to least important.

Here is a reproducible example:

library(caret)
data("GermanCredit")

Train <- createDataPartition(GermanCredit$Class, p=0.6, list=FALSE)
training <- GermanCredit[ Train, ]
testing <- GermanCredit[ -Train, ]

mod_fit <- glm(Class ~ Age + ForeignWorker + Property.RealEstate +Housing.Own + CreditHistory.Critical, data=training, family=binomial(link = 'logit'))

When I use the code:

varImp(mod_fit)

It returns:

                        Overall
Age                    1.747346
ForeignWorker          1.612483
Property.RealEstate    2.715444
Housing.Own            2.066314
CreditHistory.Critical 3.944768

I want to sort by the "Overall" column like this:

sort(varImp(mod_fit)$Overall)

It returns:

[1] 1.612483 1.747346 2.066314 2.715444 3.944768

Is there a way to return the variable name and level of importance together sorted in a descending order?

Thank you in advance.

library(caret)
data("GermanCredit")

Train <- createDataPartition(GermanCredit$Class, p=0.6, list=FALSE)
training <- GermanCredit[ Train, ]
testing <- GermanCredit[ -Train, ]

mod_fit <- glm(Class ~ Age + ForeignWorker + Property.RealEstate +Housing.Own + CreditHistory.Critical, data=training, family=binomial(link = 'logit'))

imp <- as.data.frame(varImp(mod_fit))
imp <- data.frame(overall = imp$Overall,
           names   = rownames(imp))
imp[order(imp$overall,decreasing = T),]

Usually you would be able to do:

 varImp(mod_fit, scale = TRUE)

And that would scale and order the relative importance on a scale from 0 to 100.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM