CoefficientsOrder: String;
The CoefficientsOrder property determines the coefficients order.
If there are several coefficients, specify them divided by the semicolon. For example: A1;A2;A3;A4.
To execute the example, add a link to the Stat system assembly.
Sub UserProc;
Var
NLS: SmNonLinearLeastSquare;
can, fra: Array[43] Of Double;
expl: INonLinearRegressionExplanatory;
InitEst: Array[2] Of Double;
res, i: Integer;
MC: ISlConstCoefficients;
coef: String;
SumStat: ISummaryStatistics;
Begin
// Create a method
NLS := New SmNonLinearLeastSquare.Create;
// Specify value of the can variable
can[00] := 6209; can[11] := Double.Nan; can[22] := 10863; can[33] := 14197;
can[01] := 6385; can[12] := Double.Nan; can[23] := 11693; can[34] := 15010;
can[02] := 6752; can[13] := 7722; can[24] := 12242; can[35] := Double.Nan;
can[03] := 6837; can[14] := 8088; can[25] := 12227; can[36] := Double.Nan;
can[04] := 6495; can[15] := 8516; can[26] := 12910; can[37] := Double.Nan;
can[05] := 6907; can[16] := 8941; can[27] := 13049; can[38] := 17394;
can[06] := 7349; can[17] := 9064; can[28] := 13384; can[39] := 17758;
can[07] := 7213; can[18] := 9380; can[29] := 14036; can[40] := 17308;
can[08] := 7061; can[19] := 9746; can[30] := 14242; can[41] := 16444;
can[09] := 7180; can[20] := 9907; can[31] := 14704; can[42] := 16413;
can[10] := Double.Nan; can[21] := 10333; can[32] := 13802;
// Set value of the "fra" variable
fra[00] := 4110; fra[11] := 6218; fra[22] := 10217; fra[33] := 11900;
fra[01] := 4280; fra[12] := 6521; fra[23] := 10763; fra[34] := 11986;
fra[02] := 4459; fra[13] := 6788; fra[24] := 10683; fra[35] := 12206;
fra[03] := 4545; fra[14] := 7222; fra[25] := 10494; fra[36] := 12734;
fra[04] := 4664; fra[15] := 7486; fra[26] := 10938; fra[37] := Double.Nan;
fra[05] := 4861; fra[16] := 7832; fra[27] := 11198; fra[38] := Double.Nan;
fra[06] := 5195; fra[17] := 8153; fra[28] := 11546; fra[39] := Double.Nan;
fra[07] := 5389; fra[18] := 8468; fra[29] := 11865; fra[40] := 14141;
fra[08] := 5463; fra[19] := 9054; fra[30] := 11781; fra[41] := 14141;
fra[09] := 5610; fra[20] := 9499; fra[31] := 11681; fra[42] := 14237;
fra[10] := 5948; fra[21] := 9866; fra[32] := 11903;
// Set calculation periods
NLS.ModelPeriod.FirstPoint := 1;
NLS.ModelPeriod.LastPoint := 30;
NLS.Forecast.LastPoint := 43;
// Set explained variable
NLS.Explained.Value := can;
// Set explanatory variable
expl := NLS.Explanatories.Add;
expl.Serie.Value := fra;
expl.VariableName := "fra";
// Set function
NLS.FunctionString := "A1 + A2 * fra";
// Set coefficients order
NLS.CoefficientsOrder := "A1;A2";
// Set values of primary approximations
InitEst[0] := 0.02; InitEst[1] := 0.02;
NLS.InitApproximation := InitEst;
// Primary values are not used by default
NLS.UseDefaultInitValues := False;
// Set tolerance and maximum number of iterations
NLS.Tolerance := 0.001;
NLS.MaxIteration := 600;
// Set optimization method
NLS.OptimizationMethod := NLSOptimizationMethod.LevenbergMarquardt;
// Specify missing data treatment method
NLS.MissingData.Method := MissingDataMethod.SampleAverage;
// Do not use derivatives on calculation
NLS.UseDerivatives := False;
// Take into account that coefficients are found approximately
NLS.Forecast.CoefUncertaintyInSECalc := True;
// Run calculation and output the results to console window
res := NLS.Execute;
Debug.WriteLine(NLS.Errors);
If (res = 0) Then
Debug.WriteLine("Coefficient estimations:");
Debug.Indent; coef := "A1";
For i := 0 To 1 Do
MC := NLS.ModelCoefficients(coef);
Debug.WriteLine(coef + ":");
Debug.WriteLine(" - value: " + MC.Estimate.ToString);
Debug.WriteLine(" - standard error: " + MC.StandardError.ToString);
Debug.WriteLine(" - t-statistic: " + MC.TStatistic.ToString);
Debug.WriteLine(" - probability: " + MC.Probability.ToString);
coef := "A2";
End For;
Debug.Unindent; Debug.WriteLine("");
Debug.Write("Optimal value of sum of squared residuals: ");
Debug.WriteLine(NLS.OptimalSumOfSquare);
Debug.WriteLine(""); Debug.WriteLine("Summary statistics:");
SumStat := NLS.SummaryStatistics;
Debug.WriteLine(" - determination coefficient: " + SumStat.R2.ToString);
Debug.WriteLine(" - Fisher statistic: " + SumStat.Fstat.ToString);
Debug.WriteLine(" - sum of squared residuals:" + SumStat.SSR.ToString);
Debug.WriteLine(" - number of iterations:" + SumStat.NumOfIter.ToString);
Debug.WriteLine(""); Debug.WriteLine("Explanatory series:");
Print(NLS.Explained.Value);
Debug.WriteLine(""); Debug.WriteLine("Modeling series:");
Print(NLS.Fitted);
Debug.WriteLine(""); Debug.WriteLine("Residual series:");
Print(NLS.Residuals);
Debug.WriteLine(""); Debug.WriteLine("Forecasting series:");
Print(NLS.Forecast.Value);
Debug.WriteLine(""); Debug.WriteLine("Lower confidence limit");
Print(NLS.Forecast.LowerConfidenceLevel);
Debug.WriteLine(""); Debug.WriteLine("Upper confidence limit");
Print(NLS.Forecast.UpperConfidenceLevel);
Debug.WriteLine(""); Debug.WriteLine("Actually used initial approximations");
Print(NLS.InitApproximationActual);
End If;
End Sub UserProc;
// Data output procedure
Sub Print(Data: Array Of Double);
Var
i: Integer;
Begin
Debug.Indent;
For i := 0 To Data.Length - 1 Do
Debug.WriteLine(i.ToString + " " + Data[i].ToString);
End For;
Debug.Unindent;
End Sub Print;
After executing the example the non-linear regression is calculated using the least squares method, the calculation results are displayed to the console window.
See also: