Learn R Programming

exactRankTests (version 0.7-7)

wilcox.exact: Wilcoxon Rank Sum and Signed Rank Tests

Description

Performs one and two sample Wilcoxon tests on vectors of data for possibly tied observations.

Usage

## S3 method for class 'default':
wilcox.exact(x, y = NULL, alternative = c("two.sided", "less", "greater"),
            mu = 0, paired = FALSE, exact = NULL,  
            conf.int = FALSE, conf.level = 0.95, ...)
## S3 method for class 'formula':
wilcox.exact(formula, data, subset, na.action, \dots)

Arguments

x
numeric vector of data values.
y
an optional numeric vector of data values.
alternative
the alternative hypothesis must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter.
mu
a number specifying an optional location parameter.
paired
a logical indicating whether you want a paired test.
exact
a logical indicating whether an exact p-value should be computed.
conf.int
a logical indicating whether a confidence interval should be computed.
conf.level
confidence level of the interval.
formula
a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.
data
an optional data frame containing the variables in the model formula.
subset
an optional vector specifying a subset of observations to be used.
na.action
a function which indicates what should happen when the data contain NAs. Defaults to getOption("na.action").
...
further arguments to be passed to or from methods.

Value

  • A list with class "htest" containing the following components:
  • statisticthe value of the test statistic with a name describing it.
  • p.valuethe p-value for the test.
  • pointprobthis gives the probability of observing the test statistic itself (called point-prob).
  • null.valuethe location parameter mu.
  • alternativea character string describing the alternative hypothesis.
  • methodthe type of test applied.
  • data.namea character string giving the names of the data.
  • conf.inta confidence interval for the location parameter. (Only present if argument conf.int = TRUE.)

Details

This version computes exact conditional (on the data) p-values and quantiles using the Streitberg-R"ohmel algorithm for both tied and untied samples.

If only x is given, or if both x and y are given and paired is TRUE, a Wilcoxon signed rank test of the null that the median of x (in the one sample case) or of x-y (in the paired two sample case) equals mu is performed.

Otherwise, if both x and y are given and paired is FALSE, a Wilcoxon rank sum test (equivalent to the Mann-Whitney test) is carried out. In this case, the null hypothesis is that the location of the distributions of x and y differ by mu.

By default (if exact is not specified), an exact p-value is computed if the samples contain less than 50 finite values and there are no ties. Otherwise, a normal approximation is used.

Optionally (if argument conf.int is true), a nonparametric confidence interval for the median (one-sample case) or for the difference of the location parameters x-y is computed. If exact p-values are available, an exact confidence interval is obtained by the algorithm described in Bauer (1972). Otherwise, an asymptotic confidence interval is returned.

References

Myles Hollander & Douglas A. Wolfe (1973). Nonparametric statistical inference. New York: John Wiley & Sons. Pages 27--33 (one-sample), 68--75 (two-sample).

David F. Bauer (1972). Constructing confidence sets using rank statistics. Journal of the American Statistical Association 67, 687--690.

Cyrus R. Mehta and Nitin R. Patel (1998). StatXact-4 for Windows. Manual, Cytel Software Cooperation, Cambridge, USA

See Also

perm.test for the one and two sample permutation test.

Examples

Run this code
## One-sample test.
## Hollander & Wolfe (1973), 29f.
## Hamilton depression scale factor measurements in 9 patients with
##  mixed anxiety and depression, taken at the first (x) and second
##  (y) visit after initiation of a therapy (administration of a
##  tranquilizer).
x <- c(1.83,  0.50,  1.62,  2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
wilcox.exact(x, y, paired = TRUE, alternative = "greater")
wilcox.exact(y - x, alternative = "less")    # The same.

<testonly>wt <- wilcox.test(y-x)
  we <- wilcox.exact(y -x)
  wt
  we
  stopifnot(wt$p.value == we$p.value)</testonly>

## Two-sample test.
## Hollander & Wolfe (1973), 69f.
## Permeability constants of the human chorioamnion (a placental
##  membrane) at term (x) and between 12 to 26 weeks gestational
##  age (y).  The alternative of interest is greater permeability
##  of the human chorioamnion for the term pregnancy.
x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)
we <- wilcox.exact(x, y, alternative = "g")        # greater
wt <- wilcox.exact(x, y, alternative = "g")
<testonly>stopifnot(we$p.value == wt$p.value)
 stopifnot(all(we$conf.int == wt$conf.int))</testonly>

x <- rnorm(10)
y <- rnorm(10, 2)
wilcox.exact(x, y, conf.int = TRUE)

## Formula interface.
data(airquality)
boxplot(Ozone ~ Month, data = airquality)
wilcox.exact(Ozone ~ Month, data = airquality,
            subset = Month %in% c(5, 8))


<testonly>if (!any(duplicated(c(x,y)))) {
    we <- wilcox.exact(x, y, conf.int = TRUE)
    print(we)
    wt <- wilcox.test(x, y, conf.int = TRUE)
    print(wt)
    we$pointprob <- NULL
    we$method <- NULL
    wt$parameter <- NULL
    wt$method <- NULL
    stopifnot(all.equal(wt, we))
    we <- wilcox.exact(x, conf.int = TRUE)
    print(we)
    wt <- wilcox.test(x, conf.int = TRUE)
    print(wt)
    we$pointprob <- NULL
    we$method <- NULL
    wt$parameter <- NULL
    wt$method <- NULL
    stopifnot(all.equal(wt, we))
  }</testonly>
  

