static void Main(string[] args) { REngine.SetEnvironmentVariables(); REngine RBlock = REngine.GetInstance(); DirectoryInfo di = new DirectoryInfo("C:\\Users\\" + Environment.UserName + "\\Documents\\File Attachments\\"); string searchPattern = "*"; // Set some bases. These need to be tested for each sample. int avgRowCount = 100; int avgColCount = 20; int fileCount = 0; // Get our files, Create a library to hold everything Library.ListofFiles = Library.makeList(di, searchPattern); // Look Through Each File foreach (var a in Library.ListofFiles) { Console.WriteLine(a.FileName); string fileLocation = di + a.FileName; // Open The File, Set the Page EL.singleExcel thisExcel = new EL.singleExcel().createExcel(fileLocation); EL.singleExcel.ExcelWorkSheetChange(thisExcel, 1); // Get Basic Vars new EDA.excelBasics().basicVars(a, thisExcel); // Run Series of Operations to Get More Exact Bounds EDA.lookTriggers LT = new EDA.lookTriggers(); LT.runTriggers(fileCount, a, avgRowCount, avgColCount, out avgRowCount, out avgColCount); Library.colCounts.Add(a.colCount); Library.rowCounts.Add(a.rowCount); // In the event that no rows are found, the file is not counted if (a.rowCount != 0) { fileCount++; } EL.singleExcel.CloseSheet(thisExcel); //Console.ReadLine(); Console.WriteLine("\n"); } // Run some R analysis IntegerVector rowR = RBlock.CreateIntegerVector(Library.rowCounts); IntegerVector colR = RBlock.CreateIntegerVector(Library.colCounts); RBlock.SetSymbol("rowR", rowR); RBlock.SetSymbol("colR", colR); int[] thisTemp; RBlock.Evaluate("temp <- table(as.vector(rowR))"); thisTemp = RBlock.Evaluate("names(temp)[temp == max(temp)]").AsInteger().ToArray(); Library.groupStats.modeRow = thisTemp[0]; RBlock.Evaluate("temp <- table(as.vector(colR))"); thisTemp = RBlock.Evaluate("names(temp)[temp == max(temp)]").AsInteger().ToArray(); Library.groupStats.modeCol = thisTemp[0]; thisTemp = RBlock.Evaluate("mean(rowR)").AsInteger().ToArray(); Library.groupStats.meanRow = thisTemp[0]; thisTemp = RBlock.Evaluate("mean(colR)").AsInteger().ToArray(); Library.groupStats.meanCol = thisTemp[0]; thisTemp = RBlock.Evaluate("median(rowR)").AsInteger().ToArray(); Library.groupStats.medianRow = thisTemp[0]; thisTemp = RBlock.Evaluate("median(colR)").AsInteger().ToArray(); Library.groupStats.medianCol = thisTemp[0]; thisTemp = RBlock.Evaluate("min(rowR)").AsInteger().ToArray(); Library.groupStats.minRow = thisTemp[0]; thisTemp = RBlock.Evaluate("min(colR)").AsInteger().ToArray(); Library.groupStats.minCol = thisTemp[0]; thisTemp = RBlock.Evaluate("max(rowR)").AsInteger().ToArray(); Library.groupStats.maxRow = thisTemp[0]; thisTemp = RBlock.Evaluate("max(colR)").AsInteger().ToArray(); Library.groupStats.maxCol = thisTemp[0]; thisTemp = RBlock.Evaluate("IQR(rowR)").AsInteger().ToArray(); Library.groupStats.iqrRow = thisTemp[0]; thisTemp = RBlock.Evaluate("IQR(colR)").AsInteger().ToArray(); Library.groupStats.iqrCol = thisTemp[0]; thisTemp = RBlock.Evaluate("quantile(rowR)").AsInteger().ToArray(); Library.groupStats.quantileRow = thisTemp[0]; thisTemp = RBlock.Evaluate("quantile(colR)").AsInteger().ToArray(); Library.groupStats.quantileCol = thisTemp[0]; RBlock.Dispose(); EL.singleExcel.outputObjectToExcel(Library.groupStats); // Build some training data from previous information, assumptions object[,] theWords = TSR.trainMethods.getTrainData(di, "\\testdata\\trainlist.xlsx"); List <string> words = new List <string>(); TSR.trainMethods.makeTrainList(theWords, words); theWords = TSR.trainMethods.getTrainData(di, "\\testdata\\looselist.xlsx"); List <string> looseWords = new List <string>(); TSR.trainMethods.makeTrainList(theWords, looseWords); Dictionary <string, int> discreteTermsDict = new Dictionary <string, int>(); Dictionary <string, int> betterTermsDict = new Dictionary <string, int>(); Dictionary <string, int> secondaryTermsDict = new Dictionary <string, int>(); // Run training scenarios TSR.trainMethods.runTrainScan(words, looseWords, avgColCount, Library.ListofFiles, out discreteTermsDict, out betterTermsDict, out secondaryTermsDict); TSR.trainMethods.buildTrainList(discreteTermsDict, words); TSR.trainMethods.runTrainScan(words, looseWords, avgColCount, Library.ListofFiles, out discreteTermsDict, out betterTermsDict, out secondaryTermsDict); TSR.trainMethods.buildTrainList(betterTermsDict, looseWords); TSR.trainMethods.buildTrainList(secondaryTermsDict, looseWords); // In tests, three cycles make for extremely high confidence TSR.trainMethods.runTrainScan(words, looseWords, avgColCount, Library.ListofFiles, out discreteTermsDict, out betterTermsDict, out secondaryTermsDict); // Terms for classifcation EL.singleExcel.outputListToExcel(words, "strongTrainList"); EL.singleExcel.outputListToExcel(looseWords, "learnedTrainList"); }
public async Task <IActionResult> Get() { //_rEngine.Evaluate("install.packages('forecast', dependencies = TRUE)"); //_rEngine.Evaluate("install.packages('lmtest', dependencies = TRUE)"); _rEngine.Evaluate("library(forecast)"); _rEngine.Evaluate("library(lmtest)"); string fileName = "myplot.png"; CharacterVector fileNameVector = _rEngine.CreateCharacterVector(new[] { fileName }); _rEngine.SetSymbol("fileName", fileNameVector); _rEngine.Evaluate("png(filename=fileName, width=6, height=6, units='in', res=100)"); _rEngine.Evaluate("vector <- as.numeric(AirPassengers)"); _rEngine.Evaluate("z <- ts(vector, frequency = 12, start = c(1949, 1))"); _rEngine.Evaluate("z_train <- ts(z[1:100], frequency = 12)"); _rEngine.Evaluate("z_validate <- ts(z[101:144], frequency = 12)"); _rEngine.Evaluate("fit <- Arima(y = z_train, order = c(1,1,0), seasonal = c(1,1,0), lambda = TRUE)"); _rEngine.Evaluate("predition <- forecast(fit, h = 44)"); //_rEngine.Evaluate("z_train <- ts(z[1:100], frequency = 12"); _rEngine.Evaluate("plot(predition)"); //plot and save file //_rEngine.Evaluate("lines(fit$fitted, col = 'blue')"); _rEngine.Evaluate("dev.off()"); Byte[] b = System.IO.File.ReadAllBytes(@"C:\Users\admin\source\repos\DataAnalisysApi\DataAnalisysApi\myplot.png"); // You can use your own method over here. return(File(b, "image/jpeg")); }
static void Main(string[] args) { // Sample code used for updating the documentation at the codeplex web site. using (REngine engine = REngine.GetInstance()) { var e = engine.Evaluate("x <- 3"); // You can now access x defined in the R environment. NumericVector x = engine.GetSymbol("x").AsNumeric(); engine.Evaluate("y <- 1:10"); NumericVector y = engine.GetSymbol("y").AsNumeric(); // Invoking functions; Previously you may have needed custom function definitions var myFunc = engine.Evaluate("function(x, y) { expand.grid(x=x, y=y) }").AsFunction(); var v1 = engine.CreateIntegerVector(new[] { 1, 2, 3 }); var v2 = engine.CreateCharacterVector(new[] { "a", "b", "c" }); var df = myFunc.Invoke(new SymbolicExpression[] { v1, v2 }).AsDataFrame(); // As of R.NET 1.6, more function call syntaxes are supported. var expandGrid = engine.Evaluate("expand.grid").AsFunction(); var d = new Dictionary <string, SymbolicExpression>(); d["x"] = v1; d["y"] = v2; df = expandGrid.Invoke(d).AsDataFrame(); // querying data frames engine.SetSymbol("cases", df); // As of R.NET 1.6, factor to character expressions work consistently with R engine.Evaluate("head(cases)"); /* * x y * 1 1 a * 2 2 a * 3 3 a * 4 1 b * 5 2 b * 6 3 b */ var letterCases = engine.Evaluate("cases[,'y']").AsCharacter().ToArray(); // "a","a","a","b","b","b", etc. Same as as.character(cases[,'y']) in R // This used to return "1", "1", "1", "2", "2", etc. with R.NET 1.5.5 // Equivalent: letterCases = df[1].AsCharacter().ToArray(); letterCases = df["y"].AsCharacter().ToArray(); // Accessing items by two dimensional indexing string s = (string)df[1, 1]; // "a" s = (string)df[3, 1]; // "a" s = (string)df[3, "y"]; // "b" // s = (string)df["4", "y"]; // fails because there are no row names df[3, "y"] = "a"; s = (string)df[3, "y"]; // "a" df[3, "y"] = "d"; s = (string)df[3, "y"]; // null, because we have an <NA> string in R // invoking a whole script // engine.Evaluate("source('c:/src/path/to/myscript.r')"); // TODO // Date-time objects } }
private void btnRun_Click(object sender, EventArgs e) { frmProgress pfrmProgress = new frmProgress(); pfrmProgress.lblStatus.Text = "Collecting Data:"; pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee; pfrmProgress.Show(); //Get number of variables int intnVar = dgvVariables.Rows.Count - 1; for (int j = 0; j < intnVar; j++) { if (dgvVariables.Rows[j].Cells[0].Value.ToString() == "" || dgvVariables.Rows[j].Cells[1].Value.ToString() == "") { MessageBox.Show("Please select both variable and standard errors.", "Errors"); return; } } clsSnippet pSnippet = new clsSnippet(); // Gets the variable names and indices BhattaVariables[] pBVariable = new BhattaVariables[intnVar]; int nFeature = m_pFClass.FeatureCount(null); for (int j = 0; j < intnVar; j++) { //BhattaVariables tmp = new BhattaVariables(); //tmp.variableNames = (string)dgvVariables.Rows[j].Cells[0].Value; //tmp.errorNames = (string)dgvVariables.Rows[j].Cells[1].Value; //tmp.idVar = m_pFClass.Fields.FindField(tmp.variableNames); //tmp.idError = m_pFClass.Fields.FindField(tmp.errorNames); //tmp.arrVar = new double[nFeature]; //tmp.arrError = new double[nFeature]; //pBVariable.Add(tmp); pBVariable[j] = new BhattaVariables(); pBVariable[j].variableNames = (string)dgvVariables.Rows[j].Cells[0].Value; pBVariable[j].errorNames = (string)dgvVariables.Rows[j].Cells[1].Value; pBVariable[j].idVar = m_pFClass.Fields.FindField(pBVariable[j].variableNames); pBVariable[j].idError = m_pFClass.Fields.FindField(pBVariable[j].errorNames); pBVariable[j].arrVar = new double[nFeature]; pBVariable[j].arrError = new double[nFeature]; } //Get values of var and error from shp file IFeatureCursor pFCursor = m_pFClass.Search(null, true); IFeature pFeature = pFCursor.NextFeature(); //Store independent values at Array int i = 0; while (pFeature != null) { for (int j = 0; j < intnVar; j++) { //arrInDepen[j] = new double[nFeature]; pBVariable[j].arrVar[i] = Convert.ToDouble(pFeature.get_Value(pBVariable[j].idVar)); pBVariable[j].arrError[i] = Convert.ToDouble(pFeature.get_Value(pBVariable[j].idError)); } i++; pFeature = pFCursor.NextFeature(); } pfrmProgress.lblStatus.Text = "Creating Distance Matrix"; if (cboDistance.Text == "Bhattacharyya distance") //Bhatta dist { string strStartPath = m_pForm.strPath; string pathr = strStartPath.Replace(@"\", @"/"); m_pEngine.Evaluate("source('" + pathr + "/clustering.R')"); //Create Matrix for Distance calculation m_pEngine.Evaluate("Bhatta.diss <- matrix(0, " + nFeature.ToString() + ", " + nFeature.ToString() + ")"); StringBuilder[] plotCommmand = new StringBuilder[4]; //Need to optimize 12132017 HK for (int k = 0; k < nFeature; k++) { for (int l = 0; l < k + 1; l++) { for (int j = 0; j < 4; j++) { plotCommmand[j] = new StringBuilder(); } plotCommmand[0].Append("x1 <- cbind("); plotCommmand[1].Append("x2 <- cbind("); plotCommmand[2].Append("v1 <- cbind("); plotCommmand[3].Append("v2 <- cbind("); for (int j = 0; j < intnVar; j++) { plotCommmand[0].Append(pBVariable[j].arrVar[k].ToString() + ", "); plotCommmand[1].Append(pBVariable[j].arrVar[l].ToString() + ", "); plotCommmand[2].Append(pBVariable[j].arrError[k].ToString() + ", "); plotCommmand[3].Append(pBVariable[j].arrError[l].ToString() + ", "); } for (int j = 0; j < 4; j++) { plotCommmand[j].Remove(plotCommmand[j].Length - 2, 2); plotCommmand[j].Append(")"); m_pEngine.Evaluate(plotCommmand[j].ToString()); } m_pEngine.Evaluate("Bhatta.diss[" + (k + 1).ToString() + ", " + (l + 1).ToString() + "] <- Bhatta.mdist(x1, x2, v1, v2)"); m_pEngine.Evaluate("Bhatta.diss[" + (l + 1).ToString() + ", " + (k + 1).ToString() + "] <- Bhatta.mdist(x2, x1, v2, v1)"); } } pfrmProgress.lblStatus.Text = "Finding Clusters"; m_pEngine.Evaluate("sample.hclu <- hclust(as.dist(Bhatta.diss))"); } else if (cboDistance.Text == "Euclidean distance") { StringBuilder plotCommmand = new StringBuilder(); plotCommmand.Append("Euc.dis <- dist(cbind("); for (int j = 0; j < intnVar; j++) { NumericVector vecVar = m_pEngine.CreateNumericVector(pBVariable[j].arrVar); m_pEngine.SetSymbol(pBVariable[j].variableNames, vecVar); plotCommmand.Append(pBVariable[j].variableNames + ", "); } plotCommmand.Remove(plotCommmand.Length - 2, 2); plotCommmand.Append("))"); m_pEngine.Evaluate(plotCommmand.ToString()); pfrmProgress.lblStatus.Text = "Finding Clusters"; m_pEngine.Evaluate("sample.hclu <- hclust(Euc.dis)"); } string strTitle = "Dendrogram"; string strCommand = "plot(sample.hclu);"; pSnippet.drawPlottoForm(strTitle, strCommand); int intNClasses = Convert.ToInt32(nudNClasses.Value); m_pEngine.Evaluate("sample.cut <- cutree(sample.hclu, " + intNClasses.ToString() + ")"); //Save Outputs in SHP pfrmProgress.lblStatus.Text = "Save Results"; //The field names are related with string[] DeterminedName in clsSnippet string strOutputFldName = txtOutputFld.Text; //Get EVs and residuals NumericVector nvClasses = m_pEngine.Evaluate("sample.cut").AsNumeric(); // Create field, if there isn't if (m_pFClass.FindField(strOutputFldName) == -1) { //Add fields IField newField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)newField; fieldEdit.Name_2 = strOutputFldName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; m_pFClass.AddField(newField); } else { DialogResult dialogResult = MessageBox.Show("Do you want to overwrite " + strOutputFldName + " field?", "Overwrite", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.No) { return; } } //Update Field pFCursor.Flush(); pFCursor = m_pFClass.Update(null, false); pFeature = pFCursor.NextFeature(); int featureIdx = 0; int intOutputFldIdx = m_pFClass.FindField(strOutputFldName); while (pFeature != null) { //Update Residuals pFeature.set_Value(intOutputFldIdx, (object)nvClasses[featureIdx]); pFCursor.UpdateFeature(pFeature); pFeature = pFCursor.NextFeature(); featureIdx++; } //Not working properly ////Creating unique value map //IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)m_pFLayer; //IUniqueValueRenderer uniqueValueRenderer = new UniqueValueRendererClass(); //uniqueValueRenderer.FieldCount = 1; //uniqueValueRenderer.set_Field(0, strOutputFldName); //IDataStatistics pDataStat = new DataStatisticsClass(); //pDataStat.Field = strOutputFldName; //ITable pTable = (ITable)m_pFClass; //ICursor pCursor = pTable.Search(null, true); //pDataStat.Cursor = pCursor; //int intUniqValueCnt = pDataStat.UniqueValueCount; //System.Collections.IEnumerator enumerator = pDataStat.UniqueValues; //enumerator.Reset(); //ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); //simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; //IEnumColors enumColors = CreateAlgorithmicColorRamp(intUniqValueCnt).Colors; //for (int j = 0; j < intUniqValueCnt; j++) //{ // string value = enumerator.Current.ToString(); // enumerator.MoveNext(); // simpleFillSymbol = new SimpleFillSymbolClass(); // simpleFillSymbol.Color = enumColors.Next(); // uniqueValueRenderer.AddValue(value, strOutputFldName, simpleFillSymbol as ISymbol); //} ////IFeatureCursor featureCursor = geoFeatureLayer.FeatureClass.Search(null, false); ////IFeature feature; ////if (featureCursor != null) ////{ //// IEnumColors enumColors = CreateAlgorithmicColorRamp(8).Colors; //// //int fieldIndex = geoFeatureLayer.FeatureClass.Fields.FindField("continent"); //// for (int j = 0; j < uniqueValueRenderer.ValueCount; j++) //// { //// string value = uniqueValueRenderer.get_Value(i); //// uniqueValueRenderer.set_Symbol(value, simpleFillSymbol as ISymbol); //// feature = featureCursor.NextFeature(); //// string nameValue = feature.get_Value(intOutputFldIdx).ToString(); //// simpleFillSymbol = new SimpleFillSymbolClass(); //// simpleFillSymbol.Color = enumColors.Next(); //// uniqueValueRenderer.AddValue(nameValue, strOutputFldName, simpleFillSymbol as ISymbol); //// } ////} //geoFeatureLayer.Renderer = uniqueValueRenderer as IFeatureRenderer; //m_pActiveView.Refresh(); //m_pForm.axTOCControl1.Update(); MessageBox.Show("Clustering results are stored in the source shapefile"); pfrmProgress.Close(); }
public ResourcePackage Import(string path, string agencyId) { this.harmonizingCache = new HarmonizingCache(MultilingualString.CurrentCulture); var resourcePackage = new ResourcePackage(); resourcePackage.AgencyId = agencyId; logger.Debug("Importing RData"); if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("fileName"); } if (!File.Exists(path)) { throw new ArgumentException("The specified file must exist"); } string fileNameWithExtension = Path.GetFileName(path); string fileNameOnly = Path.GetFileNameWithoutExtension(path); logger.Debug("RData import file: " + fileNameOnly); resourcePackage.DublinCoreMetadata.Title.Current = fileNameOnly; // Create the PhysicalInstance. var physicalInstance = new PhysicalInstance() { AgencyId = agencyId }; resourcePackage.PhysicalInstances.Add(physicalInstance); physicalInstance.DublinCoreMetadata.Title.Current = fileNameOnly; // File location if (path != null) { DataFileIdentification fileID = new DataFileIdentification(); Uri uri; if (Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out uri)) { fileID.Uri = uri; } fileID.Path = path; physicalInstance.FileIdentifications.Add(fileID); } // Create the DataRelationship. var dataRelationship = new DataRelationship(); physicalInstance.DataRelationships.Add(dataRelationship); dataRelationship.AgencyId = agencyId; dataRelationship.Label.Current = fileNameOnly; // Load the file into R. string pathForR = path.Replace("\\", "/"); engine.Evaluate(string.Format("load('{0}')", pathForR)); // Find all the data frames. var dataFrames = GetDataFrames(); // For each data frame in the RData file, create a LogicalRecord. foreach (var pair in dataFrames) { string name = pair.Key; var dataFrame = pair.Value; // TODO This should be tracked per record, not PhysicalInstance. physicalInstance.FileStructure.CaseQuantity = dataFrame.RowCount; var logicalRecord = new LogicalRecord() { AgencyId = agencyId }; dataRelationship.LogicalRecords.Add(logicalRecord); logicalRecord.Label.Current = name; List <string> variableLabels = null; var variableLabelsExpr = dataFrame.GetAttribute("var.labels"); if (variableLabelsExpr != null) { var labelVector = variableLabelsExpr.AsVector(); variableLabels = new List <string>(labelVector.Select(x => (string)x)); } for (int i = 0; i < dataFrame.ColumnCount; i++) { string columnName = dataFrame.ColumnNames[i]; var column = dataFrame[i]; var variable = new Variable() { AgencyId = agencyId }; logicalRecord.VariablesInRecord.Add(variable); // Name variable.ItemName.Current = columnName; // Label if (variableLabels != null) { variable.Label.Current = variableLabels[i]; } // Type if (column.Type == RDotNet.Internals.SymbolicExpressionType.NumericVector) { variable.RepresentationType = RepresentationType.Numeric; variable.Additivity = AdditivityType.Stock; } else if (column.Type == RDotNet.Internals.SymbolicExpressionType.IntegerVector) { if (column.IsFactor()) { variable.RepresentationType = RepresentationType.Code; string[] factors = column.AsFactor().GetLevels(); variable.CodeRepresentation.Codes = GetCodeList(factors, agencyId, resourcePackage); } else { variable.RepresentationType = RepresentationType.Numeric; variable.NumericRepresentation.NumericType = NumericType.Integer; variable.Additivity = AdditivityType.Stock; } } else if (column.Type == RDotNet.Internals.SymbolicExpressionType.CharacterVector) { variable.RepresentationType = RepresentationType.Text; } } } return(resourcePackage); }
private void InicializeRNet() { string rFilePath = @"C:\\Program Files\\R\\R-3.3.1\\library\\Functions.R"; engine.Evaluate("source(\"" + rFilePath + "\")"); }
public void Test(HomeController controller) { //require R 2.15, package forecast on R //var envPath = Environment.GetEnvironmentVariable("PATH"); //var rBinPath = GetRPath(); //C:\Program Files\R\R-2.15.1\bin\i386 //var BinPath = GetWinRegistryPath(); //var envPath = Environment.GetEnvironmentVariable("PATH"); // this statement not work under iis //Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + BinPath); REngine engine = REngine.GetInstance(); engine.Initialize(); string currentPath = @"C:\Workspace\SAM\SAM\VisualAnalytics\bin"; //Directory.GetCurrentDirectory(); string dataPath = currentPath + @"\data\paper.dat"; string readDataCommand = string.Format("predata <- read.table(\"{0}\", header=FALSE)", dataPath).Replace('\\', '/'); //engine.Evaluate("install.packages(\"forecast\")"); engine.Evaluate("library(jsonlite)"); engine.Evaluate("library(forecast)"); String JsonData = @"http://localhost:25910/Home/getBigTable";//controller.getBigTable().Content; log.Debug("JSON:" + JsonData); string evaluate = string.Format("data <-fromJSON(\"{0}\")", JsonData); log.Debug("evaluateString:" + evaluate); engine.Evaluate(evaluate); //log.Debug(engine.Evaluate("data")); //engine.Evaluate("data <- predata[,1]"); var model = engine.Evaluate("fit <- auto.arima(data$Amount)").AsList(); foreach (var item in model) { log.Debug(item.ToString()); } var coef = model["coef"].AsList(); int lengthData = engine.Evaluate("data$Amount").AsNumeric().Length; double[] dataSeries = new double[lengthData]; double[] errorSeries = new double[lengthData]; engine.Evaluate("data$Amount").AsNumeric().CopyTo(dataSeries, lengthData); model["residuals"].AsNumeric().CopyTo(errorSeries, lengthData); //residuals int arOrder = model["arma"].AsInteger().ElementAt(0); int maOrder = model["arma"].AsInteger().ElementAt(1); int arSeasonOrder = model["arma"].AsInteger().ElementAt(2); int maSeasonOrder = model["arma"].AsInteger().ElementAt(3); int seasonOrder = model["arma"].AsInteger().ElementAt(4); int diffOrder = model["arma"].AsInteger().ElementAt(5); int diffSeasonOrder = model["arma"].AsInteger().ElementAt(6); double[] arCoef = new double[arOrder]; double[] maCoef = new double[maOrder]; double[] arSeasonCoef = new double[arSeasonOrder]; double[] maSeasonCoef = new double[maSeasonOrder]; double intercept = 0; int n = model["coef"].AsNumeric().Length; int start = 0; model["coef"].AsNumeric().CopyTo(arCoef, arOrder, start, 0); start += arOrder; model["coef"].AsNumeric().CopyTo(maCoef, maOrder, start, 0); start += maOrder; model["coef"].AsNumeric().CopyTo(arSeasonCoef, arSeasonOrder, start, 0); start += arSeasonOrder; model["coef"].AsNumeric().CopyTo(maSeasonCoef, maSeasonOrder, start, 0); start += maSeasonOrder; if (n > start) { intercept = model["coef"].AsNumeric().ElementAt(start); } ArimaModel arimaModel = new ArimaModel(arCoef, maCoef, arSeasonCoef, maSeasonCoef, intercept, (uint)seasonOrder, (uint)diffOrder, (uint)diffSeasonOrder); Polynomial arModel = arimaModel.ComputeARModel(); Polynomial maModel = arimaModel.ComputeMAModel(); double interceptModel = arimaModel.ComputeIntercept(); double test = arimaModel.ComputeValue(dataSeries, errorSeries, dataSeries.Length); log.Info("Forecast"); log.Info(test); log.Info("Model"); log.Info(interceptModel); log.Info("Ar"); log.Info(arModel.ToString()); log.Info("Ma"); log.Info(maModel.ToString()); //Console.ReadLine(); }
private void declarePackages() { engine.Evaluate(string.Format("source('{0}')", RCodeControl.RequireFilesAddress)); }
public void test_StartItems_P(int numofItems, ModelNames.Models paramModel) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Polytomous Items CharacterVector modelName = engineObj.CreateCharacterVector(new string[] { paramModel.EnumToString() }); engineObj.SetSymbol("modelName", modelName); DataFrame PolyItems = engineObj.Evaluate("PolyItems <- genPolyMatrix(" + numofItems + ", 5, model = modelName, same.nrCat = TRUE)").AsDataFrame(); engineObj.SetSymbol("PolyItems", PolyItems); // Adapting with the existing "CAT-Items" type (Wrapper) Console.WriteLine("*******************************************"); Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString()); Console.WriteLine("*******************************************"); CATItems itemBank = new CATItems(PolyItems[0].Length, paramModel.EnumToString(), 5); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { itemBank.SetItemParamter(cols[i], PolyItems[i].Select(y => (double)y).ToArray()); } // Call "StartItems" function from R, "MFI" criterion GenericVector r_StartItems = engineObj.Evaluate("r_StartItems <- startItems(PolyItems, model = modelName, nrItems = 3, theta = 1, halfRange = 2)").AsList(); // Resulting item numbers IntegerVector items = r_StartItems[0].AsInteger(); DataFrame tempitems = r_StartItems[1].AsDataFrame(); // Resulting Items CATItems r_CatItems = new CATItems(tempitems[0].Length, paramModel.EnumToString(), 5); for (int i = 0; i < cols.Length; i++) { r_CatItems.SetItemParamter(cols[i], tempitems[i].Select(y => (double)y).ToArray()); } // Resulting NumericVector thStart = r_StartItems[2].AsNumeric(); CharacterVector startSelect = r_StartItems[3].AsCharacter(); // Call "StartItems" function from CS StartItemsModel cs_StartItems = CatRLib.StartItems(itemBank, paramModel.EnumToString(), nrItems: 3, theta: 1, halfRange: 2); // Check items if (items.Length == cs_StartItems.items.Length) { for (int ind = 0; ind < cs_StartItems.items.Length; ind++) { if (items[ind] != cs_StartItems.items[ind]) { resultFlag = false; } } } // Check starting ability values if (thStart.Length == cs_StartItems.thStart.Length) { for (int ind2 = 0; ind2 < cs_StartItems.thStart.Length; ind2++) { if (thStart[ind2] != cs_StartItems.thStart[ind2]) { resultFlag = false; } } } Assert.IsTrue(resultFlag); }
private static void printMemLimit(REngine engine) { Console.WriteLine("************"); Console.WriteLine("*** NOTE: memory.limit() returns: " + engine.Evaluate("memory.limit()").AsNumeric()[0]); Console.WriteLine("************"); }
private void listView_Click(object sender, SelectionChangedEventArgs e) { // Companies id = (Companies)e.AddedItems[0]; // string name = id.Symbol; if (lvUsers.SelectedIndex != -1) { string date; string fileName = @"myplot2.png"; Companies c = (Companies)lvUsers.SelectedItem; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("http://ichart.finance.yahoo.com/table.csv?s={0}&ignore=.csv", c.Symbol)); request.Method = WebRequestMethods.Http.Head; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close(); if (response.StatusCode != HttpStatusCode.OK) { MessageBox.Show("שגיאה: מידע פננסי עבור החברה שנבחרה לא קיים."); this.Graph.Source = null; return; } } catch { MessageBox.Show("שגיאה: מידע פננסי עבור החברה שנבחרה לא קיים."); this.Graph.Source = null; return; } //REngine engine; REngine.SetEnvironmentVariables(); // engine = REngine.GetInstance(); engine.Initialize(); List <string> dates = new List <string>(); List <float> p_list = new List <float>(); for (int i = 14; i > 0; i--) { if (DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).DayOfWeek.ToString() != "Sunday" && DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).DayOfWeek.ToString() != "Saturday") { date = "" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Year.ToString() + "-" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Month.ToString() + "-" + DateTime.Now.Subtract(new TimeSpan(i, 0, 0, 0)).Day.ToString(); dates.Add(date); } } string temp = "getYahooStockUrl <- function(symbol, start, end, type = \"d\") {\n start <- as.Date(start)\n end <- as.Date(end)\n url <-\"http://ichart.finance.yahoo.com/table.csv?s=%s&a=%d&b=%s&c=%s&d=%d&e=%s&f=%s&g=%s&ignore=.csv\"\n sprintf(url,\ntoupper(symbol),\nas.integer(format(start, \"%m\")) - 1,\nformat(start, \"%d\"),\nformat(start, \"%Y\"),\nas.integer(format(end, \"%m\")) - 1, \nformat(end, \"%d\"),\nformat(end, \"%Y\"),\ntype)} \ndata<- read.csv(getYahooStockUrl(\"" + c.Symbol + "\", \"" + dates.First().ToString() + "\", \"" + dates.Last().ToString() + "\"),\nstringsAsFactors = FALSE)"; DataFrame obj = engine.Evaluate(temp).AsDataFrame(); //engine.Evaluate("plot(data$Close,type=\"l\",col=\"red\")"); if (File.Exists(fileName)) { this.Graph.Source = null; File.Delete(fileName); } //CharacterVector uVector = engine.CreateCharacterVector(u_date_list_h); engine.SetSymbol("date", obj["Date"]); //CharacterVector pVector = engine.CreateCharacterVector(help); engine.SetSymbol("value", obj["Close"]); //engine.SetSymbol("obj", obj); CharacterVector fileNameVector = engine.CreateCharacterVector(new[] { fileName }); engine.SetSymbol("fileName", fileNameVector); engine.Evaluate("png(filename=fileName, width=6, height=6, units='in', res=100)"); engine.Evaluate("date <- as.Date(date)"); engine.Evaluate("family <- as.factor(data[,4])"); engine.Evaluate("par(bg = \"transparent\")"); engine.Evaluate("plot(value~date,pch=16, col=family, ann=FALSE)"); obj.Close(); engine.Evaluate("dev.off()"); //engine.Dispose(); var bitmap = new BitmapImage(); var stream = File.OpenRead(fileName); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = stream; bitmap.EndInit(); stream.Close(); stream.Dispose(); this.Graph.Source = bitmap; //MessageBox.Show(Convert.ToString(listView.SelectedIndices[0])); } }
private void btnRun_Click(object sender, EventArgs e) { if (cboFldnm1.Text == "" || cboFldnm2.Text == "") { MessageBox.Show("Please select target field"); return; } frmProgress pfrmProgress = new frmProgress(); pfrmProgress.lblStatus.Text = "Processing:"; pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee; pfrmProgress.Show(); REngine pEngine = m_pForm.pEngine; int nFeature = m_pFClass.FeatureCount(null); IFeatureCursor pFCursor = m_pFClass.Search(null, true); IFeature pFeature = pFCursor.NextFeature(); //Get index for independent and dependent variables //Get variable index string strVarNM1 = (string)cboFldnm1.SelectedItem; string strVarNM2 = (string)cboFldnm2.SelectedItem; int intVarIdx1 = m_pFClass.FindField(strVarNM1); int intVarIdx2 = m_pFClass.FindField(strVarNM2); //Store Variable at Array double[] arrVar1 = new double[nFeature]; double[] arrVar2 = new double[nFeature]; int i = 0; while (pFeature != null) { arrVar1[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx1)); arrVar2[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx2)); i++; pFeature = pFCursor.NextFeature(); } pFCursor.Flush(); //Plot command for R StringBuilder plotCommmand = new StringBuilder(); string strStartPath = m_pForm.strPath; string pathr = strStartPath.Replace(@"\", @"/"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_LARRY.R')"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_neighbor.R')"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_SASbi.R')"); //Get the file path and name to create spatial weight matrix string strNameR = m_pSnippet.FilePathinRfromLayer(m_pFLayer); if (strNameR == null) { return; } //Create spatial weight matrix in R pEngine.Evaluate("library(spdep); library(maptools)"); pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')"); //pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=FALSE)"); pEngine.Evaluate("sample.nb <- poly2nb(sample.shp)"); NumericVector vecVar1 = pEngine.CreateNumericVector(arrVar1); pEngine.SetSymbol("sample.v1", vecVar1); NumericVector vecVar2 = pEngine.CreateNumericVector(arrVar2); pEngine.SetSymbol("sample.v2", vecVar2); //string strRSigLv = nudRsigLv.Value.ToString(); //string strLSigLv = nudLsigLv.Value.ToString(); //string strLSig = cboLocalL.Text; //string strRsig = cboLocalPearson.Text; //string strRowStd = cboRowStandardization.Text; //string strMaxRanges = nudMaxRange.Value.ToString(); //string strHigherOrder = cboHigherOrder.Text; string strNonZero = null; if (chkDiagZero.Checked) { strNonZero = "FALSE"; } else { strNonZero = "TRUE"; } double[] dblLoclLisa = null; if (cboMeasure.Text == "Lee's L") { pEngine.Evaluate("sample.result <- LARRY.bivariate.LISA.lee(sample.v1, sample.v2, 1:length(sample.nb), sample.nb, style = 'W', diag.zero = " + strNonZero + ")"); dblLoclLisa = pEngine.Evaluate("as.numeric(sample.result$local.L)").AsNumeric().ToArray(); } else if (cboMeasure.Text == "Local Pearson") { pEngine.Evaluate("sample.result <- LARRY.bivariate.LISA.pearson(sample.v1, sample.v2, 1:length(sample.nb))"); if (cboMapOption.Text == "Local Pearson") { dblLoclLisa = pEngine.Evaluate("as.numeric(sample.result$local.pearson)").AsNumeric().ToArray(); } else if (cboMapOption.Text == "z-score of variable 1") { dblLoclLisa = pEngine.Evaluate("as.numeric(sample.result$z.x)").AsNumeric().ToArray(); } else if (cboMapOption.Text == "z-score of variable 2") { dblLoclLisa = pEngine.Evaluate("as.numeric(sample.result$z.y)").AsNumeric().ToArray(); } } //Save Output on SHP //Add Target fields to store results in the shapefile // Keep loop for (int j = 0; j < 1; j++) { string strfldName = lvFields.Items[j].SubItems[1].Text; if (m_pFClass.FindField(strfldName) == -1) { IField newField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)newField; fieldEdit.Name_2 = strfldName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; m_pFClass.AddField(newField); } } //Update Field pFCursor = m_pFClass.Update(null, false); pFeature = pFCursor.NextFeature(); string strLocalLISAFldName = lvFields.Items[0].SubItems[1].Text; int intSpQuadFldIdx = m_pFClass.FindField(strLocalLISAFldName); int featureIdx = 0; while (pFeature != null) { pFeature.set_Value(intSpQuadFldIdx, dblLoclLisa[featureIdx]); pFCursor.UpdateFeature(pFeature); pFeature = pFCursor.NextFeature(); featureIdx++; } pFCursor.Flush(); if (chkMap.Checked) { ITable pTable = (ITable)m_pFClass; pFCursor = m_pFClass.Search(null, false); IDataStatistics pDataStat = new DataStatisticsClass(); pDataStat.Field = strLocalLISAFldName; pDataStat.Cursor = (ICursor)pFCursor; IStatisticsResults pStatResults = pDataStat.Statistics; double dblMax = pStatResults.Maximum; double dblMin = pStatResults.Minimum; int intBreaksCount = m_pBiLISASym.Length + 1; double[] cb = new double[intBreaksCount]; //Assign Min and Max values for class breaks cb[0] = dblMin; cb[intBreaksCount - 1] = dblMax; for (int k = 0; k < intBreaksCount - 2; k++) { cb[k + 1] = m_pBiLISASym[k].UValue; } IClassBreaksRenderer pCBRenderer = new ClassBreaksRenderer(); pCBRenderer.Field = strLocalLISAFldName; pCBRenderer.BreakCount = intBreaksCount - 1; pCBRenderer.MinimumBreak = cb[0]; //' use this interface to set dialog properties IClassBreaksUIProperties pUIProperties = (IClassBreaksUIProperties)pCBRenderer; pUIProperties.ColorRamp = "Custom"; ISimpleFillSymbol pSimpleFillSym; //int[,] arrColors = CreateColorRamp(); ////Add Probability Value Manually //string[] strsProbLabels = new string[] { "(0.01)", "(0.05)", "(0.1)", "(0.1)", "(0.05)", "(0.01)" }; //' be careful, indices are different for the diff lists for (int j = 0; j < intBreaksCount - 1; j++) { pCBRenderer.Break[j] = cb[j + 1]; pCBRenderer.Label[j] = m_pBiLISASym[j].Label; pUIProperties.LowBreak[j] = cb[j]; pSimpleFillSym = new SimpleFillSymbolClass(); IRgbColor pRGBColor = m_pSnippet.getRGB(m_pBiLISASym[j].R, m_pBiLISASym[j].G, m_pBiLISASym[j].B); pSimpleFillSym.Color = (IColor)pRGBColor; pCBRenderer.Symbol[j] = (ISymbol)pSimpleFillSym; } IFeatureLayer pNewFLayer = new FeatureLayerClass(); pNewFLayer.FeatureClass = m_pFClass; pNewFLayer.Name = lvFields.Items[0].SubItems[0].Text + " of " + m_pFLayer.Name; IGeoFeatureLayer pGFLayer = (IGeoFeatureLayer)pNewFLayer; pGFLayer.Renderer = (IFeatureRenderer)pCBRenderer; m_pActiveView.FocusMap.AddLayer(pGFLayer); m_pActiveView.Refresh(); m_pForm.axTOCControl1.Update(); pfrmProgress.Close(); } else { MessageBox.Show("Complete. The results are stored in the shape file"); } }
private async void CalculateScores() { List <double> xRange = mWindow.ParseRange(Delays); List <double> yRange = mWindow.ParseRange(Values); if (!double.TryParse(MaxValue, out maxValueA) || (xRange.Count != yRange.Count)) { mInterface.SendMessageToOutput("Error in computing ranges, inputs must be equal in length."); mInterface.SendMessageToOutput("Counts for Delays and Values were " + xRange.Count + " and " + yRange.Count + " respectively."); mInterface.SendMessageToOutput("Max value was: " + maxValueA); MessageBox.Show("Hmm, check your ranges. These don't seem paired up"); return; } mInterface.SendMessageToOutput("---------------------------------------------------"); mDiscount.maxValue = maxValueA; mDiscount.xArray = xRange; mDiscount.yArray = yRange; mInterface.SendMessageToOutput("Results of Johnson & Bickel criteria: " + mDiscount.getJohnsonBickelCriteria()); var chartWin = new ChartingWindow(); Chart chart = chartWin.FindName("MyWinformChart") as Chart; chart.Series.Clear(); // Style chart areas chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false; chart.ChartAreas[0].AxisX.Minimum = 0; chart.ChartAreas[0].AxisX.Title = "Delays"; chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false; chart.ChartAreas[0].AxisY.Minimum = 0; chart.ChartAreas[0].AxisY.Title = "Values"; int seriesCount = 0; if (runAUC) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Calculating Area Under Curve..."); double AUC = await GetAreaUnderCurve(); mInterface.SendMessageToOutput("Results of AUC were " + AUC.ToString("0.0000")); var series = new Series { Name = "AUC", Color = System.Drawing.Color.Blue, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Point }; chart.Series.Add(series); for (int i = 0; i < xRange.Count; i++) { chart.Series[seriesCount].Points.AddXY(xRange[i], yRange[i]); } chart.Legends.Add(series.Name); seriesCount++; } if (runExp) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Fitting Exponential Model..."); double kVal = 0; try { NumericVector delays = engine.CreateNumericVector(xRange.ToArray()); engine.SetSymbol("delays", delays); NumericVector values = engine.CreateNumericVector(yRange.ToArray()); engine.SetSymbol("values", values); GenericVector testResult = engine.Evaluate("nls(values ~ " + maxValueA + " * exp(-a*delays), start = list(a = 0))").AsList(); engine.SetSymbol("exponentialModel", testResult); kVal = engine.Evaluate("coef(exponentialModel)").AsNumeric().First(); } catch (EvaluationException evalExc) { System.Console.WriteLine(evalExc.ToString()); mInterface.SendMessageToOutput("R methods failed to achieve convergence. Defaulting to SnS backups..."); /* * Fall-back method if convergence issues found */ ExponentialModel exponentialModel = await GetExponentialModel(); kVal = exponentialModel.Constant; } double exponentialED50 = mDiscount.getExponentialED50(kVal); double expR2 = mDiscount.getExponentialR2(kVal); mInterface.SendMessageToOutput("Results of Exponential fit were k: " + kVal.ToString("0.0000") + ", r2: " + expR2.ToString("0.0000")); mInterface.SendMessageToOutput("Results of exponential ED50: " + exponentialED50.ToString("0.0000")); var series = new Series { Name = "Exponential Model", Color = System.Drawing.Color.Purple, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Line }; chart.Series.Add(series); double projected; for (int i = (int)xRange[0]; i < xRange[xRange.Count - 1]; i++) { projected = maxValueA * System.Math.Exp(-kVal * i); chart.Series[seriesCount].Points.AddXY(i, projected); } chart.Legends.Add(series.Name); seriesCount++; } if (runHyp) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Fitting hyperbolic Model..."); double kVal = 0; try { NumericVector delays = engine.CreateNumericVector(xRange.ToArray()); engine.SetSymbol("delays", delays); NumericVector values = engine.CreateNumericVector(yRange.ToArray()); engine.SetSymbol("values", values); GenericVector testResult = engine.Evaluate("nls(values ~ " + maxValueA + " / (1 + a*delays), start = list(a = 0))").AsList(); engine.SetSymbol("hyperbolicModel", testResult); kVal = engine.Evaluate("coef(hyperbolicModel)").AsNumeric().First(); } catch (EvaluationException evalExc) { /* * Fall-back method if convergence issues found */ System.Console.WriteLine(evalExc.ToString()); mInterface.SendMessageToOutput("R methods failed to achieve convergence. Defaulting to SnS backups..."); HyperbolicModel hyperbolicModel = await GetHyperbolicModel(); kVal = hyperbolicModel.Constant; } double hyperbolicED50 = mDiscount.getAinslieED50(kVal); double hypR2 = mDiscount.getHyperbolicR2(kVal); mInterface.SendMessageToOutput("Results of hyperbolic fit were k: " + kVal.ToString("0.0000") + ", r2: " + hypR2.ToString("0.0000")); mInterface.SendMessageToOutput("Results of hyperbolic ED50: " + hyperbolicED50.ToString("0.0000")); var series = new Series { Name = "Hyperbolic Model", Color = System.Drawing.Color.LimeGreen, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Line }; chart.Series.Add(series); double projected; for (int i = (int)xRange[0]; i < xRange[xRange.Count - 1]; i++) { projected = maxValueA / (1 + kVal * i); chart.Series[seriesCount].Points.AddXY(i, projected); } chart.Legends.Add(series.Name); seriesCount++; } if (runQuasi) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Fitting quasi-hyperbolic Model..."); double[] kVal = new double[2]; try { NumericVector delays = engine.CreateNumericVector(xRange.ToArray()); engine.SetSymbol("delays", delays); NumericVector values = engine.CreateNumericVector(yRange.ToArray()); engine.SetSymbol("values", values); GenericVector testResult = engine.Evaluate("nls(values ~ " + maxValueA + " * a * exp(-b*delays), start = list(a = 0.5, b = 0.001))").AsList(); engine.SetSymbol("quasiModel", testResult); kVal = engine.Evaluate("coef(quasiModel)").AsNumeric().ToArray(); } catch (EvaluationException evalExc) { System.Console.WriteLine(evalExc.ToString()); mInterface.SendMessageToOutput("R methods failed to achieve convergence. Defaulting to SnS backups..."); /* * Backup methods */ QuasiHyperbolicModel quasiHyperbolicModel = await GetQuasiHyperbolicModel(); kVal = new double[] { quasiHyperbolicModel.Beta, quasiHyperbolicModel.Constant }; } double quasiED50 = mDiscount.getQuasiED50(kVal[0], kVal[1], maxValueA); double quasiR2 = mDiscount.getQuasiHyperbolicR2(kVal[0], kVal[1]); mInterface.SendMessageToOutput("Results of quasi-hyperbolic fit were beta: " + kVal[0].ToString("0.0000") + " k: " + kVal[1].ToString("0.0000") + ", r2: " + quasiR2.ToString("0.0000")); mInterface.SendMessageToOutput("Results of quasi-hyperbolic ED50: " + quasiED50.ToString("0.0000")); var series = new Series { Name = "Quasi-Hyperbolic Model", Color = System.Drawing.Color.Magenta, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Line }; chart.Series.Add(series); double projected; for (int i = (int)xRange[0]; i < xRange[xRange.Count - 1]; i++) { projected = maxValueA * kVal[0] * System.Math.Exp(-kVal[1] * i); chart.Series[seriesCount].Points.AddXY(i, projected); } chart.Legends.Add(series.Name); seriesCount++; } if (runMyerson) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Fitting hyperboloid (Myerson's) Model..."); double[] kVal = new double[2]; try { NumericVector delays = engine.CreateNumericVector(xRange.ToArray()); engine.SetSymbol("delays", delays); NumericVector values = engine.CreateNumericVector(yRange.ToArray()); engine.SetSymbol("values", values); GenericVector testResult = engine.Evaluate("nls(values ~ " + maxValueA + " / ((1 + a*delays)^b), start = list(a = 0.001, b = 0.2))").AsList(); engine.SetSymbol("myersonModel", testResult); kVal = engine.Evaluate("coef(myersonModel)").AsNumeric().ToArray(); } catch (EvaluationException evalExc) { System.Console.WriteLine(evalExc.ToString()); mInterface.SendMessageToOutput("R methods failed to achieve convergence. Defaulting to SnS backups..."); /* * Backup methods */ HyperboloidMyersonModel hyperboloidModelMyerson = mDiscount.GetHyperboloidMyerson(); kVal = new double[] { hyperboloidModelMyerson.Constant, hyperboloidModelMyerson.Scaling }; } double hyperboloidMyersonED50 = mDiscount.getMyersonED50(kVal[0], kVal[1]); double myersonR2 = mDiscount.getHyperboloidMyersonR2(kVal[0], kVal[1]); mInterface.SendMessageToOutput("Results of hyperboloid fit (Myerson) were k: " + kVal[0].ToString("0.0000") + " s: " + kVal[1].ToString("0.0000") + ", r2: " + myersonR2.ToString("0.0000")); mInterface.SendMessageToOutput("Results of hyperboloid (Myerson) ED50: " + hyperboloidMyersonED50.ToString("0.0000")); var series = new Series { Name = "Hyperboloid (M) Model", Color = System.Drawing.Color.DarkCyan, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Line }; chart.Series.Add(series); double projected; for (int i = (int)xRange[0]; i < xRange[xRange.Count - 1]; i++) { projected = mDiscount.LookupWithS(kVal[0], maxValueA, i, kVal[1]); chart.Series[seriesCount].Points.AddXY(i, projected); } chart.Legends.Add(series.Name); seriesCount++; } if (runRachlin) { mInterface.SendMessageToOutput("---------------------------------------------------"); mInterface.SendMessageToOutput("Fitting hyperboloid (Rachlin's) Model..."); /* * Nuke after verifying with R */ double[] kVal = new double[2]; try { NumericVector delays = engine.CreateNumericVector(xRange.ToArray()); engine.SetSymbol("delays", delays); NumericVector values = engine.CreateNumericVector(yRange.ToArray()); engine.SetSymbol("values", values); GenericVector mod = engine.Evaluate("nls.control(maxiter = 1000)").AsList(); GenericVector testResult = engine.Evaluate("nls(values ~ " + maxValueA + " / (1 + (a*delays)^b), start = list(a = 0.001, b = 0.2))").AsList(); engine.SetSymbol("rachlinModel", testResult); kVal = engine.Evaluate("coef(rachlinModel)").AsNumeric().ToArray(); } catch (EvaluationException evalExc) { System.Console.WriteLine(evalExc.ToString()); mInterface.SendMessageToOutput("R methods failed to achieve convergence. Defaulting to SnS backups..."); /* * Backup methods */ HyperboloidRachlinModel hyperboloidModelRachlin = await GetHyperboloidRachlin(); kVal = new double[] { hyperboloidModelRachlin.Constant, hyperboloidModelRachlin.Scaling }; } double hyperboloidRachlinED50 = mDiscount.getRachlinED50(kVal[0], kVal[1], maxValueA); double rachlinR2 = mDiscount.getHyperboloidRachlinR2(kVal[0], kVal[1]); mInterface.SendMessageToOutput("Results of hyperboloid fit (Rachlin) were k: " + kVal[0].ToString("0.0000") + " s: " + kVal[1].ToString("0.0000") + ", r2: " + rachlinR2.ToString("0.0000")); mInterface.SendMessageToOutput("Results of hyperboloid (Rachlin) ED50: " + hyperboloidRachlinED50.ToString("0.0000")); var series = new Series { Name = "Hyperboloid (R) Model", Color = System.Drawing.Color.Crimson, IsVisibleInLegend = true, IsXValueIndexed = false, ChartType = SeriesChartType.Line }; chart.Series.Add(series); double projected; for (int i = (int)xRange[0]; i < xRange[xRange.Count - 1]; i++) { projected = mDiscount.lookUpWithSInside(kVal[0], maxValueA, i, kVal[1]); chart.Series[seriesCount].Points.AddXY(i, projected); } chart.Legends.Add(series.Name); seriesCount++; } chart.Legends[0].IsDockedInsideChartArea = true; chartWin.Owner = windowRef; chartWin.Show(); }
public DataTable getCorpTable(string Rname, int stocknum) { engine.Initialize(); string fullRFilePath = Path.Combine(currentPath, Rname + ".R"); if (!File.Exists(fullRFilePath)) { return(null); } var curPath = engine.CreateCharacter(currentPath); engine.SetSymbol("stocknum", engine.CreateInteger(stocknum)); engine.SetSymbol("curPath", curPath); engine.Evaluate("setwd(curPath)"); engine.Evaluate(String.Format("source(\"{0}\")", Rname + ".R")); DataFrame output = engine.GetSymbol("output").AsDataFrame(); DataTable table = new DataTable(); table.Columns.Add("Included", typeof(bool)); foreach (var name in output.ColumnNames) { Type t; switch (output[name].Type) { case SymbolicExpressionType.NumericVector: t = typeof(double); break; case SymbolicExpressionType.IntegerVector: t = typeof(Int32); break; case SymbolicExpressionType.CharacterVector: t = typeof(string); break; case SymbolicExpressionType.LogicalVector: t = typeof(bool); break; case SymbolicExpressionType.RawVector: t = typeof(byte); break; default: t = null; break; } table.Columns.Add(name); if (t != null) { table.Columns[name].DataType = t; } } foreach (DataFrameRow row in output.GetRows()) { DataRow newRow = table.Rows.Add(); newRow["Included"] = true; foreach (var name in output.ColumnNames) { if ((output[name].Type == SymbolicExpressionType.NumericVector || output[name].Type == SymbolicExpressionType.IntegerVector) && !(name.Contains("현재가") || name.Contains("배당") || name.Contains("RANK"))) { newRow[name] = (double.Parse(row[name].ToString())) / 1e8; } else { newRow[name] = row[name]; } } } return(table); }
static void Main(string[] args) { var scriptFile = @"C:\projects\Hackathon\crmcognitive\R-scripts\Data_Grouping.r"; var jsonResponse = @"C:\projects\Hackathon\crmcognitive\R-scripts\output10json.txt"; if (args.Length > 0) { scriptFile = args[0]; jsonResponse = args[1]; } Console.Write("click to start ..."); Console.ReadKey(); REngine.SetEnvironmentVariables(); REngine engine = REngine.GetInstance(); engine.Initialize(); string text = File.ReadAllText(scriptFile); var myFunc = engine.Evaluate(text).AsFunction(); var v1 = engine.CreateCharacter(jsonResponse); var df = myFunc.Invoke(new SymbolicExpression[] { v1 }).AsDataFrame(); var dt = RDataFrameToDataSet(df); string[] cols; string[,] vals; string test = ""; var valueStrings = new string[df.RowCount]; var arrValueStrings = new string[1, df.RowCount]; var strList = new List <string>(); for (var i = 0; i < df.RowCount; i++) { for (var j = 0; j < df.ColumnCount; j++) { test = test + Convert.ToString(df[j][i]) + ","; } test = test.Remove(test.Length - 1, 1); valueStrings[i] = test; strList.Add(test); test = ""; } for (var x = 0; x < valueStrings.Length; x++) { arrValueStrings[0, x] = valueStrings[x]; } var arrString = strList.ToArray(); //Values = new string[,] { {Convert.ToString(valueStrings[0])} }; var strArr = new List <string[]> { valueStrings }; var splitStrArr = strArr.ToArray(); Console.Write("click to finish ..."); Console.ReadKey(); }
public void test_NextItem_D(int numofItems, ModelNames.CriterionTypes paramCriterion) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = " + numofItems + ")").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); // Adapting with the existing "CAT-Items" type (Wrapper) CATItems itemBank = new CATItems(DichoItems[0].Length, false); // New Dictionary itemBank.SetItemParamter(CATItems.ColumnNames.a, DichoItems[0].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.b, DichoItems[1].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.c, DichoItems[2].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.d, DichoItems[3].Select(y => (double)y).ToArray()); // Call "thetaEst" function //NumericVector r_th = engineObj.Evaluate("r_th <- thetaEst(rbind(DichoItems[3,], DichoItems[13,]), c(0, 1), method = \"WL\")").AsNumeric(); //engineObj.SetSymbol("r_th", r_th); //double cs_th = CatRLib.ThetaEst(itemBank.FindItem(new int[] { 3, 13 }), x: new int[] { 0, 1 }, model: "", method: ModelNames.EstimaatorMethods.WL.EnumToString()); // Call "NextItem" function from R with parameter "out" GenericVector r_NextItem = engineObj.Evaluate("r_NextItem <- nextItem(DichoItems, theta = 0, out = c(3, 13), criterion = \"" + paramCriterion.ToString() + "\")").AsList(); // Call "NextItem" function from R with response pattern "x" //GenericVector r_NextItem = engineObj.Evaluate("r_NextItem <- nextItem(DichoItems, x = c(0, 1), out = c(3, 13), theta = r_th, criterion = \"" + paramCriterion.ToString() + "\")").AsList(); // Selected item NumericVector item = r_NextItem[0].AsNumeric(); DataFrame par = r_NextItem[1].AsDataFrame(); // Item parameter CATItems parItems = new CATItems(par[0].Length, false); parItems.SetItemParamter(CATItems.ColumnNames.a, par[0].Select(y => (double)y).ToArray()); parItems.SetItemParamter(CATItems.ColumnNames.b, par[1].Select(y => (double)y).ToArray()); parItems.SetItemParamter(CATItems.ColumnNames.c, par[2].Select(y => (double)y).ToArray()); parItems.SetItemParamter(CATItems.ColumnNames.d, par[3].Select(y => (double)y).ToArray()); // Value of "info" NumericVector info = r_NextItem[2].AsNumeric(); // Criterion CharacterVector startSelect = r_NextItem[3].AsCharacter(); // Call "NextItem" function from CS with parameter "out" NextItemModel cs_NextItem = CatRLib.NextItem(itemBank, theta: 0, Out: new int[] { 3, 13 }, criterion: (int)paramCriterion); // Call "NextItem" function from CS with response pattern "x" //NextItemModel cs_NextItem = CatRLib.NextItem(itemBank, theta: cs_th, Out: new int[] { 3, 13 }, x: new int[] { 0, 1 }, criterion: (int)paramCriterion); // Checking item & other values if (item[0] != cs_NextItem.item) { resultFlag = false; } if (decimal.Round((decimal)info[0], 3) != decimal.Round((decimal)cs_NextItem.info, 3)) { resultFlag = false; } Assert.IsTrue(resultFlag); }
public static bool Init(Thor model) { try { _model = model; Engine.AutoPrint = false; Engine.Evaluate(".libPaths('./RPackages')"); Engine.Evaluate("if (!require('ks')) install.packages('ks', lib='./RPackages',repos='https://cran.uni-muenster.de')"); Engine.Evaluate("if (!require('ggplot2')) install.packages('ggplot2', lib='./RPackages',repos='https://cran.uni-muenster.de')"); Engine.Evaluate("if (!require('scales')) install.packages('scales', lib='./RPackages',repos='https://cran.uni-muenster.de')"); Engine.Evaluate("if (!require('gridExtra')) install.packages('gridExtra', lib='./RPackages',repos='https://cran.uni-muenster.de')"); Engine.Evaluate("if (!require('scatterplot3d')) install.packages('scatterplot3d', lib='./RPackages',repos='https://cran.uni-muenster.de')"); Engine.Evaluate("suppressMessages(require('ks', quietly=TRUE))"); Engine.Evaluate("suppressMessages(library(ks,lib.loc='./RPackages'))"); Engine.Evaluate("require('ggplot2', quietly=TRUE)"); Engine.Evaluate("suppressMessages(library(ggplot2,lib.loc='./RPackages'))"); Engine.Evaluate("require('scales', quietly=TRUE)"); Engine.Evaluate("suppressMessages(library(scales,lib.loc='./RPackages'))"); Engine.Evaluate("require(gridExtra)"); Engine.Evaluate("suppressMessages(library(gridExtra,lib.loc='./RPackages'))"); Engine.Evaluate("require(scatterplot3d)"); Engine.Evaluate("suppressMessages(library(scatterplot3d,lib.loc='./RPackages'))"); Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); return(true); } catch (EvaluationException) { return(false); } }
public void test_StartItems_D(int numofItems) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = " + numofItems + ")").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); // Adapting with the existing "CAT-Items" type (Wrapper) CATItems itemBank = new CATItems(DichoItems[0].Length, false); // New Dictionary itemBank.SetItemParamter(CATItems.ColumnNames.a, DichoItems[0].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.b, DichoItems[1].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.c, DichoItems[2].Select(y => (double)y).ToArray()); itemBank.SetItemParamter(CATItems.ColumnNames.d, DichoItems[3].Select(y => (double)y).ToArray()); // Call "StartItems" function from R, "MFI" criterion GenericVector r_StartItems = engineObj.Evaluate("r_StartItems <- startItems(DichoItems, nrItems = 3, theta = 1, halfRange = 2)").AsList(); // Resulting item numbers IntegerVector items = r_StartItems[0].AsInteger(); DataFrame tempitems = r_StartItems[1].AsDataFrame(); // Resulting Items CATItems r_CatItems = new CATItems(tempitems[0].Length, false); r_CatItems.SetItemParamter(CATItems.ColumnNames.a, tempitems[0].Select(y => (double)y).ToArray()); r_CatItems.SetItemParamter(CATItems.ColumnNames.b, tempitems[1].Select(y => (double)y).ToArray()); r_CatItems.SetItemParamter(CATItems.ColumnNames.c, tempitems[2].Select(y => (double)y).ToArray()); r_CatItems.SetItemParamter(CATItems.ColumnNames.d, tempitems[3].Select(y => (double)y).ToArray()); // Ability value NumericVector thStart = r_StartItems[2].AsNumeric(); // Criterion CharacterVector startSelect = r_StartItems[3].AsCharacter(); // Call "StartItems" function from CS StartItemsModel cs_StartItems = CatRLib.StartItems(itemBank, nrItems: 3, theta: 1, halfRange: 2); // Check selected items if (items.Length == cs_StartItems.items.Length) { for (int ind = 0; ind < cs_StartItems.items.Length; ind++) { if (items[ind] != cs_StartItems.items[ind]) { resultFlag = false; } } } // Check starting ability values if (thStart.Length == cs_StartItems.thStart.Length) { for (int ind2 = 0; ind2 < cs_StartItems.thStart.Length; ind2++) { if (thStart[ind2] != cs_StartItems.thStart[ind2]) { resultFlag = false; } } } Assert.IsTrue(resultFlag); }
/// <summary> /// Models the change. /// </summary> /// <param name="JSONmodelWithWeather">The jso nmodel with weather.</param> /// <param name="modelName">Name of the model.</param> /// <param name="wc">The wc.</param> /// <exception cref="System.Exception">ARIMA error</exception> public void modelChange(string JSONmodelWithWeather, string modelName, WeatherColumns wc) { this.Init(); // string modelData = "modelData"; string evaluate = string.Format("{0} <-fromJSON(\'{1}\')", modelData, JSONmodelWithWeather); //log.Debug("evaluateString:" + evaluate); rEngine.Evaluate(evaluate); string weatherModel = "modelData"; //evaluate = string.Format("{0} <-fromJSON(\'{1}\')", weatherModel, JSONweather); //log.Debug("evaluateString:" + evaluate); //rEngine.Evaluate(evaluate); CreateWeatherMatrix(modelData, wc); //log.Debug(engine.Evaluate("data")); //engine.Evaluate("data <- predata[,1]"); evaluate = string.Format(modelName + "<- auto.arima({0}$Amount,xreg={1})", modelData, WEATHERMATRIX); SymbolicExpression model; try { model = rEngine.Evaluate(evaluate); } catch (Exception ex) { throw new Exception("ARIMA error"); } //foreach (var item in model.AsList()) //{ // log.Debug(item.ToString()); //} var coef = model.AsList()["coef"].AsList(); int lengthData = rEngine.Evaluate(modelData + "$Amount").AsNumeric().Length; double[] dataSeries = new double[lengthData]; double[] errorSeries = new double[lengthData]; model.AsList()["x"].AsNumeric().CopyTo(dataSeries, lengthData); model.AsList()["residuals"].AsNumeric().CopyTo(errorSeries, lengthData); modelWeatherColumns = wc; //residuals }
public void testEapEST_and_EapSEM_P(int NumOfItems, ModelNames.Models paramModel) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Polytomous Items CharacterVector modelName = engineObj.CreateCharacterVector(new string[] { paramModel.EnumToString() }); engineObj.SetSymbol("modelName", modelName); DataFrame PolyItems = engineObj.Evaluate("PolyItems <- genPolyMatrix(" + NumOfItems + ", 5, model = modelName, same.nrCat = TRUE)").AsDataFrame(); engineObj.SetSymbol("PolyItems", PolyItems); // Adapting with the existing "CAT-Items" type (Wrapper) Console.WriteLine("*******************************************"); Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString()); Console.WriteLine("*******************************************"); CATItems itemBank = new CATItems(PolyItems[0].Length, paramModel.EnumToString(), 5); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { itemBank.SetItemParamter(cols[i], PolyItems[i].Select(y => (double)y).ToArray()); } #region "Test block for Ability Values (th)" double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); for (int j = 0; j < th.Length; j++) { string temp = th[j].ToString(nfi); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + "th = " + th[j].ToString(nfi) + ", PolyItems, " + "model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); // Call "EapEST" function from R NumericVector r_eapEst = engineObj.Evaluate("result_Eap <- eapEst(PolyItems, x_val, model = modelName)").AsNumeric(); // Call "EapEST" function from CatRCS double cs_eapEst = CatRLib.EapEST(itemBank, x, paramModel.EnumToString()); /* EapEst function, line 111 needs to be optimized for passing the Test * Check EapSEM function too !! */ // Call "EapSEM" function from R NumericVector r_eapSem = engineObj.Evaluate("result_EapSem <- eapSem(result_Eap, PolyItems, x_val, model = modelName)").AsNumeric(); // Call "EapSEM" function from CatRCS double cs_eapSem = CatRLib.EapSEM(cs_eapEst, itemBank, x, paramModel.EnumToString()); if (r_eapEst[0] - cs_eapEst > testEpsilon || r_eapSem[0] - cs_eapSem > testEpsilon) { resultFlag = false; } } Assert.IsTrue(resultFlag); #endregion }
private void btnRun_Click(object sender, EventArgs e) { if (cboFldnm1.Text == "" || cboFldnm2.Text == "") { MessageBox.Show("Please select target field"); return; } frmProgress pfrmProgress = new frmProgress(); pfrmProgress.lblStatus.Text = "Processing:"; pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee; pfrmProgress.Show(); REngine pEngine = m_pForm.pEngine; // Creates the input and output matrices from the shapefile// string strLayerName = cboTargetLayer.Text; int intLIndex = m_pSnippet.GetIndexNumberFromLayerName(m_pActiveView, strLayerName); ILayer pLayer = m_pForm.axMapControl1.get_Layer(intLIndex); IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFClass = pFLayer.FeatureClass; int nFeature = pFClass.FeatureCount(null); IFeatureCursor pFCursor = pFLayer.Search(null, true); IFeature pFeature = pFCursor.NextFeature(); //Get index for independent and dependent variables //Get variable index string strVarNM1 = (string)cboFldnm1.SelectedItem; string strVarNM2 = (string)cboFldnm2.SelectedItem; int intVarIdx1 = pFClass.FindField(strVarNM1); int intVarIdx2 = pFClass.FindField(strVarNM2); //Store Variable at Array double[] arrVar1 = new double[nFeature]; double[] arrVar2 = new double[nFeature]; int i = 0; while (pFeature != null) { arrVar1[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx1)); arrVar2[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx2)); i++; pFeature = pFCursor.NextFeature(); } pFCursor.Flush(); //Plot command for R StringBuilder plotCommmand = new StringBuilder(); string strStartPath = m_pForm.strPath; string pathr = strStartPath.Replace(@"\", @"/"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_LARRY.R')"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_neighbor.R')"); pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_SASbi.R')"); //Get the file path and name to create spatial weight matrix string strNameR = m_pSnippet.FilePathinRfromLayer(pFLayer); if (strNameR == null) { return; } //Create spatial weight matrix in R pEngine.Evaluate("library(spdep); library(maptools)"); pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')"); //pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=FALSE)"); pEngine.Evaluate("sample.nb <- poly2nb(sample.shp)"); NumericVector vecVar1 = pEngine.CreateNumericVector(arrVar1); pEngine.SetSymbol("sample.v1", vecVar1); NumericVector vecVar2 = pEngine.CreateNumericVector(arrVar2); pEngine.SetSymbol("sample.v2", vecVar2); string strRSigLv = nudRsigLv.Value.ToString(); string strLSigLv = nudLsigLv.Value.ToString(); string strLSig = cboLocalL.Text; string strRsig = cboLocalPearson.Text; string strRowStd = cboRowStandardization.Text; string strMaxRanges = nudMaxRange.Value.ToString(); string strHigherOrder = cboHigherOrder.Text; string strNonZero = null; if (chkDiagZero.Checked) { strNonZero = "FALSE"; } else { strNonZero = "TRUE"; } pEngine.Evaluate("sample.result <- LARRY.bivariate.spatial.range(sample.v1, sample.v2, 1:length(sample.nb), sample.nb, maxsr = " + strMaxRanges + ", pearson.sig = " + strRSigLv + ", lee.sig = " + strLSigLv + ", method.pearson = '" + strRsig + "', method.lee = '" + strLSig + "', type.higher.nb = '" + strHigherOrder + "', type.row.stand = '" + strRowStd + "', alternative = 'two', diag.zero = " + strNonZero + ")"); string[] strSPRanges = pEngine.Evaluate("as.character(sample.result$final.max.sr)").AsCharacter().ToArray(); //Save Output on SHP //Add Target fields to store results in the shapefile // Keep loop for (int j = 0; j < 1; j++) { string strfldName = lvFields.Items[j].SubItems[1].Text; if (pFClass.FindField(strfldName) == -1) { IField newField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)newField; fieldEdit.Name_2 = strfldName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFClass.AddField(newField); } } //Update Field pFCursor = pFClass.Update(null, false); pFeature = pFCursor.NextFeature(); string strSpRangeFldName = lvFields.Items[0].SubItems[1].Text; int intSpQuadFldIdx = pFClass.FindField(strSpRangeFldName); int featureIdx = 0; while (pFeature != null) { pFeature.set_Value(intSpQuadFldIdx, strSPRanges[featureIdx]); pFCursor.UpdateFeature(pFeature); pFeature = pFCursor.NextFeature(); featureIdx++; } pFCursor.Flush(); if (chkMap.Checked) { ITable pTable = (ITable)pFClass; IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass(); pUniqueValueRenderer.FieldCount = 1; pUniqueValueRenderer.set_Field(0, strSpRangeFldName); ISimpleFillSymbol pSymbol; IQueryFilter pQFilter = new QueryFilterClass(); int intTotalCount = 0; string strHeading = null; for (int j = 0; j < m_pRangeSymbols.Length; j++) { pSymbol = new SimpleFillSymbolClass(); pSymbol.Style = esriSimpleFillStyle.esriSFSSolid; pSymbol.Color = m_pSnippet.getRGB(m_pRangeSymbols[j].R, m_pRangeSymbols[j].G, m_pRangeSymbols[j].B); if (j % 5 == 0) { intTotalCount = 0; for (int k = 0; k < 5; k++) { pQFilter.WhereClause = strSpRangeFldName + " = '" + m_pRangeSymbols[j + k].Value + "'"; int intCnt = pTable.RowCount(pQFilter); intTotalCount += intCnt; } strHeading = m_pRangeSymbols[j].Heading + " (" + intTotalCount.ToString() + ")"; pUniqueValueRenderer.AddValue(m_pRangeSymbols[j].Value, strHeading, (ISymbol)pSymbol); pUniqueValueRenderer.set_Label(m_pRangeSymbols[j].Value, m_pRangeSymbols[j].Label); } else { pUniqueValueRenderer.AddValue(m_pRangeSymbols[j].Value, strHeading, (ISymbol)pSymbol); pUniqueValueRenderer.set_Label(m_pRangeSymbols[j].Value, m_pRangeSymbols[j].Label); } } //string strNotSig = "not sig."; //pSymbol = new SimpleMarkerSymbolClass(); //pSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle //pSymbol.Color = m_pSnippet.getRGB(255, 255, 255); //pQFilter.WhereClause = strFinalQuadFldName + " = '" + strNotSig + "'"; //intTotalCount = pTable.RowCount(pQFilter); //pUniqueValueRenderer.AddValue(strNotSig, null, (ISymbol)pSymbol); //pUniqueValueRenderer.set_Label(strNotSig, "Not significant (" + intTotalCount.ToString() + ")"); pUniqueValueRenderer.UseDefaultSymbol = false; IFeatureLayer pNewFLayer = new FeatureLayerClass(); pNewFLayer.FeatureClass = pFClass; pNewFLayer.Name = "Bivariate Spatial Range"; IGeoFeatureLayer pGFLayer = (IGeoFeatureLayer)pNewFLayer; pGFLayer.Renderer = (IFeatureRenderer)pUniqueValueRenderer; m_pActiveView.FocusMap.AddLayer(pGFLayer); m_pActiveView.Refresh(); m_pForm.axTOCControl1.Update(); pfrmProgress.Close(); } else { MessageBox.Show("Complete. The results are stored in the shape file"); } }
public void testEPV_D() { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = 200)").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); // Adapting with the existing "CAT-Items" type (Wrapper) CATItems itemBank = new CATItems(DichoItems[0].Select(y => (double)y).ToArray(), DichoItems[1].Select(y => (double)y).ToArray(), DichoItems[2].Select(y => (double)y).ToArray(), DichoItems[3].Select(y => (double)y).ToArray()); // test for different theta values double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); var stopwatch = new Stopwatch(); stopwatch.Restart(); for (int t = 0; t < th.Length; t++) { string temp = th[t].ToString(nfi); // Dichotomous Items DataFrame it_given_R = engineObj.Evaluate("it_given_R <- DichoItems[c(4, 8),]").AsDataFrame(); engineObj.SetSymbol("it_given_R", it_given_R); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + th[t].ToString(nfi) + ", it_given_R)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); int[] index = new int[] { 4, 8 }; CATItems it_given = new CATItems(index.Length); // itemBank.FindItem(new int[] { 4, 8 }); for (int i = 0; i < index.Length; i++) { it_given.a[i] = itemBank.a[index[i] - 1]; it_given.b[i] = itemBank.b[index[i] - 1]; it_given.c[i] = itemBank.c[index[i] - 1]; it_given.d[i] = itemBank.d[index[i] - 1]; } // Call "EPV" function from R NumericVector r_epv = engineObj.Evaluate("r_epv <- EPV(DichoItems, 1, x_val, " + th[t].ToString(nfi) + ", it_given_R)").AsNumeric(); stopwatch.Restart(); // Call "EPV" function from CatRCS double cs_epv = CatRLib.EPV(itemBank, 1, x, th[t], it_given, ""); Console.WriteLine("Time taken: " + stopwatch.ElapsedMilliseconds); if (r_epv[0] - cs_epv > testEpsilon) { resultFlag = false; } Console.WriteLine(r_epv[0] + " " + cs_epv); } Assert.IsTrue(resultFlag); }
static void runR(double[,] square) { REngine.SetEnvironmentVariables(); REngine engine = REngine.GetInstance(); // REngine requires explicit initialization. // You can set some parameters. engine.Initialize(); NumericMatrix group1 = engine.CreateNumericMatrix(square); //NumericMatrix group1 = engine.CreateNumericMatrix(new double[,] { // { 0, 0, 0, 0, 1 }, // { 0, 0, 0, 1, 1 }, // { 0, 0, 1, 1, 1 }, // { 0, 0, 0, 1, 1 }, // { 0, 0, 0, 0, 1 } //}); engine.SetSymbol("group1", group1); GenericVector testResult = engine.Evaluate("eigen(group1)").AsList(); Console.WriteLine(testResult); var hokus = testResult["values"].AsIntegerMatrix(); var eigenValues = testResult["values"].AsNumeric().ToArray(); var vectorOfVectors = testResult["vectors"].AsNumeric(); int indexNextEigenVector = 0; // double[] eigenVectors //for (int i = 0; i < eigenVectors.Length; i++) //{ // if () //} //Array.Sort(eigenValues, eigenVectors); //_allStatInfo.Sort(new Comparison<StatInfo>((x, y) => DateTime.Compare(x.date, y.date))); double sum = eigenValues.Sum(); double[] kumulative = new double[eigenValues.Length]; // bool firstOccurrence = false; int thresholdIndex = -1; double thresholdValue = 0.6; for (int i = 0; i < eigenValues.Length; i++) { double normalize = eigenValues[i] / sum; if (i == 0) { kumulative[i] = normalize; } else { kumulative[i] = normalize + kumulative[i - 1]; } if (kumulative[i] >= thresholdValue && firstOccurrence == false) { firstOccurrence = true; thresholdIndex = i; } } // engine.Evaluate("plot(group2, type = 'l', col = 'red', lwd = 10,ylab='eigen values')"); var normalizeEigenVaules = engine.CreateNumericVector(kumulative); engine.SetSymbol("v", normalizeEigenVaules); // ylim = c(0, 1),type = 'b') engine.Evaluate("plot(v, type = 'l', col = 'red', lwd = 10,ylab='eigen values',type = 'b')"); var hokz = testResult["values"].AsList().ToArray(); Console.WriteLine("P-value = {0}", hokus[1, 0]); // return eigenValues; }
public void testEPV_P(int NumOfItems, ModelNames.Models paramModel) { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Polytomous Items CharacterVector modelName = engineObj.CreateCharacterVector(new string[] { paramModel.EnumToString() }); engineObj.SetSymbol("modelName", modelName); DataFrame PolyItems = engineObj.Evaluate("PolyItems <- genPolyMatrix(" + NumOfItems + ", 5, model = modelName, same.nrCat = TRUE)").AsDataFrame(); engineObj.SetSymbol("PolyItems", PolyItems); // Adapting with the existing "CAT-Items" type (Wrapper) Console.WriteLine("*******************************************"); Console.WriteLine("Polytomous Items, Model : " + paramModel.EnumToString()); Console.WriteLine("*******************************************"); CATItems itemBank = new CATItems(PolyItems[0].Length, paramModel.EnumToString(), 5); Tuple <CATItems.ColumnNames, int>[] cols = itemBank.GetKeys(); for (int i = 0; i < cols.Length; i++) { itemBank.SetItemParamter(cols[i], PolyItems[i].Select(y => (double)y).ToArray()); } #region "Test block for Ability Values (th)" double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); for (int j = 0; j < th.Length; j++) { string temp = th[j].ToString(nfi); // Polytomous Items DataFrame it_given_R = engineObj.Evaluate("it_given_R <- PolyItems[c(4, 8),]").AsDataFrame(); engineObj.SetSymbol("it_given_R", it_given_R); //Creation of a response pattern for theta value engineObj.Evaluate("set.seed(1)"); NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + th[j].ToString(nfi) + ", it_given_R, model = modelName)").AsNumeric(); engineObj.SetSymbol("x_val", x_val); int[] x = x_val.Select(y => (int)y).ToArray(); CATItems it_given = itemBank.FindItem(new int[] { 4, 8 }); // Call "EPV" function from R NumericVector r_epv = engineObj.Evaluate("r_epv <- EPV(PolyItems, 1, x_val, " + th[j].ToString(nfi) + ", it_given_R, model = modelName)").AsNumeric(); // Call "EPV" function from CatRCS double cs_epv = CatRLib.EPV(itemBank, 1, x, th[j], it_given, paramModel.EnumToString()); if (r_epv[0] - cs_epv > testEpsilon) { resultFlag = false; } } Assert.IsTrue(resultFlag); #endregion }
private void button4_Click(object sender, EventArgs e) { int FeatureNum = app.FeaName.GetLength(0); int SampleNum = app.SamName.GetLength(0); List <double> prob = new List <double>(); List <double> stat = new List <double>(); List <double> pvalue = new List <double>(); double[] bonferroni = new double[FeatureNum]; double[] fdr = new double[FeatureNum]; //int length; int NAnum = 0; REngine.SetEnvironmentVariables(); REngine MSS = REngine.GetInstance(); MSS.Initialize(); NumericMatrix Freq = MSS.CreateNumericMatrix(app.FreqMatrix); MSS.SetSymbol("Freq", Freq); NumericMatrix Count = MSS.CreateNumericMatrix(app.CountMatrix); MSS.SetSymbol("Count", Count); CharacterVector SampleName = MSS.CreateCharacterVector(app.SamName); CharacterVector FeatureName = MSS.CreateCharacterVector(app.FeaName); MSS.SetSymbol("FeatureName", FeatureName); MSS.SetSymbol("SampleName", SampleName); List <string> SampleNameFreq = new List <string>(); for (int i = 0; i < SampleNum; i++) { SampleNameFreq.Add(SampleName[i] + "Freq"); } IntegerVector RFeaNum = MSS.CreateInteger(FeatureNum); NumericVector Ralpha = MSS.CreateNumeric(double.Parse(this.textBox1.Text.ToString())); MSS.SetSymbol("n", RFeaNum); MSS.SetSymbol("alpha", Ralpha); List <List <double> > Freqtemp = new List <List <double> >(); List <double> FreqSum = new List <double>(); MSS.Evaluate("leastnum = ceiling ( qnorm(alpha/2)^2 )"); double leastnum = MSS.Evaluate("leastnum").AsNumeric().First(); if (this.comboBox1.SelectedIndex == 0) { for (int i = 0; i < FeatureNum; i++) { pvalue.Add(100); for (int j = 0; j < SampleNum - 1; j++) { for (int k = j + 1; k < SampleNum; k++) { double probtemp = (app.CountMatrix[i, j] + app.CountMatrix[i, k]) / (app.SampleTotal[j] + app.SampleTotal[k]); double stattemp = (app.FreqMatrix[i, j] - app.FreqMatrix[i, k]) / Math.Sqrt(probtemp * (1 - probtemp) * (1 / app.SampleTotal[j] + 1 / app.SampleTotal[k])); if (double.IsNaN(stattemp)) { stattemp = 0; } NumericVector Rstat = MSS.CreateNumeric(stattemp); MSS.SetSymbol("stat", Rstat); MSS.Evaluate("p.value <- 2*(pnorm(-abs(stat)))"); double pvaluetemp; if ((this.comboBox2.SelectedIndex == 1) && (app.CountMatrix[i, j] < leastnum) && (app.CountMatrix[i, k] < leastnum)) { pvaluetemp = 100; } else { pvaluetemp = MSS.GetSymbol("p.value").AsNumeric().First(); } if (pvaluetemp != 100) { pvalue[i] = Math.Min((double)pvalue[i], (double)pvaluetemp); } } } } NumericVector Rpvalue = MSS.CreateNumericVector(pvalue); MSS.SetSymbol("p.value", Rpvalue); MSS.Evaluate("NAnum = length(p.value[p.value == 100])"); NAnum = Convert.ToInt32(MSS.GetSymbol("NAnum").AsNumeric().First()); MSS.Evaluate("p.value[p.value == 100] = NA"); MSS.Evaluate("bonferroni.p <- p.adjust(p.value,\"bonferroni\")"); MSS.Evaluate("bonferroni.p[which(bonferroni.p == NA)] = 100"); MSS.Evaluate("fdr.p <- p.adjust(p.value,\"fdr\")"); MSS.Evaluate("fdr.p[which(fdr.p == NA)] = 100"); for (int i = 0; i < FeatureNum; i++) { bonferroni[i] = MSS.GetSymbol("bonferroni.p").AsNumeric()[i]; fdr[i] = MSS.GetSymbol("fdr.p").AsNumeric()[i]; } } else if (this.comboBox1.SelectedIndex == 1) { for (int i = 0; i < FeatureNum; i++) { pvalue.Add(100); } for (int j = 0; j < SampleNum - 1; j++) { for (int k = 1; k < SampleNum; k++) { double Sum1 = 0; double Sum2 = 0; for (int i = 0; i < FeatureNum; i++) { Sum1 = Sum1 + app.CountMatrix[i, j]; Sum2 = Sum2 + app.CountMatrix[i, k]; } for (int i = 0; i < FeatureNum; i++) { NumericVector n11 = MSS.CreateNumeric(app.CountMatrix[i, j]); NumericVector n21 = MSS.CreateNumeric(app.CountMatrix[i, k]); NumericVector n12 = MSS.CreateNumeric(Sum1 - app.CountMatrix[i, j]); NumericVector n22 = MSS.CreateNumeric(Sum2 - app.CountMatrix[i, k]); MSS.SetSymbol("n11", n11); MSS.SetSymbol("n12", n12); MSS.SetSymbol("n21", n21); MSS.SetSymbol("n22", n22); MSS.Evaluate("compare <- matrix(c(n11,n12,n21,n22),nr=2)"); MSS.Evaluate("p.value <- fisher.test(compare)$p.value"); double pvaluetemp = MSS.GetSymbol("p.value").AsNumeric().First(); pvalue[i] = Math.Min((double)pvalue[i], (double)pvaluetemp); } } } MSS.Evaluate("bonferroni.p <- p.adjust(p.value,\"bonferroni\")"); MSS.Evaluate("fdr.p <- p.adjust(p.value,\"fdr\")"); for (int i = 0; i < FeatureNum; i++) { bonferroni[i] = MSS.GetSymbol("bonferroni.p").AsNumeric()[i]; fdr[i] = MSS.GetSymbol("fdr.p").AsNumeric()[i]; } } List <string> Annotation = new List <string>(); if (this.checkBox1.Checked) { if (this.radioButton2.Checked) { string strConnCOG; strConnCOG = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + System.Windows.Forms.Application.StartupPath + "/COG.xlsx" + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; OleDbConnection OleConnCOG = new OleDbConnection(strConnCOG); OleConnCOG.Open(); String sqlCOG = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter OleDaExcelCOG = new OleDbDataAdapter(sqlCOG, OleConnCOG); app.OleDsExcleCOG = new DataSet(); OleDaExcelCOG.Fill(app.OleDsExcleCOG, "Sheet1"); OleConnCOG.Close(); for (int i = 0; i < FeatureNum; i++) { for (int j = 0; j < app.OleDsExcleCOG.Tables[0].Rows.Count; j++) { if (string.Equals(FeatureName[i], app.OleDsExcleCOG.Tables[0].Rows[j][0].ToString())) { Annotation.Add(app.OleDsExcleCOG.Tables[0].Rows[j][1].ToString()); } } if (Annotation.Count < i + 1) { Annotation.Add("No Annotation!"); } } } else if (this.radioButton1.Checked) { string strConnPFAM; strConnPFAM = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + System.Windows.Forms.Application.StartupPath + "/PFAM.xlsx" + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; OleDbConnection OleConnPFAM = new OleDbConnection(strConnPFAM); OleConnPFAM.Open(); String sqlPFAM = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter OleDaExcelPFAM = new OleDbDataAdapter(sqlPFAM, OleConnPFAM); app.OleDsExclePFAM = new DataSet(); OleDaExcelPFAM.Fill(app.OleDsExclePFAM, "Sheet1"); OleConnPFAM.Close(); for (int i = 0; i < FeatureNum; i++) { for (int j = 0; j < app.OleDsExclePFAM.Tables[0].Rows.Count; j++) { if (string.Equals(FeatureName[i], app.OleDsExclePFAM.Tables[0].Rows[j][0].ToString())) { Annotation.Add(app.OleDsExclePFAM.Tables[0].Rows[j][1].ToString()); } } if (Annotation.Count < i + 1) { Annotation.Add("No Annotation!"); } } } } DataTable dt = new DataTable(); dt.Columns.Add("Feature", typeof(string)); for (int i = 0; i < SampleNum; i++) { dt.Columns.Add(app.SamName[i], typeof(double)); } dt.Columns.Add("p.value", typeof(double)); dt.Columns.Add("bonferroni.p", typeof(double)); dt.Columns.Add("fdr.p", typeof(double)); dt.Columns.Add("Annotation", typeof(string)); for (int i = 0; i < SampleNum; i++) { dt.Columns.Add(SampleNameFreq[i], typeof(double)); } for (int i = 0; i < FeatureNum; i++) { DataRow dr = dt.NewRow(); dr[0] = FeatureName[i]; for (int j = 1; j < SampleNum + 1; j++) { dr[j] = app.CountMatrix[i, j - 1]; } if (pvalue[i] == 100) { dr[SampleNum + 1] = DBNull.Value; dr[SampleNum + 2] = DBNull.Value; dr[SampleNum + 3] = DBNull.Value; } else { dr[SampleNum + 1] = pvalue[i]; dr[SampleNum + 2] = bonferroni[i]; dr[SampleNum + 3] = fdr[i]; } if (this.checkBox1.Checked) { dr[SampleNum + 4] = Annotation[i]; } else { dr[SampleNum + 4] = null; } for (int j = 0; j < SampleNum; j++) { dr[j + SampleNum + 5] = app.FreqMatrix[i, j]; } dt.Rows.Add(dr); } DataTable dtCopy = dt.Copy(); DataTable dttemp = dt.Copy(); dttemp.Clear(); DataView dv = dt.DefaultView; dv.Sort = "p.value"; dtCopy = dv.ToTable(); for (int i = 0; i < NAnum; i++) { DataRow row = dtCopy.Rows[i]; dttemp.Rows.Add(row.ItemArray); } for (int i = 0; i < NAnum; i++) { dtCopy.Rows.RemoveAt(0); } dtCopy.Merge(dttemp); Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range; long totalCount = dtCopy.Rows.Count; long rowRead = 0; float percent = 0; for (int i = 0; i < dtCopy.Columns.Count - SampleNum; i++) { worksheet.Cells[1, i + 1] = dtCopy.Columns[i].ColumnName; range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1]; range.Interior.ColorIndex = 15; range.Font.Bold = true; } for (int r = 0; r < dtCopy.Rows.Count; r++) { for (int i = 0; i < dtCopy.Columns.Count - SampleNum; i++) { worksheet.Cells[r + 2, i + 1] = dtCopy.Rows[r][i].ToString(); } rowRead++; percent = ((float)(100 * rowRead)) / totalCount; } xlApp.Visible = true; int pnum = 0; for (int i = 0; i < FeatureNum; i++) { try { if (double.Parse(dtCopy.Rows[i][SampleNum].ToString()) < double.Parse(this.textBox1.Text.ToString())) { pnum++; } } catch { } } double[,] df = new double[Math.Min(10, FeatureNum), SampleNum]; for (int i = 0; i < Math.Min(10, FeatureNum); i++) { for (int j = 0; j < SampleNum; j++) { df[i, j] = double.Parse(dtCopy.Rows[i][SampleNum + 5 + j].ToString()); } } if (this.checkBox2.Checked) { string[] rownamesdf = new string[Math.Min(10, FeatureNum)]; for (int i = 0; i < Math.Min(10, FeatureNum); i++) { rownamesdf[i] = dtCopy.Rows[i][0].ToString(); } CharacterVector Rrownamesdf = MSS.CreateCharacterVector(rownamesdf); MSS.SetSymbol("Rownamedf", Rrownamesdf); NumericMatrix Rdf = MSS.CreateNumericMatrix(df); MSS.SetSymbol("Freqdf", Rdf); NumericVector RRow = MSS.CreateNumeric(Math.Min(10, FeatureNum)); MSS.SetSymbol("selrow", RRow); MSS.Evaluate("Freqdf <- as.data.frame(Freqdf)"); MSS.Evaluate("rownames(Freqdf) <- Rownamedf"); MSS.Evaluate("colnames(Freqdf) <- SampleName"); MSS.Evaluate("colournum <- rainbow(dim(Freqdf)[2])"); MSS.Evaluate("plotdata <- t(Freqdf)"); MSS.Evaluate("windows()"); MSS.Evaluate("barplot(plotdata,main=\"features with top varition\",ylab=\"Freq\",beside=TRUE,horiz=FALSE, cex.names=0.6,col=colournum)"); MSS.Evaluate("legend(\"topright\",SampleName,fill=colournum)"); } if (pnum > 0) { double[,] dfall = new double[pnum, SampleNum]; for (int i = 0; i < pnum; i++) { for (int j = 0; j < SampleNum; j++) { dfall[i, j] = double.Parse(dtCopy.Rows[i][SampleNum + 5 + j].ToString()); } } string[] rownamesall = new string[pnum]; for (int i = 0; i < pnum; i++) { rownamesall[i] = dtCopy.Rows[i][0].ToString(); } CharacterVector Rrownamesall = MSS.CreateCharacterVector(rownamesall); MSS.SetSymbol("Rownameall", Rrownamesall); NumericMatrix Rdfall = MSS.CreateNumericMatrix(dfall); MSS.SetSymbol("Freqdfall", Rdfall); NumericVector RRowall = MSS.CreateNumeric(pnum); MSS.SetSymbol("selrowall", RRowall); MSS.Evaluate("Freqdfall <- as.data.frame(Freqdfall)"); MSS.Evaluate("rownames(Freqdfall) <- Rownameall"); MSS.Evaluate("colnames(Freqdfall) <- SampleName"); MSS.Evaluate("distance <- as.dist(1-abs(cor(Freqdfall)))"); if (this.checkBox3.Checked) { MSS.Evaluate("fit <- cmdscale(distance, eig=TRUE, k=2)"); MSS.Evaluate("x <- fit$points[,1]"); MSS.Evaluate("y <- fit$points[,2]"); MSS.Evaluate("minx <- min(x)"); MSS.Evaluate("miny <- min(y)"); MSS.Evaluate("maxx <- max(x)"); MSS.Evaluate("maxy <- max(y)"); MSS.Evaluate("randx <- maxx - minx"); MSS.Evaluate("randy <- maxy - miny"); MSS.Evaluate("llimx <- minx - randx/10"); MSS.Evaluate("hlimx <- maxx + randx/3"); MSS.Evaluate("llimy <- miny - randy/10"); MSS.Evaluate("hlimy <- maxy + randy/3"); MSS.Evaluate("windows()"); MSS.Evaluate("plot(x,y,xlab=\"Coordinate 1\",ylab=\"Coordinate 2\",main=\"MDS\", pch=c(0,1,2,5,6), col=rainbow(7), type=\"p\",xlim = c(llimx,hlimx), ylim = c(llimy,hlimy))"); if (this.comboBox3.SelectedIndex == 0) { MSS.Evaluate("legend(\"topright\",colnames(Freqdfall),pch=c(0,1,2,5,6),col=rainbow(7),cex = 0.8)"); } else if (this.comboBox3.SelectedIndex == 1) { MSS.Evaluate("text(x,y,labels=SampleName,pos=4)"); } } if (this.checkBox4.Checked) { MSS.Evaluate("windows()"); MSS.Evaluate("plot(hclust(distance),main =\"Samples Clust\")"); } } else { MessageBox.Show("No differntially abundant features!!"); } if (this.checkBox5.Checked) { int Rownum = 0; for (int i = 0; i < FeatureNum; i++) { double tempSum = 0; double tempMean = 0; for (int j = 0; j < SampleNum; j++) { tempSum = tempSum + app.FreqMatrix[i, j]; } tempMean = tempSum / (SampleNum); if (tempSum > 0) { FreqSum.Add(tempSum); List <double> tempRow = new List <double>(); for (int j = 0; j < SampleNum; j++) { tempRow.Add(app.FreqMatrix[i, j] / tempMean); } Freqtemp.Add(tempRow); Rownum = Rownum + 1; } } for (int i = 0; i < Rownum; i++) { for (int j = 0; j < SampleNum; j++) { Freqtemp[i][j] = Math.Log(Freqtemp[i][j], 2); if (Freqtemp[i][j] > 1) { Freqtemp[i][j] = 1; } else if (Freqtemp[i][j] < -1) { Freqtemp[i][j] = -1; } } } double[,] dfhm = new double[Math.Min(500, Rownum), SampleNum]; for (int i = 0; i < Math.Min(500, Rownum); i++) { for (int j = 0; j < SampleNum; j++) { dfhm[i, j] = double.Parse(Freqtemp[i][j].ToString()); } } string[] rownameshm = new string[Math.Min(500, Rownum)]; for (int i = 0; i < Math.Min(500, Rownum); i++) { rownameshm[i] = dtCopy.Rows[i][0].ToString(); } CharacterVector Rrownameshm = MSS.CreateCharacterVector(rownameshm); MSS.SetSymbol("Rownamehm", Rrownameshm); NumericMatrix Rdfhm = MSS.CreateNumericMatrix(dfhm); MSS.SetSymbol("Freqdfhm", Rdfhm); NumericVector RRowhm = MSS.CreateNumeric(Math.Min(500, Rownum)); MSS.SetSymbol("plotnum", RRowhm); MSS.Evaluate("Freqdfhm <- as.data.frame(Freqdfhm)"); MSS.Evaluate("rownames(Freqdfhm) <- Rownamehm"); MSS.Evaluate("colnames(Freqdfhm) <- SampleName"); MSS.Evaluate("Freqdfhm <- as.matrix(Freqdfhm)"); MSS.Evaluate("library(pheatmap)"); MSS.Evaluate("windows()"); if (this.checkBox6.Checked) { if (this.checkBox7.Checked) { MSS.Evaluate("pheatmap(Freqdfhm[1:plotnum,],show_rownames=T,cluster_rows=T)"); } else { MSS.Evaluate("pheatmap(Freqdfhm[1:plotnum,],show_rownames=F,cluster_rows=T)"); } } else { if (this.checkBox7.Checked) { MSS.Evaluate("pheatmap(Freqdfhm[1:plotnum,],show_rownames=T,cluster_rows=F)"); } else { MSS.Evaluate("pheatmap(Freqdfhm[1:plotnum,],show_rownames=F,cluster_rows=F)"); } } } this.Close(); }
public void testEapEST_and_EapSEM_D() { resultFlag = true; REngine.SetEnvironmentVariables(); REngine engineObj = REngine.GetInstance(); // Loading a library from R engineObj.Evaluate("library(catR)"); // Test for item banks with different number of items for (int i1 = 0; i1 < numberOfItems.Length; i1++) { // Dichotomous Items DataFrame DichoItems = engineObj.Evaluate("DichoItems <- genDichoMatrix(items = " + numberOfItems[i1] + ")").AsDataFrame(); engineObj.SetSymbol("DichoItems", DichoItems); // Adapting with the existing "CAT-Items" type (Wrapper) CATItems itemBank = new CATItems(DichoItems[0].Select(y => (double)y).ToArray(), DichoItems[1].Select(y => (double)y).ToArray(), DichoItems[2].Select(y => (double)y).ToArray(), DichoItems[3].Select(y => (double)y).ToArray()); // test for different theta values double[] th = CatRcs.Utils.CommonHelper.Sequence(-6, 6, length: 11); var stopwatch = new Stopwatch(); stopwatch.Restart(); for (int i2 = 0; i2 < th.Length; i2++) { string temp = th[i2].ToString(nfi); //Creation of a response pattern for theta value NumericVector x_val = engineObj.Evaluate("x_val <- genPattern(" + th[i2].ToString(nfi) + ", DichoItems)").AsNumeric(); int[] x = x_val.Select(y => (int)y).ToArray(); // Call "EapEST" function from R NumericVector r_eapEst = engineObj.Evaluate("result_Eap <- eapEst(DichoItems, x_val)").AsNumeric(); // Call "EapSEM" function from R NumericVector r_eapSem = engineObj.Evaluate("result_EapSem <- eapSem(result_Eap, DichoItems, x_val)").AsNumeric(); stopwatch.Restart(); // Call "EapEST" function from CatRCS double cs_eapEst = CatRLib.EapEST(itemBank, x, ""); // Call "EapSEM" function from CatRCS double cs_eapSem = CatRLib.EapSEM(cs_eapEst, itemBank, x, ""); Console.WriteLine("Time: " + stopwatch.ElapsedMilliseconds); if (r_eapEst[0] - cs_eapEst > testEpsilon || r_eapSem[0] - cs_eapSem > testEpsilon) { resultFlag = false; } } } Assert.IsTrue(resultFlag, "Test Passed!"); }
/// <summary> /// Initializes the rengine. /// </summary> /// /// <param name="label"> The label. </param> /// <param name="busyBar"> The busy bar control. </param> /// /// <returns> /// True if it succeeds, false if it fails. /// </returns> public static bool InitRengine(Label label, ProgressBar busyBar) { //! There seems to be a mismatch between the name of the MyDocuments special folder on BV5/6 (Dutch/Englis). //! See https://stackoverflow.com/questions/606483/what-is-the-meaning-of-these-windows-environment-variables-homedrive-homepath String mydocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); String mydocfolder = mydocuments.Split(new Char[] { Path.DirectorySeparatorChar }).Last(); Logger.Info($"MyDocuments: '{mydocuments}'."); String display = GetDisplayName(Environment.SpecialFolder.MyDocuments); if (mydocfolder != display) { Logger.Warn($"MyDocuments Name/DisplayName Miscmatch: '{mydocfolder}' / '{display}'."); } String homedrive = Environment.GetEnvironmentVariable("HOMEDRIVE"); String homepath = Environment.GetEnvironmentVariable("HOMEPATH"); String homeshare = Environment.GetEnvironmentVariable("HOMESHARE"); Logger.Info($"HOMEDRIVE: '{homedrive}'."); Logger.Info($"HOMEPATH: '{homepath}'."); Logger.Info($"HOMESHARE: '{homeshare}'."); //! Fix for OUNL BV5/6 (GetFolderPath is Dutch where GetDisplayName returns English). // if (!Directory.Exists(mydocuments) || !String.IsNullOrEmpty(homeshare)) // || !mydocuments.EndsWith(display) { Logger.Info($"MyDocuments issues, using alternative method"); if (!String.IsNullOrEmpty(homedrive)) { mydocuments = Path.Combine( homedrive + homepath, mydocfolder); } else if (!String.IsNullOrEmpty(homeshare)) { mydocuments = Path.Combine(homeshare + homepath, mydocfolder); } Logger.Info($"MyDocuments: '{mydocuments}'."); } label.InvokeIfRequired(o => { label.Text = "Installing R packages"; }); busyBar.InvokeIfRequired(o => { busyBar.Show(); busyBar.Value = busyBar.Maximum; }); try { //! Check R Engine version. // REngine.SetEnvironmentVariables(); //! There are several options to initialize the engine, but by default the following suffice: engine = REngine.GetInstance(); Logger.Info($"Using R v{engine.DllVersion}."); //! R uses only major.minor for custom library folders. // String ver = engine.DllVersion; if (ver.Count(p => p == '.') == 2) { ver = ver.Substring(0, ver.LastIndexOf('.')); } String rpath = $"{mydocuments.Replace("\\", "/")}/R/win-library/{ver}"; Logger.Info($"R Install Path: '{rpath}'."); //! veg 15-07-2019 // //! NOTE: We need an R version that allows Metrics 1.2 or higher. // DllVersion returns v3.6.1 (not 3.6 we need for the private library path) // Debug.WriteLine($"R.dll v{engine.DllVersion}"); //! veg 15-07-2019 // NOTE: We also need Metrics to be installed. // // install.packages('Metrics') // unloadNamespace('Metrics') // library('Metrics') // install.packages('Metrics','R_LIBS=%HOMEDRIVE/%HOMEPATH%/Documents/R/win-library/3.6') // // In My Documents .Renviron file with location library like: // R_LIBS=%HOMEDRIVE%/%HOMEPATH%/Documents/R/win-library/3.6 //---R try { Logger.Info($"Checking presence of R 'Metrics' package."); engine.Evaluate($"library('Metrics', lib.loc = '{rpath}')"); } catch (EvaluationException) { Logger.Info($"Downloading and Installing R 'Metrics' package."); Logger.Info($"Using '{CRAN}' mirror."); engine.Evaluate($"install.packages('Metrics', repos='{CRAN}', lib='{rpath}')"); Logger.Info($"Loading 'Metrics' package into R."); engine.Evaluate($"library('Metrics', lib.loc = '{rpath}')"); } //---R Logger.Info($"Checking R 'Metrics' package version."); { CharacterVector e3 = engine.Evaluate("utils::packageVersion('Metrics')").AsCharacter(); String MetricsVersion = e3[0].Trim(new char[] { 'c', '(', ')' }).Replace(',', '.'); Logger.Info($"Detected Metrics v{MetricsVersion}."); //! Check Metrics Version to be 0.1.4 to check if the R version is recent enough. // Double mul = 1; Double chk = 0; foreach (String part in MetricsVersion.Split('.')) { chk += mul * Int32.Parse(part); mul /= 10; } Logger.Info($"Detected R v{ver}."); if (chk < 0.14) { Logger.Error($"R Version to low to load Metrics v0.1.4 or higher. Please upgrade R."); return(false); } } Logger.Info($"Finding download source for 1.6.9 version of R psych package and its dependencies."); /* * //! Psych dependends on mnormt. * // * * //---R * try * { * Logger.Info($"Checking presence of R 'mnormt' package."); * * engine.Evaluate($"library('mnormt', lib.loc = '{rpath}')"); * } * catch (EvaluationException) * { * Logger.Info($"Downloading and Installing R 'mnormt' package."); * * engine.Evaluate($"install.packages('mnormt', repos='{CRAN}', lib='{rpath}')"); * } * * //---R * * //https://cran.r-project.org/package=%{packname}&version=%{version}#/%{packname}_%{version}.tar.gz * // * * try * { * Logger.Info($"Checking presence of R 'psych' package."); * * engine.Evaluate($"library('psych', lib.loc = '{rpath}')"); * } * catch (EvaluationException) * { * Logger.Info($"Downloading and Installing R 'psych' package."); * * //! Fails because downloaded file is a tar.gz (but is saved without extension). * // * //engine.Evaluate($"install.packages('{CRAN}/package=psych&version=1.6.9', repos=NULL, type='source', lib='{rpath}')"); * // * engine.Evaluate($"install.packages('{CRAN}/src/contrib/Archive/psych/psych_1.6.9.tar.gz', repos=NULL, type='source', lib='{rpath}')"); * * Logger.Info("Loading 'psych' package into R."); * engine.Evaluate($"library('psych', lib.loc = '{rpath}')"); * } * * * * //---R */ try { Logger.Info($"Checking presence of R 'psych' package."); engine.Evaluate($"library('psych', lib.loc = '{rpath}')"); } catch (EvaluationException) { Logger.Info($"Downloading and Installing R 'psych' package."); Logger.Info($"Using '{CRAN}' mirror."); engine.Evaluate($"install.packages('psych', repos='{CRAN}', lib='{rpath}')"); Logger.Info($"Loading 'psych' package into R."); engine.Evaluate($"library('psych', lib.loc = '{rpath}')"); } return(true); } catch (Exception e) { Logger.Error($"{e.GetType().Name} - {e.Message}."); MessageBox.Show( $"Error initializing Rengine:\r\n\r\n{e.Message}", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } finally { label.InvokeIfRequired(o => { label.Text = String.Empty; }); busyBar.InvokeIfRequired(o => { busyBar.Hide(); busyBar.Value = busyBar.Minimum; }); } }
private void analisServer() { //////////////// Парсинг папки с логами for (int i = 0; i < xForPlot.Count; i += 3) { if (startAnalisisSec <= Convert.ToInt32(xForPlot[i])) { startAnalisisIdx = i; break; } } for (int i = xForPlot.Count - 2; i >= 0; i -= 3) { if (endAnalisisSec >= Convert.ToInt32(xForPlot[i])) { endAnalisisIdx = i; break; } } int[] dailyWork = new int[howManyDaysBetween]; int[] scaleMass = new int[15840]; double workingTime = 0; double maxworkingTime = 0; double minworkingTime = Convert.ToInt32(xForPlot[startAnalisisIdx + 1]) - Convert.ToInt32(xForPlot[startAnalisisIdx]); for (int i = 0; i < scaleMass.Length; i++) { scaleMass[i] = 0; } for (int i = 0; i < dailyWork.Length; i++) { dailyWork[i] = 0; } for (int i = startAnalisisIdx + 1; i <= endAnalisisIdx; i += 3) { if ((Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1])) > maxworkingTime) { maxworkingTime = Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1]); } if (((Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1])) < minworkingTime) && (Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1])) != 0) { minworkingTime = Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1]); } workingTime += Convert.ToInt32(xForPlot[i]) - Convert.ToInt32(xForPlot[i - 1]); for (int j = Convert.ToInt32(xForPlot[i - 1]) / 60; j < Convert.ToInt32(xForPlot[i]) / 60; j++) { scaleMass[j] = Convert.ToInt32(xForPlot[i + 1]); } } DateTime tn = new DateTime(2016, 2, 29); int idx = 0; if (firstDate != tn) { idx = 1440 * firstDate.Day; } for (int i = 0; i < dailyWork.Length; i++) { for (int j = idx + 1440 * i; j < 1440 * i + 1440 + idx; j++) { dailyWork[i] += scaleMass[j]; } dailyWork[i] /= 1440; } //////////////////////Analis double[] dailyWorkExpSmooth = new double[dailyWork.Length]; double[] dailyWorkExpSmoothLog = new double[dailyWorkExpSmooth.Length]; double coeffReA = 0; double coeffReB = 0; double sumXY = 0; double sumX = 0; double sumY = 0; double sumSqX = 0; dailyWorkExpSmooth[0] = dailyWork[0]; for (int i = 1; i < dailyWorkExpSmooth.Length; i++) { dailyWorkExpSmooth[i] = dailyWorkExpSmooth[i - 1] + 0.5 * (dailyWork[i] - dailyWorkExpSmooth[i - 1]); } //foreach (double i in dailyWorkExpSmooth) //{ // richTextBox1.Text += Convert.ToString(i) + "\n"; //} for (int i = 0; i < dailyWorkExpSmooth.Length - 4; i++) { sumXY += Math.Log(i + 1) * dailyWorkExpSmooth[i]; sumX += Math.Log(i + 1); sumY += dailyWorkExpSmooth[i]; sumSqX += Math.Pow(Math.Log(i + 1), 2); } coeffReA = ((dailyWorkExpSmooth.Length - 4) * sumXY - sumX * sumY) / ((dailyWorkExpSmooth.Length - 4) * sumSqX - Math.Pow(sumX, 2)); coeffReB = (sumY - coeffReA * sumX) / (dailyWorkExpSmooth.Length - 4); for (int i = 0; i < dailyWorkExpSmoothLog.Length; i++) { dailyWorkExpSmoothLog[i] = coeffReA * Math.Log(i + 1) + coeffReB; } /////////////////////////// //richTextBox1.Text = ""; //richTextBox1.Text += "" + Convert.ToString(howManyDaysBetween) + "\n"; //richTextBox1.Text += Convert.ToString(firstDate) + " " + Convert.ToString(secondDate) + "\n"; //richTextBox1.Text += Convert.ToString(dailyWorkExpSmooth.Length - 4) + "\n"; //richTextBox1.Text += Convert.ToString(coeffReA) + " " + Convert.ToString(coeffReB) + "\n sumXY " + Convert.ToString(sumXY) + " SumX" + Convert.ToString(sumX) + " SumY " + Convert.ToString(sumY) + " SumX^2 " + Convert.ToString(sumSqX) + "\n"; //foreach (double i in dailyWork) //{ // richTextBox1.Text += string.Format("{0}\n", i); //} double downtime = (endAnalisisSec - startAnalisisSec - workingTime); double coefOfProd = workingTime / (endAnalisisSec - startAnalisisSec) * 100; // minworkingTime = minworkingTime / 60 / 60; label16.Visible = true; label17.Visible = true; label18.Visible = true; label19.Visible = true; label20.Visible = true; secMinHour(workingTime, label16, "Working time: "); secMinHour(downtime, label17, "Downtime: "); label18.Text = string.Format("Сoefficient of productivity: {0}%\n", (int)coefOfProd); secMinHour(maxworkingTime, label19, "Maximum worktime: "); secMinHour(minworkingTime, label20, "Minimum worktime: "); //richTextBox1.Text += string.Format("Working time: {0}\n", workingTime); //richTextBox1.Text += string.Format("Downtime: {0}\n", downtime); //richTextBox1.Text += string.Format("Сoefficient of productivity: {0}%\n", coefOfProd); //richTextBox1.Text += string.Format("Maximum worktime: {0} hours\n", maxworkingTime); //richTextBox1.Text += string.Format("Minimum worktime: {0} hours\n", minworkingTime); ///////////////////////////using R string typeOfModel = toolStripComboBox1.Text; if (dailyWork.Length <= 3) { MessageBox.Show("Time serie cannot consist from one element!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { REngine.SetEnvironmentVariables(); REngine engine = REngine.GetInstance(); // REngine requires explicit initialization. // You can set some parameters. engine.Initialize(); double[] forR = new double[dailyWork.Length]; for (int i = 0; i < forR.Length; i++) { forR[i] = dailyWork[i]; mSW.WriteLine(Convert.ToString(forR[i]) + " " + Convert.ToString(dailyWork[i] + "\n")); } NumericVector dd = engine.CreateNumericVector(forR); engine.SetSymbol("dd", dd); engine.Evaluate("library(FBN)"); engine.Evaluate("fit <- medianFilter(dd, windowSize = 3)"); engine.Evaluate("library(forecast)"); //engine.Evaluate("capture.output (fit$fitted[, 1], file = 'tmp.txt')"); //Double[] massForPlotInR = new Double [dailyWork.Length - 1]; //String[] strForMassForPlotInR = null; //if (File.Exists("tmp.txt")) //{ // StreamReader rForTmp = new StreamReader("tmp.txt"); int t = 0; // while (t < 4) // { // rForTmp.ReadLine(); // t++; // } // int ii = 0; // string str = null; // while ((str = rForTmp.ReadLine()) != null) // { // // string str = rForTmp.ReadLine(); // str = str.Replace('.', ','); // strForMassForPlotInR = str.Split(' '); // for (int j = 1; j < strForMassForPlotInR.Length; j++) // { // if ((strForMassForPlotInR[j] != "") && (!(strForMassForPlotInR[j].Contains('[')))) // { // massForPlotInR[ii] = Convert.ToDouble(strForMassForPlotInR[j]); // ii++; // } // //i = strForMassForPlotInR.Length - 1; // } // } // //for (int i = 0; i < massForPlotInR.Length; i++) // //{ // // richTextBox2.Text += Convert.ToString(massForPlotInR[i]) + "\n"; // //} // rForTmp.Close(); //} //NumericVector fit_fitted = engine.CreateNumericVector(massForPlotInR); //engine.SetSymbol("fit_fitted", fit_fitted); if (radioButton2.Checked == true) { engine.Evaluate("plot(c(dd, median(dd[(length(dd)-3):length(dd)])), type = 'p', col = 'blue', xlab = 'Time', ylab = 'Processor load')"); engine.Evaluate("lines(c(dd, NA), type = 'o')"); engine.Evaluate("legend('topleft', c('Initial data', 'Forecast'), col = c('black','blue'), pch = c(1,1))"); } else if (radioButton1.Checked == true) { engine.Evaluate("plot(dd, xlab = 'Days', ylab = 'Processor load', type = 'l')"); engine.Evaluate("lines(fit, col = 'red', type = 'l')"); engine.Evaluate("legend('topleft', c('Initial data', 'Model'), col = c('black','red'), pch = c(1,1))"); //engine.Evaluate("legend()"); //engine.Evaluate("lines(fit_fitted, col = 'red')"); } NumericVector ddRes = engine.Evaluate("median(dd[(length(dd)-3):length(dd)])").AsNumeric(); label21.Text = string.Format("Forecast on day {0}%", string.Join(", ", ddRes)); label21.Visible = true; } }
public string run_ruvseq(List <string> group1, List <string> group2) { try { engine = REngine.GetInstance(); engine.Initialize(); engine.Evaluate(string.Format("Sys.setenv(PATH = paste('{0}', ';', Sys.getenv('PATH'), sep=''))", this.rPath)); engine.Evaluate("Sys.getenv()"); engine.Evaluate("library('RUVSeq')"); engine.Evaluate("library('DESeq2')"); engine.Evaluate("library('RColorBrewer')"); engine.Evaluate("matrix <- read.table('" + inputFile + "', header=TRUE, sep=',', fill=TRUE, row.names=1)"); engine.Evaluate("matrix <- matrix + 1;" + "matrix <- round(matrix, 0);" + "filter <- apply(matrix, 1, function(x) length(x[x > 5]) >= 2);" + "filtered <- matrix[filter,];" + "genes <- rownames(filtered)[grep('^ENS', rownames(filtered))];" + "spikes <- rownames(filtered)[grep('^ERCC', rownames(filtered))];"); CharacterVector colnames = engine.Evaluate("colnames(filtered)").AsCharacter(); var x = engine.CreateCharacterVector(this.groupSeparation(colnames, group1, group2)); engine.SetSymbol("x", x); engine.Evaluate("print(x)"); engine.Evaluate("print(colnames(filtered))"); engine.Evaluate("colors <- brewer.pal(4, 'Set1');" + "set <- newSeqExpressionSet(as.matrix(filtered), phenoData = data.frame(x, row.names=colnames(filtered)));" + "set <- betweenLaneNormalization(set, which='upper');" + "set1 <- RUVg(set, spikes, k=1);" + "write.csv(normCounts(set1), paste('" + outputFileDirectory + "\\\\" + outputFile + "','_normalizedCounts.csv', sep=''));" + "write.csv(counts(set1), paste('" + outputFileDirectory + "\\\\" + outputFile + "', '_counts.csv', sep = ''));" + "write.csv(pData(set1), paste('" + outputFileDirectory + "\\\\" + outputFile + "','_colData.csv', sep = ''));"); engine.Evaluate(@"png(file = paste('" + outputFileDirectory + "\\\\" + outputFile + "','_graphRLE_RUV.png', sep = ''), height = 720, width = 1280);" + "plotRLE(set1, outline = FALSE, ylim = c(-4, 4), col = colors[x], xaxt = 'n', ann = FALSE);" + "text(x = seq(1, length(colnames(filtered))) - .2, par('usr')[3] - .4, labels = colnames(filtered), adj = c(1.5, 1.5), srt = 60, pos = 1, xpd = TRUE, cex = 1);" + "axis(1, at = seq(1, length(colnames(filtered)), by = 1), labels = FALSE);" + "dev.off();"); engine.Evaluate("png(file = paste('" + outputFileDirectory + "\\\\" + outputFile + "', '_graphPCA_RUV.png', sep = '), height = 720, width = 1280);" + "plotPCA(set1, col = colors[x], cex = 1.2);" + "dev.off();"); return("Success"); } catch (RDotNet.EvaluationException e) { return(e.ToString()); } finally { engine.Dispose(); } }
private void btnGenerate_Click(object sender, EventArgs e) { try { frmProgress pfrmProgress = new frmProgress(); pfrmProgress.lblStatus.Text = "Processing:"; pfrmProgress.Show(); int intProgress = 0; REngine pEngine = mForm.pEngine; //Declare constants double dblDistError = Convert.ToDouble(txtDistance.Text); double dblDirection = Convert.ToDouble(txtDirections.Text); int intNSimulation = Convert.ToInt32(txtNSimulation.Text); string strX = cboX.Text; string strY = cboY.Text; mForm.strValue = cboFieldName.Text; //Load Source Layer string strLayerName = cboSourceLayer.Text; int intLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strLayerName); ILayer pLayer = mForm.axMapControl1.get_Layer(intLIndex); IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFClass = pFLayer.FeatureClass; int nFeature = pFClass.FeatureCount(null); IFeatureCursor pFCursor = pFLayer.Search(null, true); IFeature pFeature = pFCursor.NextFeature(); //Load Target Layer mForm.strTargetLayerName = cboTargetLayer.Text; int intTargetIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, mForm.strTargetLayerName); ILayer pTargetLayer = mForm.axMapControl1.get_Layer(intTargetIndex); IFeatureLayer pFTargetLayer = pTargetLayer as IFeatureLayer; IFeatureClass pFTargetClass = pFTargetLayer.FeatureClass; int nTargetFeature = pFTargetClass.FeatureCount(null); //Store X, Y and Value to array double[] vecValue = new double[nFeature]; double[,] arrXY = new double[nFeature, 2]; int intXIdx = pFClass.FindField(strX); int intYIdx = pFClass.FindField(strY); int intVIdx = pFClass.FindField(mForm.strValue); int i = 0; while (pFeature != null) { vecValue[i] = Convert.ToDouble(pFeature.get_Value(intVIdx)); arrXY[i, 0] = Convert.ToDouble(pFeature.get_Value(intXIdx)); arrXY[i, 1] = Convert.ToDouble(pFeature.get_Value(intYIdx)); i++; pFeature = pFCursor.NextFeature(); } pFCursor.Flush(); ////Plot command for R //StringBuilder plotCommmand = new StringBuilder(); //Get the file path and name to create spatial weight matrix string strNameR = pSnippet.FilePathinRfromLayer(pFTargetLayer); if (strNameR == null) { return; } //Load Library and Data pEngine.Evaluate("library(maptools);library(foreign);library(sp);library(circular);"); pEngine.Evaluate("ct.shp <- readShapePoly('" + strNameR + "')"); //Create Shapepointdataframe in R NumericMatrix nmXY = pEngine.CreateNumericMatrix(arrXY); pEngine.SetSymbol("xy.coord.old", nmXY); NumericVector nvValue = pEngine.CreateNumericVector(vecValue); pEngine.SetSymbol("LEAD", nvValue); pEngine.Evaluate("v.bll <- as.data.frame(LEAD)"); pEngine.Evaluate("old.points <- SpatialPointsDataFrame(xy.coord.old, v.bll)"); string strOverMethod = ""; if (cboStat.Text == "Mean") { strOverMethod = ", fn=mean"; } //Save Original value pEngine.Evaluate("n.obs <- nrow(v.bll)"); pEngine.Evaluate("ori.value <- as.matrix(over(ct.shp, old.points" + strOverMethod + "))"); NumericVector nvOriValue = pEngine.Evaluate("ori.value").AsNumeric(); mForm.arrOrivalue = new double[nTargetFeature]; nvOriValue.CopyTo(mForm.arrOrivalue, nTargetFeature); //Create Matrix to store simulation results pEngine.Evaluate("results.matrix <- matrix(NA, nrow=nrow(ct.shp), ncol=" + intNSimulation.ToString() + ")"); //Simulation for (int j = 1; j <= intNSimulation; j++) { //Progress bar intProgress = j * 100 / intNSimulation; pfrmProgress.pgbProgress.Value = intProgress; //pfrmProgress.pgbProgress.Refresh(); pfrmProgress.lblStatus.Text = "Processing (" + intProgress.ToString() + "%)"; //pfrmProgress.lblStatus.Refresh(); pfrmProgress.Invalidate(); //Create Direction from vonmises distribution pEngine.Evaluate("dir.new <- as.numeric(rvonmises(n.obs, mu=circular(" + txtDirections.Text + ", units='degrees'), kappa=" + txtConcetration.Text + "))"); //Add distance error if (rbtRandom.Checked == true) { pEngine.Evaluate("dist.error <- runif(n.obs, 0, " + txtDistance.Text + ")"); } else { pEngine.Evaluate("dist.error <- " + txtDistance.Text); } //Create New points and save to SpatialPointsDataFrame pEngine.Evaluate("x.new <- xy.coord.old[,1] + cos(dir.new)*dist.error; y.new <- xy.coord.old[,2]+ sin(dir.new)*dist.error"); pEngine.Evaluate("xy.coord <- cbind(x.new, y.new)"); pEngine.Evaluate("new.points <- SpatialPointsDataFrame(xy.coord, v.bll)"); //Save results <- have to insert options for summary statistics, (only possilbe by Mean) 1/29/15 pEngine.Evaluate("results.matrix[," + j.ToString() + "] <- as.matrix(over(ct.shp, new.points" + strOverMethod + "))"); } //Save Simulation results to Array NumericMatrix nmResults = pEngine.Evaluate("results.matrix").AsNumericMatrix(); mForm.arrSimuResults = new double[nTargetFeature, intNSimulation]; nmResults.CopyTo(mForm.arrSimuResults, nTargetFeature, intNSimulation); //Create matrix to save summary results(mean, var, mean/var) pfrmProgress.lblStatus.Text = "Creating Summary"; pfrmProgress.Invalidate(); pEngine.Evaluate("sum.mat <- matrix(NA,nrow=nrow(ct.shp), ncol=6)"); //Calculate mean and variance of simulation for (int j = 1; j <= nTargetFeature; j++) { pEngine.Evaluate("sum.mat[" + j.ToString() + ",1] <- mean(results.matrix[" + j.ToString() + ",])"); pEngine.Evaluate("sum.mat[" + j.ToString() + ",2] <- var(results.matrix[" + j.ToString() + ",])"); pEngine.Evaluate("sum.mat[" + j.ToString() + ",5] <- min(results.matrix[" + j.ToString() + ",])"); pEngine.Evaluate("sum.mat[" + j.ToString() + ",6] <- max(results.matrix[" + j.ToString() + ",])"); pEngine.Evaluate("diff.ori <- (results.matrix[" + j.ToString() + ",]- ori.value[" + j.ToString() + "])^2"); //Needs to be changed pEngine.Evaluate("sum.mat[" + j.ToString() + ",4] <- sum(diff.ori)/" + intNSimulation.ToString()); } //Calculate mean/variance pEngine.Evaluate("sum.mat[,3] <- sum.mat[,2]/sum.mat[,1]"); //Save summary results to array NumericMatrix nmSummary = pEngine.Evaluate("sum.mat").AsNumericMatrix(); arrSummary = new double[nTargetFeature, 6]; nmSummary.CopyTo(arrSummary, nTargetFeature, 6); //Remove all memory pEngine.Evaluate("rm(list=ls(all=TRUE))"); //Enable to the histogram identify tool for simulation results if (mForm.arrSimuResults != null) { chkEnable.Enabled = true; } //Add Summary data to Target feature Class //Create filed to store results if (pFTargetClass.FindField("Ori_Mean") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "Ori_Mean"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("Mean_val") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "Mean_val"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("Var_val") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "Var_val"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("Mean_Var") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "Mean_Var"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("Var_Ori") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "Var_Ori"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("MIN") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "MIN"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } if (pFTargetClass.FindField("MAX") == -1) { IFieldEdit addField = new FieldClass(); addField.Name_2 = "MAX"; addField.Type_2 = esriFieldType.esriFieldTypeDouble; pFTargetClass.AddField(addField); } //Save summary results to Target shapefile int intOriMeanIdx = pFTargetClass.FindField("Ori_Mean"); int intMeanIdx = pFTargetClass.FindField("Mean_val"); int intVarIdx = pFTargetClass.FindField("Var_val"); int intMVIdx = pFTargetClass.FindField("Mean_Var"); int intVOIdx = pFTargetClass.FindField("Var_Ori"); int intMaxIdx = pFTargetClass.FindField("MAX"); int intMinIdx = pFTargetClass.FindField("MIN"); IFeatureCursor pTFUCursor = pFTargetClass.Update(null, false); IFeature pTFeature = pTFUCursor.NextFeature(); i = 0; while (pTFeature != null) { pTFeature.set_Value(intOriMeanIdx, mForm.arrOrivalue[i]); pTFeature.set_Value(intMeanIdx, arrSummary[i, 0]); pTFeature.set_Value(intVarIdx, arrSummary[i, 1]); pTFeature.set_Value(intMVIdx, arrSummary[i, 2]); pTFeature.set_Value(intVOIdx, arrSummary[i, 3]); pTFeature.set_Value(intMinIdx, arrSummary[i, 4]); pTFeature.set_Value(intMaxIdx, arrSummary[i, 5]); pTFeature.Store(); i++; pTFeature = pTFUCursor.NextFeature(); } pfrmProgress.Close(); MessageBox.Show("Done"); } catch (Exception ex) { MessageBox.Show(this.Handle.ToString() + " Error:" + ex.Message); return; } }