IModelling.OlsR

Fore Syntax

OlsR(Input: ITimeSeries;
     Period: IMsPeriod;
     ConstantValue: Variant;
     AROrder: Integer;
     MAOrder: Integer;
     Casewise: MsCasewise;
     Explanatories: Array): Variant;

Fore.NET Syntax

OlsR(Context: Prognoz.Platform.Interop.Fore.ForeRuntimeContext;
     Input: Prognoz.Platform.Interop.Ms.ITimeSeries;
     Period: Prognoz.Platform.Interop.Ms.IMsPeriod;
     ConstantValue: object;
     AROrder: integer;
     MAOrder: integer;
     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.

AROrder. Autoregression order.

MAOrder. Moving average order.

Casewise. Missing data treatment method.

Explanatories. Explanatory variables.

Description

The Ols method models variable data with the help of linear regression (OLS estimation). Calculation is executed using the R package.

Comment

Use the OlsR 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. Elements that correspond to variables specified via comma. Remember that the number of explanatory variables (m) must satisfy the inequality: 0 < m < n-1 for model with constant and 0 < m < n for model without constant, where n is the number of observations in the output variable.

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 the method of determinate equation and contains more than one input variable.

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 UserOlsR;
Var
    Mb: IMetabase;
    ModelSpace, ModelObj: IMetabaseObject;
    Transf: IMsFormulaTransform;
    Formula: IMsFormula;
    Model: IMsModel;
    Determ: IMsDeterministicTransform;
    TransVar: IMsFormulaTransformVariable;
    Slice: IMsFormulaTransformSlice;
    TermInfo: IMsFormulaTermInfo;
    Inp_1, Inp_2: 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_1 := TermInfo.TermInnerText;
    // Get the second input 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
    Inp_2 := TermInfo.TermInnerText;
    // Get model calculation expression
    Expr := Determ.Expression;
    Expr.References := "Ms";
    // Set model calculation expression
    Expr.AsString := "OlsR(" + Inp_1 + ", SetPeriod(2000,2015), Estimate, 1," +
        "1, MsCasewise.Yes, " + Inp_2 + ")";
    // 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 UserOlsR;

After executing the example the model models the first input variable by the linear regression method (OLS estimation) at the specified period. The value of constant is estimated automatically. The Casewise missing data treatment method is used. Calculation is executed by 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_1, Inp_2: 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_1 := TermInfo.TermInnerText;
    // Get the second input 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
    Inp_2 := TermInfo.TermInnerText;
    // Get model calculation expression
    Expr := Determ.Expression;
    Expr.References := "Ms";
    // Set model calculation expression
    Expr.AsString := "OlsR(" + Inp_1 + ", SetPeriod(2000,2015), Estimate, 1," +
        "1, MsCasewise.Yes, " + Inp_2 + ")";
    // 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:

OlsR({Chicago - population[t]}, Null, None, 0, 0, MsCasewise.Yes, {Mexico - population[t]})

Result: the Chicago - population time series will be modeled by the linear regression method (OLS estimation) at the entire period using the following parameters: no constant is used, autoregression and moving average orders are not set, the Mexico - population time series is used as the explanatory variable, the Casewise missing data treatment method is 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:

OlsR(X1, SetPeriod(2000, 2015), Estimate, 1, 2, MsCasewise.Yes, X2, X3)

Result: the X1 factor will be modeled with the linear regression method (OLS estimation) using the following parameters: calculation period - 2000-2015, constant is estimated using the IModelling.Estimate method, autoregression order is one, moving average order is set to two, the X2 and X3 factors are used as explanatory 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 | Least-Squares Method | Time Series Database: Calculator, Linear Regression   Modeling Container: The Linear Regression (OLS Estimation) Model, Editing Regressor or Formula