Avi
Avi

Reputation: 2283

How to extract nodesize default value from RandomForest in R

I know how to change the value of nodesize (minimal number of rows in terminal node) in RandomForest. However, I would like to know given a RandomForest model what is the value of nodesize.

require(party)
require (data.table)
require (e1071)
require (randomForest)
dat1 <- fread('https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data',stringsAsFactors=T)

## split data to train and test
set.seed(123)
dat1 <- subset(dat1, !is.na(V1))
smp_size<-0.8*nrow(dat1)
train_ind <- sample(seq_len(nrow(dat1)), size = smp_size)
train <- dat1[train_ind, ]
test <- dat1[-train_ind, ]
rf1 <- randomForest(V1 ~ ., data = train,keep.inbag = TRUE)
rf2 <- randomForest(V1 ~ ., data = train, ntree = 50,keep.inbag = TRUE)

Upvotes: 1

Views: 819

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

It happens that randomForest doesn't return the node size parameter. However, there are only "three" possible values of it: either it is specified by the user, or it is set by if (!is.null(y) && !is.factor(y)) 5 else 1 (1 for classification and 5 for regression). Thus, we have

getNodesize <- function(x) {
  look <- pmatch(names(x$call), "nodesize")
  if(any(!is.na(look)))
    x$call[!is.na(look)][[1]]
  else if (!is.null(x$y) && !is.factor(x$y))
    5
  else
    1
}

rf1 <- randomForest(V1 ~ ., data = train)
getNodesize(rf1)
# [1] 1
rf1 <- randomForest(V1 ~ ., data = train, nodesi = 3)
getNodesize(rf1)
# [1] 3

Upvotes: 3

Related Questions