Getting the error Error in check_dims(x = x, y = y) : nrow(x) > 1 is not TRUE when I am using Caret, can anyone help. Error is occurring in ## Train random forest model.
Input data:
enter image description here
See script below:
raw <- read.delim(“46compounds.csv”, sep=”,”)
raw <- raw[!complete.cases(raw), ]Class <- raw[,4]
Props <- raw[5:343]
training <- cbind(Class,Props)
fullSet <- names(training)[names(training) != “Class”]
Remove Near Zero variance
isNZV <- nearZeroVar(training[,fullSet], saveMetrics = TRUE, freqCut = floor(nrow(training)/5))
fullSet <- rownames(subset(isNZV, !nzv))
number of conformers is problematic
fullSet <- fullSet[fullSet != “confs”]
Some are extremely correlated, so removed
predCorr <- cor(training[,fullSet])
highCorr <- findCorrelation(predCorr, .99)
modCorr <- findCorrelation(predCorr, 0.8)
reducedSet <- fullSet[-modCorr]
fullSet <- fullSet[-highCorr]ctrl <- trainControl(method = “repeatedcv”,
number = 10,
repeats = 20,
summaryFunction = twoClassSummary,
classProbs = TRUE,
savePredictions = TRUE,
returnResamp = “all”)
mtryValues <- c(2, 4, 6, 8, 10, 20, 40, 60)
Ensure reproducibility
set.seed(10)
Train random forest model
rfFit <- train(x = training[,reducedSet],
y = training$Class,
method = “rf”,
ntree = 2000,
tuneGrid = data.frame(mtry = mtryValues),
importance = TRUE,
metric = “ROC”,
trControl = ctrl)
rfFitrfp <- merge(rfFit$pred, rfFit$bestTune)
rfCM <- confusionMatrix(rfFit, norm = “none”)
rfCM
Calculate important variables
rfImp <- varImp(rfFit)
rfImp.top <- rfImp$importance[with(rfImp$importance, order(-High)),]
rfImp.names <- rownames(rfImp.top[1:8,]) # top 5 variables
rfImp.final <- data.frame(name = rownames(rfImp.top[1:20,]), importance = rfImp.top$High[1:20])
rfImp.final <- data.matrix(rfImp.top$High[1:20])
rownames(rfImp.final) <- rownames(rfImp.top[1:20,])rfRoc <- roc(response = rfFit$pred$obs,
predictor = rfFit$pred$High,
levels = rev(levels(rfFit$pred$obs)))
This script has worked previously with the same input file, so I am not sure why it is not working now
Laura Thompson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.