How to Calculate Matthews Correlation Coefficient in R

Spread the love

Matthews Correlation Coefficient (MCC), also known as the Phi Coefficient, is a powerful metric used in machine learning to measure the quality of binary classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure, particularly useful even when the classes are of very different sizes. This article provides a comprehensive guide on calculating the Matthews Correlation Coefficient in R, encompassing the concept, applications, and practical implementation.

Introduction to Matthews Correlation Coefficient

MCC is used for binary classification problems. It is a correlation coefficient between the observed and predicted binary classifications. The MCC returns a value between -1 and 1. A coefficient of +1 represents a perfect prediction, 0 represents no better than random prediction, and -1 indicates total disagreement between prediction and observation.

The formula for calculating the Matthews Correlation Coefficient is:

MCC = (TP * TN – FP * FN) / sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN))


  • TP = True Positives
  • TN = True Negatives
  • FP = False Positives
  • FN = False Negatives

Loading Data in R

You can either use a built-in dataset or load your data from a CSV file.

# Using built-in dataset
mydata <- iris

# Or loading data from a CSV file
# mydata <- read.csv("path_to_your_file.csv")

Preparing the Data

For the calculation of MCC, you will need the counts of TP, TN, FP, and FN. These are usually derived from a confusion matrix, which you can create by comparing the observed outcomes to the predictions made by your classification model.

Calculating Matthews Correlation Coefficient

Using Base R

You can calculate the Matthews Correlation Coefficient using the formula stated earlier in base R.

# Assuming you have the counts TP, TN, FP, FN
TP <- 35
TN <- 30
FP <- 10
FN <- 5

# Calculating MCC
MCC <- (TP * TN - FP * FN) / sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN))

# Output the result

Using the mltools Package

For a more efficient calculation, you can use the mltools package. First, you need to install it.


Now, you can use the mcc function.

# Example data: actuals and predictions
actuals <- c(1, 1, 0, 1, 0, 0, 1)
preds <- c(1, 0, 0, 1, 1, 0, 1)

# Calculating MCC using mltools
MCC <- mcc(preds = preds, actuals = actuals)

# Output the result

Alternatively, if you already have the confusion matrix or the values of TP, TN, FP, FN, you can pass them directly to the function:

# Values of TP, TN, FP, FN
TP <- 35
TN <- 30
FP <- 10
FN <- 5

# Calculating MCC using mltools with TP, TN, FP, FN
MCC <- mcc(TP = TP, FP = FP, TN = TN, FN = FN)

# Output the result

Interpretation of Results

Interpreting the MCC is straightforward:

  • +1 indicates a perfect prediction.
  • 0 indicates that the model is no better than random guessing.
  • -1 indicates total disagreement between the prediction and the actual outcome.

MCC is generally considered a very balanced measure and can provide insights even when the dataset classes are imbalanced.

Applications of Matthews Correlation Coefficient

  1. Bioinformatics: MCC is widely used in bioinformatics for assessing the performance of algorithms used for protein structure prediction.
  2. Machine Learning: It is commonly used in various machine learning applications, particularly in evaluating binary classification models.
  3. Medical Diagnosis: It’s applied in the evaluation of medical diagnostic tests, where it’s essential to take into account both types of errors (false positives and false negatives).
  4. Quality Assurance: MCC is used in industries for quality testing and quality assurance purposes.


Matthews Correlation Coefficient is an important and reliable statistical measure for the evaluation of binary classification models, especially in cases where the dataset might have an imbalance between the number of positive and negative instances. Calculating it in R is straightforward, and utilizing this metric can provide valuable insights into the performance and reliability of your classification model. Whether you are working in bioinformatics, machine learning, or any other field that requires the evaluation of binary classification models, MCC is a tool that can enhance the robustness of your analyses.

Posted in RTagged

Leave a Reply