To do each example, just click the "Submit" button. You do not have to type in any R instructions or specify a dataset. That's already done for you.
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
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.
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
But these hypotheses are not what you want to test! What you want to test is the hypotheses described on p. 60 in Hollander and Wolfe: that the medians are the same or different.
Modify the data for our example above for the sign test adding a million zero differences to the data set. The zero fudge says we throw out those zeros and do exactly the same analysis getting P = 0.00046, a highly statistically significant result.
But the whole data set says we get exactly the same response in the treatment and control situations in 1,000,000 cases and a different response in only 25 cases. This is overwhelming evidence in favor of the null hypothesis (3.37) in Hollander and Wolfe. It is highly significant evidence against the tricky null hypothesis of the zero fudge test.
The moral of the story: In interpreting a test of significance it's not enough that P < 0.05. It's even more important what the null hypothesis is. Rejecting a null hypothesis of no scientific interest whatsoever is worthless.
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.
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.