Reputation: 73
I want to print 2*2 confusion matrix and label it. I am using table()
in r.
I want to add predicted and Reality label . Can anybody suggest me , how can I do it?
Upvotes: 7
Views: 24130
Reputation: 2105
This is a similar problem to the one in this question. You can follow that approach,
but simplify things a bit by just using a matrix to hold the values, and
just setting its dimension names to "predicted"
and "observed"
:
# create some fake data (2x2, since we're building a confusion matrix)
dat <- matrix(data=runif(n=4, min=0, max=1), nrow=2, ncol=2,
dimnames=list(c("pos", "neg"), c("pos", "neg")))
# now set the names *of the dimensions* (not the row/colnames)
names(dimnames(dat)) <- c("predicted", "observed")
# and we get what we wanted
dat
# output:
# observed
# predicted pos neg
# pos 0.8736425 0.7987779
# neg 0.2402080 0.6388741
Update: @thelatemail made the nice point in the comments that you can specify dimension names when creating tables. The same is true of matrices, except you supply them as names of the dimnames
list elements when calling matrix()
. So here's an even more compact way:
matrix(data=runif(n=4, min=0, max=1), nrow=2, ncol=2,
dimnames=list(predicted=c("pos", "neg"), observed=c("pos", "neg")))
Upvotes: 8