public void TestMedianPolishExamples() { var matrix = new double?[, ] { { 1677120562, 1057244128, 1573838418, 1610727572, 1700424094, 1337004885, 2096688822, 556694100, 1758444441 }, { 822328116, 698575767, 557625726, 1867460568, 1497452757, 1286780009, 2109397777, 63595003, 579931872 }, { 2021080567, 1939792651, 998333669, 309807657, 766650568, 1525104204, 98432661, 1273392816, 423985426 }, { 310985865, 1892567676, 834287094, 1778432993, 99139496, 2020064656, 1859647279, 1208352316, 673041733 } }; var expectedResult = new MedianPolish() { OverallConstant = 1250365665, RowEffects = new double[] { 367804634, -369087286, -130763091, 130763091 }, ColumnEffects = new double[] { 0, 164368154, -222460779, 194930755, -135349106, 405501630, 478518523, -495229908, -498481827 }, Residuals = new double?[, ] { { 58950263, -725294325, 178128898, -202373482, 217602900, -686667045, 0, -566246291, 638755969 }, { -58950263, -347070766, -101191874, 791251435, 751523484, 0, 749600876, -322453468, 197135321 }, { 901477993, 655821923, 101191874, -1004725672, -217602900, 0, -1499688436, 649020150, -197135321 }, { -1070142891, 347070766, -324380884, 202373482, -1146640155, 233434270, 0, 322453468, -209605196 } } }; MedianPolish medianPolish = MedianPolish.GetMedianPolish(matrix, 0.01, 10); Assert.AreEqual(expectedResult.OverallConstant, medianPolish.OverallConstant); AssertDoublesEqual(expectedResult.RowEffects, medianPolish.RowEffects); AssertDoublesEqual(expectedResult.ColumnEffects, medianPolish.ColumnEffects); AssertMatricesEqual(expectedResult.Residuals, medianPolish.Residuals); }
public void TestMedianPolishSimpleMissingValues() { var matrix = new double?[, ] { { null, 1, 2 }, { 3, 4, 5 }, { 6, null, 7 } }; MedianPolish medianPolish = MedianPolish.GetMedianPolish(matrix, 0, 100); Assert.AreEqual(4, medianPolish.OverallConstant); AssertDoublesEqual(new[] { -3.000000, 0.000000, 2.333333 }, medianPolish.RowEffects, .0001); AssertDoublesEqual(new[] { -6.666667e-01, -6.223015e-61, 1.000000e+00 }, medianPolish.ColumnEffects, .0001); }
public void TestMedianPolishSimpleMatrix() { var matrix = new double?[, ] { { 1, 3 }, { 2, 5 } }; MedianPolish medianPolish = MedianPolish.GetMedianPolish(matrix, 0.01, 10); Assert.AreEqual(2.75, medianPolish.OverallConstant); CollectionAssert.AreEqual(new[] { -.75, .75 }, medianPolish.RowEffects); CollectionAssert.AreEqual(new[] { -1.25, 1.25 }, medianPolish.ColumnEffects); }
public void TestMedianPolishMissingValues() { /* matrix(c(NA, 1057244128, 1573838418, 1610727572, 1700424094, 1337004885, 2096688822, 556694100,1758444441, * 822328116, NA, 557625726, 1867460568, 1497452757, 1286780009, 2109397777,63595003, 579931872, * 2021080567, 1939792651, 998333669, 309807657, 766650568, 1525104204, 98432661, 1273392816, 423985426, * 310985865, 1892567676, 834287094, 1778432993, 99139496, 2020064656, 1859647279, 1208352316, 673041733),nrow = 4, byrow = TRUE) */ var matrix = new double?[, ] { { null, 1057244128, 1573838418, 1610727572, 1700424094, 1337004885, 2096688822, 556694100, 1758444441 }, { 822328116, null, 557625726, 1867460568, 1497452757, 1286780009, 2109397777, 63595003, 579931872 }, { 2021080567, 1939792651, 998333669, 309807657, 766650568, 1525104204, 98432661, 1273392816, 423985426 }, { 310985865, 1892567676, 834287094, 1778432993, 99139496, 2020064656, 1859647279, 1208352316, 673041733 } }; var expectedResult = new MedianPolish() { OverallConstant = 1162563201, RowEffects = new double[] { 119681337, -129549460, 16173465, -16173465 }, ColumnEffects = new double[] { -210685625, 746177940, -246252819, 480263146, 3046729, 300066903, 763850914, -331793929, -463214936 }, Residuals = new double?[, ] { { null, -971178350, 537846699, -151780112, 415132827, -245306556, 50593371, -393756509, 939414839 }, { 0, null, -229135195, 354183682, 461392287, -46300635, 312533123, -637624809, 10133067 }, { 1053029526, 14878045, 65849823, -1349192154, -415132827, 46300635, -1844154918, 426450079, -291536304 }, { -624718246, 0, -65849823, 151780112, -1050296969, 573608017, -50593371, 393756509, -10133067 } } }; MedianPolish medianPolish = MedianPolish.GetMedianPolish(matrix, 0.01, 10); Assert.AreEqual(expectedResult.OverallConstant, medianPolish.OverallConstant, 1); AssertDoublesEqual(expectedResult.RowEffects, medianPolish.RowEffects); AssertDoublesEqual(expectedResult.ColumnEffects, medianPolish.ColumnEffects); AssertMatricesEqual(expectedResult.Residuals, medianPolish.Residuals); }
private IList <RunAbundance> SummarizeDataRowsWithMedianPolish(IList <DataRowDetails> dataRows) { IList <IGrouping <IdentityPath, DataRowDetails> > dataRowsByFeature = SumMs1Transitions(dataRows).ToLookup(row => row.IdentityPath) .Where(IncludeFeatureForMedianPolish) .ToArray(); #pragma warning disable 162 // ReSharper disable HeuristicUnreachableCode if (false) { // For debugging purposes, we might want to order these features in the same order as MSstats R code Array.Sort((IGrouping <IdentityPath, DataRowDetails>[])dataRowsByFeature, (group1, group2) => { String s1 = GetFeatureKey(SrmDocument, group1.Key); String s2 = GetFeatureKey(SrmDocument, group2.Key); return(String.Compare(s1.ToLowerInvariant(), s2.ToLowerInvariant(), StringComparison.Ordinal)); }); } // ReSharper restore HeuristicUnreachableCode #pragma warning restore 162 var featureCount = dataRowsByFeature.Count; if (featureCount == 0) { return(ImmutableList.Empty <RunAbundance>()); } var rows = new List <double?[]>(); IDictionary <int, int> replicateRowIndexes = new Dictionary <int, int>(); for (int iFeature = 0; iFeature < dataRowsByFeature.Count; iFeature++) { foreach (DataRowDetails dataRowDetails in dataRowsByFeature[iFeature]) { int rowIndex; if (!replicateRowIndexes.TryGetValue(dataRowDetails.ReplicateIndex, out rowIndex)) { rowIndex = rows.Count; rows.Add(new double?[featureCount]); replicateRowIndexes.Add(dataRowDetails.ReplicateIndex, rowIndex); } var row = rows[rowIndex]; row[iFeature] = dataRowDetails.GetLog2Abundance(); } } var matrix = new double?[rows.Count, featureCount]; for (int iRow = 0; iRow < rows.Count; iRow++) { for (int iCol = 0; iCol < featureCount; iCol++) { matrix[iRow, iCol] = rows[iRow][iCol]; } } var medianPolish = MedianPolish.GetMedianPolish(matrix); List <RunAbundance> runAbundances = new List <RunAbundance>(); foreach (var replicateIndexDetails in _replicateIndexes) { int rowIndex; if (!replicateRowIndexes.TryGetValue(replicateIndexDetails.Key, out rowIndex)) { continue; } double value = medianPolish.OverallConstant + medianPolish.RowEffects[rowIndex]; runAbundances.Add(new RunAbundance() { ReplicateIndex = replicateIndexDetails.Key, BioReplicate = replicateIndexDetails.Value.BioReplicate, Control = replicateIndexDetails.Value.IsControl, Log2Abundance = value }); } return(runAbundances); }