Skip to content

Commit 0db15e7

Browse files
committed
Modify fortran tests for debugging chi-square issue
1 parent ab51ae3 commit 0db15e7

File tree

6 files changed

+356
-22
lines changed

6 files changed

+356
-22
lines changed

misc/fortran-tests/Makefile

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
FC = gfortran -std=legacy
22

3-
ps-test: zcrvalue.o pone.o ssize.o main.o
4-
$(FC) -o ps-test zcrvalue.o pone.o ssize.o main.o
3+
ps-test: zcrvalue.o pone.o ssize.o iprelris.o chisqsiz.o bisec.o fishersi.o main.o
4+
$(FC) -o $@ $^
55

6-
zcrvalue.o: zcrvalue.for
7-
$(FC) -c -o zcrvalue.o zcrvalue.for
8-
9-
pone.o: pone.for
10-
$(FC) -c -o pone.o pone.for
11-
12-
ssize.o: ssize.for
13-
$(FC) -c -o ssize.o ssize.for
14-
15-
main.o: main.for
16-
$(FC) -c -o main.o main.for
6+
%.o: %.for
7+
$(FC) -c -o $@ $<

misc/fortran-tests/bisec.for

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
SUBROUTINE BISEC(Y1_ARG,Y2_ARG,E_ARG,E1_ARG,F,X,ERROR)
2+
C
3+
C-Description-----------------------------------------------------------
4+
IMPLICIT NONE
5+
C
6+
C Function:
7+
C bisection
8+
C This procedure evaluates a function at the end-points of a real
9+
C interval. An error exit is taken if there is no change of sign.
10+
C Finds a root by iterated bisection and evaluation at the midpoint,
11+
C halting if either the value of the function is less than E_ARG or
12+
C two successive approximations of the root differ by less than
13+
C E1_ARG.
14+
C Usage:
15+
C
16+
C Arguments:
17+
C i Y1_ARG Lower end of interval
18+
C i Y2_ARG Upper end of interval
19+
C i E_AGR If value of function <= E_ARG then done
20+
C i E1_ARG If two successive approximations differ <= E1_ARG then done
21+
C i F The function to evaluate
22+
C o X Argument to function that produces root if one is found
23+
C o ERROR 0 if solution found
24+
C 1 if no solution is found
25+
C Notes:
26+
C . Reference:
27+
C "Bisection Routine", _Collected Algorithms from ACM_, vol I,
28+
C Algorithm 4, 1980.
29+
C
30+
C-Declarations----------------------------------------------------------
31+
C
32+
C
33+
C Arguments
34+
C
35+
REAL Y1_ARG,Y2_ARG,E_ARG,E1_ARG,X
36+
INTEGER ERROR
37+
C
38+
C Functions
39+
C
40+
REAL F
41+
C
42+
C Locals
43+
C
44+
REAL F1,FX
45+
REAL Y1,Y2,E,E1
46+
INTEGER I,J,K
47+
C
48+
C-Code------------------------------------------------------------------
49+
C
50+
C Assume success.
51+
C
52+
ERROR=0
53+
C
54+
C Copy arguments to local variables.
55+
C
56+
Y1=Y1_ARG
57+
Y2=Y2_ARG
58+
E=E_ARG
59+
E1=E1_ARG
60+
C
61+
C Initialize values.
62+
C
63+
10 CONTINUE
64+
I=1
65+
J=1
66+
K=1
67+
X=Y2
68+
C
69+
C Evaluate the function at X. If result is <= E then done.
70+
C
71+
20 FX=F(X)
72+
IF (ABS(FX).LE.E) RETURN
73+
GO TO (30,40) I
74+
30 CONTINUE
75+
I=2
76+
F1=FX
77+
X=Y1
78+
GO TO 20
79+
40 CONTINUE
80+
IF (FX*F1.GT.0) GO TO (1090,70) J
81+
GO TO (50,90) K
82+
50 CONTINUE
83+
J=2
84+
K=2
85+
C
86+
C Calculate a new midpoint.
87+
C
88+
60 CONTINUE
89+
X=(Y1+Y2)/2.
90+
GO TO 20
91+
C
92+
C Set interval end to be current midpoint.
93+
C
94+
70 CONTINUE
95+
Y2=X
96+
C
97+
C If two successive iteration differ by <= E1 then end.
98+
C
99+
80 CONTINUE
100+
IF (ABS(Y1-Y2).GE.E1) GO TO 60
101+
RETURN
102+
C
103+
C Set interval end to be current midpoint.
104+
C
105+
90 CONTINUE
106+
Y1=X
107+
GO TO 80
108+
1090 CONTINUE
109+
C
110+
C Set an error flag if unable to find a root.
111+
C
112+
ERROR=1
113+
RETURN
114+
END

