How to Calculate Correlation Between Continuous & Categorical Variables


When we would like to calculate the correlation between two continuous variables, we typically use the Pearson correlation coefficient.

However, when we would like to calculate the correlation between a continuous variable and a categorical variable, we can use something known as point biserial correlation.

Point biserial correlation is used to calculate the correlation between a binary categorical variable (a variable that can only take on two values) and a continuous variable and has the following properties:

  • Point biserial correlation can range between -1 and 1.
  • For each group created by the binary variable, it is assumed that the continuous variable is normally distributed with equal variances.
  • For each group created by the binary variable, it is assumed that there are no extreme outliers.

The following example shows how to calculate a point biserial correlation in practice.

Example: Calculating a Point Biserial Correlation

Suppose a college professor would like to determine if there is a correlation between gender and score on particular aptitude exam.

He collects the following data on 12 males and 12 females in his class:

Since gender is a categorical variable and score is a continuous variable, it makes sense to calculate a point-biserial correlation between the two variables.

The professor can use any statistical software (including Excel, R, Python, SPSS, Stata) to calculate the point-biserial correlation between the two variables.

The following code shows how to calculate the point-biserial correlation in R, using the value 0 to represent females and 1 to represent males for the gender variable:

#define values for gender
gender <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

#define values for score
score <- c(77, 78, 79, 79, 82, 84, 85, 88, 89, 91, 91, 94,
           84, 84, 84, 85, 85, 86, 86, 86, 89, 91, 94, 98)

#calculate point-biserial correlation
cor.test(gender, score)

	Pearson's product-moment correlation

data:  gender and score
t = 1.3739, df = 22, p-value = 0.1833
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.1379386  0.6147832
sample estimates:
      cor 
0.2810996 

From the output we can see that the point biserial correlation coefficient is 0.281 and the corresponding p-value is 0.1833.

Since the correlation coefficient is positive, it tells us that there is a positive correlation between gender and score.

Since we coded the males as 1 and females as 0, this indicates that scores tend to be higher for males (i.e. scores tend to increase as gender “increases” from 0 to 1)

However, since the p-value is not less than .05, this correlation coefficient is not statistically significant.

Additional Resources

The following tutorials explain how to calculate point biserial correlation using different statistical software:

How to Calculate Point-Biserial Correlation in Excel
How to Calculate Point-Biserial Correlation in R
How to Calculate Point-Biserial Correlation in Python

4 Replies to “How to Calculate Correlation Between Continuous & Categorical Variables”

  1. Hi,

    I really like this statistic! thank you! could you share the python library of this point-biserial correlation, please?

  2. how to perform a correlation between qualitative variable with multiple categories (more than 2) and continuous one ?

    1. Hi Aalaa…To perform a correlation analysis between a qualitative variable with multiple categories (more than 2) and a continuous variable, you can follow these approaches depending on the nature of the qualitative variable and the type of insights you seek:

      —

      ### 1. **One-Way ANOVA (Analysis of Variance)**
      – **Use case**: Tests whether the means of the continuous variable differ significantly across the categories of the qualitative variable.
      – **Procedure**:
      1. Use the qualitative variable as the grouping variable.
      2. Conduct a one-way ANOVA to test the relationship.
      3. Interpret the F-statistic and p-value.
      – **Python Example**:
      “`python
      from scipy.stats import f_oneway

      # Example data
      continuous_data = [10, 15, 20, 25, 30, 35]
      category_data = [‘A’, ‘A’, ‘B’, ‘B’, ‘C’, ‘C’]

      # Split continuous data by categories
      group_a = [10, 15]
      group_b = [20, 25]
      group_c = [30, 35]

      # Perform one-way ANOVA
      f_stat, p_value = f_oneway(group_a, group_b, group_c)
      print(“F-statistic:”, f_stat)
      print(“p-value:”, p_value)
      “`

      —

      ### 2. **Point-Biserial or Categorical Regression**
      If you want to calculate the strength of association between a categorical variable with more than 2 levels and a continuous variable:
      1. Encode the categorical variable using one-hot encoding or dummy encoding.
      2. Perform regression to see the relationship.

      —

      ### 3. **Eta-Squared**
      – **Use case**: A measure of association strength that works for categorical vs. continuous data.
      – **Procedure**:
      1. Perform an ANOVA.
      2. Calculate Eta-squared (\( \eta^2 \)):
      \[
      \eta^2 = \frac{SS_{between}}{SS_{total}}
      \]
      where \(SS_{between}\) is the sum of squares between groups and \(SS_{total}\) is the total sum of squares.

      —

      ### 4. **Correlation Ratio (η or Eta)**
      – **Use case**: Measures the association between a categorical variable and a continuous variable.
      – **Procedure**:
      1. Calculate the variance within each group of the categorical variable.
      2. Compare the variance explained by the groups to the total variance.
      – **Python Example**:
      “`python
      import numpy as np
      import pandas as pd

      # Example data
      df = pd.DataFrame({
      ‘Category’: [‘A’, ‘A’, ‘B’, ‘B’, ‘C’, ‘C’],
      ‘Values’: [10, 15, 20, 25, 30, 35]
      })

      def eta_squared(data, categories, values):
      groups = data.groupby(categories)[values]
      overall_mean = data[values].mean()
      ss_between = sum(groups.size() * (groups.mean() – overall_mean)**2)
      ss_total = sum((data[values] – overall_mean)**2)
      return ss_between / ss_total

      eta = eta_squared(df, ‘Category’, ‘Values’)
      print(“Eta-squared:”, eta)
      “`

      —

      ### 5. **Visualization**
      Sometimes visual inspection can provide better insights:
      – Use **boxplots** to visualize the distribution of the continuous variable across categories.
      “`python
      import seaborn as sns
      import matplotlib.pyplot as plt

      sns.boxplot(x=’Category’, y=’Values’, data=df)
      plt.show()
      “`

      —

      ### Which Method to Choose?
      – Use **ANOVA** if you are interested in whether the mean differences are statistically significant.
      – Use **Eta-squared** or **correlation ratio** for a measure of effect size or association strength.
      – Use **regression** if you are incorporating the relationship into a predictive model.

Leave a Reply

Your email address will not be published. Required fields are marked *