程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> .Net調用R語言,.net調用r

.Net調用R語言,.net調用r

編輯:關於.NET

.Net調用R語言,.net調用r


 ///加載自己寫的R語言算法庫
 public List<double> GetZTFB(double[] data)
        {
            List<double> par = new List<double>();
            try
            {
                //調用R語言算法
                REngine.SetEnvironmentVariables();
                REngine engine = REngine.GetInstance(null, true, null, null);
                NumericVector x = engine.CreateNumericVector(data);
                engine.SetSymbol("x", x);
                string path = System.Windows.Forms.Application.StartupPath + "\\R_File\\ztfb.R";
                path = "source(\"" + path + "\")";
                path = path.Replace("\\", "\\\\");//這一步至關重要,否則會報錯
                engine.Evaluate(path);
                engine.Evaluate("a<- ztfb(x)");

                //                engine.Evaluate(@"normal <- function(theta){
                //                                              mu <- theta[1]
                //                                              sigma <- theta[2]
                //                                              n <- length(x)
                //                                              logL <- -0.5*n*log(2*pi) - n*log(sigma) - sum(0.5*(x - mu)^2/sigma^2)
                //                                              return (-logL)
                //                                            } 
                //                a<- nlminb(c(0,1),normal)");
                NumericVector a = engine.Evaluate("a$par[1]").AsNumeric();
                NumericVector b = engine.Evaluate("a$par[2]").AsNumeric();
                par.Add(Convert.ToDouble(a[0]));
                par.Add(Convert.ToDouble(b[0]));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return par;
        }

///加載R語言下載庫
 public List<double> GetZTFB(double[] data)
        {
            List<double> par = new List<double>();
            try
            {
                //調用R語言算法
                REngine.SetEnvironmentVariables();
                REngine engine = REngine.GetInstance(null, true, null, null);
				///加載R語言庫
				engine.Evaluate("library(\"bit\", lib.loc=\"C:/Program Files/R/R-3.3.2/library\")")
                NumericVector x = engine.CreateNumericVector(data);
                engine.SetSymbol("x", x);
                string path = System.Windows.Forms.Application.StartupPath + "\\R_File\\ztfb.R";
                path = "source(\"" + path + "\")";
                path = path.Replace("\\", "\\\\");//這一步至關重要,否則會報錯
                engine.Evaluate(path);
                engine.Evaluate("a<- ztfb(x)");

                //                engine.Evaluate(@"normal <- function(theta){
                //                                              mu <- theta[1]
                //                                              sigma <- theta[2]
                //                                              n <- length(x)
                //                                              logL <- -0.5*n*log(2*pi) - n*log(sigma) - sum(0.5*(x - mu)^2/sigma^2)
                //                                              return (-logL)
                //                                            } 
                //                a<- nlminb(c(0,1),normal)");
                NumericVector a = engine.Evaluate("a$par[1]").AsNumeric();
                NumericVector b = engine.Evaluate("a$par[2]").AsNumeric();
                par.Add(Convert.ToDouble(a[0]));
                par.Add(Convert.ToDouble(b[0]));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return par;
        }

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved