ks.test: Kolmogorov-Smirnov and Smirnov Tests

Description Usage Arguments Details Value Source References See Also Examples

View source: R/ks.test.R

Description

Perform a Kolmogorov-Smirnov (one-sample) or Smirnov (two-sample) test.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
ks.test(x, ...)
## Default S3 method:
ks.test(x, y, ...,
        alternative = c("two.sided", "less", "greater"),
        exact = NULL)
## S3 method for class 'formula'
ks.test(formula, data, subset, na.action, ...)
psmirnov(q, n.x, n.y = length(obs) - n.x, obs = NULL, 
         two.sided = TRUE, exact = TRUE, lower.tail = TRUE, 
         log.p = FALSE)
qsmirnov(p, n.x, n.y, two.sided = TRUE, exact = TRUE, ...)

Arguments

x

a numeric vector of data values.

y

either a numeric vector of data values, or a character string naming a cumulative distribution function or an actual cumulative distribution function such as pnorm. Only continuous CDFs are valid.

...

parameters of the distribution specified (as a character string) by y for ks.test or arguments of psmirnov for qsmirnov.

alternative

indicates the alternative hypothesis and must be one of "two.sided" (default), "less", or "greater". You can specify just the initial letter of the value, but the argument name must be given in full. See ‘Details’ for the meanings of the possible values.

exact

NULL or a logical indicating whether an exact p-value should be computed. See ‘Details’ for the meaning of NULL.

formula

a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs either 1 for a one-sample Kolmogorov-Smirnov test or a factor with two levels giving the corresponding groups for a two-sample Smirnov test.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(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").

q

a numeric vector of quantiles.

p

a numeric vector of probabilities.

n.x

length of x-vector, sample size in the first group in the two-sample case.

n.y

length of y-vector, sample size in the second group in the two-sample case.

obs

a numeric vector of all data values (x and y) when the exact conditional distribution of the Smirnov test statistic given the data shall be computed.

two.sided

a logical indicating whether absolute (TRUE) or raw differences of frequencies define the test statistic.

lower.tail

a logical, if TRUE (default), probabilities are P[D < q], otherwise, P[D ≥ q].

log.p

a logical, if TRUE (default), probabilities are given as log-probabilities.

Details

If y is numeric, a two-sample Smirnov test of the null hypothesis that x and y were drawn from the same distribution is performed.

Alternatively, y can be a character string naming a continuous (cumulative) distribution function, or such a function. In this case, a one-sample Kolmogorov-Smirnov test is carried out of the null that the distribution function which generated x is distribution y with parameters specified by .... The presence of ties always generates a warning in the one-sided case, since continuous distributions do not generate them. If the ties arose from rounding the tests may be approximately valid, but even modest amounts of rounding can have a significant effect on the calculated statistic.

Missing values are silently omitted from x and (in the two-sample case) y.

The possible values "two.sided", "less" and "greater" of alternative specify the null hypothesis that the true distribution function of x is equal to, not less than or not greater than the hypothesized distribution function (one-sample case) or the distribution function of y (two-sample case), respectively. This is a comparison of cumulative distribution functions, and the test statistic is the maximum difference in value, with the statistic in the "greater" alternative being D^+ = max[F_x(u) - F_y(u)]. Thus in the two-sample case alternative = "greater" includes distributions for which x is stochastically smaller than y (the CDF of x lies above and hence to the left of that for y), in contrast to t.test or wilcox.test.

Exact p-values are not available for the one-sided case in the presence of ties. If exact = NULL (the default), an exact p-value is computed if the sample size is less than 100 in the one-sample case and there are no ties, and if the product of the sample sizes is less than 10000 in the two-sample case, with or without ties (using the algorithm described in Schröer and Trenkler, 1995). Otherwise, asymptotic distributions are used whose approximations may be inaccurate in small samples. In the one-sample two-sided case, exact p-values are obtained as described in Marsaglia, Tsang & Wang (2003) (but not using the optional approximation in the right tail, so this can be slow for small p-values). The formula of Birnbaum & Tingey (1951) is used for the one-sample one-sided case.

If a one-sample Kolmogorov-Smirnov test is used, the parameters specified in ... must be pre-specified and not estimated from the data. There is some more refined distribution theory for the KS test with estimated parameters (see Durbin, 1973), but that is not implemented in ks.test.

psmirnov and qsmirnov compute the distribution and quantile function of the Smirnov test for two samples.

Value

A list with class "htest" containing the following components:

statistic

the value of the test statistic.

p.value

the p-value of the test.

alternative

a character string describing the alternative hypothesis.

method

a character string indicating what type of test was performed.

data.name

a character string giving the name(s) of the data.

Source

The two-sided one-sample distribution comes via Marsaglia, Tsang and Wang (2003).

Exact distributions for the two-sample Smirnov test are computed by the algorithm proposed by Schröer (1991) and Schröer and Trenkler (1995).

References

Vance W. Berger and YanYan Zhou (2014). Kolmogorov–Smirnov Test: Overview. In Wiley StatsRef: Statistics Reference Online (eds N. Balakrishnan, T. Colton, B. Everitt, W. Piegorsch, F. Ruggeri and J.L. Teugels). doi: 10.1002/9781118445112.stat06558.

Zygmunt W. Birnbaum and Fred H. Tingey (1951). One-sided confidence contours for probability distribution functions. The Annals of Mathematical Statistics, 22/4, 592–596. doi: 10.1214/aoms/1177729550.

William J. Conover (1971). Practical Nonparametric Statistics. New York: John Wiley & Sons. Pages 295–301 (one-sample Kolmogorov test), 309–314 (two-sample Smirnov test).

James Durbin (1973). Distribution theory for tests based on the sample distribution function. SIAM.

George Marsaglia, Wai Wan Tsang and Jingbo Wang (2003). Evaluating Kolmogorov's distribution. Journal of Statistical Software, 8/18. doi: 10.18637/jss.v008.i18.

Gunar Schröer and Dietrich Trenkler (1995). Exact and Randomization Distributions of Kolmogorov-Smirnov Tests for Two or Three Samples. Computational Statistics & Data Analysis, 20(2), 185–202. doi: 10.1016/0167-9473(94)00040-P.

See Also

shapiro.test which performs the Shapiro-Wilk test for normality.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require("graphics")

x <- rnorm(50)
y <- runif(30)
# Do x and y come from the same distribution?
ks.test(x, y)
# Does x come from a shifted gamma distribution with shape 3 and rate 2?
ks.test(x+2, "pgamma", 3, 2) # two-sided, exact
ks.test(x+2, "pgamma", 3, 2, exact = FALSE)
ks.test(x+2, "pgamma", 3, 2, alternative = "gr")

# test if x is stochastically larger than x2
x2 <- rnorm(50, -1)
plot(ecdf(x), xlim = range(c(x, x2)))
plot(ecdf(x2), add = TRUE, lty = "dashed")
t.test(x, x2, alternative = "g")
wilcox.test(x, x2, alternative = "g")
ks.test(x, x2, alternative = "l")

# with ties, example from Schröer and Trenkler (1995)
# D = 3 / 7, p = 0.2424242
ks.test(c(1, 2, 2, 3, 3), c(1, 2, 3, 3, 4, 5, 6), exact = TRUE)

# formula interface, see ?wilcox.test
kst <- ks.test(Ozone ~ Month, data = airquality,
               subset = Month %in% c(5, 8))
# quantile-quantile plot + confidence bands
plot(confband(kst)) # => null hypothesis not plausible, 
                    # shift alternative not plausible

smirnov documentation built on Sept. 5, 2021, 3 p.m.

Related to ks.test in smirnov...