Code: String;
Code: string;
The Code property determines program text.
TO get result, use the ISmR.Result property.
Add a link to the Stat system assembly.
Sub UserProc;
Var
pR: ISmR;
RManager: ISmRManager;
x : ISlSerie;
y, x1, x2: Array[12] Of Double;
code, lf: String;
i, res: Integer;
Begin
pR := New SmR.Create;
// Input series:
// Y:
y[00] := 6209; y[01] := 6385; y[02] := 6752;
y[03] := 6837; y[04] := 6495; y[05] := 6907;
y[06] := 7349; y[07] := 7213; y[08] := 7061;
y[09] := 7180; y[10] := 7132; y[11] := 7137;
x := pR.Inputs.Add;
x.Value := y;
x.Name := "y";
// X1:
x1[00] := 4110; x1[01] := 4280; x1[02] := 4459;
x1[03] := 4545; x1[04] := 4664; x1[05] := 4861;
x1[06] := 5195; x1[07] := 5389; x1[08] := 5463;
x1[09] := 5610; x1[10] := 5948; x1[11] := 6218;
x := pR.Inputs.Add;
x.Value := x1;
x.Name := "x1";
// X2:
x2[00] := 3415; x2[01] := 3673; x2[02] := 4013;
x2[03] := 4278; x2[04] := 4577; x2[05] := 5135;
x2[06] := 5388; x2[07] := 5610; x2[08] := 5787;
x2[09] := 6181; x2[10] := 6633; x2[11] := 6910;
x := pR.Inputs.Add;
x.Value := x2;
x.Name := "x2";
// Program text:
lf:=Char.Chr(10);
code:= "";
// Setting equation: y~x1+x2 - formula
// y - explained series, x1, x2- explanatory series,
// if it is required exclude constant, write x1+x2-1:
code := code + "model <- lm(y~x1+x2, na.action = na.exclude, model = TRUE, x = TRUE, y = TRUE, qr = TRUE,singular.ok = TRUE)" + lf;
// coefficients
code := code + "coe<-t(coefficients(model))" + lf;
// model series
code := code + "fit<-fitted(model)" + lf;
pR.Code := code;
// Returned result:
pR.Result.Name := "fit";
// Calculate model:
res := pR.Execute;
RManager := New SmRManager.Create;
RManager.Reset;
If RManager.IsRExist = True Then
RManager.UserRPath := "C:\Program Files\R\R-3.1.3";
Debug.WriteLine(pR.Errors);
Debug.WriteLine("Calculation result:");
Debug.Indent;
For i := 0 To pR.Result.Value.Length-1 Do
Debug.WriteLine(pR.Result.Value[i].ToString );
End For;
Debug.Unindent;
End If;
End Sub UserProc;
After executing the example the console window displays results obtained with the use of the R programming language.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
pR: ISmR;
RManager: ISmRManager;
x : ISlSerie;
y, x1, x2: Array[12] Of Double;
code, lf: String;
i, res: Integer;
Value: System.Array;
Begin
pR := New SmR.Create();
// Input series:
// Y:
y[00] := 6209; y[01] := 6385; y[02] := 6752;
y[03] := 6837; y[04] := 6495; y[05] := 6907;
y[06] := 7349; y[07] := 7213; y[08] := 7061;
y[09] := 7180; y[10] := 7132; y[11] := 7137;
x := pR.Inputs.Add();
x.Value := y;
x.Name := "y";
// X1:
x1[00] := 4110; x1[01] := 4280; x1[02] := 4459;
x1[03] := 4545; x1[04] := 4664; x1[05] := 4861;
x1[06] := 5195; x1[07] := 5389; x1[08] := 5463;
x1[09] := 5610; x1[10] := 5948; x1[11] := 6218;
x := pR.Inputs.Add();
x.Value := x1;
x.Name := "x1";
// X2:
x2[00] := 3415; x2[01] := 3673; x2[02] := 4013;
x2[03] := 4278; x2[04] := 4577; x2[05] := 5135;
x2[06] := 5388; x2[07] := 5610; x2[08] := 5787;
x2[09] := 6181; x2[10] := 6633; x2[11] := 6910;
x := pR.Inputs.Add();
x.Value := x2;
x.Name := "x2";
// Program text:
lf:=char.ConvertFromUtf32(10);
code:= "";
// Setting equation: y~x1+x2 - formula
// y - explained series, x1, x2- explanatory series,
// if it is required exclude constant, write x1+x2-1:
code := code + "model <- lm(y~x1+x2, na.action = na.exclude, model = TRUE, x = TRUE, y = TRUE, qr = TRUE,singular.ok = TRUE)" + lf;
// coefficients
code := code + "coe<-t(coefficients(model))" + lf;
// model series
code := code + "fit<-fitted(model)" + lf;
pR.Code := code;
// Returned result:
pR.Result.Name := "fit";
// Calculate model:
res := pR.Execute();
RManager := New SmRManager.Create();
RManager.Reset();
If RManager.IsRExist = True Then
RManager.UserRPath := "C:\Program Files\R\R-3.1.3";
System.Diagnostics.Debug.WriteLine(pR.Errors);
System.Diagnostics.Debug.WriteLine("Calculation results:");
System.Diagnostics.Debug.Indent();
Value := pR.Result.Value;
For i := 0 To pR.Result.Value.Length-1 Do
System.Diagnostics.Debug.WriteLine(Value[i].ToString() );
End For;
System.Diagnostics.Debug.Unindent();
End If;
End Sub;
See also: