In ?ChiSquareIndependenceTest a contingency table is needed for input.
To construct such a table from raw data is not trivial. I suggest to include this in the statistics package.
A first try:

Crosstab := proc(A,i,j,first::evaln,second::evaln)
  local k,c,r,nr,nc, Tc,Tr, K,R, ind;
# c and r are lists of different entries in the i-th and j-th column;
# Tr and Tc are "Lookup tables":
  c := sort([op(convert( A[1..-1,i], set ))]); nr := nops(c):
  for k to nr do Tr[c[k]] := k end do:
  r := sort([op(convert( A[1..-1,j], set ))]); nc := nops(r):
  for k to nc do Tc[r[k]] := k end do:
#
  K := Matrix(nr,nc): R := LinearAlgebra:-RowDimension(A):
  for k to R do
    ind := Tr[A[k,i]],Tc[A[k,j]]:
    K[ind] := K[ind]+1
  end do:
  first := Vector(c);
  second := Vector[row](r);
  K
end proc:

Example:

data := [[male, 8, green, 3, 5], [female, 7, orange, 3, 7],
         [unknown, 4, white, 7, 1], [male, 1, blue, 6, 3],
         [male, 2, green, 3, 7], [unknown, 11, green, 3, 5],
         [female, 2, blue, 4, 6], [female, 11, black, 6, 4],
         [female, 1, white, 7, 1], [female, 8, brown, 5, 5],
         [male, 3, blue, 6, 4], [male, 10, green, 3, 7],
         [male, 1, yellow, 4, 4], [male, 2, orange, 5, 5],
         [female, 11, black, 6, 2], [male, 4, orange, 5, 3],
         [male, 11, green, 3, 5], [male, 4, red, 2, 8],
         [unknown, 11, yellow, 2, 7], [female, 4, black, 6, 2]]:

C := Crosstab( Matrix(data),1,2,Q1,Q2):

Q2; Q1,C;

Vector[row](8, {(1) = 1, (2) = 2, (3) = 3, (4) = 4, (5) = 7, (6) = 8, (7) = 10, (8) = 11})

 

Vector[column](%id = 18446744080271913558), Matrix(%id = 18446744080271913438)

(1)

 


Crosstab.mw

Please Wait...