Contents
Consider the example of creating a model by variables being the object repository, which is used to calculate linear regression (OLS estimation) method.
Executing the example requires that the repository contains a modeling container with the MS_DEFAULT identifier. This modeling container must contain variables with OUTPUT_VARIABLE and FACTOR_VARIABLE identifiers.
Add links to the Cubes, Dimensions, Metabase, Ms, Stat system assemblies.
Sub ModelVariable;
Var
Mb: IMetabase;
CrInf: IMetabaseObjectCreateInfo;
Model: IMsModel;
Transform: IMsFormulaTransform;
TransformVarables: IMsFormulaTransformVariables;
Stub: IVariableStub;
TransVar: IMsFormulaTransformVariable;
Tree: IMsFormulaTransformSlicesTree;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
Linear: IMsLinearRegressionTransform;
Ar: Array[0..1] Of Integer;
TermInfo: IMsFormulaTermInfo;
Period: IMsModelPeriod;
Begin
Mb := MetabaseClass.Active;
// Specify basic parameters of the model as a repository object
CrInf := Mb.CreateCreateInfo;
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSMODEL;
// Specify model identifier
CrInf.Id := Mb.GenerateId("MODEL_LINEAR_REGRESSION", Mb.ItemById("MS_DEFAULT").Key);
// Specify model name
CrInf.Name := "Model of linear regression";
// Specify modeling container which will contain the model
CrInf.Parent := Mb.ItemById("MS_DEFAULT");
// Create a model
Model := Mb.CreateObject(CrInf).Edit As IMsModel;
// Get object to set up model parameters
Transform := Model.Transform;
// Get object to work with output variable
TransformVarables := Transform.Outputs;
// Cast the OUTPUT_VARIABLE variable to the abstract data source
Stub := Mb.ItemByIdNamespace("OUTPUT_VARIABLE", Mb.GetObjectKeyById("MS_DEFAULT")).Bind As IVariableStub;
// Use the OUTPUT_VARIABLE variable as the output variable
TransVar := TransformVarables.Add(Stub);
// Specify output variable slice
Tree := TransVar.SlicesTree(TransVar);
Slice := Tree.CreateSlice(1);
// Get model settings for output variable slice
Selector := Transform.CreateSelector;
Selector.Slice := Slice;
Formula := Transform.Transform(Selector);
// Specify the Linear Regression (OLS Estimation) calculation method
Formula.Kind := MsFormulaKind.LinearRegression;
// Set the calendar frequency of calculation
Formula.Level := DimCalendarLevel.Year;
// Get the object to set up linear regression
Linear := Formula.Method As IMsLinearRegressionTransform;
// Determine autoregression order
Ar[0] := 2;
Ar[1] := 4;
Linear.ARMA.OrderAR := Ar;
// Do not use the constant
Linear.ConstantMode := InterceptMode.None;
// Cast the FACTOR_VARIABLE variable to the abstract data source
Stub := Mb.ItemByIdNamespace("FACTOR_VARIABLE", Mb.GetObjectKeyById("MS_DEFAULT")).Bind As IVariableStub;
// Add the FACTOR_VARIABLE variable to the model as the factor
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree(TransVar);
Slice := Tree.CreateSlice(1);
// Get the factor as an expression element
TermInfo := Transform.CreateTermInfo;
TermInfo.Slice := Slice;
// Specify linear regression calculation formula
Linear.Explanatories.Add.Expression.AsString := TermInfo.TermInnerText;
// Specify model calculation periods
Period := Transform.Period;
Period.IdentificationStartDate := DateTime.ComposeDay(2000, 1, 1);
Period.IdentificationEndDate := DateTime.ComposeDay(2014, 12, 31);
Period.ForecastStartDate := DateTime.ComposeDay(2015, 1, 1);
Period.ForecastEndDate := DateTime.ComposeDay(2020, 12, 31);
// Save the model to the repository
(Model As IMetabaseObject).Save;
End Sub ModelVariable;
Imports Prognoz.Platform.Interop.Cubes;
Imports Prognoz.Platform.Interop.Dimensions;
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
Mb: IMetabase;
CrInf: IMetabaseObjectCreateInfo;
Model: IMsModel;
Transform: IMsFormulaTransform;
TransformVarables: IMsFormulaTransformVariables;
Stub: IVariableStub;
TransVar: IMsFormulaTransformVariable;
Tree: IMsFormulaTransformSlicesTree;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
Linear: IMsLinearRegressionTransform;
Ar: Array[0..1] Of Integer;
TermInfo: IMsFormulaTermInfo;
Period: IMsModelPeriod;
Begin
Mb := Params.Metabase;
// Specify basic parameters of the model as a repository object
CrInf := Mb.CreateCreateInfo();
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSMODEL As integer;
// Specify model identifier
CrInf.Id := Mb.GenerateId("MODEL_LINEAR_REGRESSION", Mb.ItemById["MS_DEFAULT"].Key);
// Specify model name
CrInf.Name := "Model of linear regression";
// Specify modeling container which will contain the model
CrInf.Parent := Mb.ItemById["MS_DEFAULT"];
// Create a model
Model := Mb.CreateObject(CrInf).Edit() As IMsModel;
// Get object to set up model parameters
Transform := Model.Transform;
// Get object to work with output variable
TransformVarables := Transform.Outputs;
// Cast the OUTPUT_VARIABLE variable to the abstract data source
Stub := Mb.ItemByIdNamespace["OUTPUT_VARIABLE", Mb.GetObjectKeyById("MS_DEFAULT")].Bind() As IVariableStub;
// Use TSDB as a data source of output variable
TransVar := TransformVarables.Add(Stub);
// Specify output variable slice
Tree := TransVar.SlicesTree[TransVar];
Slice := Tree.CreateSlice(1);
// Get model settings for output variable slice
Selector := Transform.CreateSelector();
Selector.Slice := Slice;
Formula := Transform.Transform[Selector];
// Specify the Linear Regression (OLS Estimation) calculation method
Formula.Kind := MsFormulaKind.mfkLinearRegression;
// Set the calendar frequency of calculation
Formula.Level := DimCalendarLevel.dclYear;
// Get the object to set up linear regression
Linear := Formula.Method As IMsLinearRegressionTransform;
// Determine autoregression order
Ar[0] := 2;
Ar[1] := 4;
Linear.ARMA.OrderAR := Ar;
// Do not use the constant
Linear.ConstantMode := InterceptMode.imNone;
// Cast the FACTOR_VARIABLE variable to the abstract data source
Stub := Mb.ItemByIdNamespace["FACTOR_VARIABLE", Mb.GetObjectKeyById("MS_DEFAULT")].Bind() As IVariableStub;
// Add the FACTOR_VARIABLE variable to the model as the factor
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree[TransVar];
Slice := Tree.CreateSlice(1);
// Get the factor as an expression element
TermInfo := Transform.CreateTermInfo();
TermInfo.Slice := Slice;
// Specify linear regression calculation formula
Linear.Explanatories.Add().Expression.AsString := TermInfo.TermInnerText;
// Specify model calculation periods
Period := Transform.Period;
Period.IdentificationStartDate := DateTime.Parse("2000, 1, 1");
Period.IdentificationEndDate := DateTime.Parse("2014, 12, 31");
Period.ForecastStartDate := DateTime.Parse("2015, 1, 1");
Period.ForecastEndDate := DateTime.Parse("2020, 12, 31");
// Save the model to the repository
(Model As IMetabaseObject).Save();
End Sub;
After executing the example in the MS_DEFAULT modeling container the model which uses variables being repository objects will be created. To calculate the model the linear regression method (OLS estimation) method is used with the following settings:
Output variable - OUTPUT_VARIABLE, factor - FACTOR_VARIABLE.
Constant is absent in the model.
Values of autoregression orders: "2, 4".
Annual calculation frequency is used.
Sample period: start - "2000", end - "2014".
Forecasting period: start - "2015", end - "2020'.
See also: