# Two Independent Samples

Tags
Statistics
Visualization
R
Wilcoxon
Asymptotic
Date
May 2, 2019
Description
With two independent samples, we may ask about the centrality of the population distribution and see if there's a shift. Wilcoxon-Mann-Whitney is here!
Slug
nonparametric-methods-two-independent-samples
We often have two independent random samples instead of paired samples, and want to make inferences about the two populations from which the samples are drawn.
for convenience and without loss of generality, we assume . What can we say about the populations from which these samples were drawn? We may want to ask about the centrality or location of the population distributions. In particular, our usual question is are these the same for the two. More generally, do the population distributions coincide, or do they differ by a shift in location? We’re often interested in whether one population tends to yield larger values than the other, or do they tend to yield similar values.
The most common approach is a modification of Wilcoxon for two independent samples. It has a different formulation called the Mann-Whitney test. Usually they’re talked together as the WMW test.

### The Mann-Whitney test

There are three different (but essentially equivalent) ways of formulating the hypotheses of interest:
• In terms of the underlying population distributions:
where is the distribution corresponding to (first sample) and is the distribution corresponding to (both are CDFs).
• In terms of means / expectations:
Here we can use WMW as a test of mean comparison.
• In terms of probabilities:
Usually we see the hypotheses in the or form. One-sided questions could also be of interest:
Here all the alternatives are for tend to be smaller than .

#### Test statistic formulations

After forming the hypothesis, we may define our test statistic as:
which is the sum of the ranks from sample 1, and is the rank from the combined sample. Equivalently, we can use - sum of ranks from sample 2. From this we can derive the Mann-Whitney formulation:
There’s equivalent information in the two statistics, and we can easily derive . This test statistic can be computed without combining the samples, and without using ranks.
Consider the following example: we have time to complete a set of calculations in minutes using two different types of calculator. Is there a difference between the two types?
 Group A 23 18 17 25 22 19 31 26 29 33 Group B 21 28 32 30 41 24 35 34 27 39 36
In a Wilcoxon test, we combine the two samples and rank them together:
The ones marked with are from group B. In our case, :
In the Mann-Whitney test, we order the two groups separately:
 Group A 17 18 19 22 23 25 26 29 31 33 Group B 21 24 27 28 30 32 34 35 36 39 41
We go through each observation in sample A, and count how many observations in sample B are greater than the observation from A. In other words, we count up the exceedances:
The Wilcoxon is an extension of the one-sample test, and the Mann-Whitney counts exceedances of one sample relative to the other. The two are equivalent in terms of information about the samples, as shown by the formulations. We can easily convert the values of one test to another.

#### Other extensions

Other aspects of the one-sample situation extend in a relatively straightforward way to the two-sample case. For example, getting confidence intervals for the “shift” uses a similar idea to the Walsh average approach from the one-sample test, but using :
 17 18 19 ⋯ 29 31 33 21 -4 -3 -2 ⋯ 8 10 12 24 -7 -6 -5 ⋯ 5 7 9 ⋮ ⋮ ⋮ ⋮ ⋱ ⋮ ⋮ ⋮ 39 -22 -21 -20 ⋯ -10 -8 -6 41 -24 -23 -22 ⋯ -12 -10 -8
To get critical values from the table, count from most negative up and count from most positive down. The point estimator for the difference, the Hodges-Lehmann estimator, is the median of the differences.
Also as before, we can use a normal approximation when are reasonably large (or in the case of ties).
With ties, we make our standard modifications. For the Wilcoxon, we use mid-ranks as before. For the Mann-Whitney, ties from one sample to the other (i.e.  for some ) counts as in our calculation of .
Another thing we can do is to use a normal approximation with the score representation. Let be the sum of the scores test statistic ( or ) based on ranks or tied ranks. The score for observation is . Then

#### Implementation in R

In R, wilcox.test handles all 3 cases (one sample, paired samples, two independent samples):
• One sample: wilcox.test(x)
• Paired samples: wilcox.test(x, y, paired = T)
• x and y should be two data vectors of the same length
• Independent samples: wilcox.test(x, y)
• x and y are two data vectors of possibly different lengths
• paired = F by default
We can get confidence intervals for all 3 cases by setting the parameter conf.int = T and conf.level to a desired value.

### The median test

