A Graphical Approach to Showing the Result of Classification Models

This is one of my favorite charts, it easily allows one to see how many predictions are right, and it allows one to see where the wrong ones are as well. It is the equivalent of a confusion matrix, but sometimes a picture is worth a thousand words. Some sample code is included below.

Rplot04

 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    <http://www.gnu.org/licenses/>.
    Developed by Mario Segal


#requires ggplot2. Data has to be in a dataset with three columns: actual, predicted and match.
#match is Yes if actual and predicted match, No otherwise.

 

require(ggplot2)

fitchart <- ggplot(chartdata,aes(x=actual,y=predicted,color=actual,shape=match))+geom_jitter(alpha=.6)+theme_bw()
fitchart <- fitchart + ylab(“Predicted Activity”)+xlab(“Actual Activity”)+ggtitle(“Summary of Classification Accuracy”)
fitchart <- fitchart+theme(legend.position=”bottom”)+theme(plot.title = element_text(size=14,color=”blue”, face=”bold”))
fitchart <- fitchart+theme(axis.title.x = element_text(face=”bold”,size=14),axis.title.y = element_text(face=”bold”,size=14))
fitchart <- fitchart+theme(axis.text.x=element_text(angle=0,color=”black”,size=12),axis.text.y=element_text(color=”black”,size=12))
fitchart <- fitchart + theme(legend.text = element_text(colour=”black”, size = 10),legend.title = element_text( face=”bold”))
fitchart <- fitchart + scale_color_discrete(name=”Actual\nActivity”)+scale_shape_manual(values=c(4,20),name=”Correct\nPrediction”)
fitchart

Advertisements

2 thoughts on “A Graphical Approach to Showing the Result of Classification Models

  1. This looks like a great chart, thanks for the tutorial.
    I was wondering, though, how to get the three columns of actual, predicted and match?
    I have created my classification tree using rpart, and then applied the prediction function, but from there how do I get the results?

    • the predict function for part should output the predicted class (you may need to tweak the parameters if you are getting multiple columns of class probabilities). The actuals come from your data. the element chart data on my example is a data,frame with 2 columns actual and predicted. I am not sure why you need the match for my chart, the % matched by class is of course interesting and important

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s