Mood’s Median Test (for two samples)

Basic Concepts

The Mood’s Median Test, essentially a two-sample version of the Sign Test, is used to determine whether the medians of two independent samples are equal (the null hypothesis). To perform this test, you need to execute the following steps:

  • Calculate the median m of the combination of the two samples.
  • Create a 2 × 2 contingency table whose first row consists of the number of elements in each sample that are greater than m and whose second row consists of the number of elements in each sample that are less than or equal to m. The columns correspond to the two samples.
  • Perform a chi-square test of independence
  • If p-value < α then there is a significant difference between the medians of the populations from which the two samples are derived; otherwise no significant difference between the medians is found.

Example

Example 1: Repeat Example 2 of Two Sample t-Test with Equal Variance to determine whether the Drug and Control groups have the same median based on the data in range B3:C15 of Figure 1.

As can be seen in Wilcoxon Ranked Sum Test, when we do this example using the Wilcoxon Ranked Sum test, we find that the data are not normally distributed, and so we need to use a non-parametric test.

We execute the steps in Mood’s Median Test as shown on the right side of Figure 1. Column I shows the formulas used in column F.

Mood's two-sample test

Figure 1 – Mood’s Median Tests for two samples

Since p-value = .0405 < .05 = α, we reject the null hypothesis and conclude there is a significant difference between the two population medians.

Observation: Generally, the Wilcoxon Rank Sum or Mann-Whitney test is used instead of Mood’s Median Test since they provide more accurate results.

Worksheet Functions

Real Statistics Functions: The Real Statistics Pack provides the following functions where R1 contains the sample data:

MOODS_STAT(R1) = the chi-square test statistic for Mood’s Median test.

MOODS_TEST(R1) = the p-value statistic for Mood’s Median test.

The data range R1 has two columns, one for sample 1 and the other for sample 2. All blanks and non-numeric values are ignored. The two samples don’t have to have the same number of elements.

For Example 1, MOODS_STAT(B4:C15) = 4.196 and MOODS_TEST(B4:C15) = .0405.

Observation: These functions can also be used for testing more than two samples. See Mood’s Median Test.

Examples Workbook

Click here to download the Excel workbook with the examples described on this webpage.

Reference

Mangiafico, S. S. (2016) Mood’s median test for two-sample data
https://rcompanion.org/handbook/F_05.html

