## 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