44 #include "../../App/ApplicationTools.h"
45 #include "../Random/ContingencyTableGenerator.h"
46 #include "../Random/RandomTools.h"
47 #include "../VectorTools.h"
57 margin1_(table.size()),
61 size_t n = table.size();
63 throw Exception(
"ContingencyTableTest. Table size should be at least 2x2!");
64 size_t m = table[0].size();
66 throw Exception(
"ContingencyTableTest. Table size should be at least 2x2!");
68 for (
size_t j = 0; j < m; ++j)
73 for (
size_t i = 0; i < n; ++i)
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)
79 size_t c = table[i][j];
86 for (
size_t i = 0; i < n; ++i)
91 for (
size_t j = 0; j < m; ++j)
99 df_ =
static_cast<double>((m - 1) * (n - 1));
102 for (
size_t i = 0; i < n; ++i)
104 for (
size_t j = 0; j < m; ++j)
106 long double c = table[i][j];
107 long double e =
static_cast<long double>(
margin1_[i] *
margin2_[j]) /
static_cast<long double>(tot);
109 statistic_ +=
static_cast<double>(std::pow(c - e, 2.L) / e);
113 if (nbPermutations > 0)
117 for (
unsigned int k = 0; k < nbPermutations; ++k)
123 for (
size_t i = 0; i < n; ++i)
125 for (
size_t j = 0; j < m; ++j)
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);
135 pvalue_ =
static_cast<double>(
count + 1) /
static_cast<double>(nbPermutations + 1);
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)....
std::string toString(T t)
General template method to convert to a string.
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.