Instrumental: ISlSeries;
Instrumental: Prognoz.Platform.Interop.Stat.ISlSeries;
The Instrumental property determines instrumental variables.
To determine whether the constant is used as an instrumental variable, use the ISm2SLS.UseConstantAsInstrument property.
This example shows how to set parameters and calculate linear regression (estimate using instrumental variables method) for the data series determined by the real array "can". Explanatory series are determined by the real arrays "fra" and "ger". Instrumental series are set with the real arrays jpn and uk.
To execute the example, add a link to the Stat system assembly.
Sub UserProc;
Var
TwoSLS: Sm2SLS;
can, fra, ger, jpn, uk: Array[15] Of Double;
C: IIntercept;
MC: ICoefficients;
X: ISlSerie;
res,i: Integer;
Begin
// Set values for variables
can[00] := Double.Nan; fra[00] := 4110; ger[00] := 3415; jpn[00] := 1475; uk[00] := 5320;
can[01] := 6385; fra[01] := 4280; ger[01] := 3673; jpn[01] := 1649; uk[01] := 5484;
can[02] := 6752; fra[02] := 4459; ger[02] := 4013; jpn[02] := Double.Nan; uk[02] := 5517;
can[03] := 6837; fra[03] := 4545; ger[03] := 4278; jpn[03] := 1884; uk[03] := 5791;
can[04] := 6495; fra[04] := 4664; ger[04] := 4577; jpn[04] := 1972; uk[04] := 5971;
can[05] := 6907; fra[05] := Double.Nan; ger[05] := 5135; jpn[05] := 2108; uk[05] := Double.Nan;
can[06] := 7349; fra[06] := 5195; ger[06] := 5388; jpn[06] := 2249; uk[06] := 6238;
can[07] := 7213; fra[07] := 5389; ger[07] := 5610; jpn[07] := 2394; uk[07] := 6322;
can[08] := 7061; fra[08] := 5463; ger[08] := Double.Nan; jpn[08] := 2505; uk[08] := 6340;
can[09] := 7180; fra[09] := 5610; ger[09] := 6181; jpn[09] := 2714; uk[09] := 6569;
can[10] := 7132; fra[10] := 5948; ger[10] := 6633; jpn[10] := 3052; uk[10] := 6813;
can[11] := Double.Nan; fra[11] := 6218; ger[11] := 6910; jpn[11] := 3453; uk[11] := 6974;
can[12] := 7473; fra[12] := 6521; ger[12] := 7146; jpn[12] := 3666; uk[12] := 6994;
can[13] := 7722; fra[13] := 6788; ger[13] := 7248; jpn[13] := 4008; uk[13] := 7220;
can[14] := 8088; fra[14] := 7222; ger[14] := 7689; jpn[14] := 4486; uk[14] := Double.Nan;
// Create model
TwoSLS := New Sm2SLS.Create;
// Set sample period parameters
TwoSLS.ModelPeriod.FirstPoint := 1;
TwoSLS.ModelPeriod.LastPoint := 10;
// Set forecast period parameters
TwoSLS.Forecast.LastPoint := 15;
// Use auto estimation of constant value
C:=TwoSLS.ModelCoefficients.Intercept;
C.Mode := InterceptMode.AutoEstimate;
// Set explained variable
TwoSLS.Explained.Value := can;
// Set explanatory variables
X:=TwoSLS.Explanatories.Add;
X.Value := fra;
X.Name:= "fra";
X:=TwoSLS.Explanatories.Add;
X.Value := ger;
X.Name:= "ger";
// Use constants in instrumental variables
TwoSLS.UseConstantAsInstrument := True;
// Set instrumental variables
TwoSLS.Instrumental.Add.Value := jpn;
TwoSLS.Instrumental.Add.Value := uk;
// Set missing data treatment method
TwoSLS.MissingData.Method := MissingDataMethod.AnyValue;
// Run calculation and show results
res := TwoSLS.Execute;
If res <> 0 Then
Debug.WriteLine(TwoSLS.Errors);
Else
Debug.WriteLine("=== Model coefficients ===");
Debug.WriteLine("Constant: " + C.Estimate.ToString);
MC := TwoSLS.ModelCoefficients.Coefficients;
For i := 0 To MC.Estimate.Length - 1 Do
Debug.WriteLine(TwoSLS.Explanatories.Item(i).Name + ": " + MC.Estimate[i].ToString);
End For;
Debug.WriteLine(" === Descriptive statistics === ");
Debug.WriteLine("Determination coefficient: " + TwoSLS.SummaryStatistics.R2.ToString);
Debug.WriteLine("Sum of residuals squares: " + TwoSLS.SummaryStatistics.SSR.ToString);
Debug.WriteLine("Standard regression error: " + TwoSLS.SummaryStatistics.SE.ToString);
Debug.WriteLine("");
Debug.WriteLine(" === Model residuals === ");
For i := 0 To TwoSLS.Residuals.Length - 1 Do
Debug.WriteLine(i.ToString + " " + TwoSLS.Residuals[i].ToString);
End For;
Debug.WriteLine(" === Modeling series === ");
For i := 0 To TwoSLS.Fitted.Length - 1 Do
Debug.WriteLine(i.ToString + " " + TwoSLS.Fitted[i].ToString);
End For;
Debug.WriteLine(" === Forecast series === ");
For i := 10 To TwoSLS.Forecast.Value.Length - 1 Do
Debug.WriteLine(i.ToString + " " + TwoSLS.Forecast.Value[i].ToString);
End For;
End If;
End Sub UserProc;
After executing the example the specific linear regression model is created and calculated. The two-step least squares method is used to estimate coefficients. Calculation results are displayed to the console window.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
TwoSLS: Sm2SLS;
can, fra, ger, jpn, uk: Array[15] Of Double;
C: IIntercept;
MC: ICoefficients;
X: ISlSerie;
res,i: Integer;
Estimate, Residuals, Forecast: System.Array;
Begin
// Set values for variables
can[00] := Double.Nan; fra[00] := 4110; ger[00] := 3415; jpn[00] := 1475; uk[00] := 5320;
can[01] := 6385; fra[01] := 4280; ger[01] := 3673; jpn[01] := 1649; uk[01] := 5484;
can[02] := 6752; fra[02] := 4459; ger[02] := 4013; jpn[02] := Double.Nan; uk[02] := 5517;
can[03] := 6837; fra[03] := 4545; ger[03] := 4278; jpn[03] := 1884; uk[03] := 5791;
can[04] := 6495; fra[04] := 4664; ger[04] := 4577; jpn[04] := 1972; uk[04] := 5971;
can[05] := 6907; fra[05] := Double.Nan; ger[05] := 5135; jpn[05] := 2108; uk[05] := Double.Nan;
can[06] := 7349; fra[06] := 5195; ger[06] := 5388; jpn[06] := 2249; uk[06] := 6238;
can[07] := 7213; fra[07] := 5389; ger[07] := 5610; jpn[07] := 2394; uk[07] := 6322;
can[08] := 7061; fra[08] := 5463; ger[08] := Double.Nan; jpn[08] := 2505; uk[08] := 6340;
can[09] := 7180; fra[09] := 5610; ger[09] := 6181; jpn[09] := 2714; uk[09] := 6569;
can[10] := 7132; fra[10] := 5948; ger[10] := 6633; jpn[10] := 3052; uk[10] := 6813;
can[11] := Double.Nan; fra[11] := 6218; ger[11] := 6910; jpn[11] := 3453; uk[11] := 6974;
can[12] := 7473; fra[12] := 6521; ger[12] := 7146; jpn[12] := 3666; uk[12] := 6994;
can[13] := 7722; fra[13] := 6788; ger[13] := 7248; jpn[13] := 4008; uk[13] := 7220;
can[14] := 8088; fra[14] := 7222; ger[14] := 7689; jpn[14] := 4486; uk[14] := Double.Nan;
// Create model
TwoSLS := New Sm2SLS.Create();
// Set sample period parameters
TwoSLS.ModelPeriod.FirstPoint := 1;
TwoSLS.ModelPeriod.LastPoint := 10;
// Set forecast period parameters
TwoSLS.Forecast.LastPoint := 15;
// Use auto estimation of constant value
C:=TwoSLS.ModelCoefficients.Intercept;
C.Mode := InterceptMode.imAutoEstimate;
// Set explained variable
TwoSLS.Explained.Value := can;
// Set explanatory variables
X:=TwoSLS.Explanatories.Add();
X.Value := fra;
X.Name:= "fra";
X:=TwoSLS.Explanatories.Add();
X.Value := ger;
X.Name:= "ger";
// Use constants in instrumental variables
TwoSLS.UseConstantAsInstrument := True;
// Set instrumental variables
TwoSLS.Instrumental.Add().Value := jpn;
TwoSLS.Instrumental.Add().Value := uk;
// Set missing data treatment method
TwoSLS.MissingData.Method := MissingDataMethod.mdmAnyValue;
// Run calculation and show results
res := TwoSLS.Execute();
If res <> 0 Then
System.Diagnostics.Debug.WriteLine(TwoSLS.Errors);
Else
System.Diagnostics.Debug.WriteLine(" === Model coefficients ===");
System.Diagnostics.Debug.WriteLine("Constant: " + C.Estimate.ToString());
MC:=TwoSLS.ModelCoefficients.Coefficients;
Estimate := MC.Estimate;
For i:=0 To MC.Estimate.Length-1 Do
System.Diagnostics.Debug.WriteLine(TwoSLS.Explanatories.Item[i].Name+": "+Estimate[i].ToString());
End For;
System.Diagnostics.Debug.WriteLine(" === Descriptive statistics === ");
System.Diagnostics.Debug.WriteLine("Determination coefficient: " + TwoSLS.SummaryStatistics.R2.ToString());
System.Diagnostics.Debug.WriteLine("Sum of residual squares: "+TwoSLS.SummaryStatistics.SSR.ToString());
System.Diagnostics.Debug.WriteLine("Standard regression error: "+TwoSLS.SummaryStatistics.SE.ToString());
System.Diagnostics.Debug.WriteLine("");
System.Diagnostics.Debug.WriteLine(" === Model residuals === ");
Residuals := TwoSLS.Residuals;
For i := 0 To TwoSLS.Residuals.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + " " + Residuals[i].ToString());
End For;
System.Diagnostics.Debug.WriteLine(" === Modeling series === ");
Fitted := TwoSLS.Fitted;
For i := 0 To TwoSLS.Fitted.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + " " + Fitted[i].ToString());
End For;
System.Diagnostics.Debug.WriteLine(" === Forecast series === ");
Forecast := TwoSLS.Forecast.Value;
For i:=10 To TwoSLS.Forecast.Value.Length-1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + " " + Forecast[i].ToString());
End For;
End If;
End Sub;
See also: