R(Period: IMsPeriod;
Code: String;
Inputs: Array): Variant;
Period. Period, at which the method is calculated
Code. Expression for method calculation.
Inputs. Array of input variables.
The R method transforms data using R package methods.
Integration with R must be set up in the repository to use this method. To set up integration, see the Integration with R section.
Features of setting parameters:
Period. If the parameter is set to Null, the method is calculated at the entire time period.
Code. Expression is set in the R language. For detailed description of R language see R documentation. Input variables in an expression are indicated as X1, X2 and so on, where 1, 2 is the index of the variable in the Inputs array. Indexing in the array is continuous and starts with zero.
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 determinate equation method and contains at least one input variable.
Integration with R must be set up in the repository.
Add links to the Metabase and Ms system assemblies.
Sub UserR;
Var
Mb: IMetabase;
ModelSpace, ModelObj: IMetabaseObject;
Transf: IMsFormulaTransform;
Formula: IMsFormula;
Model: IMsModel;
Determ: IMsDeterministicTransform;
TransVar: IMsFormulaTransformVariable;
Slice: IMsFormulaTransformSlice;
TermInfo: IMsFormulaTermInfo;
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 model calculation expression
Expr := Determ.Expression;
Expr.References := "Ms;Stat";
// Set model calculation expression
Expr.AsString := "R(SetPeriod(" +
"""" + "01.01.2000" + """" + "," + """" + "01.01.2015" + """" + ")," +
"""" + "Y <- X1 + 1;" + """" + "," + TermInfo.TermInnerText + ") ";
// 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 UserR;
After executing the example the determinate equation model is will be calculated using R.
Expression 1:
r(setperiod("01.01.2000", "01.01.2015"), "Y <- X1 + 1;", {Chicago - population[t]})
Result: values of the Chicago - population time series are increased by one using R package methods.
Use: it can be used in formulas of cross functional expression editor in any platform tool where it is available.
Expression 2:
R(SetPeriod("01.01.2000", "01.01.2015"), "Y <- X1 + 1;", X1, X2)
Result: value of the X1 variable is increased by one using R package methods.
Use: it can be used in model formulas of modeling container.
See also:
IModelling | Time Series Database: R methods Modeling Container: The R Model