A dataset may include both classification variables and general variables. The classification variables, known as factors, take a small number of values known as levels. For example, the factor sex would have the levels male and female. These can be coded as
and
respectively. Given several factors, a multi-way table can be constructed such that each cell of the table represents one level from each factor. For example, the two factors sex and habitat, habitat having three levels (inner-city, suburban and rural) define the
contingency table
Sex |
Habitat |
|
Inner-city |
Suburban |
Rural |
Male |
|
|
|
Female |
|
|
|
For each cell statistics can be computed. If a third variable in the dataset was age, then for each cell the average age could be computed:
Sex |
Habitat |
|
Inner-city |
Suburban |
Rural |
Male |
25.5 |
30.3 |
35.6 |
Female |
23.2 |
29.1 |
30.4 |
The tables created by
nag_contab_tabulate_stat (g11ba) and stored in
table,
icount and, depending on
stat, also in
auxt are stored in the following way. Let there be
factors defining the table with factor
having
levels, then the cell defined by the levels
,
of the factors is stored in the
th cell given by
where
, for
and
.
The data, given by
John and Quenouille (1977), is for a
factorial experiment in
blocks of
units. The data is input in the order, blocks, factor with
levels, factor with
levels, yield. The
table of treatment means for yield over blocks is computed and printed.
function g11ba_example
fprintf('g11ba example results\n\n');
ifac = [int64(1),1,1; 1,2,1; 1,3,1; 1,1,2; 1,2,2; 1,3,2;
1,1,3; 1,2,3; 1,3,3; 1,1,4; 1,2,4; 1,3,4;
1,1,5; 1,2,5; 1,3,5; 1,1,6; 1,2,6; 1,3,6;
2,1,1; 2,2,1; 2,3,1; 2,1,2; 2,2,2; 2,3,2;
2,1,3; 2,2,3; 2,3,3; 2,1,4; 2,2,4; 2,3,4;
2,1,5; 2,2,5; 2,3,5; 2,1,6; 2,2,6; 2,3,6;
3,1,1; 3,2,1; 3,3,1; 3,1,2; 3,2,2; 3,3,2;
3,1,3; 3,2,3; 3,3,3; 3,1,4; 3,2,4; 3,3,4;
3,1,5; 3,2,5; 3,3,5; 3,1,6; 3,2,6; 3,3,6];
y = [ 274; 361; 253; 325; 317; 339;
326; 402; 336; 379; 345; 361;
352; 334; 318; 339; 393; 358;
350; 340; 203; 397; 356; 298;
382; 376; 355; 418; 387; 379;
432; 339; 293; 322; 417; 342;
82; 297; 133; 306; 352; 361;
220; 333; 270; 388; 379; 274;
336; 307; 266; 389; 333; 353];
weight = 'U';
wt = [];
stat = 'A';
update = 'I';
lfac = [int64(3); 3; 6];
isf = [int64(0); 1; 1];
maxt = prod(lfac(isf~=0));
table = zeros(maxt,1);
icount = zeros(maxt,1,'int64');
auxt = zeros(2*maxt,1);
ncells = int64(0);
[table, ncells, ndim, idim, icount, auxt, ifail] = ...
g11ba( ...
stat, update, weight, isf, lfac, ifac, y, wt, ...
table, ncells, icount, auxt);
fprintf(' Table\n\n');
ncol = idim(ndim);
nrow = ncells/ncol;
table = transpose(reshape(table,[ncol,nrow]));
icount = transpose(reshape(icount,[ncol,nrow]));
for i = 1:nrow
row = [table(i,:); double(icount(i,:))];
fprintf('%8.2f(%2d)', row);
fprintf('\n');
end