如何使用 R.Net 在 C# 中执行 ADF 测试



我正在尝试使R(及其广泛的分析功能(在我的C#项目中可用。

R 库和 C# 环境之间的最终桥梁是 R.net。

我完成了 R.Net 的设置,并开始编写我的第一个应用程序,基本上

  1. 执行简单测试(使用 R 引擎(
  2. 并在控制台上显示测试结果。
  3. 就是这样。

以下是我到目前为止能够起草的代码,它返回错误!

那么,如何调试代码以使 R.Net 执行 ADF 测试?

提前感谢您的时间和帮助!

最好

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;
using System.IO;
static void Main(string[] args)
{
// set the environment for RDotNet dll
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:Program FilesRR-3.4.1bini386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
// create one instance of RDotNet dll
REngine engine = REngine.CreateInstance("RDotNet");
engine.Initialize();
// some test data - alternative 1- from .NET array to R vector.
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
// some test data - alternative 2 - from direct coding to R vector.
NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
#region: t-test as an example
string testCommand = "t.test(group1, group2)";
GenericVector tTest = engine.Evaluate(testCommand).AsList();
double p = tTest["p.value"].AsNumeric().First();
// present the results on console
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
// THIS WORKED WELL!! OK!! NO ISSUES SO FAR!!
#endregion
#region: adf test as another example
// write the command
testCommand = @"adf.test(group1,alternative=""stationary"")";
Console.WriteLine("testCommand: " + testCommand);
// get the result
engine.Evaluate(testCommand);
GenericVector adfTest = engine.Evaluate(testCommand).AsList();
// THIS RETURNS ERROR!! ERROR MESSAGE IS NOT CLEAR TO UNDERSTAND AND RESOLVE!
#endregion.
// you should always dispose of the REngine properly.
// After disposing of the engine, you cannot reinitialize nor reuse it
engine.Dispose();
}

adf 不工作的基本原因是,执行 adf 测试所需的库应该可用(即通过 R 的 library(( 方法加载和/或附加(

加载库后,此代码未运行的唯一问题只是拼写错误和其他语法错误。

我修改了代码,它现在可以正常工作。

修订后的代码如下(简化版本(:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;
using System.IO;
using commonTools;
namespace RDesk
{
class Program
{


static void Main(string[] args)
{

// set the environment for RDotNet dll
var envPath = Environment.GetEnvironmentVariable("PATH");
var rBinPath = @"C:Program FilesRR-3.4.1bini386";
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
// create one instance of RDotNet dll
REngine engine = REngine.CreateInstance("RDotNet");
engine.Initialize();
// some test data - alternative 1- from .NET array to R vector.
NumericVector group1 = engine.CreateNumericVector(new double[] { 30.02, 29.99, 30.11, 29.97, 30.01, 29.99 });
engine.SetSymbol("group1", group1);
// some test data - alternative 2 - from direct coding to R vector.
NumericVector group2 = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric();
#region: t-test as an example
string testCommand = "t.test(group1, group2)";
GenericVector tTest = engine.Evaluate(testCommand).AsList();
double p = tTest["p.value"].AsNumeric().First();
// present the results on console
Console.WriteLine("Group1: [{0}]", string.Join(", ", group1));
Console.WriteLine("Group2: [{0}]", string.Join(", ", group2));
Console.WriteLine("P-value = {0:0.000}", p);
#endregion

#region: adf test as another example

string adfTestCommand
= " library(tseries) " + System.Environment.NewLine
+ " library(forecast) " + System.Environment.NewLine
+ "adfTest = " + @"adf.test(group1,alternative=""stationary"", k=0)" 
+ System.Environment.NewLine// df test
;
// get the result
// engine.Evaluate(RCommand);
GenericVector adfTest = engine.Evaluate(RCommand).AsList();
#endregion.


// you should always dispose of the REngine properly.
// After disposing of the engine, you cannot reinitialize nor reuse it
engine.Dispose();
}
}
}

最新更新