toString method

  1. @override
String toString()
override

Returns a string representation of the polynomial regression model. return a string representation of the polynomial regression model, including the best-fit polynomial and the coefficient of determination R2

Implementation

@override
String toString() {
  var s = <String>[];
  var j = beta.row - 1;

  // ignoring leading zero coefficients
  while (j >= 0 && beta[j][0].abs() < 1E-5) {
    j--;
  }

  // create remaining terms
  while (j >= 0) {
    if (j == 0) {
      s.add('${beta[j][0]}');
    } else if (j == 1) {
      s.add('${beta[j][0]} $variableName + ');
    } else {
      s.add('${beta[j][0]} $variableName^$j + ');
    }
    j--;
  }
  s.add('  (R^2 = ${R2()})');

  // replace "+ -2n" with "- 2n"
  return s.join(' ').replaceAll('+ -', '- ');
}