The median test is a simple “quick and dirty” method which is neither very efficient nor robust. The test asks a very specific question: location (median) shift.
Here’s the basic setup. Suppose we have two samples of independent observations of sizes and . They come from distributions that differ, if at all, only by a shift of location.
The general idea of shifting would be
Under , no shift, which means there’s a common median. The sample number of observations above the common median is .
Suppose two different methods of growing corn were randomly assigned to number of different plots of land, and the yield per acre was computed for each plot.
 Method 1 () 83 91 94 89 89 96 91 92 90 Method 2 () 91 90 81 83 84 83 88 91 89 84
The common median of the combined samples , and this is a point estimate for the joint median in the population under . We arrange the data in a contingency table:
 Method 1 Method 2 6 3 9 3 7 10 ㅤ 9 10 19
The third row, or the column margins, is fixed by design (9 and 10). The row margins are fixed by the definition of median. With the margins fixed, if we know one value in the table, we can fill in the other three values.
Note that different approaches are possible, for example the Sprent and Smeeton book would drop the values exactly equal to , which yields
 Method 1 Method 2 6 3 9 1 5 6 ㅤ 7 8 15
The most common analysis approach is the Fisher's exact test. The idea is to consider all tables that are “as or more extreme than” the observed, consistent with the fixed margins. For example, if the cell happened to be 4 instead of 6:
 Method 1 Method 2 > 89 4 5 9 ≤ 89* 5 5 10 ㅤ 9 10 19
we get a more balanced table than the observed one so it’s less extreme, making it more favorable to . We get a more extreme table (evidence against ) if the (1, 1) cell is really big or small. in the (1, 1) cell are all more extreme than the observed in one direction. are as extreme or more than the observed in the other direction.
The other benefit of the particular table structure is that we can easily work out the exact probability of each configuration. In our example, we have two independent samples, one from each method. Each behaves as a binomial with the appropriate number of trials () and probability of being above or below the common median under .
But they are also conditional on the fixed row totals, which are also binomial - the number of trials is . So, for our particular configuration, the probability is:
We need to compute this type of probability for all the more extreme tables mentioned above, and add them all up.
In general, with two independent samples of sizes and ; with values above the combined sample median in the first sample, and no sample values are equal to the combined sample median (just for ease of notation as clearly we can do the computation), the probability of that table configuration is

### Two-sample distribution tests

We can also extend the tests for a single sample’s distribution. We’d like to ask if it’s reasonable to assume that two samples of data come from the same distribution. Note that we haven’t specified which distribution.

#### The Kolmogorov-Smirnov test

The test to use is called the Smirnov or K-S test. Our hypotheses are
This is inherently a two-sided alternative. One-sided alternatives exist, but are very rarely used. Recall that in the one sample case, we compared our empirical CDF from the data with the CDF of the distribution hypothesized under . In this more general case, we will now compare the two CDFs. The test statistic is the difference of greatest magnitude between the two.
As before, this is taken as the vertical distance between empirical CDFs. A simplified version would look like the table below, where we record the distance between the empirical distribution function of the two samples.
 Ranked Observation 0 0 1 1 1 0
The K-S test is a test for general or overall difference in distributions. You can get a more sensitive analysis with a specific test for particular characteristics, if that is your goal (test for difference in means, differences in variance, etc.)
In R, we use the ks.test() as in the one-sample case. By default, the option exact = NULL is used. An exact p-value is computed if . There are also asymptotic approximation which will be used for even larger samples.
Interestingly, the K-S statistic depends only on the order of the and ’s in the ordered combined sample - the actual numerical values are not needed, because empirical CDFs jump at data points. Under standard two-sided tests, all orderings are equally likely, so the exact distribution of the test statistic can be known for the given sample sizes using just the ranks.

#### Cramer-von Mises test

The Cramer-von Mises test is another test which examines whether two samples of data come from the same distribution. The difference is that it looks at all the differences , where is the empirical CDF of sample 1, and is the empirical CDF of sample 2. These values are evaluated at all pairs.
The test statistic is:
Similarly, this test is only used for a two-sided alternative. Values of have been tabulated and there is an approximation that is not strongly dependent on sample size. The exact null distribution can be found, as for other tests, by considering all ordered arrangements of the combined sample to be equally likely, and by computing T for each ordered arrangement.
Note that ties won’t affect the results unless the sample sizes are really small. Usually the tests assume by nature there’s a true ordering.