# Non Parametric Tests and Its Application Using R

Before we start with Non-Parametric Tests, We know within a parametric framework at first, we assume an explicit functional form of the population distribution function which is labelled by a parameter ϴ where ϴ is unknown or not completely known and subsequently any specific feature of the population distribution is objectively expressed as a function of ϴ.

On the other hand, within a non-parametric framework, any assumption relating to the functional form of the population distribution function is not made. Here some very general assumptions are made such as the population distribution is absolutely continuous. In the non-parametric inferential problem, we do have parameters but they failed to label the population distribution.

Suppose we are given a sample to validate any conjecture about the population median and we don’t have any idea about the population distribution function (or maybe the structural form the distribution is too complicated to deal with). Here the median doesn’t label the distribution function of the population. In this case, we use non-parametric tests.

There are many situations where the non-parametric test is a good choice.

1. If the sample size is too small to validate the assumptions (e.g. normality) of parametric tests, then non-parametric tests are the better option.
2. If the data are given in form of ranks, scores, grades etc we can adopt non-parametric tests. Usually, non-parametric tests are based on fractiles, ranks, concordance, discordance etc.

Parametric tests cannot apply if the data is ordinal or nominal, but in non-parametric tests, there are no such assumptions.

An important feature of the non-parametric test is the test statistic we use here is distribution free i.e. the distribution of the test statistic does not depend on the population distribution. If there are both parametric and non-parametric tests exist then the formal is likely to be more powerful than the later.

Let me discuss some important non-parametric tests and their implementation into R.

## SIGN Test:

One-sample:

One sample sign test is used to check whether the population median is equal to some constant or not. Suppose we have a data on the reviews (1-10) of a film. Now we want to check whether the median is 7 or not. So, we have the data as,

6,8,9,4,7,6,9,7,6,9,7,4,3,6,4,6,7,5,4,8,6,9,7,4,6,5,4.

Now our null hypothesis is population median is 7 against the alternative “not 7”. Now to perform the test in R we have the following codes,

CODE:

OUTPUT:

Here the p-value is greater than 0.05, so we can’t reject the null hypothesis.

Two-sample:

Let us consider an example to understand the two-sample sign test. Suppose a juice shop introduced two types of juice to the market. Now the owner wants to know whether people like both of the juice or not. He took random samples of size 16 of the customer’s reviews which one they prefer most. In the sample, he noticed that 6 peoples out of 16 preferred the first juice. Here null hypothesis is both the juices are equally effective. In that case, we should have the proportion of the preference of one juice is 0.5. So, actually, we are testing if the success probability of the binomial distribution (with n=16) is 0.5 or not.

Here we can perform the test in R as follows,

CODE:

OUTPUT:

Here we have the p-value 0.4545 > 0.05, so we can’t reject the null hypothesis. So, the owner can say at 5% level of significance there is not enough evidence to conclude that the juices are not equally preferred by people. From the output, we also get a 95% confidence interval. We can change the probability of success as we needed under the null by changing the value of “p” in the function “binom.test()”.

If there is numerical data on two varieties then we just subtract one from another. Then take the positive signs as success and count the success. Then we can have the binomial test as before.

The main disadvantage of the sign test is it doesn’t take into account the magnitude of the differences. So, we have to find another test which takes into account the magnitude of the differences.

Let us consider the data on the reviews of two schools from 15 individuals. On the basis of this reviews, one has to decide whether both schools are equally effective for his child.

## Wilcoxon Rank Sum test and Mann-Whitney test:

Suppose we want to test whether a distribution is of the right side or the left side of another distribution or both is on the same support. So here we want to compare the locations of the two distributions.

Consider the class test marks in the mathematics of a class. Now we want to know that if the boys got more marks than girls. Here don’t have any knowledge of the distributions of the marks of boys and girls. So, we will conduct a non-parametric test. To solve this query, we have Wilcoxon rank sum test.

Suppose the marks of the 15 boys are,

66, 71, 85, 87, 86, 70, 88, 61, 68, 79, 73, 69, 67, 82, 60

and marks of 10 girls are,

60, 51, 67, 68, 52, 65, 63, 84, 50, 62, 69, 73, 57, 74, 85

Our null hypothesis is the location shift is zero (i.e. both are on the same support) against the alternative the location shift is greater than zero (i.e. the boys’ marks greater than girls’ marks)

Here we can perform Wilcoxon rank sum test using R as follow,

CODE:

OUTPUT:

Here we get the p-value less than 0.05 but greater than 0.01. So, one can say that the boys’ marks are greater than girls’ marks at 5% level of significance, but at 1% level of significance, we don’t have enough evidence to say that boys got more marks than girls.

We can visualize the result of the test by using r code as, NOTE: In the function wilcox.test () we must have to put “paired = FALSE” to get Wilcoxon rank sum test. This test is equivalent to the Mann-Whitney test. So, this is also known as Wilcoxon–Mann–Whitney test.

## Wilcoxon Signed-Rank test:

This is the same as the Wilcoxon rank sum test but this is used for paired data. Suppose we have data on 10 children that how many days they play football and video games in a week. Now, we want to test whether they play video games more than football in a week. As we have the paired data on the same population, so we apply the Wilcoxon Signed-Rank test for testing the claims.