misc/fortran-tests/chisqsiz.for

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
REAL FUNCTION CHISQSIZE(P1)
2+
IMPLICIT NONE
3+
C
4+
C This function returns the sample size associated with P1 and the
5+
C other input parameters. This formula should be used for studies that
6+
C are analyzed with an uncorrected chi-squared test.
7+
C
8+
C Arguments
9+
C
10+
REAL P1
11+
C
12+
C Functions
13+
C
14+
REAL ZCRVALUE
15+
C
16+
C Locals
17+
C
18+
REAL ZALPHA,ZBETA,P,Q
19+
C
20+
C Common from main
21+
C
22+
REAL ALPHA,BETA,P0,N,M,Q0,Q1
23+
COMMON/IPCOM/ALPHA,BETA,P0,N,M,Q0,Q1
24+
C
25+
C Code:
26+
ZALPHA=ZCRVALUE(ALPHA/2.)
27+
ZBETA=ZCRVALUE(BETA)
28+
P=(N*P1+M*N*P0)/(M*N+N)
29+
Q=1.-P
30+
Q1=1.-P1
31+
Q0=1.-P0
32+
CHISQSIZE=
33+
. (
34+
. (ZALPHA*SQRT((1.+1./M)*P*Q) + ZBETA*SQRT((P0*Q0)/M+(P1*Q1)))**2
35+
. /
36+
. (P0-P1)**2
37+
. ) - N
38+
C
39+
C Done.
40+
C
41+
RETURN
42+
END

misc/fortran-tests/fishersi.for

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
REAL FUNCTION FISHERSIZ(P1)
2+
IMPLICIT NONE
3+
C
4+
C This function returns the sample size associated with P1 and the
5+
C other input parameters. This formula should be used for studies that
6+
C are analyzed with Fisher's Exact test.
7+
C
8+
C Arguments
9+
C
10+
REAL P1
11+
C
12+
C Functions
13+
C
14+
REAL ZCRVALUE
15+
C
16+
C Locals
17+
C
18+
REAL ZALPHA,ZBETA,P,Q,NPRIME
19+
C
20+
C Common from main
21+
C
22+
REAL ALPHA,BETA,P0,N,M,Q0,Q1
23+
COMMON/IPCOM/ALPHA,BETA,P0,N,M,Q0,Q1
24+
C
25+
C Code:
26+
ZALPHA=ZCRVALUE(ALPHA/2.)
27+
ZBETA=ZCRVALUE(BETA)
28+
P=(N*P1+M*N*P0)/(M*N+N)
29+
Q=1.-P
30+
Q1=1.-P1
31+
Q0=1.-P0
32+
NPRIME=
33+
. (ZALPHA*SQRT((1.+1./M)*P*Q) + ZBETA*SQRT((P0*Q0)/M+(P1*Q1)))**2
34+
. /
35+
. (P0-P1)**2
36+
FISHERSIZ=
37+
. (NPRIME*(1.+SQRT(1.+2.*(M+1.)/(NPRIME*M*ABS(P0-P1))))**2/4.) - N
38+
C
39+
C Done.
40+
C
41+
RETURN
42+
END

