1
votes

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?

1

1 Answers

4
votes

You appear to be a bit confused. predRF already is a prediction by definition - it is not something you can meaningfully feed into predict(). Therefore, you will get your confusion matrix simply as follows:

> confusionMatrix(testing$cyl,predRF)
Confusion Matrix and Statistics

          Reference
Prediction 4 6 8
         4 2 0 0
         6 0 1 0
         8 0 0 3

In addition, a line starting inTrain = inTrain <- looks a bit strange, though syntactically valid. I strongly suggest you look closely at your code, as I suspect there may be a few more errors hidden there.