8 #include "../../App/ApplicationTools.h" 9 #include "../Random/ContingencyTableGenerator.h" 10 #include "../Random/RandomTools.h" 11 #include "../VectorTools.h" 21 margin1_(table.size()),
25 size_t n = table.size();
27 throw Exception(
"ContingencyTableTest. Table size should be at least 2x2!");
28 size_t m = table[0].size();
30 throw Exception(
"ContingencyTableTest. Table size should be at least 2x2!");
32 for (
size_t j = 0; j < m; ++j)
37 for (
size_t i = 0; i < n; ++i)
39 if (table[i].size() != m)
40 throw Exception(
"ContingencyTableTest. Input array has non-homogeneous dimensions!");
41 for (
size_t j = 0; j < m; ++j)
43 size_t c = table[i][j];
50 for (
size_t i = 0; i < n; ++i)
55 for (
size_t j = 0; j < m; ++j)
63 df_ =
static_cast<double>((m - 1) * (n - 1));
66 for (
size_t i = 0; i < n; ++i)
68 for (
size_t j = 0; j < m; ++j)
70 long double c = table[i][j];
71 long double e =
static_cast<long double>(
margin1_[i] *
margin2_[j]) / static_cast<long double>(tot);
73 statistic_ +=
static_cast<double>(std::pow(c - e, 2.L) / e);
77 if (nbPermutations > 0)
81 for (
unsigned int k = 0; k < nbPermutations; ++k)
87 for (
size_t i = 0; i < n; ++i)
89 for (
size_t j = 0; j < m; ++j)
91 long double c = table_rep(i, j);
92 long double e = expc(i, j);
93 stat_rep +=
static_cast<double>(std::pow(c - e, 2.L) / e);
99 pvalue_ =
static_cast<double>(count + 1) / static_cast<double>(nbPermutations + 1);
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
RowMatrix< size_t > rcont2()
std::vector< size_t > margin2_
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). Destructor is already virtual (from std::exception)
Generate a random contingency matrix with given marginal counts.
std::string toString(T t)
General template method to convert to a string.
std::vector< size_t > margin1_