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