University of Minnesota, Twin Cities School of Statistics Stat 5601 Rweb
sign.test
1 - pbinom(b - 1, n, 1 / 2) pbinom(n - b, n, 1 / 2)the first line here does exactly the same as line five in the example, but is less accurate for very small P-values. The second does exactly the same as line five of the example because of the symmetry of the binomial distribution with
p = 1 / 2
.
pbinom(b, n, 1 / 2)
1 - 2 * pbinom(k - 1, n, 1 / 2)for different values of
k
. The vectorwise operation of R
functions can give them all at once
k <- seq(1, 100) k <- k[1 - 2 * pbinom(k - 1, n, 1 / 2) > 0.5] 1 - 2 * pbinom(k - 1, n, 1 / 2)If one adds these lines to the form above, one sees that there's not much choice, only three achieved levels
0.9854, 0.9567, 0.8922between 0.99 and 0.80.
k
to be any integer between
zero and n / 2
just before the second to last line in the form
(cat . . .
). A confidence interval with some
achieved confidence level will be produced.
alpha
rather than
alpha / 2
in the fifth line of the form. Then make either
the lower limit minus infinity or the upper limit plus infinity, as desired.
sign.test
All of the above can be done in one shot with the R function
sign.test
(on-line help).
Many authorities recommend (at least lukewarmly) the following procedure
for dealing with zero differences (differences equal to the hypothesized
value μ if not zero) in the sign test. After defining the
vector z
of differences, do
z <- z[z != mu] n <- length(z)which treates zero differences as if they were not part of the data (and the sample size is reduced accordingly).
The best that can be said for this is
Thus the zero fudge can give results of arbitrary bogosity. See the counterexample.
The alternative to the zero fudge, what Hollander and Wolfe call the conservative approach is to count the zero differences as evidence in favor of the null hypothesis. This is a bit tricky, since no matter how you do it, the recipe for the test must be altered. In my opinion, the easiest way is to change the definition of the test statistic for the upper-tailed test.
For the lower tailed test, assuming the vector of differences z
,
the sample size n
, and the hypothesisized value of the median
mu
have already been defined,
b <- sum(z >= mu) pbinom(b, n, 1 / 2)calculates the P-value for the lower-tailed test. Note that we need the weak inequality (
>=
) to include the zero differences
in the tail area calculated by the following statement.
Reversing the inquality gives the conservative upper-tailed test.
blow <- sum(z <= mu) pbinom(blow, n, 1 / 2)Note that this isn't the test statistic described in the book, but obviously does the right thing by symmetry.
The sign.test
function described in
the preceding section has an optional argument that
controls whether it does the zero fudge (zero.fudge=TRUE
or the conservative procedure (zero.fudge=FALSE
).
Perhaps inadvisedly, zero.fudge=TRUE
is the default.