IModelling.TslsR

Fore Syntax

TslsR(Input: ITimeSeries;
      Period: IMsPeriod;
      ConstantValue: Variant;
      Casewise: MsCasewise;
      Explanatories: Array): Variant;

Fore.NET Syntax

TslsR(Context: Prognoz.Platform.Interop.Fore.ForeRuntimeContext;
      Input: Prognoz.Platform.Interop.Ms.ITimeSeries;
      Period: Prognoz.Platform.Interop.Ms.IMsPeriod;
      ConstantValue: object;
      Casewise: Prognoz.Platform.Interop.Ms.MsCasewise;
      Explanatories: array of object): object;

Parameters

Context. Context. The parameter is used only in Fore.NET.

Input. Output variable.

Period. Period, at which the method is calculated. If the parameter is set to Null, the method is calculated at the entire time period.

ConstantValue. Constant used in calculations.

Casewise. Missing data treatment method.

Explanatories. Exogenous and instrumental variables.

Description

The TslsR method models variable data using linear regression (instrumental variables estimation). Calculation is executed using the R package.

Comments

Use the TslsR method only when series mode of calculation is applied.

Integration with R must be set up in the repository to use this method. For details about integration setup see the How to Set Up Integration with R? section.

ConstantValue. The value of the constant can be determined by the user or estimated automatically. Use the IModelling.Estimate method to estimate values automatically. If the model must be calculated without constant, use the IModelling.None method.

Explanatories. Exogenous and instrumental variables are specified via commas. Use the Null value to separate these types of variables. The number of instrumental variables must be greater or equal to the number of exogenous variables.

Fore Example

Executing the example requires that the repository contains a modeling container with the MS identifier. This container includes a model with the MODEL_D identifier that is calculated by method of determinate equation and contains more than two input variables.

Integration with R must be set up in the repository. For details about integration setup see the How to Set Up Integration with R? section.

Add links to the Metabase, Ms system assemblies.

Sub UserTslsR;
Var
    Mb: IMetabase;
    ModelSpace, ModelObj: IMetabaseObject;
    Transf: IMsFormulaTransform;
    Formula: IMsFormula;
    Model: IMsModel;
    Determ: IMsDeterministicTransform;
    TransVar: IMsFormulaTransformVariable;
    Slice: IMsFormulaTransformSlice;
    TermInfo: IMsFormulaTermInfo;
    Inp, Instr, Exogen: String;
    Expr: IExpression;
Begin
    // Get repository
    Mb := MetabaseClass.Active;
    // Get modeling container
    ModelSpace := Mb.ItemById("MS").Bind;
    // Get model
    ModelObj := Mb.ItemByIdNamespace("MODEL_D", ModelSpace.Key).Edit;
    Model := ModelObj As IMsModel;
    // Get model calculation parameters
    Transf := Model.Transform;
    Formula := Transf.FormulaItem(0);
    Determ := Formula.Method As IMsDeterministicTransform;
    // Get the first input variable
    TransVar := Transf.Inputs.Item(0);
    Slice := TransVar.Slices.Item(0);
    TermInfo := Transf.CreateTermInfo;
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.Pointwise;
    // Get internal view of the variable as a text
    Inp := TermInfo.TermInnerText;
    // Get the second input variable.
    // It will be used as an exogenous variable.
    TransVar := Transf.Inputs.Item(1);
    Slice := TransVar.Slices.Item(0);
    TermInfo := Transf.CreateTermInfo;
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.Pointwise;
    // Get internal view of the variable as a text
    Exogen := TermInfo.TermInnerText;
    // Get the third input variable.
    // It will be used as an instrumental variable.
    TransVar := Transf.Inputs.Item(2);
    Slice := TransVar.Slices.Item(0);
    TermInfo := Transf.CreateTermInfo;
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.Pointwise;
    // Get internal view of the variable as a text
    Instr := TermInfo.TermInnerText;
    // Get model calculation expression
    Expr := Determ.Expression;
    Expr.References := "Ms;Stat";
    // Set model calculation expression
    Expr.AsString := "TslsR(" + Inp + ", SetPeriod(2000,2015), Estimate," +
        "MsCasewise.Yes, " + Exogen + ", Null, " + Instr + ")";
    // Check if the expression is correct
    If Expr.Valid
        // If the expression is set correctly, save the model
        Then ModelObj.Save;
        // If the expression is incorrect, display a message to the console window 
        Else Debug.WriteLine("Model is not saved: error in the formula");
    End If;
