public void BinaryContingencyNullTest() { // Create a table with no significant association and test for it. ContingencyTable e0 = CreateExperiment(0.25, 0.33, 0.33, 128); Assert.IsTrue(e0.Total == 128); Assert.IsTrue(e0.RowTotal(0) + e0.RowTotal(1) == e0.Total); Assert.IsTrue(e0.ColumnTotal(0) + e0.ColumnTotal(1) == e0.Total); UncertainValue lnr = e0.Binary.LogOddsRatio; Assert.IsTrue(lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e0.Binary.OddsRatio; Assert.IsTrue(r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult p = e0.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability > 0.05); TestResult f = e0.Binary.FisherExactTest(); Assert.IsTrue(f.Probability > 0.05); }
public void BinaryContingencyTest() { // Create a table with significant association and test for it. ContingencyTable e1 = CreateExperiment(0.50, 0.50, 0.75, 128); Assert.IsTrue(e1.RowTotal(0) + e1.RowTotal(1) == e1.Total); Assert.IsTrue(e1.ColumnTotal(0) + e1.ColumnTotal(1) == e1.Total); UncertainValue lnr = e1.Binary.LogOddsRatio; Assert.IsTrue(!lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e1.Binary.OddsRatio; Assert.IsTrue(!r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult p = e1.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability < 0.05); TestResult f = e1.Binary.FisherExactTest(); Assert.IsTrue(f.Probability < 0.05); }
public static void ContingencyTable() { ContingencyTable <string, bool> contingency = new ContingencyTable <string, bool>( new string[] { "P", "N" }, new bool[] { true, false } ); contingency["P", true] = 35; contingency["P", false] = 65; contingency["N", true] = 4; contingency["N", false] = 896; IReadOnlyList <string> x = new string[] { "N", "P", "N", "N", "P", "N", "N", "N", "P" }; IReadOnlyList <bool> y = new bool[] { false, false, false, true, true, false, false, false, true }; ContingencyTable <string, bool> contingencyFromLists = Bivariate.Crosstabs(x, y); foreach (string row in contingency.Rows) { Console.WriteLine($"Total count of {row}: {contingency.RowTotal(row)}"); } foreach (bool column in contingency.Columns) { Console.WriteLine($"Total count of {column}: {contingency.ColumnTotal(column)}"); } Console.WriteLine($"Total counts: {contingency.Total}"); foreach (string row in contingency.Rows) { UncertainValue probability = contingency.ProbabilityOfRow(row); Console.WriteLine($"Estimated probability of {row}: {probability}"); } foreach (bool column in contingency.Columns) { UncertainValue probability = contingency.ProbabilityOfColumn(column); Console.WriteLine($"Estimated probablity of {column}: {probability}"); } UncertainValue sensitivity = contingency.ProbabilityOfRowConditionalOnColumn("P", true); Console.WriteLine($"Chance of P result given true condition: {sensitivity}"); UncertainValue precision = contingency.ProbabilityOfColumnConditionalOnRow(true, "P"); Console.WriteLine($"Chance of true condition given P result: {precision}"); UncertainValue logOddsRatio = contingency.Binary.LogOddsRatio; Console.WriteLine($"log(r) = {logOddsRatio}"); TestResult pearson = contingency.PearsonChiSquaredTest(); Console.WriteLine($"Pearson χ² = {pearson.Statistic.Value} has P = {pearson.Probability}."); TestResult fisher = contingency.Binary.FisherExactTest(); Console.WriteLine($"Fisher exact test has P = {fisher.Probability}."); }
public void BinaryContingencyTest() { // Create a table with significant association and test for it. ContingencyTable e1 = CreateExperiment(0.50, 0.50, 0.75, 128); Assert.IsTrue(e1.RowTotal(0) + e1.RowTotal(1) == e1.Total); Assert.IsTrue(e1.ColumnTotal(0) + e1.ColumnTotal(1) == e1.Total); UncertainValue lnr = e1.Binary.LogOddsRatio; Assert.IsTrue(!lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e1.Binary.OddsRatio; Assert.IsTrue(!r.ConfidenceInterval(0.95).ClosedContains(1.0)); // Chi square should detect association TestResult p = e1.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability < 0.05); // Fisher exact should detect association TestResult f = e1.Binary.FisherExactTest(); Assert.IsTrue(f.Probability < 0.05); // Phi should be the same as Pearson correlation coefficient List <double> x = new List <double>(); List <double> y = new List <double>(); for (int i = 0; i < e1[0, 0]; i++) { x.Add(0); y.Add(0); } for (int i = 0; i < e1[0, 1]; i++) { x.Add(0); y.Add(1); } for (int i = 0; i < e1[1, 0]; i++) { x.Add(1); y.Add(0); } for (int i = 0; i < e1[1, 1]; i++) { x.Add(1); y.Add(1); } double s = x.CorrelationCoefficient(y); Assert.IsTrue(TestUtilities.IsNearlyEqual(s, e1.Binary.Phi)); }
public void ContingencyTableProbabilities() { // Construct data where (i) there are both reference-nulls and nullable-struct-nulls, // (ii) all values of one column are equally, (iii) values of other column depend on value of first column List <string> groups = new List <string>() { "A", "B", "C", null }; FrameTable data = new FrameTable(); data.AddColumn <string>("Group"); data.AddColumn <bool?>("Outcome"); int n = 512; double pOutcomeNull = 0.05; Func <int, double> pOutcome = groupIndex => 0.8 - 0.2 * groupIndex; Random rng = new Random(10101010); for (int i = 0; i < n; i++) { int groupIndex = rng.Next(0, groups.Count); string group = groups[groupIndex]; bool? outcome = (rng.NextDouble() < pOutcome(groupIndex)); if (rng.NextDouble() < pOutcomeNull) { outcome = null; } data.AddRow(group, outcome); } // Form a contingency table. ContingencyTable <string, bool?> table = Bivariate.Crosstabs(data["Group"].As <string>(), data["Outcome"].As <bool?>()); // Total counts should match Assert.IsTrue(table.Total == n); // All values should be represented foreach (string row in table.Rows) { Assert.IsTrue(groups.Contains(row)); } // Counts in each cell and marginal totals should match foreach (string group in table.Rows) { int rowTotal = 0; foreach (bool?outcome in table.Columns) { FrameView view = data.Where(r => ((string)r["Group"] == group) && ((bool?)r["Outcome"] == outcome)); Assert.IsTrue(table[group, outcome] == view.Rows.Count); rowTotal += view.Rows.Count; } Assert.IsTrue(rowTotal == table.RowTotal(group)); } // Inferred probabilities should agree with model Assert.IsTrue(table.ProbabilityOfColumn(null).ConfidenceInterval(0.99).ClosedContains(pOutcomeNull)); for (int groupIndex = 0; groupIndex < groups.Count; groupIndex++) { string group = groups[groupIndex]; Assert.IsTrue(table.ProbabilityOfRow(group).ConfidenceInterval(0.99).ClosedContains(0.25)); Assert.IsTrue(table.ProbabilityOfColumnConditionalOnRow(true, group).ConfidenceInterval(0.99).ClosedContains(pOutcome(groupIndex) * (1.0 - pOutcomeNull))); } Assert.IsTrue(table.ProbabilityOfColumn(null).ConfidenceInterval(0.99).ClosedContains(pOutcomeNull)); // Pearson test should catch that rows and columns are corrleated Assert.IsTrue(table.PearsonChiSquaredTest().Probability < 0.05); }