Learn R Programming

randtests (version 1.0.1)

Runs: Distribution of the Wald Wolfowitz Runs Statistic

Description

Probability function, distribution function, quantile function and random generation for the distribution of the Runs statistic obtained from samples with \(n_1\) and \(n_2\) elements of each type.

Usage

druns(x, n1, n2, log = FALSE)
pruns(q, n1, n2, lower.tail = TRUE, log.p = FALSE)
qruns(p, n1, n2, lower.tail = TRUE, log.p = FALSE)
rruns(n, n1, n2)

Arguments

x, q

a numeric vector of quantiles.

p

a numeric vector of probabilities.

n

number of observations to return.

n1, n2

the number of elements of first and second type, respectively.

log, log.p

logical; if TRUE, probabilities p are given as log(p).

lower.tail

logical; if TRUE (default), probabilities are P[X \(\le\) x] otherwise, P[X > x].

Value

druns gives the probability function, pruns gives the distribution function and qruns gives the quantile function.

Details

The Runs distribution has probability function $$ P(R=r)= \left\{ \begin{array}{cc} \frac{2{n_1-1 \choose r/2-1}{n_2-1 \choose r/2-1}}{{n_1+n_2 \choose n_1}}, & \mbox{if } r \mbox{ is even}\\ \frac{{n_1-1 \choose (r-1)/2}{n_2-1 \choose (r-3)/2}\,+\,{n_1-1 \choose (r-3)/2}{n_2-1 \choose (r-1)/2}}{{n_1+n_2 \choose n_1}}, & \mbox{if } r \mbox{ is odd}\\ \end{array} \right. %\qquad r=2,3,\ldots, n_1+n_2. $$ for \(r=2,3,\ldots, 2\min(n_1+n_2)+c\) with \(c=0\) if \(n_1=n_2\) or \(c=1\) if \(n_1 \neq n_2\).

If an element of x is not integer, the result of druns is zero.

The quantile is defined as the smallest value \(x\) such that \(F(x) \ge p\), where \(F\) is the distribution function.

References

Swed, F.S. and Eisenhart, C. (1943). Tables for Testing Randomness of Grouping in a Sequence of Alternatives, Ann. Math Statist. 14(1), 66-87.

Examples

Run this code
# NOT RUN {
##
## Example: Distribution Function
## Creates Table I in Swed and Eisenhart (1943), p. 70,
## with n1 = 2 and n1 <= n2 <= 20
##
m <- NULL
for (i in 2:20){
  m <- rbind(m, pruns(2:5,2,i))  
}
rownames(m)=2:20
colnames(m)=2:5
#
#              2         3         4 5
# 2  0.333333333 0.6666667 1.0000000 1
# 3  0.200000000 0.5000000 0.9000000 1
# 4  0.133333333 0.4000000 0.8000000 1
# 5  0.095238095 0.3333333 0.7142857 1
# 6  0.071428571 0.2857143 0.6428571 1
# 7  0.055555556 0.2500000 0.5833333 1
# 8  0.044444444 0.2222222 0.5333333 1
# 9  0.036363636 0.2000000 0.4909091 1
# 10 0.030303030 0.1818182 0.4545455 1
# 11 0.025641026 0.1666667 0.4230769 1
# 12 0.021978022 0.1538462 0.3956044 1
# 13 0.019047619 0.1428571 0.3714286 1
# 14 0.016666667 0.1333333 0.3500000 1
# 15 0.014705882 0.1250000 0.3308824 1
# 16 0.013071895 0.1176471 0.3137255 1
# 17 0.011695906 0.1111111 0.2982456 1
# 18 0.010526316 0.1052632 0.2842105 1
# 19 0.009523810 0.1000000 0.2714286 1
# 20 0.008658009 0.0952381 0.2597403 1
# 
# }

Run the code above in your browser using DataLab