CoefficientsNames: Array;
CoefficientsNames: System.Array;
The CoefficientsNames property returns names of coefficients.
To get values of coefficients, use the IMsDiagnosticTestResults.ModelCoefficients property.
Executing the example requires that the repository contains a modeling container with the MS identifier containing a modeling problem with the MODEL_RESETTEST_WEB identifier. The problem must be created in the web application and contain only a linear regression model.
Add links to the Metabase, Ms, Stat system assemblies.
Sub DiagnosticTests;
Var
mb: IMetabase;
MSKey: Integer;
Problem: IMsProblem;
Meta: IMsMetaModel;
ChainEnts: IMsCalculationChainEntries;
ChainEl: IMsCalculationChainEntry;
pModel: IMsModel;
pTransform: IMsFormulaTransform;
pFormula: IMsFormula;
pRegress: IMsLinearRegressionTransform;
pTestList: IMsDiagnosticTestList;
Test: IMsDiagnosticTest;
VarTrans: IMsFormulaTransformVariable;
Settings: IMsDiagnosticTestSettings;
Calc: IMsMethodCalculation;
Coord: IMsFormulaTransformCoord;
Res: IMsDiagnosticTestResults;
Stat: ISpecificationTestStatistic;
i: Integer;
Coef: ICoefficients;
CoefNames: Array Of String;
Begin
mb := MetabaseClass.Active;
// Get modeling problem
MSKey := mb.GetObjectKeyById("MS");
Problem := mb.ItemByIdNamespace("MODEL_RESETTEST_WEB", MSKey).Bind As IMsProblem;
// Get internal metamodel
Meta := Problem.MetaModel;
// Get linear regression problem
ChainEnts := Meta.CalculationChain;
For i := 0 To ChainEnts.Count - 1 Do
ChainEl := ChainEnts.Item(i);
If ChainEl.Type = MsCalculationChainEntryType.Model Then
pModel := (ChainEl As IMsCalculationChainModel).Model;
End If;
End For;
// Get model calculation parameters
pTransform := pModel.Transform;
pFormula := pTransform.FormulaItem(0);
pRegress := pFormula.Method As IMsLinearRegressionTransform;
// Get set of diagnostic tests
pTestList := pRegress.DiagnosticTests;
// Find the Ramsey RESET test
Test := pTestList.FindByType(MsDiagnosticTestType.RamseyResset);
VarTrans := pTransform.Outputs.Item(0);
// Get test settings
Settings := Test.Settings;
// Set significance level
Settings.ConfidenceLevel := 0.75;
// Check if test can be calculated by means of R package
If Settings.SupportsR
Then
Settings.UseR := True;
Debug.WriteLine("Test is calculated by means of connection to R");
Else
Debug.WriteLine("Test does not support calculation by means of R");
End If;
Debug.WriteLine("");
// Set testing periods
Coord := pTransform.CreateCoord(VarTrans);
Calc := pModel.CreateCalculation As IMsMethodCalculation;
Calc.Period.IdentificationStartDate := pTransform.Period.IdentificationStartDate;
Calc.Period.IdentificationEndDate := pTransform.Period.IdentificationEndDate;
Calc.Period.ForecastStartDate := pTransform.Period.ForecastStartDate;
Calc.Period.ForecastEndDate := pTransform.Period.ForecastEndDate;
// Execute testing
Res := Test.Execute(Calc As IMsMethodCalculation, Coord);
// Output results
If Res.Error <> "" Then
Debug.WriteLine(Res.Error);
Else
Stat := Res.ChiTest;
Debug.WriteLine("-- Chi-square statistic --");
Debug.WriteLine(" Value: " + Stat.Statistic.ToString);
Debug.WriteLine(" Probability: " + Stat.Probability.ToString);
If Res.FTestResult Then
Debug.WriteLine(" The hypothesis of functional form acceptability is accepted");
Else
Debug.WriteLine(" The hypothesis functional form acceptability is rejected");
End If;
Stat := Res.FTest;
Debug.WriteLine("-- Fisher statistic --");
Debug.WriteLine(" Value: " + Stat.Statistic.ToString);
Debug.WriteLine(" Probability: " + Stat.Probability.ToString);
If Res.ChiTestResult Then
Debug.WriteLine(" The hypothesis of functional form acceptability is accepted");
Else
Debug.WriteLine(" The hypothesis of functional form acceptability is rejected");
End If;
CoefNames := Res.CoefficientsNames;
Debug.WriteLine("-- Model coefficients values --");
Coef := Res.ModelCoefficients.Coefficients;
For i := 0 To Coef.Estimate.Length - 1 Do
Debug.WriteLine(" Coefficient " + CoefNames[i]);
Debug.WriteLine(" Value: " + Coef.Estimate[i].ToString);
Debug.WriteLine(" Probability: " + Coef.Probability[i].ToString);
End For;
CoefNames := Res.CoefficientsARNames;
If CoefNames.Length <> 0 Then
Debug.WriteLine("-- Autoregression coefficients --");
Coef := Res.CoefficientsAR;
For i := 0 To CoefNames.Length - 1 Do
Debug.WriteLine(" Coefficient " + CoefNames[i]);
Debug.WriteLine(" Value: " + Coef.Estimate[i].ToString);
Debug.WriteLine(" Probability: " + Coef.Probability[i].ToString);
End For;
End If;
CoefNames := Res.CoefficientsMANames;
If CoefNames.Length <> 0 Then
Debug.WriteLine("-- Moving average coefficients --");
Coef := Res.CoefficientsMA;
For i := 0 To CoefNames.Length - 1 Do
Debug.WriteLine(" Coefficient " + CoefNames[i]);
Debug.WriteLine(" Value: " + Coef.Estimate[i].ToString);
Debug.WriteLine(" Probability: " + Coef.Probability[i].ToString);
End For;
End If;
End If;
End Sub DiagnosticTests;
Example execution result: the Ramsey RESET test is executed for the model, results are displayed in the console window. R package will be used in calculation.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
mb: IMetabase;
MSKey: uinteger;
Problem: IMsProblem;
Meta: IMsMetaModel;
ChainEnts: IMsCalculationChainEntries;
ChainEl: IMsCalculationChainEntry;
pModel: IMsModel;
pTransform: IMsFormulaTransform;
pFormula: IMsFormula;
pRegress: IMsLinearRegressionTransform;
pTestList: IMsDiagnosticTestList;
Test: IMsDiagnosticTest;
VarTrans: IMsFormulaTransformVariable;
Settings: IMsDiagnosticTestSettings;
Calc: IMsMethodCalculation;
Coord: IMsFormulaTransformCoord;
Res: IMsDiagnosticTestResults;
Stat: ISpecificationTestStatistic;
i: Integer;
Coef: ICoefficients;
CoefNames: System.Array;
Begin
mb := Params.Metabase;
// Get modeling problem
MSKey := mb.GetObjectKeyById("MS");
Problem := mb.ItemByIdNamespace["MODEL_RESETTEST_WEB", MSKey].Bind() As IMsProblem;
// Get internal metamodel
Meta := Problem.MetaModel;
// Get linear regression problem
ChainEnts := Meta.CalculationChain;
For i := 0 To ChainEnts.Count - 1 Do
ChainEl := ChainEnts.Item[i];
If ChainEl.Type = MsCalculationChainEntryType.mccetModel Then
pModel := (ChainEl As IMsCalculationChainModel).Model;
End If;
End For;
// Get model calculation parameters
pTransform := pModel.Transform;
pFormula := pTransform.FormulaItem[0];
pRegress := pFormula.Method As IMsLinearRegressionTransform;
// Get set of diagnostic tests
pTestList := pRegress.DiagnosticTests[Null, Null];
// Find the Ramsey RESET test
Test := pTestList.FindByType[MsDiagnosticTestType.mdttRamseyResset];
VarTrans := pTransform.Outputs.Item[0];
// Get test settings
Settings := Test.Settings;
// Set significance level
Settings.ConfidenceLevel := 0.75;
// Check if test can be calculated by means of R package
If Settings.SupportsR
Then
Settings.UseR := True;
System.Diagnostics.Debug.WriteLine("Test is calculated by means of connection to R");
Else
System.Diagnostics.Debug.WriteLine("Test does not support calculation by means of R");
End If;
System.Diagnostics.Debug.WriteLine("");
// Set testing periods
Coord := pTransform.CreateCoord(VarTrans);
Calc := pModel.CreateCalculation() As IMsMethodCalculation;
Calc.Period.IdentificationStartDate := pTransform.Period.IdentificationStartDate;
Calc.Period.IdentificationEndDate := pTransform.Period.IdentificationEndDate;
Calc.Period.ForecastStartDate := pTransform.Period.ForecastStartDate;
Calc.Period.ForecastEndDate := pTransform.Period.ForecastEndDate;
// Execute testing
Res := Test.Execute(Calc As IMsMethodCalculation, Coord);
// Output results
If Res.Error <> "" Then
System.Diagnostics.Debug.WriteLine(Res.Error);
Else
Stat := Res.ChiTest;
System.Diagnostics.Debug.WriteLine("-- Chi-square statistic --");
System.Diagnostics.Debug.WriteLine(" Value: " + Stat.Statistic.ToString());
System.Diagnostics.Debug.WriteLine(" Probability: " + Stat.Probability.ToString());
If Res.FTestResult Then
System.Diagnostics.Debug.WriteLine(" The hypothesis of functional form acceptability is accepted");
Else
System.Diagnostics.Debug.WriteLine(" The hypothesis of functional form acceptability is rejected");
End If;
Stat := Res.FTest;
System.Diagnostics.Debug.WriteLine("-- Fisher statistic --");
System.Diagnostics.Debug.WriteLine(" Value: " + Stat.Statistic.ToString());
System.Diagnostics.Debug.WriteLine(" Probability: " + Stat.Probability.ToString());
If Res.ChiTestResult Then
System.Diagnostics.Debug.WriteLine(" The hypothesis of functional form acceptability is accepted");
Else
System.Diagnostics.Debug.WriteLine(" The hypothesis of functional form acceptability is rejected");
End If;
CoefNames := Res.CoefficientsNames;
System.Diagnostics.Debug.WriteLine("-- Values of model coefficients --");
Coef := Res.ModelCoefficients.Coefficients;
For i := 0 To Coef.Estimate.Length - 1 Do
System.Diagnostics.Debug.WriteLine(" Coefficient " + CoefNames[i]);
System.Diagnostics.Debug.WriteLine(" Value: " + Coef.Estimate.GetValue(i).ToString());
System.Diagnostics.Debug.WriteLine(" Probability: " + Coef.Probability.GetValue(i).ToString());
End For;
CoefNames := Res.CoefficientsARNames;
If CoefNames.Length <> 0 Then
System.Diagnostics.Debug.WriteLine("-- Autoregression coefficients --");
Coef := Res.CoefficientsAR;
For i := 0 To CoefNames.Length - 1 Do
System.Diagnostics.Debug.WriteLine(" Coefficient " + CoefNames[i]);
System.Diagnostics.Debug.WriteLine(" Value: " + Coef.Estimate.GetValue(i).ToString());
System.Diagnostics.Debug.WriteLine(" Probability: " + Coef.Probability.GetValue(i).ToString());
End For;
End If;
CoefNames := Res.CoefficientsMANames;
If CoefNames.Length <> 0 Then
System.Diagnostics.Debug.WriteLine("-- Moving average coefficients --");
Coef := Res.CoefficientsMA;
For i := 0 To CoefNames.Length - 1 Do
System.Diagnostics.Debug.WriteLine(" Coefficient " + CoefNames[i]);
System.Diagnostics.Debug.WriteLine(" Value: " + Coef.Estimate.GetValue(i).ToString());
System.Diagnostics.Debug.WriteLine(" Probability: " + Coef.Probability.GetValue(i).ToString());
End For;
End If;
End If;
End Sub;
See also: