ArKo21
ArKo21

Reputation: 97

How to obtain coefficient for Matthews correlation after running these two lines?

I am trying the following example:

target_cm_table_pred <- prediction(data_item_col, T_or_F_col)
target_cm_table_perf <- performance(target_cm_table_pred,"mat")

What I don't understand is why I can't obtain the coefficients as a single number from the output like with the "auc", any idea on how? I mean what would I have to write in order to get the value of the coefficients?

Upvotes: 0

Views: 1539

Answers (1)

ShubhamA
ShubhamA

Reputation: 322

Here is a small function I wrote to get the value of the Matthews correlation. This takes in Confusion Matrix as the input value. Based on your question, you just need to create a confusion matrix from your classes of the predicted values. This you can create with the function confusionMatrix in the caret package.

conf_matrix <- confusionMatrix(data=logit1_trc, train$y))

logit1_trc: Predicted values , train$y: Response variable

You can directly pass this confusion matrix in the created function and get the coefficient.

Matt_Coef <- function (conf_matrix)
{
  TP <- conf_matrix$table[1,1]
  TN <- conf_matrix$table[2,2]
  FP <- conf_matrix$table[1,2]
  FN <- conf_matrix$table[2,1]

  mcc_num <- (TP*TN - FP*FN)
  mcc_den <- 
  as.double((TP+FP))*as.double((TP+FN))*as.double((TN+FP))*as.double((TN+FN))

  mcc_final <- mcc_num/sqrt(mcc_den)
  return(mcc_final)
}

I hope this helps

Upvotes: 2

Related Questions