# How to Perform Mood’s Median Test in R

Mood’s Median Test is used to compare the medians of two or more independent groups.

The median_test function from the coin library can be used to perform this test in R, which uses the following syntax:

median_test(response~group, data)

where:

• response: a vector of response values
• group: a vector of grouping values
• data: a data frame containing the response and group vectors

The following example illustrates how to use this function to perform Mood’s Median Test in R.

### Example: Mood’s Median Test in R

Suppose a teacher wants to know whether or not two different studying methods produce different exam scores among her class of students. To test this, she randomly assigns 10 students to use one studying method and another 10 students to use another. After two weeks, each student takes the same exam.

She decides to use Mood’s Median Test to determine if the median exam score differs between the two groups.

Step 1: Create the data frame.

```#create data
method = rep(c('method1', 'method2'), each=10)
score = c(75, 77, 78, 83, 83, 85, 89, 90, 91, 97, 77, 80, 84, 84, 85, 90, 92, 92, 94, 95)
examData = data.frame(method, score)

#view data
examData

method score
1  method1    75
2  method1    77
3  method1    78
4  method1    83
5  method1    83
6  method1    85
7  method1    89
8  method1    90
9  method1    91
10 method1    97
11 method2    77
12 method2    80
13 method2    84
14 method2    84
15 method2    85
16 method2    90
17 method2    92
18 method2    92
19 method2    94
20 method2    95
```

Step 2: Perform Mood’s Median Test.

```#load the coin library
library(coin)

#perform Mood's Median Test
median_test(score~method, data = examData)

#output
Asymptotic Two-Sample Brown-Mood Median Test

data:  score by method (method1, method2)
Z = -0.43809, p-value = 0.6613
alternative hypothesis: true mu is not equal to 0```

The p-value of the test is 0.6613. Since this value is not less than 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say that there is a statistically significant difference in the median exam scores between the two groups.

By default, this function assigns a score of 0 to observations that are exactly equal to the median. However, you can specify this value to instead be 0.5 or 1 by using the mid.score argument.

For example, the following code performs the exact same Mood’s Median Test but it assigns a value of 0.5 to observations that are equal to the median:

```#perform Mood's Median Test
median_test(score~method, mid.score="0.5", data = examData)

#output
Asymptotic Two-Sample Brown-Mood Median Test

data:  score by method (method1, method2)
Z = -0.45947, p-value = 0.6459
alternative hypothesis: true mu is not equal to 00```

The p-value of the test turns out to be 0.6459, which is slightly smaller than the previous p-value of 0.6613. However, the conclusion of the test is still the same: we do not have sufficient evidence to say that the median exam scores between the two groups is significantly different.