16.6 C
London
Thursday, July 4, 2024
HomePythonChi-Square Tests in PythonHow to Perform McNemar’s Test in Python

How to Perform McNemar’s Test in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

McNemar’s Test is used to determine if there is a statistically significant difference in proportions between paired data.

This tutorial explains how to perform McNemar’s Test in Python.

Example: McNemar’s Test in Python

Suppose researchers want to know if a certain marketing video can change people’s opinion of a particular law. They survey 100 people to find out if they do or do not support the law. Then, they show all 100 people the marketing video and survey them again once the video is over.

The following table shows the total number of people who supported the law both before and after viewing the video:

Before Marketing Video
After Marketing Video Support Do not support
Support 30 40
Do not Support 12 18

To determine if there was a statistically significant difference in the proportion of people who supported the law before and after viewing the video, we can perform McNemar’s Test.

Step 1: Create the data.

First, we will create a table to hold our data:

data = [[30, 40],
         [12, 18]]

Step 2: Perform McNemar’s Test

Next, we can use the mcnemar() function from the statsmodels Python library, which uses the following syntax:

mcnemar(table, exact=True, correction=True) 

where:

  • table: A square contingency table
  • exact: If exact is true, then the binomial distribution will be used. If exact is false, then the Chi-Square distribution will be used
  • correction: If true, a continuity correction is used. As a rule of thumb, this correction is typically applied when any of the cell counts in the table are less than 5.

The following code shows how to use this function in our specific example:

from statsmodels.stats.contingency_tables import mcnemar

#McNemar's Test with no continuity correction
print(mcnemar(data, exact=False))

pvalue      0.000181
statistic   14.019

#McNemar's Test with continuity correction
print(mcnemar(data, exact=False, correction=False))

pvalue      0.000103
statistic   15.077

In both cases – whether we apply the continuity correction or not – the p-value of the test is less than 0.05.

This means in both cases we would reject the null hypothesis and conclude that the proportion of people who supported the law before and after watching the marketing video was statistically significant different.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories