public static TwoByTwo GetInstance(KeyValuePair <string, IDictionary <string, int> > varAndCidToVal1, KeyValuePair <string, IDictionary <string, int> > varAndCidToVal2, bool rememberCases) { TwoByTwo twoByTwo = rememberCases ? (TwoByTwo) new TwoByTwoCases() : (TwoByTwo) new TwoByTwoCounts(); twoByTwo.Var1 = varAndCidToVal1.Key; twoByTwo.Var2 = varAndCidToVal2.Key; twoByTwo.TabulateCounts(varAndCidToVal1.Value, varAndCidToVal2.Value); return(twoByTwo); }
public static TwoByTwo GetInstance(IDictionary <string, int> cidToVal1, IDictionary <string, int> cidToVal2, bool rememberCases) { TwoByTwo twoByTwo = rememberCases ? (TwoByTwo) new TwoByTwoCases() : (TwoByTwo) new TwoByTwoCounts(); twoByTwo.Var1 = null; twoByTwo.Var2 = null; twoByTwo.TabulateCounts(cidToVal1, cidToVal2); twoByTwo.Invalidate(); return(twoByTwo); }
public static IEnumerable <TwoByTwo> GetCollection(bool lowMemory, bool unsorted, string inputSparseFileName1, string inputSparseFileName2, bool rememberCases) { if (unsorted) { return(TwoByTwo.GetUnsortedCollection(inputSparseFileName1, inputSparseFileName2, lowMemory, rememberCases)); } else { return(TwoByTwo.GetSortedCollection(inputSparseFileName1, inputSparseFileName2, lowMemory, rememberCases)); } }
private static IEnumerable <TwoByTwo> ProcessCore(string inputSparseFileName1, IEnumerable <KeyValuePair <string, IDictionary <string, int> > > varToCidToVal2, bool rememberCases) { foreach (KeyValuePair <string, IDictionary <string, int> > varAndCidToVal1 in GroupByVariableLowMemory(inputSparseFileName1)) { foreach (KeyValuePair <string, IDictionary <string, int> > varAndCidToVal2 in varToCidToVal2) { TwoByTwo twoByTwo = TwoByTwo.GetInstance(varAndCidToVal1, varAndCidToVal2, rememberCases); yield return(twoByTwo); } } }
public TwoByTwo SampleFromNull(int N) { int sum = Sum(); double p1 = (double)RowSum(true) / sum; double p2 = (double)ColSum(true) / sum; Random rand = new Random((int)DateTime.Now.Ticks); TwoByTwo nullTable = TwoByTwo.GetInstance(this.Var1, this.Var2, false); for (int i = 0; i < N; i++) { nullTable.IncrementCell(MachineInvariantRandom.GlobalRand.NextDouble() < p1, MachineInvariantRandom.GlobalRand.NextDouble() < p2); } return(nullTable); }
public static IEnumerable <TwoByTwo> GetCollectionFromMatrices(bool unsorted, string inputMatrixFileName1, string inputMatrixFileName2, ParallelOptions parallelOptions) { MatrixFactory <string, string, SufficientStatistics> mf = MatrixFactory <string, string, SufficientStatistics> .GetInstance(); var m1 = mf.Parse(inputMatrixFileName1, MissingStatistics.GetInstance(), parallelOptions); var m2 = mf.Parse(inputMatrixFileName2, MissingStatistics.GetInstance(), parallelOptions); List <TwoByTwo> tableList = new List <TwoByTwo>(); foreach (string key in m1.RowKeys) { if (m2.ContainsRowKey(key)) { TwoByTwo table = TwoByTwo.GetInstance(inputMatrixFileName1, key, false); var m1NonMissing = m1.RowView(key).Select(kvp => kvp); var m2NonMissing = m2.RowView(key).Select(kvp => kvp); // m1 is T for first col, m2 is F for first Col. table.TT = m1NonMissing.Select(kvp => (int)kvp.Value.AsDiscreteStatistics()).Sum(); table.TF = m1NonMissing.Count() - table.TT; table.FT = m2NonMissing.Select(kvp => (int)kvp.Value.AsDiscreteStatistics()).Sum(); table.FF = m2NonMissing.Count() - table.FT; if (unsorted) { yield return(table); } else { tableList.Add(table); } } } if (!unsorted) { tableList.Sort((t1, t2) => t1.FisherExactTest.CompareTo(t2.FisherExactTest)); foreach (var table in tableList) { yield return(table); } } }
public TwoByTwo SampleFromAlt(int N) { int sum = Sum(); double pRow = (double)RowSum(true) / sum; double pColGivenRowIsTrue = (double)TT / RowSum(true); double pColGivenRowIsFalse = (double)FT / RowSum(false); TwoByTwo altTable = TwoByTwo.GetInstance(this.Var1, this.Var2, false); for (int i = 0; i < N; i++) { if (MachineInvariantRandom.GlobalRand.NextDouble() < pRow) { altTable.IncrementCell(true, MachineInvariantRandom.GlobalRand.NextDouble() < pColGivenRowIsTrue); } else { altTable.IncrementCell(false, MachineInvariantRandom.GlobalRand.NextDouble() < pColGivenRowIsFalse); } } return(altTable); }