~ubuntu-branches/ubuntu/trusty/commons-math/trusty

« back to all changes in this revision

Viewing changes to src/test/R/chiSquareTestCases

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-03-15 20:20:21 UTC
  • Revision ID: james.westby@ubuntu.com-20090315202021-zto3nmvqgcf3ami4
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
#
 
8
#      http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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.
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
# R source file to validate ChiSquare tests in
 
18
# org.apache.commons.math.stat.inference.ChiSquareTestTest
 
19
#
 
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>")
 
23
#
 
24
# R functions used
 
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
 
32
 
 
33
source("testFunctions")           # utility test functions
 
34
 
 
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)
 
39
    } else {
 
40
        displayPadded(c(desc, " p-value test"), FAILED, WIDTH)
 
41
    } 
 
42
    if (assertEquals(expectedStat, results$statistic, tol,
 
43
       "ChiSquare Statistic")) {
 
44
        displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH)
 
45
    } else {
 
46
        displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH)
 
47
    } 
 
48
}
 
49
 
 
50
verifyHomogeneity <- function(obs, exp, expectedP, expectedStat, 
 
51
  tol, desc) {
 
52
    results <- chisq.test(obs,p=exp,rescale.p=TRUE)
 
53
    chi <- results$statistic
 
54
    p <- results$p.value
 
55
    if (assertEquals(expectedP, p, tol, "p-value")) {
 
56
        displayPadded(c(desc, " p-value test"), SUCCEEDED, WIDTH)
 
57
    } else {
 
58
        displayPadded(c(desc, " p-value test"), FAILED, WIDTH)
 
59
    } 
 
60
    if (assertEquals(expectedStat, chi, tol,
 
61
       "ChiSquare Statistic")) {
 
62
        displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH)
 
63
    } else {
 
64
        displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH)
 
65
    }    
 
66
}
 
67
 
 
68
cat("ChiSquareTest test cases\n")
 
69
 
 
70
observed <- c(10, 9, 11)
 
71
expected <- c(10, 10, 10)
 
72
verifyHomogeneity(observed, expected, 0.904837418036, 0.2, tol,
 
73
   "testChiSquare1")
 
74
 
 
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")
 
79
 
 
80
observed <- c(2372383, 584222, 257170, 17750155, 7903832, 489265,
 
81
              209628, 393899)
 
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")
 
86
 
 
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")
 
90
 
 
91
counts <- matrix(c(10, 15, 30, 40, 60, 90), nc = 3);
 
92
verifyTable(counts, 0.918987499852, 0.168965517241, tol,  
 
93
   "testChiSquareIndependence2")
 
94
 
 
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")
 
98
 
 
99
displayDashes(WIDTH)
 
100
            
 
101