- This topic has 3 replies, 2 voices, and was last updated 5 years, 2 months ago by  Penpitcha Thawong. Penpitcha Thawong.
- 
		AuthorPosts
- 
		
			- 
2020-08-28 at 12:04 pm #22050 Penpitcha ThawongParticipant Penpitcha ThawongParticipantI ran code confusionMatrix(predicted2, actual), but the result showed Error: dataandreferenceshould be factors with the same levels. I try to assign the levels levels(actual)<- c(“no”,”yes”), but it didn’t work. please give me some suggestions.
- 
2020-08-28 at 12:51 pm #22051 Penpitcha ThawongParticipant Penpitcha ThawongParticipantI can fix it with confusionMatrix(table(predicted2, actual)) 
- 
2020-08-28 at 9:25 pm #22052 Pimphen CharoenKeymaster Pimphen CharoenKeymasterWell done, you worked it out! One of problems that you will come across quite often in R is the format of an input. You need to find out which format is required and if your input is not in the correct format, you have to re-format it accordingly. This might involve commands as.vector, as.factor, as.numeric, as.character, as.matrix, etc. Perhaps the explanation below might also help. It does take time and practice to get familiar with R and we hope learning-by-doing should help you to go through this 🙂 To use confusionMatrix(data, reference), you need to have both data and reference as a factor (use help in R by typing ?confusionMatrix or just google the command, for ex. this link will also do 
 https://www.rdocumentation.org/packages/caret/versions/3.45/topics/confusionMatrix)# assuming you have “predicted2” as a factor 
 > predicted2 <- as.factor(c("no","yes","no","no","yes","no","yes","yes","yes","yes"))
 > predicted2
 [1] no yes no no yes no yes yes yes yes
 Levels: no yes# assuming you have “actual” as a vector 
 > actual <- as.vector( c("no","yes","yes","no","yes","yes","yes","no","yes", "no"))
 > actual
 [1] “no” “yes” “yes” “no” “yes” “yes” “yes” “no” “yes” “no”> cbind(predicted2,actual) 
 predicted2 actual
 [1,] “1” “no”
 [2,] “2” “yes”
 [3,] “1” “yes”
 [4,] “1” “no”
 [5,] “2” “yes”
 [6,] “1” “yes”
 [7,] “2” “yes”
 [8,] “2” “no”
 [9,] “2” “yes”
 [10,] “2” “no”# to use confusionMatrix(data, reference), you need to have both data and reference as a factor # you can use as.factor to do this however you should always print this out to double check. Sometimes you still need to play with it to get the right format. In this case you would also want it to have the same levels, for ex. Levels: no yes > as.factor(actual) 
 [1] no yes yes no yes yes yes no yes no
 Levels: no yes
 > predicted2
 [1] no yes no no yes no yes yes yes yes
 Levels: no yes> confusionMatrix(predicted2, as.factor(actual)) 
 Confusion Matrix and StatisticsReference 
 Prediction no yes
 no 2 2
 yes 2 4Accuracy : 0.6 
 95% CI : (0.2624, 0.8784)
 No Information Rate : 0.6
 P-Value [Acc > NIR] : 0.6331Kappa : 0.1667 Mcnemar’s Test P-Value : 1.0000 Sensitivity : 0.5000 
 Specificity : 0.6667
 Pos Pred Value : 0.5000
 Neg Pred Value : 0.6667
 Prevalence : 0.4000
 Detection Rate : 0.2000
 Detection Prevalence : 0.4000
 Balanced Accuracy : 0.5833‘Positive’ Class : no 
- 
2020-08-29 at 10:49 pm #22075 Penpitcha ThawongParticipant Penpitcha ThawongParticipantthank you so much, very helpful 🙂 
 
- 
- 
		AuthorPosts
You must be logged in to reply to this topic. Login here

