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

如何使用R.Net在C#中执行ADF测试,c#,r,r.net,C#,R,R.net,我试图使R(及其广泛的分析能力)在我的C#项目中可用 R库和C#环境之间的最终桥梁是R.net 我完成了R.Net的设置,并开始编写我的第一个应用程序 执行简单测试(使用R引擎) 并在控制台上给出了测试结果 就是这样 下面是到目前为止我能够起草的代码,它返回错误 那么,如何调试代码,使R.Net执行ADF测试呢 提前感谢您的时间和帮助 最好的 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading

我试图使R(及其广泛的分析能力)在我的C#项目中可用

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

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

  • 执行简单测试(使用R引擎)
  • 并在控制台上给出了测试结果
  • 就是这样
  • 下面是到目前为止我能够起草的代码,它返回错误

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

    提前感谢您的时间和帮助

    最好的

    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Threading.Tasks;
    使用RDotNet;
    使用System.IO;
    静态void Main(字符串[]参数)
    {
    //设置RDotNet dll的环境
    var envPath=Environment.GetEnvironmentVariable(“路径”);
    var rBinPath=@“C:\Program Files\R\R-3.4.1\bin\i386”;
    Environment.SetEnvironmentVariable(“PATH”,envPath+PATH.PathSeparator+rBinPath);
    //创建一个RDotNet dll实例
    REngine engine=REngine.CreateInstance(“RDotNet”);
    engine.Initialize();
    //一些测试数据-备选方案1-从.NET数组到R向量。
    NumericVector组1=engine.CreateNumericVector(新的双精度[]{30.02,29.99,30.11,29.97,30.01,29.99});
    发动机设置符号(“组1”,组1);
    //一些测试数据-备选方案2-从直接编码到R向量。
    
    NumericVector group2=engine.Evaluate(“group2adf不工作的基本原因是,执行adf测试所需的库应可用(即通过R的library()方法加载和/或附加)

    加载库之后,此代码无法运行的唯一问题就是打字错误和其他语法错误

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

    修订守则如下(简化版):

    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Threading.Tasks;
    使用RDotNet;
    使用System.IO;
    使用通用工具;
    名称空间RDesk
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    //设置RDotNet dll的环境
    var envPath=Environment.GetEnvironmentVariable(“路径”);
    var rBinPath=@“C:\Program Files\R\R-3.4.1\bin\i386”;
    Environment.SetEnvironmentVariable(“PATH”,envPath+PATH.PathSeparator+rBinPath);
    //创建一个RDotNet dll实例
    REngine engine=REngine.CreateInstance(“RDotNet”);
    engine.Initialize();
    //一些测试数据-备选方案1-从.NET数组到R向量。
    NumericVector组1=engine.CreateNumericVector(新的双精度[]{30.02,29.99,30.11,29.97,30.01,29.99});
    发动机设置符号(“组1”,组1);
    //一些测试数据-备选方案2-从直接编码到R向量。
    NumericVector group2=引擎。评估(“group2
    
    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();
    
            }
    
    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();
    
            }
        }
    }