I try predicting "cyl" in the "mtcars" data with "cyl" as a factor variable:
data(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
I split the dataset to "training" and "testing":
inTrain = inTrain <- createDataPartition(y=mtcars$cyl,p=0.75, list=FALSE)
training = mtcars[ inTrain,]
testing = mtcars[-inTrain,]
and fit a random forests model:
modelRF <- train(cyl ~ .,method="rf",data=training)
predRF <- predict(modelRF,testing)
Currently I try obtaining prediction accuracy with confusionMatrix function:
confusionMatrix(testing$cyl, predict(predRF, newdata = testing))
...but I keep getting this error:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "factor"
What am I doing wrong? Is there any better method for obtaining prediction accuracy?