misc/fortran-tests/iprelris.for

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
SUBROUTINE IPRELRISK
3+
. (XALPHA,POWER,XP0,XN,XM,UORF,P1L,ODDSL,RL,P1H,ODDSH,RH)
4+
!MS$ATTRIBUTES REFERENCE :: P1L
5+
!MS$ATTRIBUTES REFERENCE :: ODDSL
6+
!MS$ATTRIBUTES REFERENCE :: RL
7+
!MS$ATTRIBUTES REFERENCE :: P1H
8+
!MS$ATTRIBUTES REFERENCE :: ODDSH
9+
!MS$ATTRIBUTES REFERENCE :: RH
10+
IMPLICIT NONE
11+
12+
REAL*4 XALPHA,POWER,XP0,XN,XM,P1L,ODDSL,RL,P1H,ODDSH,RH
13+
INTEGER*4 UORF
14+
C
15+
C Relative risk CALCULATIONS FOR INDEPENDENT PROPORTIONS
16+
C DERIVE Relative risk FOR EACH GROUP IN A TRIAL OF TWO TREATMENTS
17+
C OR A CASE-CONTROL STUDY.
18+
C NORMAL APPROXIMATION ASSUMED
19+
C
20+
C Power formula from: Schlesselman: Case-control Studies: Design,
21+
C Conduct, Analysis. New York: Oxford U. Press 1982: 144-152.
22+
C See also: FRIEDMAN, FURBERG AND DEMETS:
23+
C FUNDAMENTALS OF CLINICAL TRIALS. BOSTON: WRIGHT PSG. 1982:75.
24+
C
25+
C INPUT:
26+
C ALPHA Type I error probability (two tailed)
27+
C POWER The desired statistical power
28+
C P0 True probability of exposure in control group when CASECTL=Y
29+
C True probability of event in control group when CASECTL=N
30+
C N Number of case patients
31+
C M Ratio of control to case
32+
C CASECTRL 1 for case-control studies
33+
C 2 for prospective studies
34+
C UORF =1 for uncorrected chi-square test
35+
C =2 for Fisher's exact test
36+
C OUTPUT:
37+
C RELRISK Relative risk of cases relative to controls that can be
38+
C detected with power POWER (when casectl=N)
39+
C ODDSRATIO Oddsratio of cases to controls that can be detected
40+
C with power POWER (when casectl=Y)
41+
C P1 True probability of exposure in case group that can be
42+
C detected with power POWER when CASECTL=Y
43+
C True probability of event in experimantal group that can
44+
C be detected with power POWER when CASECTL=N
45+
C
46+
C
47+
C Globals
48+
C
49+
C Declare variables in common to be made available to the function.
50+
C
51+
REAL ALPHA,BETA,P0,N,M,Q0,Q1
52+
COMMON/IPCOM/ALPHA,BETA,P0,N,M,Q0,Q1
53+
C
54+
C Declare functions.
55+
C
56+
EXTERNAL CHISQSIZE
57+
REAL CHISQSIZE
58+
EXTERNAL FISHERSIZ
59+
REAL FISHERSIZ
60+
C
61+
C Locals
62+
C
63+
INTEGER ERRORL,ERRORH
64+
REAL E1,EPS,Y1,Y2,Q1L,Q1H
65+
C
66+
C Code:
67+
C
68+
ALPHA=XALPHA
69+
BETA=1.-POWER
70+
P0=XP0
71+
N=XN
72+
M=XM
73+
C
74+
C Set error tolerance values.
75+
C
76+
EPS=.0001*MIN(P0,1.-P0)
77+
E1=.0001*MIN(P0,1.-P0)
78+
C
79+
C Use bisection with appropriate routine for Chi-squared or Fisher's
80+
C exact test.
81+
C
82+
IF (UORF.EQ.1) THEN
83+
C
84+
C Set initial end points for lower end point.
85+
C
86+
Y1=0.0
87+
Y2=P0-EPS
88+
C
89+
C Solve the equation for the lower solution.
90+
C
91+
CALL BISEC(Y1,Y2,EPS,E1,CHISQSIZE,P1L,ERRORL)
92+
C
93+
C Set initial end points for high end point.
94+
C
95+
Y1=P0+EPS
96+
Y2=1.
97+
C
98+
C Solve the equation for the upper solution.
99+
C
100+
CALL BISEC(Y1,Y2,EPS,E1,CHISQSIZE,P1H,ERRORH)
101+
ELSE
102+
C
103+
C Case of Fisher's exact test.
104+
C
105+
Y1=0.0
106+
Y2=0.9999*P0
107+
CALL BISEC(Y1,Y2,EPS,E1,FISHERSIZ,P1L,ERRORL)
108+
Y1=0.9999*P0+0.0001
109+
Y2=1.
110+
CALL BISEC(Y1,Y2,EPS,E1,FISHERSIZ,P1H,ERRORH)
111+
END IF
112+
C
113+
C Calculate the relative risk and odds ratio for low end.
114+
C 21-Jan-1992 - Added calculation of Q1L to fix calculation of ODDSL.
115+
C
116+
IF (ERRORL.EQ.0) THEN
117+
RL=P1L/P0
118+
Q1L=1.-P1L
119+
ODDSL=(P1L*Q0)/(P0*Q1L)
120+
ELSE
121+
P1L=0
122+
RL=0
123+
Q1L=0
124+
ODDSL=0
125+
END IF
126+
C
127+
C Calculate the relative risk and odds ratio for high end.
128+
C 21-Jan-1992 - Added calculation of Q1H to fix calculation of ODDSH.
129+
C
130+
IF (ERRORH.EQ.0) THEN
131+
RH=P1H/P0
132+
Q1H=1.-P1H
133+
ODDSH=(P1H*Q0)/(P0*Q1H)
134+
ELSE
135+
P1H=0
136+
RH=0
137+
Q1H=0
138+
ODDSH=0
139+
END IF
140+
C
141+
C Done.
142+
C
143+
RETURN
144+
END

misc/fortran-tests/main.for

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
C vim: set noexpandtab:
22
PROGRAM MAIN
33
IMPLICIT NONE
4-
REAL ALPHA, BETA, PHI, P0, PSI, XN
5-
INTEGER M, ERR
4+
REAL*4 ALPHA,POWER,P0,N,M,P1L,ODDSL,RL,P1H,ODDSH,RH
5+
INTEGER*4 UORF
66

77
ALPHA = 5D-2
8-
BETA = 2D-1
9-
PHI = 7D-1
10-
P0 = 3D-1
11-
M = 1
12-
PSI = 2.
13-
CALL SSIZE(ALPHA, BETA, PHI, P0, M, PSI, XN, ERR)
8+
POWER = 8D-1
9+
P0 = 1D-1
10+
N = 325
11+
M = 9
12+
UORF = 1
1413

15-
PRINT *, "SSIZE(0.05, 1-0.8, 0.7, 0.3, 1, 2) =", XN, ERR
14+
CALL IPRELRISK(ALPHA,POWER,P0,N,M,UORF,P1L,ODDSL,RL,P1H,ODDSH,RH)
15+
16+
PRINT *, "IPRELRISK(0.05, 0.8, 0.1, 325, 9, 1) =", P1L, P1H
1617
END

0 commit comments

Comments
 (0)