*! version 1.0.0 18jul2014
help alignedranks

Title

alignedranks-- Two-sample aligned rank-sum (Hodges-Lehmann) test with exact statistics for small samples

Syntax

alignedranks depvar [if] [in] , by(groupvar)
[ strata(varlist) location(string) exact porder ]

options

Main
* by(varlist:groupvar) grouping variable

strata(varlist:varlist) stratify on varlist

location(string) measure of location with which alignment is performed (i.e., mean, median, etc.)

exact request that exact p-values be estimated if the total size of the two samples is <= 25

porder probability that depvar for the first group is larger than depvar for the second group

* by(groupvar) is required.

by is allowed with alignedranks;
see [D] by.

Description

alignedranks tests the hypothesis that two independent stratified samples are from populations with the same distribution.
Hodges & Lehmann (1962) developed an approach that involves ranking observations after they have undergone alignment
within their respective strata, thereby removing strata-specific effects from the data. The alignment is done by subtracting a symmetric
and translation-invariant measure of location (i.e., mean, median, etc.) from each observation within their corresponding stratum. These
transformed observations are then ranked without respect to treatment assignment or strata, followed by the application of the Wilcoxon
rank-sum test (also known as the Mann-Whitney two-sample statistic) to estimate treatment effects.

Options

Main

by(groupvar) is required. It specifies the
name of the grouping variable.

strata(varlist) specifies the variable on which to stratify the analysis. Observations will be
ignored for missing values of strata. If strata() is not specified, alignedranks defaults to [R] ranksum
and no alignment is performed.

location(string) When strata() is specified, location represents the measure of location with which alignment is performed.
Available location types are mean, median, sd, and hl (see the Remarks section below for discussion); default is mean.

exact requests that exact p-values be estimated if the total size of the two samples is <= 25, according to the method described in
Harris & Hardin (2013) and implemented in ranksumex.

porder displays an estimate of the probability that a random draw from the first population is larger than a random draw from the second
population.

Remarks

Lehmann (2006, p. 140) suggests that any measure of location may be used for alignment, with the only stipulation being that the same transformation
is applied to all observations within the same stratum, thereby preserving the validity of the null distribution. Mehrotra et al. (2010) report achieving
more robust results with the Hodges–Lehmann estimator of location than when using either the sample mean or median of the pooled responses in each stratum.
The Hodges–Lehmann estimator of location (Hodges & Lehmann, 1963) can be defined as the median of all pairwise means of the pooled responses in each stratum.
It is computed by first generating all unique pairwise combinations of depvar within each stratum (equalling the combinatorial function
of [n!/{k!(n - k)!], where n is the total number of observations in the stratum, and k = 2). Next, the mean is computed for each pair, and finally, the median
of all pairwise means is estimated.

Given the iterative process involved in computing the Hodges–Lehmann estimator of location, users can expect alignedranks to return results slowly in large
data sets when the location(hl) option is specified. In timing the command with various sample sizes, alignedranks returned results in 4.93 seconds with a sample size of 1003 (275 strata);
10.71 seconds when N = 10,030 (275 strata); 75.35 seconds when N = 100,300 (275 strata); and 882.93 seconds when N = 1,003,000 (275 strata). These tests were
performed using Stata SE/13.1 on a MS Windows 7.0, 64-bit operating system with an Intel(R) Core(TM) Duo CPU, 3.0 GHz processor and 4.0 GB of RAM.

Examples

Load example data

. webuse cattaneo2, clear

Perform aligned rank-sum test on bweight by using the two groups defined by
mbsmoke and specifying medu as the strata variable

. alignedranks bweight, by(mbsmoke) strata(medu)

Same as above, but specify hl as the location measure, and include an estimate of the probability that the value of
bweight for an observation with mbsmoke = 0 is greater than the value of bweight for an observation when mbsmoke = 1

. alignedranks bweight, by(mbsmoke) strata( medu) location(hl) porder

We now limit the dataset to 12 observations from each mbsmoke group, and specify the exact option to get exact statistics.

. sample 12, by(mbsmoke) count

. alignedranks bweight, by(mbsmoke) strata(medu) location(median) porder exact

Saved results

alignedranks saves the following in r() if n>25:

Scalars

r(N_1) sample size n_1

r(N_2) sample size n_2

r(z) z statistic

r(Var_a) adjusted variance

r(group1) value of variable for first group

r(sum_obs) actual sum of ranks for first group

r(sum_exp) expected sum of ranks for first group

r(porder) probability that draw from first population is larger than draw from second population

alignedranks saves the following in r() if n<=25:

Scalars

r(x1) value (x) of the distribution such that P(X<=x1)

r(x2) value (x) of the distribution such that P(X>=x2)

r(nx1) numerator of probability for P(X<=x1)

r(nx2) numerator of probability for P(X>=x2)

r(den) denominator of probabilities

r(p) p-value = P(X<=x1) + P(X>=x2)

References

Harris, T., and J. W. Hardin. 2013. Exact Wilcoxon signed-rank and Wilcoxon Mann–Whitney ranksum tests.
Stata Journal 13: 337-343.

Hodges, J. L., and E. L. Lehmann. 1962. Rank methods for combination of
independent experiments in the analysis of variance.
Annals of Mathematical Statistics 33: 482–497.

Hodges, J. L., and E. L. Lehmann. 1963. Estimation of location based on ranks.
Annals of Mathematical Statistics 34: 598–611.

Lehmann, E. L. 2006. Nonparametrics: statistical methods based on ranks (Rev. ed.)
New York: Springer.

Mann, H. B., and D. R. Whitney. 1947. On a test whether one of two random
variables is stochastically larger than the other.
Annals of Mathematical Statistics 18: 50-60.

Mehrotra, D. V., Lu, X. and X. Li. 2010. Rank-based analyses of stratified experiments:
alternatives to the van Elteren Test. The American Statistician 64: 121-130.

McNeil, R. B., and R. F. Woolson. 2007. Aligned Rank Test. Wiley Encyclopedia of Clinical Trials. 1–4.

Wilcoxon, F. 1945. Individual comparisons by ranking methods.
Biometrics 1: 80-83.

Citation of alignedranks

alignedranks is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Linden, Ariel. 2014.
alignedranks: Stata module for implementing a two-sample aligned rank-sum (Hodges-Lehmann) test with exact statistics for small samples.

Author

Ariel Linden

President, Linden Consulting Group, LLC

Ann Arbor, MI, USA

alinden@lindenconsulting.org

http://www.lindenconsulting.org

Acknowledgments

I wish to thank Nicholas J. Cox for his support while developing alignedranks

Also see

Online: ranksum, egen(rank() function), emh (if installed),
vanelteren (if installed), ranksumex (if installed), somersd (if installed)