# Two-way contingency table

Posted 9 years ago
13191 Views
|
6 Replies
|
4 Total Likes
|
 Hi folks,Is there a way to compute, like in the R language, a two-way contingency table? I don't find any built-in command to do that, any clues?Steeve
6 Replies
Sort By:
Posted 3 years ago
 There is a function in Wolfram Function Repository for making contingency matrices: CrossTabulate.
Posted 7 years ago

Fully explained how to do with several examples in this blog post "Contingency tables creation examples".

First get the package MathematicaForPredictionUtilities.m from MathematicaForPrediction at GitHub.

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


## Generating data

Data generated in this Stack Overflow answer http://stackoverflow.com/a/8101951 by Mr.Wizard.

Needs["Calendar"]
key = # -> #2[[1]] &~MapIndexed~{"Region", "Gender", "Style", "Ship Date", "Units", "Price",  "Cost"};
choices = {{"North", "South", "East", "West"}, {"Boy", "Girl"}, {"Tee", "Golf", "Fancy"}, IntegerString[#, 10, 2] <> "/2011" & /@ Range@12,
Range@15, Range[8.00, 15.00, 0.01], Range[6.00, 14.00, 0.01]};
data = RandomChoice[#, 250] & /@ choices // Transpose;


columnNames = {"Region", "Gender", "Style", "Ship Date", "Units", "Price", "Cost", "TotalCost", "DateObject"};

(* <|"Region" -> 1, "Gender" -> 2, "Style" -> 3, "Ship Date" -> 4, "Units" -> 5, "Price" -> 6, "Cost" -> 7, "TotalCost" -> 8, "DateObject" -> 9|> *)

data = Map[Append[#, #[[aColumnNames["Units"]]]*#[[aColumnNames[["Cost"]]]]] &, data];
data = Map[Append[#, DateObject@#[[aColumnNames["Ship Date"]]]] &, data];


This how the data looks like:

Pane[GridTableForm[data, TableHeadings -> columnNames],  ImageSize -> {600, 400}, Scrollbars -> True]


## Using CrossTabulate

xtab = CrossTabulate[
data[[All, aColumnNames /@ {"Gender", "Style", "TotalCost"}]]];
MatrixForm[#1, TableHeadings -> {#2, #3}] & @@ xtab


Install R first:

Needs["RLink"]
InstallR[]


The function xtabsViaRLink calls R's xtabs :

rxtab = FromRXTabsForm@
data[[All,
aColumnNames /@ {"Gender", "Style", "TotalCost"}]], {"Gender",
"Style", "TotalCost"}, "TotalCost ~ Gender + Style"];
MatrixForm[#1, TableHeadings -> {#2, #3}] & @@ rxtab


Posted 9 years ago
Posted 9 years ago
 I don't yet have enough of a clue to answer your questions but I think the lack of an answer might just be a matter of vocabulary.  Given the number of questions/answers related to Cross-tabs, pivot tables, and DataFrames on the mathematica Stack Exchange I think you are right that it is not built-in (yet, see functions planned for version 10) but it is easy to build for experienced users.Related?http://mathematica.stackexchange.com/questions/21117/how-can-i-create-an-advanced-grid-interface-on-mathematicahttp://mathematica.stackexchange.com/questions/19136/creating-a-r-dataframe-like-construct-in-mathematicahttp://mathematica.stackexchange.com/questions/9269/the-gap-between-mapat-and-replacepart-for-2d-data-tables-with-headers/9279#9279
Posted 9 years ago
 I think your question is very general? Perhaps you could give an example of what kind of table (2x2 or n*n) you want and what you would like to do with it?I think you have to build your own function to do so.
Posted 9 years ago
 Well, I think that nobody have a clue ;).
Community posts can be styled and formatted using the Markdown syntax.