I am new to Caret and R and have a question regarding Stacking. I am getting the following error message for the confusionMatrix:
"Error in table(data, reference, dnn = dnn, ...) : all arguments must have the same length"
Can you please provide your input on how to resolve this, thanks. Providing the details below.
Step1. I divided the data into training, validation and test sets. Using the train function, I created the objects from the training set using 3 methods (rf, gbm, lda).
trCtrl <- trainControl(method = 'cv', number = 3);
rfObj <- train(diagnosis~., data = training, method = "rf", trControl = trCtrl); gbmObj <- train(diagnosis~., data = training, method = "gbm", trControl = trCtrl, verbose = F); ldaObj <- train(diagnosis~., data = training, method = "lda")
Step2. Using the above objects in the validation set, I made predictions on the validation set and collected this information in a data frame. So this data frame has 4 columns, 3 from the predictions and one from validation$y.
rfPred <- predict(rfObj, newdata = validation); gbmPred <- predict(gbmObj, newdata = validation); ldaPred <- predict(ldaObj, newdata = validation);
metaTrain <- data.frame(rfPred, gbmPred, ldaPred, diagnosis = validation$diagnosis)
Using the train function on this data frame using Random Forests, I created the final object.
metaObj <- train(diagnosis~., data = metaTrain, method = "rf", trControl = trCtrl)
Step3. Finally, using the testing set, I got predictions using the objects created from Step1. I combined these predictions into a data frame (has 3 columns). Next, using the object created from Step2, I predict on this data frame just created.
rfPredTest <- predict(rfObj, newdata = testing); gbmPredTest <- predict(gbmObj, newdata = testing); ldaPredTest <- predict(ldaObj, newdata = testing); finalDF <- data.frame(rfPredTest, gbmPredTest, ldaPredTest); finalPred <- predict(metaObj, newdata = finalDF)
I am getting the following message here: "'newdata' had 82 rows but variables found have 44 rows."
And then -
confusionMatrix(finalPred, testing$diagnosis)
I am getting the following error:
Error in table(data, reference, dnn = dnn, ...) : all arguments must have the same length
Can you please let me know what I am doing wrong? Thank you for your help with this.