2

Currently, i created a random forest model in R called:

my_rforest

I'm trying to access the variables used by the random forest of my dataset, but so far, i did:

my_var <- my_rforest$importance

which gives me the output:

                      MeanDecreaseGini
temperature           6.51
wind                  4.67
heat                  0.3
.
.
.

Is there any way i could obtain the variable column, the first column of the output?

I'm currently using the following packages for my random forest.

library(randomForest)
library(caret)       
Maxxx
  • 3,688
  • 6
  • 28
  • 55

1 Answers1

0

We can use row.names to get the variables

library(randomForest)
my_var <- iris.rf$importance[,5, drop = FALSE]
row.names(my_var)
#[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 

NOTE: Using a reproducible example

data

set.seed(71)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
                     proximity=TRUE)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • could you explain what does [,5,drop=FALSE] means in the 2nd line? – Maxxx May 22 '19 at 02:16
  • @Maxxx It is just to align with the example you posted. If you chekc `iris.rf$importance`, there are other columns as well. I extracted the 'MeanDecreaseGini' which is the 5th column, and by default the `?Extract` uses `drop = TRUE` to drop the dimensions when there is a single column/row. So, to prevent that `drop = FALSE` was used – akrun May 22 '19 at 02:18