We have the data,

 Sl No Football Video Games 1 5 7 2 2 4 3 6 2 4 0 6 5 6 7 6 1 2 7 6 0 8 0 4 9 5 0 10 4 6

Here our null hypothesis is there is no difference between median values of the number of days a child plays football and video games against the alternative that the difference between the median values is negative i.e. the median value for football is less than the median value for video games.

So we can easily conduct the test using R,

CODE:

OUTPUT:

So, we get the p-value 0.3794 which is greater than 0. 05. So, we can’t reject the null hypothesis at 5% level of significance. So, one can conclude that at 5% level of significance there is not enough evidence to say that children play video games more than football in a week.

Now we can have a view on how the medians of two data are by running the r codes, From the above box plot, we can see that the medians are likely to be closer to each other.

## Mood’s Median Test:

This is a non-parametric test for checking equality of medians of two or more groups. Suppose a new company is started selling t-shirts. Now he is checking the reviews of his products and wants to compare with the other two products if his one is as good as others or not.

Suppose the data is,

 Brand Nike Nike Nike Nike Nike Nike Nike Adidas Adidas Ratings 5 4.5 3 2.5 4 5 4.5 3 3 T-shirt Adidas Adidas Adidas Adidas Adidas Adidas Adidas Adidas New Brand 3 3.5 4 4.5 5 4 3.5 4 4.5 T-shirt New New New New New New New New New Brand 2.5 4 3.5 4.5 5 5 5 4.5 3

Now, he is checking the medians of the ratings of the three t-shirts are equal or not. So, he can use Mood’s median test. The null hypothesis is the medians are the same against the alternative at least one equality doesn’t hold.

Now to perform Mood’s median test we have many functions in R like mood.medtest() in RVAideMemoire package, median_test() in coin package, Median.test() in  agricolae package.

CODE:

OUTPUT:

Here we get the p-value of the test is 0.1453557 > 0.05. Here we can’t the null hypothesis. So, we have not enough evidence to say that his product is good or bad compared to others.

We use Median.test() because it provides Post hoc analysis, from which we can easily check which medians are not equal if our null get rejected. In the last portion, we can see that some

“a” in the column groups. The groups with the letter “a” have more or less equal medians. If there is another letter “b” then the groups with the different letter have different medians. So, this is how we conduct the median test with post hoc analysis in R.

## RUN Test:

Equality of Distributions:
Suppose we have data of some boys and girls that how many hours per day they spend times watching Television. Here our interest is to know if a boy and a girl equally spend their times in watching television. So, here want to test whether the distribution of a time spent in watching tv of a boy and the distribution time spent in watching tv of a girl is equal or not. So, this is a test of equality of two distributions.

Data:  Boys (in hours): 3, 5, 3.5, 4.5, 6, 2, 6, 4, 9, 3, 4, 6

Girls (in hours): 5, 6, 5.5, 8, 6.5, 3, 8, 6.5, 7, 5, 3

So, here our null hypothesis H0 is F(x) = G(y), where F() is the distribution function for the boys and G() is the distribution function for girls. But here we don’t have any functional form of F or G. So, we adopt non-parametric test.

Here at first, we combine two samples and then sort them. After sorting, we replace the sorted values with unique characters corresponding to specific groups.

So using R we can have the test as follows-

CODE:

OUTPUT:

So, from the output of the test p-value is 0.01913 which is less than 0.05 but greater than 0.01. So we can say at 5% level of significance that a boy and a girl do not equally spend time in watching television but at 1% level of significance, we don’t have enough evidence to conclude that they spend unequal time in watching tv.

NOTE: Here we can see if the distributions are equal then the number of runs becomes larger. Here we have smaller runs if the distributions are not equal. From this concept, we use the run test for checking the equality of two distributions.

## Checking Randomness:

Now consider, some chocolates were given to a class monitor (Rohit) and asked to distribute those to all the students in the class. Now, Mithun (one of his rivals) had doubt on Rohit that maybe he gave his close friends more chocolates than other students. So, how can Mithun check this?

So, where he can have a non-parametric run test to verify whether the class monitor distributed the chocolates randomly (equal distribution to all / no biases) or not.

Here the null hypothesis H0 is chocolates are randomly distributed and the alternative is “not randomly distributed”.

Now he asked everyone in the class from the first bench to the last benches, how much candy they had received.

So, he got the data,

3, 5, 5, 3, 3, 3, 3, 4, 4, 5, 4, 6, 4, 5, 7, 6, 7, 3, 4, 8, 9, 3, 6, 6, 5, 3, 8, 8, 4, 8, 6, 5.

First, he has found the median as 5. Then replaced the original data by “U” and “D” according to as “>5” and “≤5”. After that, he performs the run test for checking whether the chocolates are independently distributed or not.

We can have this in R as follows,

CODE:

OUTPUT:

Here p-value of the run test is 0.2491 > 0.05. So, he hasn’t enough evidence to say Rohit had a bias for his close friends in distributing the chocolates.