This example uses method of estimating model coefficientsdescribed in the Library Of Methods And Models section. Seasonal autoregression order and moving average order are to be specified for this method. Such settings are used by the ARIMA model in modeling container.
To execute the example, add a link to the Stat system assembly.
Sub UserProc;
Var
lr: ISmLinearRegress;
Y, X: array[43] Of double;
ARMA: ISlARMA;
Inits: Array[2] Of Double;
res: Integer;
CoefficientsAR, CoefficientsMA, CoefficientsX: ICoefficients;
ModelCoefficients: IModelCoefficients;
Sub Print(Arr: array Of Double);
Var
i: Integer;
Begin
For i := 0 To Arr.Length - 1 Do
Debug.WriteLine(" " + Arr[i].ToString);
End For;
End Sub Print;
Begin
lr := New SmLinearRegress.Create;
// explained series values
Y[00] := 6209; Y[15] := 8516; Y[30] := 14242;
Y[01] := 6385; Y[16] := 8941; Y[31] := 14704;
Y[02] := 6752; Y[17] := 9064; Y[32] := 13802;
Y[03] := 6837; Y[18] := 9380; Y[33] := 14197;
Y[04] := 6495; Y[19] := 9746; Y[34] := 15010;
Y[05] := 6907; Y[20] := 9907; Y[35] := 15589;
Y[06] := 7349; Y[21] := 10333; Y[36] := 15932;
Y[07] := 7213; Y[22] := 10863; Y[37] := 16631;
Y[08] := 7061; Y[23] := 11693; Y[38] := 17394;
Y[09] := 7180; Y[24] := 12242; Y[39] := 17758;
Y[10] := 7132; Y[25] := 12227; Y[40] := 17308;
Y[11] := 7137; Y[26] := 12910; Y[41] := 16444;
Y[12] := 7473; Y[27] := 13049; Y[42] := 16413;
Y[13] := 7722; Y[28] := 13384;
Y[14] := 8088; Y[29] := 14036;
// explanatory series values
X[00] := 4110; X[15] := 7486; X[30] := 11781;
X[01] := 4280; X[16] := 7832; X[31] := 11681;
X[02] := 4459; X[17] := 8153; X[32] := 11903;
X[03] := 4545; X[18] := 8468; X[33] := 11900;
X[04] := 4664; X[19] := 9054; X[34] := 11986;
X[05] := 4861; X[20] := 9499; X[35] := 12206;
X[06] := 5195; X[21] := 9866; X[36] := 12734;
X[07] := 5389; X[22] := 10217; X[37] := 12990;
X[08] := 5463; X[23] := 10763; X[38] := 13516;
X[09] := 5610; X[24] := 10683; X[39] := 13866;
X[10] := 5948; X[25] := 10494; X[40] := 14141;
X[11] := 6218; X[26] := 10938; X[41] := 14141;
X[12] := 6521; X[27] := 11198; X[42] := 14237;
X[13] := 6788; X[28] := 11546;
X[14] := 7222; X[29] := 11865;
// Sample period
lr.ModelPeriod.FirstPoint := 1;
lr.ModelPeriod.LastPoint := 20;
lr.Forecast.LastPoint := 40;
lr.MissingData.Method := MissingDataMethod.Casewise;
lr.Explained.Value := Y;
// exogenous variable is to be used in the model
lr.Explanatories.Clear;
lr.Explanatories.Add.Value := X;
ModelCoefficients := lr.ModelCoefficients;
// a constant is used in the model
ModelCoefficients.Intercept.Mode := InterceptMode.AutoEstimate;
ARMA := lr.ARMA;
// seasonal autoregression order
ARMA.ParseARSeas("3");
// seasonal moving average order
ARMA.ParseMASeas("1");
// Method of determining initial approximations
ARMA.CalcInitMode := ARMAInitType.Auto;
// initial approximations of seasonal autoregression
// seasonal/non-seasonal autoregression and seasonal/non-seasonal moving
Inits[0] := 0.0025;
Inits[1] := 0.0025;
ARMA.InitAR := Inits;
ARMA.InitMA := Inits;
ARMA.InitARSeas := Inits;
ARMA.InitMASeas := Inits;
// seasonal/non-seasonal difference
ARMA.DiffSeas := 0;
ARMA.Diff := 2;
// seasonality period
ARMA.PeriodSeas := 4;
// optimization method
ARMA.EstimationMethod := ARMAEstimationMethodType.GaussNewton;
//number of iterations for optimization method
ARMA.MaxIteration := 50;
// model calculation
res := lr.Execute;
Debug.WriteLine(lr.Errors);
If (res = 0) Then
// autoregression coefficients
Debug.WriteLine("----Seasonal autoregression coefficients' estimation----");
CoefficientsAR := ARMA.CoefficientsARSeas;
Debug.WriteLine(" Value:");
Print(CoefficientsAR.Estimate);
Debug.WriteLine("Standard error:");
Print(CoefficientsAR.StandardError);
Debug.WriteLine(" t-statistic:");
Print(CoefficientsAR.TStatistic);
Debug.WriteLine(" Probability:");
Print(CoefficientsAR.Probability);
//Moving average coefficients' estimates
Debug.WriteLine("----Seasonal moving average coefficients' estimation----");
CoefficientsMA := ARMA.CoefficientsMASeas;
Debug.WriteLine(" Value:");
Print(CoefficientsMA.Estimate);
Debug.WriteLine("Standard error:");
Print(CoefficientsMA.StandardError);
Debug.WriteLine(" t-statistic:");
Print(CoefficientsMA.TStatistic);
Debug.WriteLine(" Probability:");
Print(CoefficientsMA.Probability);
// exogenous variable coefficients
Debug.WriteLine("----Coefficient estimates X----");
CoefficientsX := ModelCoefficients.Coefficients;
Debug.WriteLine(" Value:");
Print(CoefficientsX.Estimate);
Debug.WriteLine("Standard error:");
Print(CoefficientsX.StandardError);
Debug.WriteLine(" t-statistic:");
Print(CoefficientsX.TStatistic);
Debug.WriteLine(" Probability:");
Print(CoefficientsX.Probability);
// explained series values
Debug.WriteLine("----Explained series----");
Print(lr.Explained.Value);
// Model series values
Debug.WriteLine("----Modeling series----");
Print(lr.Fitted);
// forecast series values
Debug.WriteLine("----Forecast series----");
Print(lr.Forecast.Value);
End If;
End Sub UserProc;
After executing the example a linear regression model with the following parameters is created:
Explained and explanatory variables;
Orders of seasonal autoregression and seasonal moving average;
Initial approximations of seasonal or non-seasonal autoregresion and seasonal or non-seasonal moving average;
Difference, seasonal difference, seasonality period;
The Quasi-Newton method is used as an optimization method.
The console window displays values of coefficients for seasonal autoregression, seasonal moving average, explanatory variable and values of explained, model and forecasting series:
No errors
----Seasonal autoregression coefficients' estimation----
Value:
0.43490707409749907
Standard error:
0.2734980430624076
t-statistic:
1.5901652137169431
Probability:
0.14010396559499899
----Estimates for seasonal moving average coefficients----
Value:
0.18484173929552522
Standard error:
0.34372507506364647
t-statistic:
0.53776041582447442
Probability:
0.60145259222766068
----Coefficients' estimates X----
Value:
-0.085392355517884433
Standard error:
0.11621187624989106
t-statistic:
-0.73479887145324774
Probability:
0.47783497431661037
----Explained series----
6209
6385
6752
6837
6495
6907
7349
7213
7061
7180
7132
7137
7473
7722
8088
8516
8941
9064
9380
9746
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
----Model series----
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
6387.2302929023381
6378.7277682049989
6285.405571028482
6543.5292131985507
6861.3182832802295
7059.6741634377986
7314.8511042611171
7739.3388404068264
8162.9940757614158
8608.6490097510541
9219.4043335455754
9757.2216352950072
10331.513511943327
10825.774150882695
11287.576127818098
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
----Forecast series----
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
11657.541261930295
11865.3597446065
11923.511224107375
11776.073777507245
11412.930110495705
10868.682212136939
10101.372242464256
9081.4270870175988
7787.15982313526
6189.5716843245982
4292.6298840343716
2108.3657950774118
-382.94251526214703
-3182.4330708414723
-6295.9308590365545
-9742.5548279478699
-13567.998489133301
-17793.461768225756
-22464.005707318353
-27609.781335809264
See also: