ContingencyTable.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* ContingencyTable.cpp
  2. *
  3. * Copyright (C) 1993-2018 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. #include "ContingencyTable.h"
  19. #include "TableOfReal_extensions.h"
  20. #include "NUM2.h"
  21. Thing_implement (ContingencyTable, TableOfReal, 0);
  22. #define TINY 1e-30
  23. void structContingencyTable :: v_info () {
  24. structDaata :: v_info ();
  25. double ndf;
  26. double h, hx, hy, hygx, hxgy, uygx, uxgy, uxy, chisq;
  27. ContingencyTable_getEntropies (this, & h, & hx, & hy, & hygx, & hxgy, & uygx, & uxgy, & uxy);
  28. ContingencyTable_chisq (this, & chisq, & ndf);
  29. MelderInfo_writeLine (U"Number of rows: ", numberOfRows);
  30. MelderInfo_writeLine (U"Number of columns: ", numberOfColumns);
  31. MelderInfo_writeLine (U"Entropies (y is row variable):");
  32. MelderInfo_writeLine (U" Total: ", h);
  33. MelderInfo_writeLine (U" Y: ", hy);
  34. MelderInfo_writeLine (U" X: ", hx);
  35. MelderInfo_writeLine (U" Y given x: ", hygx);
  36. MelderInfo_writeLine (U" X given y: ", hxgy);
  37. MelderInfo_writeLine (U" Dependency of y on x: ", uygx);
  38. MelderInfo_writeLine (U" Dependency of x on y: ", uxgy);
  39. MelderInfo_writeLine (U" Symmetrical dependency: ", uxy);
  40. MelderInfo_writeLine (U" Chi squared: ", chisq);
  41. MelderInfo_writeLine (U" Degrees of freedom: ", ndf);
  42. MelderInfo_writeLine (U" Probability: ", ContingencyTable_chisqProbability (this));
  43. }
  44. autoContingencyTable ContingencyTable_create (integer numberOfRows, integer numberOfColumns) {
  45. try {
  46. autoContingencyTable me = Thing_new (ContingencyTable);
  47. TableOfReal_init (me.get(), numberOfRows, numberOfColumns);
  48. return me;
  49. } catch (MelderError) {
  50. Melder_throw (U"ContingencyTable not created.");
  51. }
  52. }
  53. double ContingencyTable_chisqProbability (ContingencyTable me) {
  54. double chisq, df;
  55. ContingencyTable_chisq (me, & chisq, & df);
  56. if (chisq == 0.0 && df == 0.0) {
  57. return 0.0;
  58. }
  59. return NUMchiSquareQ (chisq, df);
  60. }
  61. double ContingencyTable_cramersStatistic (ContingencyTable me) {
  62. if (my numberOfRows == 1 || my numberOfColumns == 1) {
  63. return 0.0;
  64. }
  65. double sum = NUMsum (my data.get());
  66. integer nmin = my numberOfColumns < my numberOfRows ? my numberOfColumns : my numberOfRows;
  67. nmin --;
  68. double chisq, df;
  69. ContingencyTable_chisq (me, & chisq, & df);
  70. if (chisq == 0.0 && df == 0.0) {
  71. return 0.0;
  72. }
  73. return sqrt (chisq / (sum * nmin));
  74. }
  75. double ContingencyTable_contingencyCoefficient (ContingencyTable me) {
  76. double chisq, df, sum = NUMsum (my data.get());
  77. ContingencyTable_chisq (me, & chisq, & df);
  78. if (chisq == 0.0 && df == 0.0) {
  79. return 0.0;
  80. }
  81. return sqrt (chisq / (chisq + (double) sum));
  82. }
  83. void ContingencyTable_chisq (ContingencyTable me, double *out_chisq, double *out_df) {
  84. autoVEC rowSums = VECsumPerRow (my data.get());
  85. autoVEC columnSums = VECsumPerColumn (my data.get());
  86. double totalSum = NUMsum (my data.get());
  87. integer nrow = my numberOfRows, ncol = my numberOfColumns;
  88. for (integer irow = 1; irow <= my numberOfRows; irow ++)
  89. if (rowSums [irow] == 0.0)
  90. nrow --;
  91. if (nrow == 0) {
  92. if (out_chisq) *out_chisq = undefined;
  93. if (out_df) *out_df = undefined;
  94. return;
  95. }
  96. for (integer icol = 1; icol <= my numberOfColumns; icol ++)
  97. if (columnSums [icol] == 0.0)
  98. ncol --;
  99. if (out_df)
  100. *out_df = (nrow - 1.0) * (ncol - 1.0);
  101. if (out_chisq) {
  102. longdouble chisq = 0.0;
  103. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  104. if (rowSums [irow] > 0.0) {
  105. for (integer icol = 1; icol <= my numberOfColumns; icol ++) {
  106. if (columnSums [icol] > 0.0) {
  107. longdouble expected = rowSums [irow] * columnSums [icol] / totalSum;
  108. longdouble difference = my data [irow] [icol] - expected;
  109. chisq += difference * difference / expected;
  110. }
  111. }
  112. }
  113. }
  114. *out_chisq = (double) chisq;
  115. }
  116. }
  117. 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) {
  118. MAT_getEntropies (my data.get(), out_h, out_hx,
  119. out_hy, out_hygx, out_hxgy, out_uygx, out_uxgy, out_uxy);
  120. }
  121. autoContingencyTable Confusion_to_ContingencyTable (Confusion me) {
  122. try {
  123. autoContingencyTable thee = Thing_new (ContingencyTable);
  124. my structTableOfReal :: v_copy (thee.get());
  125. return thee;
  126. } catch (MelderError) {
  127. Melder_throw (me, U": not converted to ContingencyTable.");
  128. }
  129. }
  130. autoContingencyTable TableOfReal_to_ContingencyTable (TableOfReal me) {
  131. try {
  132. Melder_require (TableOfReal_checkNonNegativity (me), U"All values in the table should be positive.");
  133. autoContingencyTable thee = Thing_new (ContingencyTable);
  134. my structTableOfReal :: v_copy (thee.get());
  135. return thee;
  136. } catch (MelderError) {
  137. Melder_throw (me, U": not converted to ContingencyTable.");
  138. }
  139. }
  140. // End of file ContingencyTable.cpp