Creating a Model with Variables on Attributes

Contents

Description

Requirements

Fore Example

Fore.NET Example

Result of the Fore and Fore.NET Examples Execution

Description

Consider the example of creating a model with variables on attributes, which uses the linear regression method (OLS estimation).

Requirements

Executing the example requires that the repository contains a modeling container with the MS identifier and a database with the TSDB identifier. This time series database must contain mandatory custom attributes of series being references to the dictionary.

Add links to the Cubes, Dimensions, Metabase, Ms, Stat system assemblies.

Fore Example

Sub ModelAttr;
Var
    Mb: IMetabase;
    CrInf: IMetabaseObjectCreateInfo;
    Model: IMsModel;
    Transform: IMsFormulaTransform;
    TransformVarables: IMsFormulaTransformVariables;
    RubrDescr: IMetabaseObjectDescriptor;
    Stub: IVariableStub;
    TransVar: IMsFormulaTransformVariable;
    Tree: IMsFormulaTransformSlicesTree;
    Slice: IMsFormulaTransformSlice;
    Selector: IMsFormulaTransformSelector;
    Formula: IMsFormula;
    Linear: IMsLinearRegressionTransform;
    Ar: Array[0..1Of 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").Key);
    // Specify model name
    CrInf.Name := "Model of linear regression";
    // Specify modeling container which will contain the model
    CrInf.Parent := Mb.ItemById("MS");
    // 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;
    // Get time series database (TSDB)
    RubrDescr := Mb.ItemById("TSDB");
    // Cast obtained TSDB to abstract data source
    Stub := RubrDescr.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.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;
    // Use automatic constant estimation
    Linear.ConstantMode := InterceptMode.AutoEstimate;
    // Add factor with the TSDB data source to model
    TransVar := Transform.Inputs.Add(Stub);
    Tree := TransVar.SlicesTree(TransVar);
    Slice := Tree.CreateSlice(2);
    // 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(200011);
    Period.IdentificationEndDate := DateTime.ComposeDay(20141231);
    Period.ForecastStartDate := DateTime.ComposeDay(201511);
    Period.ForecastEndDate := DateTime.ComposeDay(20201231);
    // Save the model to the repository
    (Model As IMetabaseObject).Save;
End Sub ModelAttr;

Fore.NET Example

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;
    RubrDescr: IMetabaseObjectDescriptor;
    Stub: IVariableStub;
    TransVar: IMsFormulaTransformVariable;
    Tree: IMsFormulaTransformSlicesTree;
    Slice: IMsFormulaTransformSlice;
    Selector: IMsFormulaTransformSelector;
    Formula: IMsFormula;
    Linear: IMsLinearRegressionTransform;
    Ar: Array[0..1Of 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"].Key);
    // Specify model name
    CrInf.Name := "Model of linear regression";
    // Specify modeling container which will contain the model
    CrInf.Parent := Mb.ItemById["MS"];
    // Create a model
    Model := Mb.CreateObject(CrInf).Edit() As IMsModel;
    // Get the object to set up model parameters
    Transform := Model.Transform;
    // Get the object to work with output variable
    TransformVarables := Transform.Outputs;
    // Get time series database (TSDB)
    RubrDescr := Mb.ItemById["TSDB"];
    // Cast the obtained TSDB to the abstract data source
    Stub := RubrDescr.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;
    // Use automatic constant estimation
    Linear.ConstantMode := InterceptMode.imAutoEstimate;
    // Add factor with the TSDB data source to model
    TransVar := Transform.Inputs.Add(Stub);
    Tree := TransVar.SlicesTree[TransVar];
    Slice := Tree.CreateSlice(2);
    // 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;

Result of the Fore and Fore.NET Examples Execution

After executing the example the model that uses variables on attributes is created in the MS modeling container. To calculate the model the linear regression method (OLS estimation) method is used with the following settings:

See also:

Examples | IMsModel