# Function in R, using precision, recall and F statistics
check.model.accuracy <- function(predicted.class, actual.class){
result.tbl <- as.data.frame(table(predicted.class,actual.class ) )
result.tbl$Var1 <- as.character(result.tbl$predicted.class)
result.tbl$Var2 <- as.character(result.tbl$actual.class)
colnames(result.tbl)[1:2] <- c("Pred","Act")
cntr <- 0
for (pred.class in unique(result.tbl$Pred) ){
cntr <- cntr+ 1
tp <- sum(result.tbl[result.tbl$Pred==pred.class & result.tbl$Act==pred.class, "Freq"])
tp.fp <- sum(result.tbl[result.tbl$Pred == pred.class , "Freq" ])
tp.fn <- sum(result.tbl[result.tbl$Act == pred.class , "Freq" ])
presi <- tp/tp.fp
rec <- tp/tp.fn
F.score <- 2*presi*rec/(presi+rec)
if (cntr == 1 ) F.score.row <- cbind(pred.class, presi,rec,F.score)
if (cntr > 1 ) F.score.row <- rbind(F.score.row,cbind(pred.class,presi,rec,F.score))
}
F.score.row <- as.data.frame(F.score.row)
return(F.score.row)
}
check.model.accuracy(predicted.df,actual.df)
# For multiclass, average across all classes