File: unif.test.statistic.R

package info (click to toggle)
r-cran-dicedesign 1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 516 kB
  • sloc: ansic: 237; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,781 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
unif.test.statistic <- function(x, type, transform=NULL) {


if (!is.null(transform)) {
	if (transform=="spacings") {
		ordered.sample <- sort(x)
		ordered.sample <- c(0, ordered.sample, 1)
		spacings <- diff(ordered.sample)
		ordered.spacings <- sort(spacings)
		n <- length(x)
		S0 <- (n+1)*ordered.spacings[1]
		S <- (n+1-(1:n))*diff(ordered.spacings)
		V <- cumsum(c(S0, S))
		x <- V
	}
}

if (type=="greenwood") {
	
	ordered.sample <- sort(x)
	ordered.sample <- c(0, ordered.sample, 1)
	spacings <- diff(ordered.sample)
	stat <- sum(spacings^2)
	
} else if (type=="spacings.max") {
	
	ordered.sample <- sort(x)
	ordered.sample <- c(0, ordered.sample, 1)
	spacings <- diff(ordered.sample)
	n <- length(x)
	stat <- max(abs(spacings-1/n))
}

else if (type=="qm") {    # Quesenberry-Miller

	ordered.sample <- sort(x)
	n.sample <- length(x)
	ordered.sample <- c(0, ordered.sample, 1)
	spacings <- diff(ordered.sample)
	stat <- sum(spacings^2)+sum(spacings[1:n.sample]*spacings[2:(n.sample+1)])

} else if (type=="ks") {

	ordered.sample <- sort(x)
	n.sample <- length(x)
	Dplus <- max((1:n.sample)/n.sample - ordered.sample)
	Dminus <- max(ordered.sample - (0:(n.sample-1))/n.sample)
	stat <- max(Dplus, Dminus)

} else if (type=="V") {

	ordered.sample <- sort(x)
	n.sample <- length(x)
	Dplus <- max((1:n.sample)/n.sample - ordered.sample)
	Dminus <- max(ordered.sample - (0:(n.sample-1))/n.sample)
	stat <- Dplus + Dminus

} else if (type=="cvm") {

	ordered.sample <- sort(x)
	n.sample <- length(x)
	stat <- sum((ordered.sample - (seq(from = 1, to = (2 * n.sample - 1), by = 2)/(2 * n.sample)))^2) + 1/(12 * n.sample)

} else stop("The goodness-of-fit test must be one of: Greenwood, Quesenberry-Miller, Kolmogorov-Smirnov, V=D+ +  D-, or Cramer-Von Mises")


return(stat)
}