Error in tuneRF in R in if (n == 0) stop("data (x) has 0 rows") : argument is of length zero

690 views Asked by At

I would like to optimize the values of hyperparameters of ctree() (randomforest). I use the function tuneRF. I get the following error:

Error in if (n == 0) stop("data (x) has 0 rows") : 
  argument is of length zero

Here is my code:

library(party)
library(randomForest)
library(mlbench)
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 =92
train_ind <- sample(seq_len(nrow(dat1)), size = smp_size)
train <- dat1[train_ind, ]
test <- dat1[-train_ind, ]

ct <- ctree(V1 ~ ., data = train)

And here is my trial for finding the mtry

bestmtry <- tuneRF(train$V1,  train[V2:V9], stepFactor=1.5, improve=1e-5, ntree=500)
1

There are 1 answers

10
Julius Vainora On BEST ANSWER

There were a couple of issues,

bestmtry <- tuneRF(train[, V2:V9], train$V1, stepFactor = 1.5, improve = 1e-5, ntree = 500)

works.