home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
engineer
/
ksprob21.zip
/
KSMISC.EXE
/
KSGOF3.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-19
|
4KB
|
159 lines
program ksgof3; {simulation of the chi-square test for independence
in an r x c contingency table}
{This program simulates the chi-square goodness of fit test for
H0: The row and column variables are independent
against H1: These variables are not independent.
The input file must be an ascii file with the following
information in successive lines:
1. The number of rows and the number of columns in the table.
2. one line for each row, containing the observed counts
for that row.
separate numbers in the same line with spaces.
ksgof3.pas is a sample data set.
The results written to screen are:
the number of tables simulated with chi-square greater than
the chi-square for the observed data.
the number of tables simulated.
the upper and lower limits of a 95% confidence interval for
the pvalue of the chi-square goodness of fit test.
Program limits:
number of rows > 1
number of columns > 1
number of rows * number of columns <= 100
sample size n <= 32767
number of simulated samples <= 2147483647
}
{$B-,D+,E+,F+,L+,N+,R-,V-}
uses dos,crt,ksutils;
type
real = double;
var
origobs,simobs : array [1..100] of word;
e : array [1..100] of real;
rsum,csum : array [1..50] of word;
n,r,c : word;
numlarger,ntosim : longint;
origchisq,simchisq,critz : real;
ifiln : string[80];
ifil : text;
procedure getdata;
var
i,j : word;
begin
clrscr;
gotoxy(1,1); write('input file: ');
readln(ifiln);
assign(ifil,ifiln); {$I-} reset(ifil); {$I+}
if ioresult <> 0 then halt;
readln(ifil,r,c);
if (r * c > 100) or (r < 2) or (c < 2)
then begin close(ifil); halt end;
for i := 1 to r do
begin
for j := 1 to c do read(ifil,origobs[(i - 1) * c + j]);
readln(ifil)
end;
close(ifil)
end; {getdata}
procedure getstarted;
var
i,j,k : word;
begin
clrscr;
for i := 1 to 100 do origobs[i] := 0;
for i := 1 to 100 do simobs[i] := 0;
for i := 1 to 100 do e[i] := 0.0;
for i := 1 to 50 do rsum[i] := 0;
for i := 1 to 50 do csum[i] := 0;
getdata;
for i := 1 to r do
for j := 1 to c do
begin
k := (i - 1) * c + j;
rsum[i] := rsum[i] + origobs[k];
csum[j] := csum[j] + origobs[k]
end;
n := 0;
for i := 1 to r do n := n + rsum[i];
origchisq := 0.0;
for i := 1 to r do
for j := 1 to c do
begin
k := (i - 1) * c + j;
e[k] := rsum[i] * csum[j] / n;
origchisq := origchisq + sqr(origobs[k] - e[k]) / e[k]
end;
numlarger := 0;
critz := zinv(1.0 - 0.025,10);
clrscr;
gotoxy(1,1); write('number of tables to simulate: ');
readln(ntosim);
gotoxy(1,1); write(' ':40);
end; {getstarted}
procedure generatesamples;
var
simrsum,simcsum : array [1..50] of word;
i,j,cellid,ntodo : word;
k : longint;
ranum,cumprob,halfwidth,ll,ul : real;
begin
for k := 1 to ntosim do
begin
for i := 1 to r * c do simobs[i] := 0;
for i := 1 to r do simrsum[i] := 0;
for j := 1 to c do simcsum[j] := 0;
for ntodo := n downto 1 do
begin
ranum := randnum1;
cumprob := 0.0;
cellid := 0;
repeat
inc(cellid);
i := (cellid - 1) div c + 1;
j := (cellid - 1) mod c + 1;
cumprob := cumprob + ((rsum[i] - simrsum[i]) / ntodo) *
((csum[j] - simcsum[j]) / ntodo);
until (cumprob >= ranum) or (cellid = r * c);
inc(simobs[cellid]);
inc(simrsum[i]);
inc(simcsum[j]);
end;
simchisq := 0.0;
for i := 1 to r * c do
simchisq := simchisq + sqr(simobs[i] - e[i]) / e[i];
if simchisq >= origchisq then inc(numlarger);
gotoxy(1,1); write(' ':14);
gotoxy(1,1); write(numlarger, ' ',k)
end;
halfwidth := sqrt((numlarger/k)*((k - numlarger)/k)/k)*critz;
ul := numlarger / k + halfwidth;
ll := numlarger / k - halfwidth;
clrscr; gotoxy(1,1); write(numlarger, ' ',k,' ',ll:3:10,' ',ul:3:10)
end; {generatesamples}
begin {main}
clrscr;
initrand1;
getstarted;
generatesamples
end.