Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

prediction - R: how to make a confusion matrix for a predictive model?

I have a dataframe. first column contains my the predictive score (range from 0 to 100, smaller values is expected to be in class A, larger values is expected to be in class B) for my model, 2nd column contains the real classification of the entries (either "class A" or "class B").

How to get confusion matrix with R for different cut off values, as I cannot decide where I should define values < 20 or < 50 as class A yet?

How to do this comparison efficiently with R?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a number of ways to do this, a reproducible example with your data would have been desirable:

set.seed(12345)
test <- data.frame(pred=c(runif(50,0,75),runif(50,25,100)), group=c(rep("A",50), rep("B",50)) )
table(test$pred<50,test$group)

gives

         A  B
  FALSE 18 34
  TRUE  32 16

So this says 32 A's were under 50 and 34 B's were over 50, while 18 A's were over 50 (wrongly classified) and 16 B's were under 50 (wrongly classified)

set.seed(12345)
test <- data.frame(pred=c(runif(50,0,60),runif(50,40,100)), group=c(rep("A",50), rep("B",50)) )
table(test$pred<50,test$group)

gives

         A  B
  FALSE  8 40
  TRUE  42 10

In this example, cause of the chosen sampling, your classification is much better.

The '50' in this can then be changed to anything you want, 20, 30, etc.

table(test$pred<50,test$group)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...