R 随机森林变量选择

R 随机森林变量选择,r,machine-learning,random-forest,R,Machine Learning,Random Forest,我有一个随机森林,它目前是建立在100个不同的变量上。我只希望能够选择最重要的变量来构建我的随机林,以尝试提高性能,但除了从rf$重要性中获取重要性之外,我不知道从哪里开始 我的数据只是由数字变量组成,这些变量都经过了缩放 下面是我的射频代码: rf.2 = randomForest(x~., data=train,importance=TRUE, ntree=1501) #train rf_prob_train = data.frame(predict(rf.2, newdata=train

我有一个随机森林,它目前是建立在100个不同的变量上。我只希望能够选择最重要的变量来构建我的随机林,以尝试提高性能,但除了从rf$重要性中获取重要性之外,我不知道从哪里开始

我的数据只是由数字变量组成,这些变量都经过了缩放

下面是我的射频代码:

rf.2 = randomForest(x~., data=train,importance=TRUE, ntree=1501)

#train
rf_prob_train = data.frame(predict(rf.2, newdata=train, type="prob"))
rf_prob_train <-data.frame(rf_prob_train$X0)
val_rf_train<-cbind(rf_prob_train,train$x)
names(val_rf_train)<-c("Probs","x")

##Run accuracy ratio
x<-data.frame(rcorr.cens(-val_rf_train$Probs, val_rf_train$x))
rf_train_AR<-x[2,1]
rf_train_AR

#test
rf_prob_test = data.frame(predict(rf.2, test, type="prob"))
rf_prob_test <-data.frame(rf_prob_test$X0)
val_rf_test<-cbind(rf_prob_test,test$x)
names(val_rf_test)<-c("Probs","x")

##Run accuracy ratio
x<-data.frame(rcorr.cens(-val_rf_test$Probs, val_rf_test$x))
rf_test_AR<-x[2,1]
rf_test_AR

今天很忙,所以我没法早点给你。这为您提供了使用通用数据集的总体思路

library(randomForest)
library(datasets)

head(iris)
#To make our formula for RF easier to manipulate

var.predict<-paste(names(iris)[-5],collapse="+")
rf.form <- as.formula(paste(names(iris)[5], var.predict, sep = " ~ "))

print(rf.form)
#This is our current itteration of the formula we're using in RF

iris.rf<-randomForest(rf.form,data=iris,importance=TRUE,ntree=100)

varImpPlot(iris.rf)
#Examine our Variable importance plot

to.remove<-c(which(data.frame(iris.rf$importance)$MeanDecreaseAccuracy==min(data.frame(iris.rf$importance)$MeanDecreaseAccuracy)))
#Remove the variable with the lowest decrease in Accuracy (Least relevant variable)

#Rinse, wash hands, repeat

var.predict<-paste(names(iris)[-c(5,to.remove)],collapse="+")
rf.form <- as.formula(paste(names(iris)[5], var.predict, sep = " ~ "))

iris.rf<-randomForest(rf.form,data=iris,importance=TRUE,ntree=100)

varImpPlot(iris.rf)
#Examine our Variable importance plot

to.remove<-c(to.remove, which(data.frame(iris.rf$importance)$MeanDecreaseAccuracy==min(data.frame(iris.rf$importance)$MeanDecreaseAccuracy)))

#And so on...

你知道或知道哪些变量可能是多重线性的吗?我发现减少多线性变量的数量会有所帮助。另外,您是否正在规范化连续变量?这也为我带来了绩效提升。但是是的,仅仅用$importance来称呼他们基本上就是这样做的。你也可以看看解释的%方差,但他们说的或多或少是一样的。谢谢你,tbh我真的不知道确切的是什么,但我可以有一个有根据的猜测。一旦我用$important打电话给他们,你知道如何做下一步,然后只包括更重要的吗?现在我有一个变量列表,意思是你只需要自己决定保留哪些变量,拒绝哪些变量。当你看到基尼时,它看起来是渐进的吗?你可能只是抓住上面关于拐点的所有东西,然后离开剩下的。如果您需要基于解释的方差之类的帮助子设置,请回复,我会将其写下来作为答案。仅供参考,random forest非常擅长避免共线和自正则化问题。我强烈怀疑您是否会从删除变量中看到性能方面的任何好处。我阅读的所有文档都与您@Vincentmajor的观点一致,但是,我个人使用随机森林的经验表明,当我减少多线性变量的数量时,我可以获得更好的每变量%VarExplained。这是有道理的;如果许多变量描述的或多或少是同一件事,那么当两个变量都包含在RF模型中时,它们会分割方差。根据您使用射频的目的,这可能很重要,也可能不重要。我发现自己不得不解释为什么我在模型中包含变量的频率比我想的要高得多,所以对我来说,越少越好。我希望,如果我们在函数中有一个解决方案,可以在数据上运行,并且只返回前五个最重要的变量。