home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
engineer
/
ksprob21.zip
/
KSMISC.EXE
/
KSGOF1.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-19
|
4KB
|
137 lines
program ksgof1; {simulates the chi-square goodness of fit test
when expected frequencies are completely
determined by sample size and the null hypothesis}
{This program simulates the chi-square goodness of fit test for
H0: The observed data comes from the distribution
specified by the expected frequencies.
against H1: The observed data comes from some other distribution.
The input file must contain the number of cells on the first
line.
Succeeding lines, one per cell, must contain the observed
count and the cumulative expected frequency for that cell.
Note that the cumulative expected frequency for the last cell
must be 1.0.
ksgof1.dat 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 cells <= 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
observed : array [1..100] of word;
expected,cumef : array [1..100] of real;
n,ncells : word;
numlarger,ntosim : longint;
obschisq,critz : real;
ifiln : string[80];
ifil : text;
procedure findchisq(var chi : real);
var
chis : real;
i : word;
begin
chis := 0.0;
for i := 1 to ncells do
chis := chis + sqr(observed[i] - expected[i]) / expected[i];
chi := chis
end; {findchisq}
procedure getdata;
var
i : 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,ncells);
if ncells > 100 then begin close(ifil); halt end;
for i := 1 to ncells do readln(ifil,observed[i],cumef[i]);
close(ifil)
end; {getdata}
procedure getstarted;
var
i : word;
begin
numlarger := 0;
for i := 1 to 100 do observed[i] := 0;
for i := 1 to 100 do expected[i] := 0;
for i := 1 to 100 do cumef[i] := 0;
ifiln := '';
critz := zinv(1.0 - 0.025,10);
getdata;
clrscr;
gotoxy(1,1); write('number of tables to simulate: ');
read(ntosim);
gotoxy(1,1); write(' ':40);
n := 0;
for i := 1 to ncells do n := n + observed[i];
expected[1] := n * cumef[1];
for i := 2 to ncells do expected[i] := (cumef[i] - cumef[i-1]) * n;
findchisq(obschisq)
end; {getstarted}
procedure generatesamples;
var
i,cellid : word;
k : longint;
ranum,cs,cumprob,halfwidth,ll,ul : real;
begin
for k := 1 to ntosim do
begin
for i := 1 to ncells do observed[i] := 0;
for i := 1 to n do
begin
ranum := randnum1;
cumprob := 0.0;
cellid := 0;
repeat inc(cellid) until (ranum <= cumef[cellid]) or (cellid = ncells);
inc(observed[cellid]);
end;
findchisq(cs);
if cs >= obschisq 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.