[英]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.我正在构建一些逻辑回归模型,并发现自己使用了 caret 包中的 varImp('model name') 函数。 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.这将在 0 到 100 的范围内对相对重要性进行缩放和排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.