I
I.
I
I
I
t
t
I
il
I_
I
I
I
I
I
I
I.
I
'I
A COMPUTER PROGRAM FOR THE ANALYSIS OF
TWO-WAY CONTINGENCY TABLES
by
Dana Quade
Department of Biostatistics
University of North Carolina School of Public Health
Institute of Statistics Mimeo Series No. 567
February 1968
•
This program has been placed in the Call-A-Computer
library under the name ANATAB*** (for "ANAlysis of TABulation").
The former program TABLE* and its writeup are hereby superseded •
•
ERRATUM
The formula for
should be replaced by
•
Dana Quade
March 5, 1968
...
a1
on page 10 is incorrect and
I
I.
I
I
I
I
I
I
I
,.
I
I
I
I
I
I
I.
I
I
SUMMARY
This program, written in Time-sharing Fortran for Ca11-a-Computer,
may be used for the analysis of two-way contingency tables with up to
50 rows, and. up to 25 columns, and up to 524287 observations.
are not permitted.
Empty rows
The program includes
1)
Input of data
2)
(Option)
Printout of table
3)
(Option)
Expected values and "contributions to chi-square"
4)
Chi-square
5)
Fisher's test (for 2x2 tables)
6)
(Option)
Tests of symmetry (for square tables)
7)
(Option)
Comparison of rows
8)
(Option)
Correlation analysis
9)
(Option)
Repeat same analysis with different data, or reset.
A complete listing of the program is given as an Appendix.
I
'.I
I
I
I
I
I
I
I_
I
I
I
I
I
I
I.
I
I
DETAILED DESCRIPTION OF THE PROGRAM
Throughout this write-up the following notation is used:
number of rows is I, the number of columns J.
1
~
i
~
The row totals are R.,
~
j
~
J.
I, the column totals are C., 1
~
j
~
J, and the grand total number
ij
, 1
i
~
The number of observations
I, 1
in the (i,j) cell is n
~
the
J
~
of observations is N.
Consider the following contingency table as an example:
3
7
8
2
20
4
5
9
6
24
3
1
2
11
17
2
7
6
8
23
12
20
25
27
84
For this table I = J = 4 and N = 84.
1.
INPUT OF DATA
The program asks first for the number of rows and columns in the
table and then requests the data for each row, one at a time.
result of entering the example table might appear as follows:
HOW MANY ROWS (2 TO 50) AND COLUMNS (2 TO 25)?
744
ENTER ROW 1
? 3 7 8 2
ENTER ROW 2
? 459 6
ENTER ROW 3
731211
ENTER ROW 4
? 2 7 6 8
Thus the
I
I.
I
I
I
t
I
I
I
,.
I
I
I
t
I,
I
'.
I
I
-2-
For small tables, such as the example, it is convenient to feed in
all the data in response to the first question, since this avoids returning
control to the computer for each row.
Then the result would appear thus:
HOW MANY ROWS (2 TO 50) AND COLUMNS (2 TO 25) ?
? 4 4 3 7 8 2 4 5 9 6 3 1 2 11 2 7 6 8
ENTER ROW 1
ENTER ROW 2
ENTER ROW 3
ENTER ROW 4
Data entries must be separated by spaces, as shown, or (optionally)
by commas.
If the table to be analyzed is very large, or if there are a
great number of tables to be done, it will be convenient to prepare a data
tape in advance.
Note that the maximum capacity of the program is N
observations.
= 524287
A running total is kept as the data is fed in, and if this
ever exceeds 524287 the program comes to an immediate halt, with the error
message "STOP BIG".
The program also halts if any attempt is made to enter
a negative cell frequency (error message "STOP NEG") or an empty row (error
message "STOP ROW").
2.
(OPTION)
Empty columns are permitted, however.
PRINTOUT OF TABLE
The example table will appear as follows if this option is taken:
I
I.
I
I
-3-
DO YOU WANT THE TABLE PRINTED OUT?
?YES
R\C
1
2
3
4
TOTAL
1
3
7
8
2
20
2
4
5
9
6
24
3
3
1
2
11
17
4
2
7
6
8
23
12
20
25
27
84
t
,
I
I
I
I··
I
I
I
t
I
I
'.
I
I
TOTAL
If the table has more than 8 columns (J > 8) it will be "folded
over" and "interlaced" and hence somewhat difficult to read.
It
will
sometimes be convenient to avoid this, i f the number of rows is 8 or fewer,
by entering the data with rows and columns transposed.
Note:
The grand total number of observations (N) is always given, whether
or not the whole table is printed out.
3.
(OPTION)
EXPECTED VALUES AND "CONTRIBUTIONS TO CHI-SQUARE"
If this option is taken for the example table, the output will
appear as follows:
DO YOU WANT EXPECTED VALUES?
?YES
ROW
COLUMN
OBSERVED
EXPECTED
(0-E)t2/E
1
1
1
1
2
2
2
2
3
3
3
3
4
4
4
4
1
2
3
7
8
2
4
5
9
6
3
1
2
11
2
7
6
8
2.86
4.76
5.95
6.43
3.43
5.71
7.14
7.71
2.43
4.05
5.06
5.46
3.29
5.48
6.85
7.39
.0071
1.0519
.7044
3.0508
.0952
.0893
.4829
.3810
.1345
2.2947
1.8501
5.6081
.5031
.4240
.1044
.0499
3
4
1
2
3
4
1
2
3
4
1
2
3
4
I
I.
I·
I
I
-4-
Note that the third column gives the observed frequencies n .. ) which may be
1)
used to check data input if this is not done in (B).
The expected values
are computed as
E .. = RiC.lN
1J
J
and the last column gives the "contributions to chi-square",
,
I
I
I
I,e
I
I
t"
4.
CHI-SQUARE
For the example table this part of the output appears as follows:
CHI-SQUARE = 16.831 WITH 9 DEGREES OF FREEDOM
(SMALLEST EXPECTED VALUE: 2.43)
Chi-square is obtained by adding the "contributions" displayed in the last
column of the output from (3), and the degrees of freedom are (I-1)(J-1).
The smallest expected value is always given, as a help in deciding how much
faith to put in the accuracy of the chi-square approximation.
If any column of the table is empty the output appears thus
instead:
CHI-SQUARE NOT COMPUTED
(SMALLEST EXPECTED VALUE:
The correct chi-square for the non-empty columns can be obtained by adding
the "contributions" given in the last column of the output from (3), and
the degrees of freedom will be (I-1)(J'-1) where J' is the number of non-
I
I
empty columns.
'.
I
5.
I,
0.00)
Or, if convenient, the table may just be re-entered with
the empty columns omitted.
FISHER'S TEST
This is computed for 2x2 tables only (thus not for the example),
-5-
I
'.I
I
I
I
I
I
I
"eI
I
I
I
I
I
'.
I
I
if min(R , R , C ' C )
I
I
2
2
~
50.
(When all row and column totals exceed 50 the
chi-square approximation should be adequate.)
Here is given the P-value, or level of significance, for testing
independence against the one-sided alternative of positive association,
using the Fisher-Irwin "exact" test procedure.
This is appropriate if both
margins of the table were fixed in advance, or if a conditional test, given
the margins, is desired.
The formula is
P-positive =
To obtain a P-value (say "P-negative") for testing against negative association, re-enter the table with rows or columns (not
both~)
reversed.
For a two-sided P-value, against positive or negative association,
use the upper bound P-absolute
~
2 x min(P-positive, P-negative).
upper bound is exact if R =R or if C =C or both.
l 2
l 2
This
Note that P-positive
and P-negative cannot both be less than \; hence, if P-positive is
~
\
it is the minimum of the two and twice its value is an upper bound on
P-absolute, the computation of P-negative then being unnecessary.
6.
(OPTION)
TESTS OF SYMMETRY
(This option is available only for square tables:
I = J = M, say.)
i.e. if
For the example table the output for this option, if it
is taken, will appear as follows:
DO YOU WANT TO TEST SYMMETRY?
?YES
FOR TESTING SYMMETRY AGAINST GENERAL ALTERNATIVES:
BOWKER'S CHI-SQUARE = 22.077 WITH 6 DEGREES OF FREEDOM
(SMALLEST EXPECTED VALUE: 2.00)
I
I.
I
I
I
I
I
-6-
FOR TESTING SYMMETRY AGAINST 'DIAGONAL SKEWNESS':
SEN'S CHI-SQUARE = 14.807 WITH 3 DEGREES OF FREEDOM
(SMALLEST EXPECTED VALUE: 2.00)
FOR SIGN TEST OF STOCHASTIC EQUALITY OF MARGINALS:
APPROXIMATE NORMAL DEVIATE: 2.339
(43 OBSERVATIONS ABOVE DIAGONAL AND 23 BELOW)
The hypothesis of symmetry for a bivariate categorical random
variable with M categories is that p .. = p .. for 1
~J
6.1
~
i < j
M, where p ..
~J
Bowker's chi-square (omitted i f I = J = 2)
The formula is:
Xi
2
=
(n .. - n .. )
2
2 L; L; -=-~]"'-_'&":]~~
i<j
n ij + n ji
with M(M-l)/2 degrees of freedom.
I
The minimum expected
value is
I_
min
i<j
and if this equals zero then
xi is not computed.
Bowker's
chi-square may be used to test symmetry against the general
alternative that p .. :fp .. for some i < j, 1
~J
6.2
J~
~
i < j
~
k
Sen's chi-sguare (omitted if I = J = 2)
The formula is:
M-l (F
- F
k=l
+
2 L;
'I
I
I
I.
I
I
~
is the probability for the (i,j) cell.
I
I
I
I
J~
k
Fk
-k
F_ k
)2
with (M-l) degrees of freedom, where
M-k
I
n
u+k,u
for -(M-I)
~ -1
u=l
F =
k
M-k
I
u=I
n
u,u+k
for 1
~
k
~
(M-l).
~
M.
-7-
I
I.
I
The minimum expected value is
min
F + F_
k
k
1 ~ k :::;; M-l
2
and if this equals zero then
J
I
alternative ("diagonal skewness") that Pk;P-
I
k
for some
k, where
M-k
L
for -(M-l) :::;; k
~ -1
u=l
P =
k
M-k
I
for 1 :::;; k
~
(M-l).
u=l
For further discussion see P. K. Sen, "On some
I_
'.
I
Sen's
chi-square may be used to test symmetry against the
I
I
I
I
I
J
I
I
I
I
~ is not computed.
nonparametric tests for symmetry in twoway tables",
Journal of the Indian Statistical Association,
Vol. 4 (1966).
6.3
Sign test
This is just a comparison of the number of observations
above the diagonal (say, A) with the number of observations below the diagonal (say, B); the approximate normal
deviate is
Z
=
{
~A-BI-l
if A
B
if A ;
B.
/A+B
For a 2x2 table this test is sometimes known as the
McNemar test for the significance of changes.
It may
be thought of as testing the equality of the marginals
of the bivariate distribution against an alternative of
difference in location.
I
Ie
I
I
I
I
I
I
I
I_
I
I
I
I
I
I
I
.e
I
-8-
7.
(OPTION)
COMPARISON OF ROWS
This option is not available if there are only two columns (J=2) ,
and it is not meaningful unless the ordering of the columns is relevant.
Note that a comparison of columns is not available as such, but can be obtained by re-entering the table with rows and columns transposed.
For the example table, the output for this option would appear
as follows:
DO YOU WANT TO COMPARE ROWS, ASSUMING COLUMN ORDERING IS RELEVANT'?
?YES
ROW
1
2
3
4
TOTAL
N
MEAN
MEDIAN
20
24
17
23
2.450
2.708
3.235
2.870
2.500
2.833
3.727
2.917
84
2.798
2.900
MEDIAN TEST
CHI-SQUARE
=
PROBABILITY EFFECT
-.204
-.050
.267
.032
2.745 WITH 3 DEGREES OF FREEDOM
RANK ANALYSIS OF VARIANCE (KRUSKAL-WALLIS TEST)
F = 2.384 WITH 2.96 AND 78.85 DEGREES OF FREEDOM
STANDARD ANALYSIS OF VARIANCE (ASSUMING EQUALLY-SPACED COLUMNS)
F = 1.865 WITH 3 AND 80 DEGREES OF FREEDOM
7.1
Means
For 1
~
i
~
I, the mean of the i-th row is
x. =
1
J
L: j n . .fR.
j=l
1J
1
the overall mean is
==
x
J
L: j C./N
J
j=l
These means have no reasonable interpretation unless
it is assumed that the columns are equally spaced along
some scale.
I
I.
I
I
I
I
I
I
I
-9-
7.2
Medians
The medians are calculated on the assumption that the
columns resulted from grouping continuous data, and that
in particular any column -- say, the j-th -- represents
the interval from
(j-~)
(j+~).
to
(If the columns actually
represent a discrete variable then each median as given
may be rounded to the nearest integer.)
Warning:
if the median of any row lies inside an
empty interval, or between two empty intervals, it will
be printed out incorrectly as the upper end of the last
preceding nonempty interval.
I_
For example, in the table
1
2
2
o
5
4
o
o
2
2
the correct row medians are 4.0 and 2.5 but they would
I
I
I
I
I
I
I
••
I
appear as 3.5 and 1.5.
overall median.
7.3
Similar remarks hold for the
(This does not affect the median test.)
Probability effects
Let X. be an observation chosen at random from the
~
population represented by the i-th row, say, and let
X be an observation chosen at random from the overall
population (or the mixture of row populations in which
the proportions are as in the table at hand).
Then the
true probability effect of the i-th row is
8.~
= p[X.
~
> X} - p[X.~ < X} .
(These are genuine "effects",in that L:R.8.
~
~
= 0.)
I
I.
I
I
I
I
I
I
I
-10-
Let Y. be the column-rank of any observation
J
in the j-th column, computed as
Yj
I.
I
I
j
~
'<j
C., + \(CJ.+l)
J
for 1
~
j :::; J
and let
be the mean rank in the i-th row.
estimate of 9
i
Then an unbiased
is
and this is what the computer prints out.
7.4
I_
I
I
I
I
I
I
=
Median test
This is the chi· square test for testing homogeneity of
rows in the table which results after the J columns have
been collapsed into two in such a way as to make the two
column totals as nearly equal as possible (without reordering).
For the example, the resulting table being
tested is as follows:
7.5
10
10
20
9
15
24
4
13
17
9
14
23
52
84
32
Rank analysis of variance
Rank analysis of variance is the appropriate
procedure for testing homogeneity of rows against
I
I.
I
I
I
I
I
I
I
-11-
the alternative that at least one probability effect
is 'non-zero.
probability effects will all be zero, although the
converse does not necessarily hold.)
••
I
The test statistic
is
I
-2
2
Ri Xi - n(n+l) /4
~
F
i=l
= N-I
1-1
I
J
~
~
2
I
Y.n .. - ~ R.
~
i=l j=l J ~J
i=l
x:
~
-
where X. and Y. are as defined in (7.3) above.
~
J
This
is related to the more commonly used Kruskal-Wallis
H-statistic (when the latter is corrected for ties in
the customary manner) by the formula
1_
J
I
I
I
I
I
I
(If the hypothesis is true, then the
H=
(N-l) (I-l)F
(N-I) + (I-l)F
The usual Kruskal-Wallis test is based on treating H
as a chi-square variable with (1-1) degrees of freedom.
However, the program uses the improved approximation
obtained by treating F as an F-variable with (I-l)D
and (N-I)D degrees of freedom, where
D
This is the "B
2
=1
6{N+l)
- (N-l)(5N+6)
approximation" suggested by David L.
Wallace in the Journal of the American Statistical
Association, Vol. 54 (1959); it should be quite
satisfactory if all row totals are at least 5 or more.
7.6
Standard analysis of variance
Standard analysis of variance is the appropriate
I
I.
I
I
I
I
I
I
I
-12-
procedure for testing homogeneity of rows against
the alternative that the row means are not all equal.
(If the hypothesis is true then the row means will all be
equal, although the converse does not necessarily hold.)
The test statistic is
I
N-I
F = -1-1
i=l
I
J
L:
L:
i=l j=l
where x and the
above.
-2 N x=2
L: R.~ x.~
x.
~
IS
.2
..
J n ~J
-
I
L:
i=l
-2
R. x.
~
~
are the means as defined in (7.1)
The statistic F has asymptotically, under the
hypothesis of homogeneity, an F-distribution with (1-1)
and (N-I) degrees of freedom.
(If column subscripts
Ie
were exactly normally distributed -- which is obviously
I
I
I
I
I
I
I
be exact even for small N.)
,.
I
not possible -- then the asymptotic distribution would
The test has no reasonable
interpretation unless it is assumed that the columns
are equally spaced along some scale.
8.
(OPTION)
CORRELATION ANALYSIS
This option is, of course, not meaningful unless the orderings of
the rows and columns are both relevant.
would appear as follows:
DO YOU WANT A CORRELATION ANALYSIS?
7YES
TOTAL PAIRS:
CONCORDANT:
DISCORDANT:
ROW TIES:
COLUMN TIES:
3486.
1198.
770.
855.
907.
For the example table the output
I
I.
I
I
I
I
I
I
I
I_
-13-
KENDALL CORRELATION (TAU-B): .1643
NORMAL DEVIATE FOR TEST OF INDEPENDENCE:
UNCONDITIONAL INDEX (TAU-A):
STANDARD ERROR: .0616
.1228
GOODMAN-KRUSKAL (CONDITIONAL) INDEX (G):
STANDARD ERROR: .1087
SPEARMAN RANK CORRElATION:
1. 7834
.2175
.1944
PRODUCT-MOMENT CORRElATION: .1789
(ASSUMING EQUALLY-SPACED ROWS AND COLUMNS)
8.1
Counts of pairs
The total number of pairs is
N(N-1)
NP = \
.
Let C.. =
L::
i '<j
L::
j '<j
n i , j' + L::
i '>i
L::
j '>j
n i , j'
and D
ij
=
L::
i '<i
L::
j '>j
n i , j' + L::
i '>i
L::
j '<j
n i , j'
for 1
i
~
LJ
~
I, 1
~
j
~
J.
Then Cij is the number of
observations concordant with anyone observation in
I
I
I
I
I
I
I
••
I
the (i,j) cell, and the number of concordant pairs is
NC
=\
L::L::n i .C ..•
ij J LJ
Similarly, D is the number of observations discordant
ij
with anyone observation in the (i,j) cell, and the
number of discordant pairs is
ND = \ L::L::n ..D..•
.• LJ LJ
LJ
The number of row ties is
NX
=\
L::R.
(Ri-l)
• L
L
and the number of column ties is
NY = ~ r:C. (C. -1)
j J J
The total number of ties, eliminating the duplicate
"double ties", may be calculated as
I
I.
I
I
I
I
I
I
I
-14-
NT = NP - NC - ND;
this is not printed out, however.
8.2
The Kendall Correlation coefficient, or "tau-bit, is
calculated as
tau- b
A test of independence based on this coefficient uses
Z
I.
I
I
N(N-1)(N-2)
Q Q + l8(N-2)(NP-NX)(NP-NY)
l 2
= 3(NC-ND)
as a normal deviate, where
Q1
= N(N-l)(N-2)
Q
= N(N-l) (N-2)
-
~R.(R.-l)(R.-2)
.
~
~
~
~
and
2
-
~C.(C.-l)(C.-2)
j
J
J
J
This value of Z is fully adjusted for ties.
The approxi-
mation to normality may be regarded as reasonably accurate
if N > = 20, excellent if N >= 50.
8.3
I
I
I
I
= r==N:C==-=::ND====J(NP-NX) (NP-NY)
I_
I
I
Kendall correlation
Unconditional index
The unconditional index of order-association, or Kendall's
"tau-a", is
tau-a
= NC-ND
NP
Its standard error is
8.4
Goodman-Kruskal or conditional index
The conditional index of order-association, or Goodman
and Kruskal' s ItG It , is
I
I.
I
I
I
I
I
I
I
I_
I
I
I
I
I
I
-15-
NC-NO
G = NC+ND
and its standard error is
2
2
2
2
(NO) L:L:n .. C.. - 2(NC) (ND)L:L:n .. Ci.D .. + (NC) L:L:n.jD ..
~J
8.5
~J
~J
~
Spearman rank correlation
=J
(This is not printed out if I
2, in which
z
case it would be identical with the Kendall correlation.)
Define
X.~
·I Ri , + \
s:
(R.+l)
~
for 1
~
i
(C.+l)
for 1
~
j ~ J;
I
i'<i
and
Y.
J
" '<jI cj ' +
~
J
j
Xi may be interpreted as the row-rank of any observation in the i-th raw and Y as the column-rank of any
j
observation in the j-th column.
Then the sample
Spearman rank correlation between rows and columns is
where
I
J
i = L: XiR./N
. 1
~=
~
and
Y= L:
j=l
Y.Cj/N.
J
(This is just the ordinary product-moment correlation
between row-ranks and column-ranks.)
I.
I
I
J
~J
~J
I
I.
I
I
I
I
I
I
I
-16-
8.6
Product-moment correlation
I.
I
I
in which
case it would be identical with the Kendall Correlation.)
The sample product-moment correlation between rows
and columns is
r
=
L:L:(i-I) (j- j)n ..
~l
where
I
i = L: iR. IN
i=l ~
J
and
j
= L:
jC.lN
j=l
J
This measure of correlation has no reasonable interpretation unless it is assumed that the rows and the
I_
I
I
I
I
I
I
= J = 2,
(This is not printed out if I
columns both represent equally-spaced values on
corresponding scales.
9.
OPTION TO REPEAT SAME ANALYSIS WITH DIFFERENT DATA
When the analysis of the contingency table is completed, the
computer asks:
FOR YOUR NEXT TABLE, DO YOU WANT TO CHANGE DIMENSIONS OR OPTIONS?
If the user answers "YES", the computer returns to (1.) and the
program begins again as if it had just been called.
If the user answers "NO", the computer understands this to mean
that the next table has exactly as many rows and columns as the one just
analyzed, and that the same options are desired.
the request
ENTER ROW 1
It then goes directly to
I
I.
I
I
I
I
I
I
I
1_
I
I
I
I
I
I
I.
I
I
-17-
and demands the same amount of data as before, and performs the same
analysis without asking further questions.
This option makes the work go
much faster when the user has a series of similar tables to analyze.
If the user has no more tables to analyze, he should answer
"STOP" •
There follows another example of the output from the program.
This time a 2x2 table has been used, in order to illustrate the printout
for Fisher's test and the curtailment of the other analyses.
I
I.
I
I
I
I
I
I
I
I_
I
I
I
I
I
I
I.
I
I
-18-
THIS PROGRAM ANALYZES TWO-WAY CONTINGENCY TABLES.
HOW MANY ROWS (2 TO 50) AND COLUMNS (2 TO 25) ?
?2 2
ENTER ROW 1
?39 16
ENTER ROW 2
?21 34
DO YOU WANT THE TABLE PRINTED OUT?
?YES
R\C
1
2
TOTAL
1
39
16
55
2
21
34
55
60
50
110
TOTAL
DO YOU WANT EXPECTED VALUES?
?YES
ROW
COLUMN
OBSERVED
EXPECTED
(O-E)t2/E
1
1
2
1
2
1
2
2
39
16
21
34
30.00
25.00
30.00
25.00
2.7000
3.2400
2.7000
3.2400
CHI-SQUARE = 11.880 WITH 1 DEGREES OF FREEDOM
(SMALLEST EXPECTED VALUE: 25.00)
FISHER P-VALUE AGAINST POSITIVE ASSOCIATION:
.000518
DO YOU WANT TO TEST SYMMETRY?
?YES
FOR SIGN TEST OF STOCHASTIC EQUALITY OF MARGINALS:
APPROXIMATE NORMAL DEVIATE: .658
(16 OBSERVATIONS ABOVE DIAGONAL AND 21 BELOW)
I
I.
I
I
I
I
I
I
I
I_
I
I
I
I
I
I
I.
I
I
-19-
DO YOU WANT A CORRELATION ANALYSIS?
?YES
TOTAL PAIRS:
CONCORDANT:
DISCORDANT:
ROW TIES:
COLUMN TIES:
5995.
1326.
336.
2970.
2995.
KENDALL CORRELATION (TAU-B): .3286
NORMAL DEVIATE FOR TEST OF INDEPENDENCE:
UNCONDITIONAL INDEX (TAU-A):
STANDARD ERROR: .0453
3.4310
.1651
GOODMAN-KRUSKAL (CONDITIONAL) INDEX (G):
STANDARD ERROR: .1311
.5957
FOR YOUR NEXT TABLE, DO YOU WANT TO CHANGE DIMENSIONS OR OPTIONS?
?STOP
RAN 42 SEC.
I
I.
I
I
I
I
I
I
I
-20-
APPENDIX;
/
I_
I
I
I
I
I
I
I.
I
I
LISTING OF THE PROGRAM
10 DIMENSION NC50"25>,, NRC50>, NC(25)J 50 FORMATC/"ENTER ROW ",,11>
20 PRINT"THIS PROGRAM ANAl.YZES TWO-WAY CONTI NGENCY TABl.ES."
30 80S=U8IPRHJT" t"HOW MANY ROWS C2 TO 50) AND COLUMNS C2 TO 25) ?"
40 INPUT"l.I,LJJIFCLI-2)81;IF(50-LI)8IJIFCLJ-2)8IJIFC25-LJ)8IJ82U=IJG=0
50 D024J= 1" LJJ 24NCC J) =0; D06 I = I, LIl NRC I )=OJ 38PRI NT 50" I J DO 5J= 1" LJJ INPUT" t>1
, 60 IFCM)31JG=G+MJIFC524287-G)32;NRCI)=NRCI)+MJNC(J)=NCCJ):~J5NCI,J)=M
70 IFCNRCI)-1>33J6JIFCS)25;PRINT,t"DO YOU WANT THE TABLE PRINTED OUT?"
80 INPUT"KU 25IFCK1-"NO") 19" 27" 19; 27PRINT, t"N ="" ; GOT036
90 19PRINT, t" R'C",,; PRINTI 1, (J" J= 1,LJ), J PRI NT"
TOTAL"
100 DOI21=I,LIJPRINTI0,(I,,(~(I,J),J=I,LJ»,;PRINT"",;12PRINTll,NR(I)
110 PRINT" t"TOTAL",,;PRINTI 1",OJCCJ)"J=I"LJ), ;PRINT" ".I; 36PRINTI I,G; IF( 5)9
120 28PRINT, t t"DO YOU WANT EXPECTED VALUES?"; INPUT"K2J9IFCI1'2-"NO")84,1
130 84PRINT"ff"ROW COLUMN OBSERVED EXPECTED CO-E)'2/E".OGOT085;IU=-1
140 85Q=G;X=OJD0911=I.LIJD091J=I,LJJE=NRCI)*CNCCJ)/G).;IFCU)87
150 PRINT86.I,J,NCI.J)"E.J86FORMATCI3,I8,II0,FI0.2)J87IFCE)90,90
160 W=(CN(I"J)-E)t2)/EJX=X+W;90IFCU)91;IFCE)89,,89;PRINT88,W,
170 89PRINn 91 Q=MI N1FC E, Q); DF=CLI -I) * CLJ-I) J PRINT, t t "CHI - SQUARE""
180 IFC-E)92JPRINT78JGOT093;92PRINT77"X"DF;93PRINT79"Q
190 79FORMATC"CSMALL.EST.EXPECTED VALUE: ",F3.2,")",/)
200 IF(I-DF)6QJA=MINOFCNC,NR)JB=MAXOFCNC,NR);C=MINOFCNC(2)"NR(2»;L=A-N
210 I F( A- C) 43JA=C; B=MAXOFCNCC 2) ,NR(2) ) JL=A-NC 2" 2); -li31 FC 50-A)~; H=Q=T= 1
220 D044K=I,A;44H=H*CB-A+K)/CG-A+~);IFCl.-l)46JD045K=I,L
230 Q=Q*( C(A-K+ I ),*( G-B-X+ I» J'CK*CB"l"A+K»); 45T=T+QJ 46P=H*TJ PRINT52, P
240 52FORMATC/"FISHER P-VALUF; AGAINST POSITIVE ASSOCIATION: ",F7.6,/)
,250 60IFCLI-LJ)~,4,3;4IF(S)61JPRINT,"'OOYOU WANT TO TEST SYMMETRY?"
260 INPUT,K3; 611 F( K3-!'NO") 62',3; 621 FCLI -3) 70 PRINT" '''FOR TESTI NG SYMMETRY
270 + AGAINST GENERAL ALTERtolATIVESI"JPRINT"BOWKER'S CHI-SQUARE""JX=OJE=G
280 D064I =2,LI; K= 1- U D064J= I,KJ Q=NC I, J)+N( J. I ); E=MIN 1F( E" Q)J I F( Q) 6 5,65
290 64X=X+«N(I,J)-NCJ,I»t2)/QJPRINT77,,2*X.LI*CLI-l)/2JGOT066J65PRINT78
300 66PRINT79. E/2.; 78FORMAT(" NOT COMPUTED"); 7FORMATC F5. 3)
310 PRINT,t"FOR TESTING 'SYMMETRY AGAINST 'DIAGONAL SKEWNESS':"
320 PRINT"SEN' S CHI - SQUARE",,'; X=Q; E=G; 0068M=2, L1 J A=B=O; K=LI -M+ 1; D067J= I"K
330 A=A+NC J,J+M-I); 67B=B+NC J+M-I ,J); Q=A+B; E=MINI FC E, QV IFC Q) 69, 69
340 68X=X+CCA-B)t2)/QJPRINT77,2*X,LI-l;GOT070;69PRINT78J70PRINT79,El2
350 71PRINT, t"FOR SIGN, TEST OF STOCHASTIC EQUALITY OF MARGINALS:"
360 PRINT"APPROXIMATE NORMAL DEVIATEI ",;A=B=W=OJD073I=I,LIJD073J=I,LJ
370 Q=N(I,J)lIFCJ-I)72,,73JA=A+Q;GOT073;72B=B+Q;73;Q=SQRT(A+B);IFC-Q)74
380 PRINT78JGOT076;74Z=ABSCA-B)-lJI~CZ)75;W=Z/Q;75PRINT7"W;76PRINT8,A,B
390 8FORMATC"C""I I." OBSERVATIONS ABOVE DIAGONAL AND ",,11," BELOW)")
400 77FORMATC" = ",F4'.3,'· WITH ",11," DEGREES OF FREEDOM")
410 3IFCLJ-3) 39; IFCS)40JPRINT,t '"DO YOU WANT TO COMPARE ROWS, ASSUMING
420 + COLUMN ORDERING IS RELEVANT?,"J I NPUT.. K4; 401 FCK4-"NO") 49.39
430 49P~INT, '''ROW
N
MEAN
MEDIAN
PROBABILITY EFFECT""
4 40 Q= 1.rR= CG-LI ) I CLI - I); A=E=F=T=U=V=W=O; DO 51 J= 1" LJ; H=NC( J); W=W+H
450 I FC Q) 50 I FC W-G/2) 50 Q=- U C=J+. 5-(W-G/2) IHJ B=WJ I F( J- C) 51
460 B=B-HJ51A=A+J*H;D059I=I,LIJQ=1;P=W=X=Y=Z=01H=NRCI)JD058J=1"LJ
470 0=NCI,J);W=W+.5*NCCJ),Y=Y+0*WJF=F+O*W*W;W=W+.5*NCCJ);E=E+O*J*J
480 X=X+O*J;P=P+O; I FC C-J) 57; Z=Z+O; 571 FC Q) 56J IFC P-H/2) 58; Q=-I
490 D=J+.5-CP-H/2)/O'58;T=T+X*X/HJU=U+Y*Y/H1V=V+CCG*Z-B*H) t2)/H
I
I.
I
I
I
I
I
I
I
II
I
I
I
I
I
I.
I
I
-21-
500
510
520
530
540
550
560
570
580
590
600
6 10
620
630
640
650
660
670
680
690
700
710
720
7 30
740
750
760
770
780
790
800
810
820
830
840
850
860
8 70
59PRINT63,I,R,X/H,D,C2*Y)/CG*H)-tJD-I-C6*G+6)/CS*G*G+G-6)
PRINT99,G,A/G,C,V/CB*CG-B»,LI-l,R*CU-.25*G'3)/CF~U),
+D*CLI-t),D*CG-LI),R*CT-A*A/G)/CE-T),LI-I,G-LI
99FORMATC/"TG-TAL n , I9,2Fl0. 3,// /"MEDIAN-' TEST",/ 5X"CHI - SQUARE = ",
+FJh3," WITH ",11," DEGREES OF FREEDOM",//nRANK ANALYSIS OF VARIANCE
+ CKRUSKAL-WALLIS TEST)",/SX"F - ",F4.3," WITH ",F3.2," AND ",F3.2,
+" DEGREES OF FREEDOM",//"STANDARD ANALYSIS OF VARIANCE
+ CASSUM1NG E(;lUALLY-SPACED CoLtJllWS)",/5X"F - ".F4.3," WITH ",
+I1I n AND ",11," DEGREES OF P'RE&BO"""88FORNATCF11.iI)
39IFC S)4'7.JPRINT,' '"00 YOU WART A CORRELATION ANALYSIS1"
INPUT,K5; 47IFCK5-"N0") 3JII48J 34SC-Sn-SCCaSCD-SDI)1aOJP-G*C G-l )/2
DO 1 5 I 1, L U DO 1 5J= I, LJ; C- D= 0 J 10 FO RMATC / I 4, X, 8 I 7, 3 C/ 5X, 8 I 7) )
DOI4IP=I,LIJDOI4JP=I,LJJIF(CI-IP).CJ-JP»t3,14JC-C+NCIP,JP);GOT014
13D=D+NCIP,JP>.J til;aOFORMATC"STANDARD ERRORt ",F5.iI)
SCC=SCC+NC I,J)*C*CJ SCDaSCD+N( I,J)*G*DJ SDD-SDD+NC I,J)*D*D
SC=SC+NC I,J) *CJ 15SD-SD+~CI ,J)4tDJ peaSC/2; PD-SD/2J QQs 2*P*C G-2)
TR=O; QR=QQ; DO 161 1, LI J v-nc·!) In-TR+V*C V-I) /21 16QR-QR-W*CW- t ) *C W-2)
TC=O; QC=QQ; DO 17J= 1,LJJ V=PfCC Jp ,.CoTC+•• CV-I> /2; 17QC=QC-W*C W-I> *c w-a)
PRINT,ff"TOTAL PAIRS' .'·"PRlNT29"PJ29FORMATCP'9.0)
.
PRINT"CONCORDANTl ",JPRINT29,PC;PRINT"DI SCORDANT, ",jPRINT29,PD
PRINT"ROW TIES'
",JPRINT29,TRlPRINT"COLUMN .TIES. ",JPRINT29,TC
IFCPC+PD)48,48JTB-CPC-PD)/SQRT«P-TR).CP-T,C»JI8FORMATCF5.4)
=
=
Z=3.*(PC-PD)*SQRTCQQ/CQR.QC~18.*CG·2)*CP-TR)*(P-TC»)
SA= 5QRTC SCC-2* SCD+ SDD-( CSC- SOH !)/G)/PJll ,"ORMATC 817:. 3C / 5X, 81 7»
56=( C2/ (SC+SD) )**2) *SQRT( SCC*SD.SD-S*SC*SD*SCD+ SDD* SC*5C)
PRINT, '"KENDALL CORRELATION (TAU-B). n,; PRINT18, 1B
.
PRINT"NORMAL DEVIATE FOR TEST 0' IMDEPENDENCE: ",JPRINT18,Z
PRINT,'"UNCONDITIONAL INDEX CTAU-A>I ",;PRINT18,(PC-PD)/P
PRINT20,SAJPRINT,'"GOODMAN-J(RUSKAL .CCONDITIONAL) INDEX (6): ",
PRINT18, CPC-PD) /CPC+PD>J PRINT20, SG; IFCDF-2) 48
A=B=C=O; X=-GJ D0231=1 ... LUXaX+NR·C I) JY.-GJD022J= l,LJ; Y=Y+NCC.})
Q=NC 1" J) J A=A+Q*X*X; J3=B+Q~T*YJ C=C+Q*X*Y; a2Y=Y+NCC J} J 23X=X+NRC I)
PRINT,rnSPEARMAN RANK CORRELATIONI ",IPRINT18,C/SQRTCA*B)
X=Y=A=BaC=OlD053Ust ,LUS3X-X+U*NRCln .t.X=X/GJDOS4V=t, LJ; 54Y:IlY+V*NCC V)
Y=Y/G; DO 551=1 ,LU DOS5J.l .. LJJ Z=NC !.,JH A=A+,Z.( I -X), a; B=B+Z*C J-Y), 2
SSC=C+Z*C I·X)*C J-Y)J PRINTS6, Cit SQRTCA*B); 56F'<lRMATC P·PRODUCT-MOMENT· + CORRELATION: ",F5. 4j/"(ASSOMING EQUALLY~ SPACED ROWS AND COLt,mNS) ")
48PRI NT, , "'FOR YOUR ·NEXT TABLE, DO' YOU VANT TO CHANGE DIMENSIONS OR
8g0 + OPTIONS?"; INPUT,KII i'CK""MO") 60, 37J$QJ 63FO~MATCI2, I 12, 3Fl 0.3>
890 37S=-lJ GOT082; 31 STI)P"NE:G"J 32STOP"BIG"; 33S'MP"ROW"; END
.
© Copyright 2026 Paperzz