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

时间:2017-08-23 11:06:30

标签: c# r r.net

我正在努力使R(及其广泛的分析能力)可用于我的C#项目。

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

我最终确定了R.Net的设置并开始编写我的第一个基本上

的应用程序
  1. 执行简单测试(使用R引擎)
  2. 并在控制台上显示测试结果。
  3. 就是这样。
  4. 以下是我到目前为止能够起草的代码,它返回错误!

    那么,我如何调试我的代码以便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 Files\R\R-3.4.1\bin\i386";
            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();
    
            }
    

1 个答案:

答案 0 :(得分:0)

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

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

我修改了代码,现在它正常运行。

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

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 Files\R\R-3.4.1\bin\i386";
            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();

        }
    }
}