# Data from the StatXact-4 manual, page 221, diastolic blood pressure

treat <- c(94, 108, 110, 90)
contr <- c(80, 94, 85, 90, 90, 90, 108, 94, 78, 105, 88)

# StatXact 4 for Windows: p.value = 0.0989, point prob = 0.019

we <- wilcox.exact(contr, treat, conf.int=TRUE)
we
<testonly>stopifnot(round(we$p.value,4) == 0.0989)</testonly>

we <- wilcox.exact(contr, treat, conf.int=TRUE, exact=FALSE)
we
<testonly>stopifnot(round(we$p.value,4) == 0.0853)</testonly>

<testonly># StatXact page 221
  we <- wilcox.exact(treat, contr, conf.int=TRUE)
  stopifnot(we$conf.int[1] == -4)
  stopifnot(we$conf.int[2] == 22)
  stopifnot(we$conf.estimate == 9.5)</testonly> 

# StatXact 4 for Windows: p.value = 0.0542, point prob = 0.019
 
we <- wilcox.exact(contr, treat, alternative="less", conf.int=TRUE) 
we
<testonly>stopifnot(round(we$p.value,4) == 0.0542)</testonly>

# paired observations
# Data from the StatXact-4 manual, page 167, serum antigen level

# StatXact 4 for Windows: p.value=0.0021 (page 168)

pre <- c(149, 0, 0, 259, 106, 255, 0, 52, 340, 65, 180, 0, 84, 89, 212, 554,
500, 424, 112, 2600)
post <- c(0, 51, 0, 385, 0, 235, 0, 0, 48, 65, 77, 0, 0, 0, 53, 150, 0, 165,
98, 0)

we <- wilcox.exact(pre, post, paired=TRUE, conf.int=TRUE)
we
<testonly>stopifnot(round(we$p.value,4) == 0.0021)</testonly>

<testonly># StatXact page 175
  we <- wilcox.exact(post, pre, paired=TRUE, conf.int=TRUE)
  stopifnot(we$estimate > we$conf.int[1] & we$estimate < we$conf.int[2])
  stopifnot(we$conf.int[1] == -292)
  stopifnot(we$conf.int[2] == -54)
  stopifnot(round(we$estimate,1) == -137.8)</testonly>

we <- wilcox.exact(pre,post, paired=TRUE, conf.int=TRUE, exact=FALSE)
we
<testonly>stopifnot(round(we$p.value,4) == 0.0038)</testonly>


<testonly># Hollander & Wolfe (1999), second edition, Example 4.2., page 112

contr <- c(1042, 1617, 1180, 973, 1552, 1251, 1151, 728, 1079, 951, 1319)
SST <- c(874, 389, 612, 798, 1152, 893, 541, 741, 1064, 862, 213)

wilcox.exact(contr, SST, conf.int=TRUE) 

# page 110, Example 4.1

term <- c(0.8, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
weeks <- c(1.15, 0.88, 0.90, 0.74, 1.21)

wilcox.exact(weeks, term, conf.int=TRUE)</testonly>

# Hollander & Wolfe, p. 39, results p. 40 and p. 53

x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)

we <- wilcox.exact(y,x, paired=TRUE, conf.int=TRUE)
we 

<testonly>stopifnot(round(we$p.value,4) == 0.0391)
  stopifnot(round(we$conf.int,3) == c(-0.786, -0.010))
  stopifnot(round(we$estimate,3) == -0.46)</testonly>

# Hollander & Wolfe, p. 110, results p. 111 and p. 126

x <- c(0.8, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)

we <- wilcox.exact(y,x, conf.int=TRUE)
we
<testonly>stopifnot(round(we$p.value,4) == 0.2544)
  stopifnot(round(we$conf.int,3) == c(-0.76, 0.15))
  stopifnot(round(we$estimate,3) == -0.305)</testonly>

wel <- wilcox.exact(y,x, conf.int=TRUE, alternative="less")
weg <- wilcox.exact(y,x, conf.int=TRUE, alternative="greater")

<testonly>stopifnot(we$estimate == wel$estimate & we$estimate == weg$estimate)
  stopifnot(we$conf.int[1] <= weg$conf.int[1] & we$conf.int[2] >= wel$conf.int[2])</testonly>

<testonly>stopifnot(wilcox.exact(1:8)$p.value == 0.0078125)
stopifnot(wilcox.exact(c(1:7,7))$p.value == 0.0078125)
stopifnot(wilcox.exact(c(1,1,1))$p.value == 0.25)

x <- rnorm(10)
y <- rnorm(10)
stopifnot(wilcox.test(x,y,conf.int=TRUE)$estimate ==
          wilcox.exact(x,y,conf.int=TRUE)$estimate)
stopifnot(wilcox.test(x,conf.int=TRUE)$estimate ==
          wilcox.exact(x,conf.int=TRUE)$estimate)</testonly>

Run the code above in your browser using DataLab