public static double kruskalWallisPermutationTest(string[] columnNames, IEnumerable[] ie_cofounders, List <string> cof_typeof) { //block (intercept) REngine from printing to the Console //we are just redirecting the output of it to some StringWriter var stdOut = Console.Out; Console.SetOut(new StringWriter()); //create the cofounder data frame regrCovars rCovar = covarsDf(ie_cofounders, cof_typeof); if (cof_typeof.Count > 0) { DataFrame cofounders = rEngineInstance.engine.CreateDataFrame(rCovar.IEcofounders, columnNames: rCovar.cofounders.ToArray()); rEngineInstance.engine.SetSymbol("cofounders", cofounders); } //create the data frame DataFrame df = rEngineInstance.engine.CreateDataFrame(dataFrameValues, columnNames: columnNames); rEngineInstance.engine.SetSymbol("df", df); //Approximative (Monte Carlo) multivariate Kruskal-Wallis test //For two samples (phenotypes), the Kruskal-Wallis test is equivalent to the W-M-W (Wilcoxon-Mann-Whitney) test rEngineInstance.engine.Evaluate(string.Format(@"kwres <- independence_test(df[,'{0}']{2} ~ factor(df[,'{1}']), teststat = ""quadratic"", distribution = approximate(B = {3}), ytrafo = function(data) trafo(data, numeric_trafo = rank_trafo))", columnNames[1], columnNames[0], rCovar.cof_string, publicVariables.numberOfPermutations.ToString())); rEngineInstance.engine.Evaluate("rm(df)"); //Re-enable Console printings Console.SetOut(stdOut); return(Math.Round(rEngineInstance.engine.Evaluate(@"kwres@distribution@pvalue(kwres@statistic@teststatistic)[1]").AsNumeric().First(), 5)); }
public static msMetabolite.stats.regressValues linearRegressionTest(string[] columnNames, string _typeof, IEnumerable[] ie_cofounders, List <string> cof_typeof) { //block (intercept) REngine from printing to the Console //we are just redirecting the output of it to some StringWriter var stdOut = Console.Out; Console.SetOut(new StringWriter()); //create the cofounder data frame regrCovars rCovar = covarsDf(ie_cofounders, cof_typeof); if (cof_typeof.Count > 0) { DataFrame cofounders = rEngineInstance.engine.CreateDataFrame(rCovar.IEcofounders, columnNames: rCovar.cofounders.ToArray()); rEngineInstance.engine.SetSymbol("cofounders", cofounders); } //create the data frame DataFrame df = rEngineInstance.engine.CreateDataFrame(dataFrameValues, columnNames: columnNames); rEngineInstance.engine.SetSymbol("df", df); //Exact permutation test intends to run a complete permutation test which is extremely time consuming //When there are more that 10 observations it will switch from Exact to Prob which approximates the permutation distribution by randomly exchanging pairs of Y elements //in case we want to use Exact the we should change the limit of Exact from 10 to something greater by using maxExact = X (where X>10) //nCycle performs a complete random permutation, instead of pairwise exchanges, every nCycle cycles //run permutation test and take the pvalue rEngineInstance.engine.Evaluate(string.Format(@"myrestmp <- lmPerm::lmp({0}(df[,'{1}']) ~ as.numeric(df[,'{2}']){3}, perm = ""{4}"", seqs = {5}, " + @"center = {5}, projections = {6}, qr = {6}, maxIter = {7}, nCycle = {8})", (_typeof == "factor") ? "as.factor" : ((_typeof == "number") ? "as.numeric" : "Regression failed"), columnNames[0], columnNames[1], rCovar.cof_string, "Prob", "FALSE", "TRUE", publicVariables.numberOfPermutations, (publicVariables.numberOfPermutations / 1000).ToString())); rEngineInstance.engine.Evaluate(@"myres <- summary(myrestmp)"); rEngineInstance.engine.Evaluate(@"myresbeta <- unname(lm.beta::coef.lm.beta(lm.beta::lm.beta(myrestmp))[2])"); //rEngineInstance.engine.Evaluate("print(myres)"); rEngineInstance.engine.Evaluate("rm(df, cofounders, myrestmp)"); //Re-enable Console printings Console.SetOut(stdOut); //Console.WriteLine("name " + columnNames.First()); //Console.WriteLine("pv " + Math.Round(rEngineInstance.engine.Evaluate("myres$coefficients[2, 3]").AsNumeric().First(), 5)); //Console.WriteLine("r2 " + Math.Round(rEngineInstance.engine.Evaluate("myres$adj.r.squared").AsNumeric().First(), 5)); //Console.WriteLine("beta " + Math.Round(rEngineInstance.engine.Evaluate("myresbeta").AsNumeric().First(), 5)); return(new msMetabolite.stats.regressValues() { clinical_data_name = columnNames.First(), regrPvalue = Math.Round(rEngineInstance.engine.Evaluate("myres$coefficients[2, 3]").AsNumeric().First(), 5), regrAdjRsquare = Math.Round(rEngineInstance.engine.Evaluate("myres$adj.r.squared").AsNumeric().First(), 5), regrBeta = Math.Round(rEngineInstance.engine.Evaluate("myresbeta").AsNumeric().First(), 5) }); }