Click or drag to resize

CategoricalDataSetGetContingencyTable Method

Gets the contingency table representing the joint absolute frequency distribution of the specified categorical variables.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public DoubleMatrix GetContingencyTable(
	int rowVariableIndex,
	int columnVariableIndex
)

Parameters

rowVariableIndex
Type: SystemInt32
The index of the variable whose categories will be assigned to the rows of the contingency table.
columnVariableIndex
Type: SystemInt32
The index of the variable whose categories will be assigned to the columns of the contingency table.

Return Value

Type: DoubleMatrix
The contingency table of the specified variables.
Exceptions
ExceptionCondition
ArgumentOutOfRangeExceptionrowVariableIndex 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.
Examples

In the following example, a contingency table is computed for two variables in a categorical data set.

Getting a contingency table
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                
// 
See Also