Site icon R-bloggers

R: How to Transform “prob” Predictions to a Single Column of Predicted Values

[This article was first published on R – The Hack-R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
# Recombine Test + Training ———————————————–
a <- cbind(x1, y1)
b <- cbind(x, y)

a$actual <- a$y1
b$actual <- b$y
a$y1     <- NULL
b$y      <- NULL

c <- rbind(a, b)

# Run Predictions for Entire Data Set ————————————-
all_preds <- predict(rf, newdata = c, type = “prob”)
colSums(all_preds)
summary(c$actual)

c$predicted <- apply(all_preds, 1, which.max)

then you can replace the column number with the descriptive category name or whatever
NOTE: This is NOT the same result that you’ll get by doing colSums on the “prob” type prediction, however

To leave a comment for the author, please follow the link and comment on their blog: R – The Hack-R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.