Ryan Hill
Ryan Hill

Reputation: 136

Get level of factors from trained randomForest in R

I have a trained randomForest model in R, that I load from an RDS file. One of the predictors is a non-ordered factor, I no longer have the training data that was used to produce this, but would like to know what levels this factor had so I can ensure data I am trying to predict on only has those levels for this factor. Is this possible?

Upvotes: 2

Views: 338

Answers (1)

user1808924
user1808924

Reputation: 4926

The data structure of a randomForest object varies slightly depending on whether it was trained using a "formula interface" or "matrix interface". However, the information about independent variables is stored in both cases as a list object randomForest$forest$xlevels.

library("randomForest")

df = read.csv("Audit.csv")

rf = randomForest(Adjusted ~ ., data = df)
print(rf$forest$xlevels)
print(rf$forest$xlevels["Education"])

Upvotes: 2

Related Questions