End Sub UserTslsR;

After executing the example the model models the first input variable by the linear regression (instrumental variables estimation). Constant value is auto estimated, the Casewise missing data treatment method is used. Calculation is executed using the R package.

Fore.NET Example

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.ForeSystem;

Public Shared Sub Main(Params: StartParams);
Var
    Mb: IMetabase;
    ModelSpace, ModelObj: IMetabaseObject;
    Transf: IMsFormulaTransform;
    Formula: IMsFormula;
    Model: IMsModel;
    Determ: IMsDeterministicTransform;
    TransVar: IMsFormulaTransformVariable;
    Slice: IMsFormulaTransformSlice;
    TermInfo: IMsFormulaTermInfo;
    Inp, Instr, Exogen: String;
    Expr: IExpression;
Begin
    // Get repository
    Mb := Params.Metabase;
    // Get modeling container
    ModelSpace := Mb.ItemById["MS"].Bind();
    // Get model
    ModelObj := Mb.ItemByIdNamespace["MODEL_D", ModelSpace.Key].Edit();
    Model := ModelObj As IMsModel;
    // Get model calculation parameters
    Transf := Model.Transform;
    Formula := Transf.FormulaItem[0];
    Determ := Formula.Method As IMsDeterministicTransform;
    // Get the first input variable
    TransVar := Transf.Inputs.Item[0];
    Slice := TransVar.Slices.Item[0];
    TermInfo := Transf.CreateTermInfo();
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.mfttPointwise;
    // Get internal view of the variable as a text
    Inp := TermInfo.TermInnerText;
    // Get the second input variable.
    // It will be used as an exogenous variable.
    TransVar := Transf.Inputs.Item[1];
    Slice := TransVar.Slices.Item[0];
    TermInfo := Transf.CreateTermInfo();
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.mfttPointwise;
    // Get internal view of the variable as a text
    Exogen := TermInfo.TermInnerText;
    // Get the third input variable.
    // It will be used as an instrumental variable.
    TransVar := Transf.Inputs.Item[2];
    Slice := TransVar.Slices.Item[0];
    TermInfo := Transf.CreateTermInfo();
    TermInfo.Slice := Slice;
    // Set mode of passing variable into calculation
    TermInfo.Type := MsFormulaTermType.mfttPointwise;
    // Get internal view of the variable as a text
    Instr := TermInfo.TermInnerText;
    // Get model calculation expression
    Expr := Determ.Expression;
    Expr.References := "Ms;Stat";
    // Set model calculation expression
    Expr.AsString := "TslsR(" + Inp + ", SetPeriod(2000,2015), Estimate," +
        "MsCasewise.Yes, " + Exogen + ", Null, " + Instr + ")";
    // Check if the expression is correct
    If Expr.Valid
        // If the expression is set correctly, save the model
        Then ModelObj.Save();
        // If the expression is incorrect, display a message to the console window 
        Else System.Diagnostics.Debug.WriteLine("Model is not saved: error in the formula");
    End If;
End Sub;

Example of Use in Expressions

Expression 1:

Tsls({Brazil|BCA},Estimate,"","",MsCasewise.Yes,{China|BCA},Null,{Japan|BCA})

TslsR({Chicago - population[t]}, Null, Estimate, MsCasewise.No, {Mexico - unemployment rate[t]}, Null, {Anchorage - population[t]})

Result: the Chicago - population time series is modeled at the entire time period using the linear regression method (instrumental variables estimation), with the following parameters: constant value is auto estimated, the Mexico - unemployment rate time series is used as the exogenous variable, the Anchorage - population time series is used as the instrumental variable, missing data treatment is not used. Calculation is executed using the R package.

Use: it can be used in formulas of calculated series of time series database and model formulas of modeling container that is a child of the time series database.

Expression 2:

TslsR(X1, SetPeriod(2000, 2015), None, MsCasewise.Yes, X4, Null, X2, X3)

Result: the X1 factor is modeled using the linear regression method (instrumental variables estimation) with the following parameters: calculation period - 2000-2015, the constant is not specified, the X4 factor is used as the exogenous variable, the X2 and X3 factors are used as instrumental variables, the Casewise missing data treatment method is used. Calculation is executed using the R package.

Use: it can be used in model formulas of modeling container.

See also:

IModelling | Instrumental variables method | Time Series Database: Calculator Modeling Container: The Linear regression (instrumental variables estimation) Model, Editing Regressor or Formula