41 #include "../Random/ContingencyTableGenerator.h" 43 #include "../../App/ApplicationTools.h" 44 #include "../VectorTools.h" 45 #include "../Random/RandomTools.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)
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;
81 for (
size_t i = 0; i < n; ++i)
84 for (
size_t j = 0; j < m; ++j)
90 df_ =
static_cast<double>((m - 1) * (n - 1));
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);
98 statistic_ +=
static_cast<double>(std::pow(c - e, 2.L) / e);
102 if (nbPermutations > 0) {
105 for (
unsigned int k = 0; k < nbPermutations; ++k) {
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);
120 pvalue_ =
static_cast<double>(count + 1) / static_cast<double>(nbPermutations + 1);
This class allows to perform a correspondence analysis.
static std::string toString(T t)
General template method to convert to a string.
std::vector< size_t > margin2_
RowMatrix< size_t > rcont2(const RandomFactory &generator= *RandomTools::DEFAULT_GENERATOR)
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.