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.
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
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?
Hello Olgierd,
If you email me an Excel spreadsheet with this example, I will try to figure out what is going wrong.
Charles
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.
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.
Hello Zakaria,
I don’t know what the paper that you are using says so that it is difficult for me to give a precise answer. Also, I don’t use R and so I can’t give an answer that pertains to R.
In any case, in the next few days, I plan to add a webpage describing how to use simulation to calculate the p-value for the chi-square test for independence (a quasi-exact test).
Charles
Thank you charles.
If you cannot implement that in R then this is not a problem because i am a good R programmer .The real problem is that I ca not understand him I mean understand the statistic T that should be used in order to build the randomization (simulation of 10000 samples. Please below is the link of the paper ( second page in the pdf):
https://www.researchgate.net/publication/302566971_Robust_nonparametric_tests_for_the_two-sample_location_problem
Hello Zakaria,
Thanks for sending me the paper. There are many approaches for using simulation to obtain a non-parametric test. In fact, I used one such approach to obtain simulated Wilcoxon signed ranks and two independent sample tests. These are explained at
https://real-statistics.com/non-parametric-tests/mann-whitney-test/mann-whitney-simulation/
https://real-statistics.com/non-parametric-tests/wilcoxon-signed-ranks-test/signed-ranks-simulation/
The paper that you sent me uses an approach based on the Hodges-Lehmann shift estimator. This is probably the estimator described on the following webpage:
https://real-statistics.com/non-parametric-tests/mann-whitney-test/mann-whitney-median-confidence-interval/
As I said in a previous response, this week I am going to add a webpage that describes another approach to using simulation to perform a non-parametric test.
Is the moods test a two sided test? If so can I just divide the p-value by 2 to get the one sided significance?
Hello Wesley,
I don’t think that this is appropriate.
Charles
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?
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
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!
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.
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….
Tariq,
That may be true, I can’t say for sure, but since Kruskal-Wallis uses ranks it should be pretty robust to outliers.
Charles
Thank you for explanation.
Mood’s median test is same as median test ?
if different then how they are different ?
I presume that they are the same, although perhaps there is another median test that I am not aware of.
Charles
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
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
Charles,
Thank you for your reply. It really helped.
Best,
Yarui
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 =
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
Can any one tell me the formula of The Median test for four samples??
Zanish,
See the following webpage:
Mood’s Median Test
Charles
Hello Charles,
I can’t find the =chi_stat, or =chi_test formulas on excel.
Do I need an add-in?
Thank you in advance,
Pablo
Pablo,
Yes, you need the Real Statistics add-in.
Charles
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
You need to tell me more about the nature of your data before I am able to answer your question.
Charles
Hi Charles,
From this test, is it possible to tell whether or not two samples came from the same population?
K
No. This test doesn’t go that far.
Charles
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.
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
Tank you very much, for your answer.
Maximilian
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.
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
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)
Madhu,
The usual nonparametric test is Friedman’s Test.
Charles
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
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