bpp-core  2.2.0
ContingencyTableTest.cpp
Go to the documentation of this file.
1 //
2 // File: ContingencyTableTest.cpp
3 // Created by: Julien Dutheil
4 // Created on: Thu Dec 09 14:20 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for numerical calculus.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "ContingencyTableTest.h"
41 #include "../Random/ContingencyTableGenerator.h"
42 
43 #include "../../App/ApplicationTools.h"
44 #include "../VectorTools.h"
45 #include "../Random/RandomTools.h"
46 
47 #include <iostream>
48 #include <algorithm>
49 
50 using namespace bpp;
51 using namespace std;
52 
53 ContingencyTableTest::ContingencyTableTest(const std::vector< std::vector<size_t> >& table, unsigned int nbPermutations, bool warn):
54  statistic_(0),
55  pvalue_(0),
56  df_(0),
57  margin1_(table.size()),
58  margin2_(0)
59 {
60  //Compute marginals:
61  size_t n = table.size();
62  if (n < 2)
63  throw Exception("ContingencyTableTest. Table size should be at least 2x2!");
64  size_t m = table[0].size();
65  if (m < 2)
66  throw Exception("ContingencyTableTest. Table size should be at least 2x2!");
67  margin2_.resize(m);
68  for (size_t j = 0; j < m; ++j)
69  margin2_[j] = 0;
70  bool test = false;
71  for (size_t i = 0; i < n; ++i) {
72  if (table[i].size() != m)
73  throw Exception("ContingencyTableTest. Input array has non-homogeneous dimensions!");
74  for (size_t j = 0; j < m; ++j) {
75  size_t c = table[i][j];
76  if (c <= 5) test = true;
77  margin1_[i] += c;
78  margin2_[j] += c;
79  }
80  }
81  for (size_t i = 0; i < n; ++i)
82  if (margin1_[i] == 0)
83  throw Exception("ContingencyTableTest. Row " + TextTools::toString(i) + " sums to 0.");
84  for (size_t j = 0; j < m; ++j)
85  if (margin2_[j] == 0)
86  throw Exception("ContingencyTableTest. Column " + TextTools::toString(j) + " sums to 0.");
87 
88 
89  size_t tot = VectorTools::sum(margin1_);
90  df_ = static_cast<double>((m - 1) * (n - 1));
91 
92  RowMatrix<long double> expc(n, m);
93  for (size_t i = 0; i < n; ++i) {
94  for (size_t j = 0; j < m; ++j) {
95  long double c = table[i][j];
96  long double e = static_cast<long double>(margin1_[i] * margin2_[j]) / static_cast<long double>(tot);
97  expc(i, j) = e;
98  statistic_ += static_cast<double>(std::pow(c - e, 2.L) / e);
99  }
100  }
101 
102  if (nbPermutations > 0) {
103  size_t count = 0;
105  for (unsigned int k = 0; k < nbPermutations; ++k) {
106  //Randomize:
107  RowMatrix<size_t> table_rep = ctgen.rcont2();
108  //Recompute statistic:
109  double stat_rep = 0;
110  for (size_t i = 0; i < n; ++i) {
111  for (size_t j = 0; j < m; ++j) {
112  long double c = table_rep(i , j);
113  long double e = expc(i, j);
114  stat_rep += static_cast<double>(std::pow(c - e, 2.L) / e);
115  }
116  }
117  if (stat_rep >= statistic_)
118  count++;
119  }
120  pvalue_ = static_cast<double>(count + 1) / static_cast<double>(nbPermutations + 1);
121  } else {
122  if (test && warn)
123  ApplicationTools::displayWarning("Unsufficient observations, p-value might be incorrect.");
124 
125  //Compute p-value:
127  }
128 }
129 
static void displayWarning(const std::string &text)
Print a warning message.
static double pChisq(double x, double v)
cumulative probability function.
Definition: RandomTools.h:430
This class allows to perform a correspondence analysis.
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:614
STL namespace.
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
std::vector< size_t > margin2_
Matrix storage by row.
Definition: Matrix.h:129
RowMatrix< size_t > rcont2(const RandomFactory &generator= *RandomTools::DEFAULT_GENERATOR)
Exception base class.
Definition: Exceptions.h:57
Generate a random contingency matrix with given marginal counts.
std::vector< size_t > margin1_
ContingencyTableTest(const std::vector< std::vector< size_t > > &table, unsigned int nbPermutations=0, bool warn=true)
Build a new test object and perform computations.