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

March 13, 2015

(This article was first published on R – The Hack-R Blog, and kindly contributed to R-bloggers)

# 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”)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)