function [cram,pears,stat] = freqcontg(X,sig)

// Contingency table

// X ... data marix of absolute frequencies
// sig ... significance level of association (default 0.05)

// stat ... chi-square computed stat. and quantil [computed tabulated]
// pears ... Pearson's contingency coefficient
// cram ... Cramer's V

// Literature: Dowdy, S., Wearden, S. and Chilko, D. (2004). Statistics
// for research, Wiley: New Jersey

// Copyright (C): Lukáš Malec; University of Business in Prague,
// Czech Republic
// Date: $2016-01-12$

[lhs,rhs]=argn(0)

if X==[] then stat=%nan, return, end

[n,m] = size(X), sX = sum(X)

if n < 3&m < 3 then
    error(msprintf(gettext("%s: Better is to analyse 2 x 2 table\n"),...
 "Error #1"))

end

rsum = sum(X,1), csum = sum(X,2)

Xt = []
for i = 1:n
    for j = 1:m
        Xt(i,j) = rsum(j)*csum(i)/sX

    end
end

f = find(Xt < 5)
if length(f) > n*m/5 then
    error(msprintf(gettext("%s: The condition on theoretical...
 frequencies is not satisfied\n"),"Error #2"))

end

f = find(Xt < 1)
if length(f) > 0 then
    error(msprintf(gettext("%s: The condition on theoretical...
 frequencies is not satisfied\n"),"Error #3"))

end

if rhs==1
    sig = 0.05

end

chip = sum((X - Xt).^2 ./Xt)
chi = cdfchi("X",(n-1)*(m-1),1-sig,sig)

stat = [chip chi]

if chip > chi then
    mprintf(gettext("%s: The null hypothesis of no association...
 is rejected\n"),"H0 decision")
else
    mprintf(gettext("%s: The null cannot be rejected\n"),"H0 decision")

end

pears = sqrt(chip/(chip + sX))
cram = sqrt(chip/(sX*min((n-1),(m-1))))

endfunction
