CategoricalDataSetGetContingencyTable Method |
Namespace: Novacta.Analytics
public DoubleMatrix GetContingencyTable( int rowVariableIndex, int columnVariableIndex )
Exception | Condition |
---|---|
ArgumentOutOfRangeException | rowVariableIndex is negative. -or- rowVariableIndex is greater than or equal to the NumberOfColumns of this instance. -or- columnVariableIndex is negative. -or- columnVariableIndex is greater than or equal to the NumberOfColumns of this instance. |
In the following example, a contingency table is computed for two variables in a categorical data set.
using System; using System.IO; namespace Novacta.Analytics.CodeExamples { public class CategoricalGetContingencyTableExample0 { public void Main() { // Create a data stream. string[] data = new string[7] { "COLOR,NUMBER", "Red,Negative", "Green,Zero", "White,Positive", "Red,Negative", "Blue,Negative", "Blue,Positive" }; MemoryStream stream = new(); StreamWriter writer = new(stream); for (int i = 0; i < data.Length; i++) { writer.WriteLine(data[i].ToCharArray()); writer.Flush(); } stream.Position = 0; // Encode the categorical data set. StreamReader streamReader = new(stream); char columnDelimiter = ','; IndexCollection extractedColumns = IndexCollection.Range(0, 1); bool firstLineContainsColumnHeaders = true; var dataSet = CategoricalDataSet.Encode( streamReader, columnDelimiter, extractedColumns, firstLineContainsColumnHeaders); // Assign the categories of variable NUMBER // to the rows of the table. int rowVariableIndex = 1; // Assign the categories of variable COLOR // to the columns of the table. int columnVariableIndex = 0; // Get the NUMBER-by-COLOR table. var table = dataSet.GetContingencyTable( rowVariableIndex, columnVariableIndex); // Show the table. Console.WriteLine("Contingency table:"); Console.WriteLine(table); } } } // Executing method Main() produces the following output: // // Contingency table: // [Red] [Green] [White] [Blue] // [Negative] 2 0 0 1 // [Zero] 0 1 0 0 // [Positive] 0 0 1 1 //