| 1 | /* ContingencyTable.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "ContingencyTable.h" |
| 20 | #include "TableOfReal_extensions.h" |
| 21 | #include "NUM2.h" |
| 22 | |
| 23 | Thing_implement (ContingencyTable, TableOfReal, 0); |
| 24 | |
| 25 | #define TINY 1e-30 |
| 26 | |
| 27 | void structContingencyTable :: v1_info () { |
| 28 | structDaata :: v1_info (); |
| 29 | |
| 30 | double ndf; |
| 31 | double h, hx, hy, hygx, hxgy, uygx, uxgy, uxy, chisq; |
| 32 | ContingencyTable_getEntropies (this, & h, & hx, & hy, & hygx, & hxgy, & uygx, & uxgy, & uxy); |
| 33 | ContingencyTable_chisq (this, & chisq, & ndf); |
| 34 | |
| 35 | MelderInfo_writeLine (U"Number of rows: ", numberOfRows); |
| 36 | MelderInfo_writeLine (U"Number of columns: ", numberOfColumns); |
| 37 | MelderInfo_writeLine (U"Entropies (y is row variable):"); |
| 38 | MelderInfo_writeLine (U" Total: ", h); |
| 39 | MelderInfo_writeLine (U" Y: ", hy); |
| 40 | MelderInfo_writeLine (U" X: ", hx); |
| 41 | MelderInfo_writeLine (U" Y given x: ", hygx); |
| 42 | MelderInfo_writeLine (U" X given y: ", hxgy); |
| 43 | MelderInfo_writeLine (U" Dependency of y on x: ", uygx); |
| 44 | MelderInfo_writeLine (U" Dependency of x on y: ", uxgy); |
| 45 | MelderInfo_writeLine (U" Symmetrical dependency: ", uxy); |
| 46 | MelderInfo_writeLine (U" Chi squared: ", chisq); |
| 47 | MelderInfo_writeLine (U" Degrees of freedom: ", ndf); |
| 48 | MelderInfo_writeLine (U" Probability: ", ContingencyTable_chisqProbability (this)); |
| 49 | } |
| 50 | |
| 51 | autoContingencyTable ContingencyTable_create (integer numberOfRows, integer numberOfColumns) { |
| 52 | try { |
| 53 | autoContingencyTable me = Thing_new (ContingencyTable); |
| 54 | TableOfReal_init (me.get(), numberOfRows, numberOfColumns); |
| 55 | return me; |
| 56 | } catch (MelderError) { |
| 57 | Melder_throw (U"ContingencyTable not created."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | double ContingencyTable_chisqProbability (ContingencyTable me) { |
| 62 | double chisq, df; |
| 63 | ContingencyTable_chisq (me, & chisq, & df); |
| 64 | if (chisq == 0.0 && df == 0.0) |
| 65 | return 0.0; |
| 66 | return NUMchiSquareQ (chisq, df); |
| 67 | } |
| 68 | |
| 69 | double ContingencyTable_cramersStatistic (ContingencyTable me) { |
| 70 | if (my numberOfRows == 1 || my numberOfColumns == 1) |
| 71 | return 0.0; |
| 72 | |
| 73 | const double sum = NUMsum (my data.all()); |
| 74 | |
| 75 | integer nmin = std::min (my numberOfColumns, my numberOfRows); |
| 76 | |
| 77 | nmin --; |
| 78 | |
| 79 | double chisq, df; |
| 80 | ContingencyTable_chisq (me, & chisq, & df); |
| 81 | if (chisq == 0.0 && df == 0.0) |
| 82 | return 0.0; |
| 83 | return sqrt (chisq / (sum * nmin)); |
| 84 | } |
| 85 | |
| 86 | double ContingencyTable_contingencyCoefficient (ContingencyTable me) { |
| 87 | const double sum = NUMsum (my data.all()); |
| 88 | double chisq, df; |
| 89 | ContingencyTable_chisq (me, & chisq, & df); |
| 90 | if (chisq == 0.0 && df == 0.0) |
| 91 | return 0.0; |
| 92 | |
| 93 | return sqrt (chisq / (chisq + (double) sum)); |
| 94 | } |
| 95 | |
| 96 | void ContingencyTable_chisq (ContingencyTable me, double *out_chisq, double *out_df) { |
| 97 | |
| 98 | autoVEC rowSums = rowSums_VEC (my data.get()); |
| 99 | autoVEC columnSums = columnSums_VEC (my data.get()); |
| 100 | const double totalSum = NUMsum (my data.all()); |
| 101 | |
| 102 | integer nrow = my numberOfRows, ncol = my numberOfColumns; |
| 103 | |
| 104 | for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 105 | if (rowSums [irow] == 0.0) |
| 106 | nrow --; |
| 107 | |
| 108 | if (nrow == 0) { |
| 109 | if (out_chisq) |
| 110 | *out_chisq = undefined; |
| 111 | if (out_df) |
| 112 | *out_df = undefined; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 117 | if (columnSums [icol] == 0.0) |
| 118 | ncol --; |
| 119 | |
| 120 | if (out_df) |
| 121 | *out_df = (nrow - 1.0) * (ncol - 1.0); |
| 122 | if (out_chisq) { |
| 123 | longdouble chisq = 0.0; |
| 124 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 125 | if (rowSums [irow] > 0.0) { |
| 126 | for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 127 | if (columnSums [icol] > 0.0) { |
| 128 | const longdouble expected = rowSums [irow] * columnSums [icol] / totalSum; |
| 129 | const longdouble difference = my data [irow] [icol] - expected; |
| 130 | chisq += difference * difference / expected; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | *out_chisq = (double) chisq; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void ContingencyTable_getEntropies (ContingencyTable me, double *out_h, double *out_hx, double *out_hy, double *out_hygx, double *out_hxgy, double *out_uygx, double *out_uxgy, double *out_uxy) { |
| 140 | NUMgetEntropies (my data.get(), out_h, out_hx, |
| 141 | out_hy, out_hygx, out_hxgy, out_uygx, out_uxgy, out_uxy); |
| 142 | } |
| 143 | |
| 144 | autoContingencyTable Confusion_to_ContingencyTable (Confusion me) { |
| 145 | try { |
| 146 | autoContingencyTable thee = Thing_new (ContingencyTable); |
| 147 | my structTableOfReal :: v1_copy (thee.get()); |
| 148 | return thee; |
| 149 | } catch (MelderError) { |
| 150 | Melder_throw (me, U": not converted to ContingencyTable."); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | autoContingencyTable TableOfReal_to_ContingencyTable (TableOfReal me) { |
| 155 | try { |
| 156 | Melder_require (TableOfReal_isNonNegative (me), |
| 157 | U"No cell in the table should be negative."); |
| 158 | autoContingencyTable thee = Thing_new (ContingencyTable); |
| 159 | my structTableOfReal :: v1_copy (thee.get()); |
| 160 | return thee; |
| 161 | } catch (MelderError) { |
| 162 | Melder_throw (me, U": not converted to ContingencyTable."); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // End of file ContingencyTable.cpp |