Here is the code I'm using:
# data set for debugging in RStudio
data("imports85")
input<-imports85
# settings
set.seed(1)
dependent <- make.names("make")
training.share <- 0.75
impute <- "yes"
type <- "class" # either "class" or "regr" from SF doc prop
# split off rows w/o label and then split into test/train using stratified sampling
input.labelled <- input[complete.cases(input[,dependent]),]
train.index <- createDataPartition(input.labelled[,dependent], p=training.share, list=FALSE)
rf.train <- input.labelled[train.index,]
rf.test <- input.labelled[-train.index,]
# create cleaned train data set w/ or w/o imputation
if (impute=="no") {
rf.train.clean <- rf.train[complete.cases(rf.train),] #drop cases w/ missing variables
} else if (impute=="yes") {
rf.train.clean <- rfImpute(rf.train[,dependent] ~ .,rf.train)[,-1] #impute missing variables and remove added duplicate of dependent column
}
# define variables Y and dependent x
Y <- rf.train.clean[, names(rf.train.clean) == dependent]
x <- rf.train.clean[, names(rf.train.clean) != dependent]
# upsample minorty classes (classification only)
if (type=="class") {
rf.train.upsampled <- upSample(x=x, y=Y)
}
# train and tune RF model
cntrl<-trainControl(method = "oob", number=5, p=0.9, sampling = "up", search='grid') # oob error to tune model
tunegrid <- expand.grid(.mtry = (1:5)) #create tunegrid with 5 values from 1:5 for mtry to tunning model
rf <- train(x, Y, method="rf", metric="Accuracy", trControl=cntrl, tuneGrid=tunegrid)
The 1st error is kind of related to this but using caret and randomForest instead of lars and I don't get it...
Error in order(x[, 1]) : 'x' must be an atomic vector for 'sort.list' - did you call 'sort' on a list?
And no, I did not call 'sort' on a list... at least not that I'm aware of ;-)
I checked the documentation for caret / train and it says that x should be a dataframe which is the case according to str(x).
If I used k-fold x-validation instead of oob error like so
cntrl<-trainControl(method = "repeatedcv", number=5, repeats = 2, p=0.9, sampling = "up", search='grid')
There is another funny error: Can't have empty classes in y
Checking complete.cases(Y) seems to indicate there are no empty classes though...
Does anyone have a hint for me?
Thanks, Mark