Examples - pi.science.regression.PIPolynomialRegression
1. How to compute polynomial regression, degree 2, for X and Y values ?
/* change decimal places count in formulas */ PIConfiguration.REGRESSION_DECIMAL_PLACES = 6; /* - prepare X data for regression */ PIVariable X = new PIVariable(); X.AddMoreValues( 50.0, 3 ); X.AddMoreValues( 70.0, 3 ); X.AddMoreValues( 80.0, 3 ); X.AddMoreValues( 90.0, 3 ); X.AddMoreValues( 100.0, 3 ); /* - prepare Y data for regression */ PIVariable Y = new PIVariable(); Y.AddValues( new double[] { 3.3, 2.8, 2.9, 2.3, 2.6, 2.1, 2.5, 2.9, 2.4, 3.0, 3.1, 2.8, 3.3,3.5,3.0 } ); /* - create and compute regression */ PIPolynomialRegression polynomialRegression = new PIPolynomialRegression( X, Y, 2 ); /* show degree */ Console.WriteLine( "degree = " + polynomialRegression.Get_degree() ); /* calculate */ polynomialRegression.Calc(); /* - show results */ Console.WriteLine( polynomialRegression.GetTextFormula() ); Console.WriteLine( polynomialRegression.GetTextFormulaFilled() ); PIDebug.Blank(); Console.WriteLine( polynomialRegression.Get_coefficients().AsString( 3 ) ); /* - calc prediction for X = 80 */ PIDebug.Blank(); Console.WriteLine( "Prediction for X=80 : " + polynomialRegression.CalcPredictedY( 80.0 ) ); PIDebug.Blank(); Console.WriteLine( "Prediction errors:" ); Console.WriteLine( polynomialRegression.GetErrors().AsString( 5 ) );
Output:
degree = 2 y = A + Bx + Cx^2 y = 7.960481 - 0.153711x + 0.001076x^2 7.960;-0.154;0.001 Prediction for X=80 : 2.5474226804128497 Prediction errors: 0.33608;-0.16392;-0.06392;-0.17113;0.12887;-0.37113;-0.04742;0.35258;-0.14742;0.16117; 0.26117;-0.03883;-0.04536;0.15464;-0.34536