1
# Licensed to the Apache Software Foundation (ASF) under one or more
2
# contributor license agreements. See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License. You may obtain a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
16
#------------------------------------------------------------------------------
17
# R source file to validate ChiSquare tests in
18
# org.apache.commons.math.stat.inference.ChiSquareTestTest
20
# To run the test, install R, put this file and testFunctions
21
# into the same directory, launch R from this directory and then enter
22
# source("<name-of-this-file>")
25
#chisq.test(x, y = NULL, correct = TRUE,
26
# p = rep(1/length(x), length(x)),
27
# simulate.p.value = FALSE, B = 2000)
28
#------------------------------------------------------------------------------
29
tol <- 1E-9 # error tolerance for tests
30
#------------------------------------------------------------------------------
31
# Function definitions
33
source("testFunctions") # utility test functions
35
verifyTable <- function(counts, expectedP, expectedStat, tol, desc) {
36
results <- chisq.test(counts)
37
if (assertEquals(expectedP, results$p.value, tol, "p-value")) {
38
displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH)
40
displayPadded(c(desc, " p-value test"), FAILED, WIDTH)
42
if (assertEquals(expectedStat, results$statistic, tol,
43
"ChiSquare Statistic")) {
44
displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH)
46
displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH)
50
verifyHomogeneity <- function(obs, exp, expectedP, expectedStat,
52
results <- chisq.test(obs,p=exp,rescale.p=TRUE)
53
chi <- results$statistic
55
if (assertEquals(expectedP, p, tol, "p-value")) {
56
displayPadded(c(desc, " p-value test"), SUCCEEDED, WIDTH)
58
displayPadded(c(desc, " p-value test"), FAILED, WIDTH)
60
if (assertEquals(expectedStat, chi, tol,
61
"ChiSquare Statistic")) {
62
displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH)
64
displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH)
68
cat("ChiSquareTest test cases\n")
70
observed <- c(10, 9, 11)
71
expected <- c(10, 10, 10)
72
verifyHomogeneity(observed, expected, 0.904837418036, 0.2, tol,
75
observed <- c(500, 623, 72, 70, 31)
76
expected <- c(485, 541, 82, 61, 37)
77
verifyHomogeneity(observed, expected, 0.06051952647453607, 9.023307936427388,
78
tol, "testChiSquare2")
80
observed <- c(2372383, 584222, 257170, 17750155, 7903832, 489265,
82
expected <- c(3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78,
83
543628.0, 232921.0, 437665.75)
84
verifyHomogeneity(observed, expected, 0, 114875.90421929007, tol,
85
"testChiSquareLargeTestStatistic")
87
counts <- matrix(c(40, 22, 43, 91, 21, 28, 60, 10, 22), nc = 3);
88
verifyTable(counts, 0.000144751460134, 22.709027688, tol,
89
"testChiSquareIndependence1")
91
counts <- matrix(c(10, 15, 30, 40, 60, 90), nc = 3);
92
verifyTable(counts, 0.918987499852, 0.168965517241, tol,
93
"testChiSquareIndependence2")
95
counts <- matrix(c(40, 0, 4, 91, 1, 2, 60, 2, 0), nc = 3);
96
verifyTable(counts, 0.0462835770603, 9.67444662263, tol,
97
"testChiSquareZeroCount")