Creating a contingency table

Links to implementations

See the bottom of this package, CrossTabulate.m for different implementations (CrossTabulate):

  • one is based on Tally, GatherBy, and SparseArray,

  • the other is based in R' xtabs (via RLink).

The function CrossTensorate generalizes the contingency value finding.

The making of contingency matrices and tensors with those functions is discussed in these posts:

  • "Contingency tables creation examples" at WordPress,

  • "Contingency tables creation examples" at community.wolfram.com.

Here is an alternative implementation at Stack Overflow by Mr.Wizard.

Code to try

Get package

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/CrossTabulate.m"]

Get data

titanicData = 
  Flatten@*List @@@ 
   ExampleData[{"MachineLearning", "Titanic"}, "Data"];
titanicData = DeleteCases[titanicData, {___, _Missing, ___}];

titanicColumnNames = 
  Flatten@*List @@ 
   ExampleData[{"MachineLearning", "Titanic"}, "VariableDescriptions"];
aTitanicColumnNames = 
  AssociationThread[
   titanicColumnNames -> Range[Length[titanicColumnNames]]];

Make a contingency matrix

ctCounts = 
  CrossTabulate[
   titanicData[[All, 
     aTitanicColumnNames /@ {"passenger class", "passenger survival"}]]];

MatrixForm[ctCounts]

enter image description here

Make a contingency tensor (using a formula)

ctRes = CrossTensorate[
   Count == 
    "passenger survival" + "passenger sex" + "passenger class", 
   titanicData, aTitanicColumnNames];
MatrixForm[ctRes]

enter image description here


There is a function in Wolfram Function Repository for making contingency tables: CrossTabulate.

enter image description here