Regression.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Regression.cpp
  2. *
  3. * Copyright (C) 2005-2011,2014,2015,2016,2017 Paul Boersma
  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.
  13. * See the GNU 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 "Regression.h"
  19. #include "NUM2.h"
  20. #include "oo_DESTROY.h"
  21. #include "Regression_def.h"
  22. #include "oo_COPY.h"
  23. #include "Regression_def.h"
  24. #include "oo_EQUAL.h"
  25. #include "Regression_def.h"
  26. #include "oo_CAN_WRITE_AS_ENCODING.h"
  27. #include "Regression_def.h"
  28. #include "oo_WRITE_TEXT.h"
  29. #include "Regression_def.h"
  30. #include "oo_WRITE_BINARY.h"
  31. #include "Regression_def.h"
  32. #include "oo_READ_TEXT.h"
  33. #include "Regression_def.h"
  34. #include "oo_READ_BINARY.h"
  35. #include "Regression_def.h"
  36. #include "oo_DESCRIPTION.h"
  37. #include "Regression_def.h"
  38. Thing_implement (RegressionParameter, Daata, 0);
  39. void structRegression :: v_info () {
  40. Regression_Parent :: v_info ();
  41. MelderInfo_writeLine (U"Factors:");
  42. MelderInfo_writeLine (U" Number of factors: ", our parameters.size);
  43. for (integer ivar = 1; ivar <= our parameters.size; ivar ++) {
  44. RegressionParameter parm = our parameters.at [ivar];
  45. MelderInfo_writeLine (U" Factor ", ivar, U": ", parm -> label.get());
  46. }
  47. MelderInfo_writeLine (U"Fitted coefficients:");
  48. MelderInfo_writeLine (U" Intercept: ", intercept);
  49. for (integer ivar = 1; ivar <= our parameters.size; ivar ++) {
  50. RegressionParameter parm = our parameters.at [ivar];
  51. MelderInfo_writeLine (U" Coefficient of factor ", parm -> label.get(), U": ", parm -> value);
  52. }
  53. MelderInfo_writeLine (U"Ranges of values:");
  54. for (integer ivar = 1; ivar <= our parameters.size; ivar ++) {
  55. RegressionParameter parm = our parameters.at [ivar];
  56. MelderInfo_writeLine (U" Range of factor ", parm -> label.get(), U": minimum ",
  57. parm -> minimum, U", maximum ", parm -> maximum);
  58. }
  59. }
  60. Thing_implement (Regression, Daata, 0);
  61. void Regression_init (Regression me) {
  62. //my parameters = Ordered_create ();
  63. }
  64. void Regression_addParameter (Regression me, conststring32 label, double minimum, double maximum, double value) {
  65. try {
  66. autoRegressionParameter thee = Thing_new (RegressionParameter);
  67. thy label = Melder_dup (label);
  68. thy minimum = minimum;
  69. thy maximum = maximum;
  70. thy value = value;
  71. my parameters.addItem_move (thee.move());
  72. } catch (MelderError) {
  73. Melder_throw (me, U": parameter not added.");
  74. }
  75. }
  76. integer Regression_getFactorIndexFromFactorName_e (Regression me, conststring32 factorName) {
  77. for (integer iparm = 1; iparm <= my parameters.size; iparm ++) {
  78. RegressionParameter parm = my parameters.at [iparm];
  79. if (Melder_equ (factorName, parm -> label.get())) return iparm;
  80. }
  81. Melder_throw (me, U" has no parameter named \"", factorName, U"\".");
  82. }
  83. Thing_implement (LinearRegression, Regression, 0);
  84. autoLinearRegression LinearRegression_create () {
  85. try {
  86. autoLinearRegression me = Thing_new (LinearRegression);
  87. Regression_init (me.get());
  88. return me;
  89. } catch (MelderError) {
  90. Melder_throw (U"LinearRegression not created.");
  91. }
  92. }
  93. autoLinearRegression Table_to_LinearRegression (Table me) {
  94. try {
  95. integer numberOfIndependentVariables = my numberOfColumns - 1, numberOfParameters = my numberOfColumns;
  96. if (numberOfParameters < 1) // includes intercept
  97. Melder_throw (U"Not enough columns (has to be more than 1).");
  98. integer numberOfCells = my rows.size;
  99. if (numberOfCells == 0)
  100. Melder_throw (U"Not enough rows (0).");
  101. if (numberOfCells < numberOfParameters) {
  102. Melder_warning (U"Solution is not unique (more parameters than cases).");
  103. }
  104. autoMAT u = MATraw (numberOfCells, numberOfParameters);
  105. autoVEC b = VECraw (numberOfCells);
  106. autoLinearRegression thee = LinearRegression_create ();
  107. for (integer ivar = 1; ivar <= numberOfIndependentVariables; ivar ++) {
  108. double minimum = Table_getMinimum (me, ivar);
  109. double maximum = Table_getMaximum (me, ivar);
  110. Regression_addParameter (thee.get(), my columnHeaders [ivar]. label.get(), minimum, maximum, 0.0);
  111. }
  112. for (integer icell = 1; icell <= numberOfCells; icell ++) {
  113. for (integer ivar = 1; ivar < numberOfParameters; ivar ++) {
  114. u [icell] [ivar] = Table_getNumericValue_Assert (me, icell, ivar);
  115. }
  116. u [icell] [numberOfParameters] = 1.0; // for the intercept
  117. b [icell] = Table_getNumericValue_Assert (me, icell, my numberOfColumns); // the dependent variable
  118. }
  119. autoVEC x = NUMsolveEquation (u.get(), b.get(), NUMeps * numberOfCells);
  120. thy intercept = x [numberOfParameters];
  121. for (integer ivar = 1; ivar <= numberOfIndependentVariables; ivar ++) {
  122. RegressionParameter parm = thy parameters.at [ivar];
  123. parm -> value = x [ivar];
  124. }
  125. return thee;
  126. } catch (MelderError) {
  127. Melder_throw (me, U": linear regression not performed.");
  128. }
  129. }
  130. /* End of file Regression.cpp */