Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions inst/crosstab.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
## Create a cross-tabulation (contingency table) @var{t} from data vectors.
##
## The inputs @var{x1}, @var{x2}, ... @var{xn} must be vectors of equal length
## with a data type of numeric, logical, char, or string (cell array).
## with a data type of numeric, logical, char, categorical, or cell array of
## strings.
##
## As additional return values @code{crosstab} returns the chi-square statistics
## @var{chisq}, its p-value @var{p} and a cell array @var{labels}, containing
Expand All @@ -53,8 +54,8 @@

for i = 1:nargin
vector = varargin{i};
## If char array, convert to numerical vector
if (ischar (vector) || iscellstr (vector))
## Convert char, cellstr, or categorical to indexed numeric vector
if (ischar (vector) || iscellstr (vector) || iscategorical (vector))
try
[vector, gnames] = grp2idx (vector);
catch
Expand Down Expand Up @@ -145,3 +146,23 @@
%! y = [1, 2, 3, NaN];
%! t = crosstab (x, y);
%! assert (t, [1, 0, 0; 0, 1, 0]);

## Test categorical input
%!test
%! x = categorical ({'A', 'B', 'A', 'C', 'B'});
%! y = [1, 2, 1, 3, 2];
%! t = crosstab (x, y);
%! assert (size (t), [3, 3]);
%! assert (t(1, 1), 2); # A with 1
%! assert (t(2, 2), 2); # B with 2
%!test
%! x = categorical ({'low', 'med', 'high', 'low', 'med'});
%! y = categorical ({'X', 'Y', 'X', 'Y', 'X'});
%! [t, ~, ~, labels] = crosstab (x, y);
%! assert (size (t), [3, 2]);
%!test
%! ## Test categorical with numeric
%! x = categorical ([10, 20, 10, 30, 20]);
%! y = [1, 2, 1, 3, 2];
%! t = crosstab (x, y);
%! assert (t, [2, 0, 0; 0, 2, 0; 0, 0, 1]);