39 thoughts on “Mood’s Median Test (for two samples)”

  1. Dear Charles,
    I recalculated your example using both methods: direct – as presented in Figure 2 and using the function MOODS_STAT. The values of the chi-square statistic were, as expected, the same. Then, I added a new observation (20) to the second sample. The new value of the chi-square statistic calculated according to Figure 2 was 4.812, but the value of the MOODS_STAT function for the extended range was different (4,891). I observed the same effect for other data sets. What is a possible reason for this difference?

    Reply
      • Dear Charles,
        Sorry, it was my error. In the calculation of the test statistic I counted cases X<Xmed, and not X<=Xmed, as it should be according to the definition of the median test. Therefore, there was a difference when the total sample size was odd, and the observation equal to the median value was assigned to a different category, changing the contingency table.

        Reply
  2. I need to build a simulation. Here is the scenario: “I have two distribution X1….Xn and Y1…Ym.The second sample is left-shifted from the other. The sample sizes are small (10).I want to conduct a simulation in r in order to get the randomized p values.In the paper that i am using to understand how to conduct a permutation test of the mood´s test,it is said that we must use the “”number of values in Y1…..Ym greater than the pooled median which follows hypergeometric distribution””” .I can not understand how to use this trick to build my simulation especially the information “hypergeometric distribution.

    Reply
  3. hi Charles
    I was running a one sided hypothesis testing of comparing medians between two groups … serial measurements of a pain score(0-10) were taken 2 hours apart for 12 hours ie at 0 hour ,2 hour,4hour,6hour,8 hour,10 hour and 12 hour.. median score at o hour, 2 hour and 4 hour is same …but p value by mann-whitney u test is <0.05…..is mood's median test better in this case?

    Reply
    • Why is a p-value from the Mann-Whitney test a problem? Mood’s median test doesn’t take the rankings into account and so I don’t have any reason to think that that test is better.
      Charles

      Reply
  4. Hi Charles,

    I thought of another test you could add if you think it would be worthwhile – I decided to comment here since it compares the medians of two groups, much the same as Moods Median Test, but it is a parametric approach that should be more accurate. It’s called the Fligner-Policello test. In case you’re not familiar with it I’ll describe the procedure as succinctly as possible.

    1) Arrange the values from the 2 groups in ascending numerical order.
    2) For each value in group 1, figure out how many values in group 2 are greater than this value, and call this number x. Increase x by 0.5 for each tie found in group 2.
    3) Add up all the x values, including the additions for ties if needed, and divide by the number of observations in group 1. This is x-bar.
    4) Calculate the sum of the squared differences between each value in group 1 and x-bar.
    5) Repeat steps 2-4 using the second group – find out how many values in group 1 are greater than each value in group 2, calculating x-bar and the sum of the squared differences as before.
    6) The numerator of our test statistic is (Group 1 size*Group 1 x-bar) – (Group 2 size*Group 2 x-bar).
    7) For the denominator of the test statistic, add together the sums of the squared differences for both groups, add to that the product of the two x-bars, then take the square root, and finally double the result.
    8) Evaluate this against a z-score for the normal distribution with 2 tails; at an alpha of 0.05 in 2 tails this corresponds to +/- 1.96. So if the test statistic is more than 1.96 or less than -1.96 we can say that the null hypothesis of equal medians for the two groups is false.

    If you need a written example, I’ll be happy to email you one. Perhaps you might find this to be an interesting alternative for a parametric approach. Thanks!

    Reply
    • Sorry, but I made an error above. Apparently the FP test is in fact nonparametric in nature, but it should be more robust than Moods Test.

      Reply
  5. Dear Charles
    i am Tariq Hussain, Ph.D. scholor.
    i read that Mood’s Median test is more Robust to outliers as compared to Krushkall wallis test. But i did not knew how to prove it?
    Help me plz….

    Reply
      • Hi Charles,

        Thanks for the article. I used the excel function chi_stat to analyst two independent samples. Somehow the result is different from the one used for a nonparametric test in a stats book my professor gave me.(2.524 for the moods test, 1.96 for the test in the stats book.)

        The median formula in the book is:
        N[(|AD-BC|-N/2)^2]/[(A+B)*(C+D)*(A+C)*(B+D).

        By any chance do you know what’s causing the difference?

        Yarui

        Reply
        • Yarui,
          The formula in the stats book uses a continuity correction factor of .5 (this is related to the N/2 term in the formula you have listed). Real Statistics doesn’t use this factor. There is some debate as to whether or not this factor should be used.
          Charles

          Reply
  6. Dear Charles,
    I tested your two sample example of your site (median = 18, x^2 = 4.195, p = 0.0405) with the corresponding median test calculator of IFA (http://www.fon.hum.uva.nl/Service/Statistics/Median_Test.html) and I obtain the following results : median = 18, p = 0.0995 but without the corresponding numerical result for x^2.
    Dear Charles,
    I would strongly appreciate if you could give me a “scientific” explanation about this discrepancy.

    Yours faithfully,

    Alcibiades (B.C. 450 – B.C. 404)

    In comparing your results with the corresponding ones from IFA (Holland) site, I found :

    yours : p =

    Reply
    • Alcibiades,

      Thanks for your question. It forced me to relook at Mood’s Median test. The version that I explain on the Real Statistics website is based on a chi-square test for independence, which is reasonably accurate for larger samples, especially when none of the cells is less than 5. Since z is normally distributed if and only z^2 has a chi-square distribution, probably the approximation described on the referenced website that you provided is equivalent for large samples (or at least similar). I hedge because the site doesn’t say that it is using the Mood’s test, although it probably is.

      Since I don’t have the data you used, I looked at the data on the website that you referenced. The samples there are small (9 and 12) and so they didn’t use the normal approximation, but used a permutation test instead. This is probably similar to the Fisher exact test, although I can’t say for sure, especially since they said that they would carry out the calculations favoring the null hypothesis. They came up with a result of p = .0484.

      When I carried out the Mood Median test using the Real Statistics software I got p-value = .00372. This is based on a chi-square test where one of the four cells has a value of 1. This is not a good fit for the chi-square test. When I used the Fisher Exact Test, I got p-value = .00752.

      I then noted that one of the values in sample 2 is 774, the exact same value as the overall median. In this case, we can optionally change the cell with value 1 to 2 (thereby favoring the null hypothesis). In this case, Fisher’s Exact Test yields the value .0300, which is still not the same as the value obtained on the referenced website, but much closer to that value.

      I haven’t invested much effort into Mood’s Median test, simply because it is usually not used since other tests are more powerful. I any case, I plan to add the option to use the Fisher Exact Test version for small samples.

      Charles

      Reply
  7. thank you very much Mr Charles.
    i just need a clarification. i compared survival rates between two fly groups using both mood’s median test and Mann whitney. all were not significant, i wish to know wich one is best to report on and possibly why.
    Best regards
    Bertrand

    Reply
  8. Hey Charles,
    Very many thanks for the fantastic info about the Test and your Excel Add-on.
    I have one Question. How is it possible, or is it possible to use the Mood’s Median-Test for a one side test ? Respectively to evaluate which sample has the smaller values.

    Reply
    • Maximillian,
      I am not aware of a one-sided version of Mood’s Median Test.
      If the test shows a significant difference between the medians, then it is safe to say that the group with the smaller sample median is the one with the smaller population median (with 100xalpha degree of confidence).
      Charles

      Reply
  9. Hi Charles,
    I still do not understand Mood”s median test (for two samples), does it work for two samples with two different sample size? If so, I just use SAS did, proc code as following:
    proc npar1way mood median data=expensediff;
    var difference;
    class group_cd;
    run;
    and the outputs for this step include median scores, median two_sample test, frequency above and below the overall median, and mood scores, Mood Two-Sample Test, distribution of mood scores.

    but I am still confusing about the purpose for Mood’s Median Test, because I did not learn anything about it when I was in university.

    Reply
    • Yang,
      Mood’s median test can be used with samples of different sizes.
      Usually there are other tests that can be used instead which have more power, which is probably why you didn’t learn about it when you were in university.
      Charles

      Reply
  10. Charles,

    I want to know which statistical test has to be used to compare medians among 3 groups i.e which is the analogous test similar to repeated measures ANOVA i.e median scores compared for same group at 3 time intervals (baseline, 3 months and 6 months)

    Reply
  11. Hi Charles,

    Very many thanks for all of the fantastic info on your site.

    I am trying to replicate the example you have here in R and am failing to get the same p-value as yourself. If I use the exact method based on Fisher I get a p-value = 0.0995. I can get to this value by either using this code:

    control = c(11,15,9,4,34,17,18,14,12,13,26,31)
    drug = c(34,31,35,29,28,12,18,30,14,22,10,29)
    data = c(control, drug)
    label = gl(2, 12, labels = c(“Control”,”Drug”))
    mood.medtest(data~label, exact = TRUE)

    or by using this function:
    median.test <- function(x, y){
    z <- c(x, y)
    g <- rep(1:2, c(length(x), length(y)))
    m <- median(z)
    fisher.test(z < m, g)$p.value
    }
    median.test(drug,control)

    If I set exact in the first example to FALSE, which then uses a chi-squared test with a Yates continuity correction, I get a p-value of 0.1013, and if I try the following on the contingency table you give above:
    a = matrix(data = c(3,8,9,4), nrow=2, ncol=2, byrow=TRUE)
    chisq.test(a)
    I also get a p-value of 0.1013

    Can you shed some light on what might be happening here?

    Thanks again for the really great resource you've created here.

    Phil

    Reply
    • Phil,
      Thanks for the clarification. It is difficult for me to comment since I don’t use R and so don’t know what assumptions are being made. Did you see a specific error in what I wrote?
      Charles

      Reply

Leave a Comment