C --------------------------------------------------------------------------
C  ALPHANEW.FOR           GJL 9-6-89   12-11-89
C   18:00
C  
C  Comparing Alpha Coefficients for Independent Samples
C  Algorithm adapted from formulae in:
C  Feldt, Woodruff & Salih (1987).  Statistical inference for coefficient
C        alpha.  APPLIED PSYCH MEAS., 11, 93-103.
C
C
      CHARACTER INF*24,OUTF*24,FMT*80,FMT2*80
      INTEGER DF,I,N,TYPE
      DIMENSION A(20),NI(20),NN(20),R(20,20)
      REAL*8 A,MU,NB,Q,QQ,SA,SI,SS,SM,UX,ZCHI,PCHI,RDF,TDF,R,HMN,NC,SUMN
      REAL*8 CM,CI,CP,NI,NN
C  Variable definitions ...
C   NB    DF + 1   or   NB - 1 = DF for test i
C   NI(I)    the number of items in test i
C   NN(I)    number of respondents who took test i
C   SI       the estimated variance of alpha for test i
C   A(I)     Alpha Coefficient for test i 
C   Q        Constant = 1/3    for transforming alphas
C   QQ       Constant = 2/3            "
C   SA    the sum of unweighted transformed alphas (used to get MU below)
C   MU    the simple unweighted average of the transformed alphas
C   na    the number of alphas (or # of groups)
C   UX    the Chi-squared value for omnibus Ho:  A(1) = A(2) = ... = A(na)
C 
C
      WRITE (*,1)
 1    FORMAT(//,    
     +'   Lautenschlager, G. J. (1989).  ALPHATST: Testing for'/
     +'      differences in coefficient alpha.'/ 
     +'      Applied Psychological Measurement, 13, 284.'//)
C
  27  WRITE(*,28)
  28  FORMAT('    ENTER INPUT FILENAME:  ',\)
      READ(*,29) INF
  29  FORMAT(A24)
      OPEN(5,FILE=INF,STATUS='OLD')   
           READ(5,31) TYPE,N,OUTF
  31       FORMAT(I1,I2,A24)
C
      WRITE (6,32) TYPE,N,OUTF
  32  FORMAT(' ------------- ALPHATST OUTPUT -------------------------'/
     +1X,'TYPE =',I1,'N = ', I2, 'INPUT FILE =',A24)
C
      IF (TYPE.GT.2) GOTO 700
      IF (TYPE.LT.1) GOTO 700
C   N = # OF GROUPS (IND) OR # OF SUBTESTS COMPARED ACROSS GROUPS   
C   OUTF = file name for output
C
           OPEN(6,FILE=OUTF,STATUS='NEW')   
           READ(5,33) FMT
C  FMT  = FIRST input format
  33       FORMAT(A80)
C
      WRITE (6,34) FMT
  34  FORMAT('FORMAT=',A80)
C
C
C 
      Q = .33333333333333333
      QQ = .66666666666666666
C       
C  READ IN ALPHA, # ITEMS & SAMPLE SIZE INFORMATION   
      READ(5,FMT) ( A(I),NI(I),NN(I),I=1,N ) 
       WRITE(6,FMT) ( A(I),NI(I),NN(I),I=1,N )
C   
      MU = 0
      SA = 0
      DO 45 I = 1,N
       SA = SA + ( 1 / (1 - A(I))**Q)
  45  CONTINUE
       MU = SA / N
C
C ******************************************************************
C
C Solution of INDEPENDENT GROUPS case when TYPE=1
      IF (TYPE.EQ.1) THEN 
C
C
C  EACH SI    FOUND USING FORMULA (19)    AND  SUM INTO   SS
C      SEE PARAGRAPH ABOVE FORMULA (19) FOR MEANING OF  NB
C  FIND VARIANCE OF USING FORMULA (20)
      SS = 0
      SM = 0
      DO 80 I = 1,N
       NB =  ( (NI(I) - 1) * NN(I) ) / (NI(I)+1.0)
       SI    = 2 / ( 9 * (NB - 1) * ((1-A(I))**QQ) )
C
       SS = SS + SI   
  80  CONTINUE
C
      SM = SS / N
C
C  CALCULATE INDEPENDENT GROUPS CHI-SQUARED       USING FORMULA (21)
      UX = 0      
      DO 85 I = 1,N
       UX = UX +  ( ( ( (1/(1-A(I))**Q) -MU) **2) /SM ) 
  85  CONTINUE     
      DF=N-1
      RDF=N-1
C
      ENDIF
C
C ******************************************************************
C
C  Solution of DEPENDENT GROUP case when TYPE=2
      IF (TYPE.EQ.2) THEN 
C
C  FMT2  = CORRELATION MATRIX input format  ONLY FOR DEPENDENT TESTS
           READ(5,33) FMT2
  90       FORMAT(A80)
C 
C  READ IN CORRELATION MATRIX lower triangular regardless of input file
      DO 100 I=1,N
      READ(5,FMT2)(R(I,J),J=1,I)
       WRITE(6,FMT2)(R(I,J),J=1,I) 
 100  CONTINUE
C
      SUMN=0
      DO 110 I=1,N
        SUMN=SUMN+(1/NI(I))
 110  CONTINUE
      HMN=N/SUMN
      I=1 
      NC=NN(I)*(HMN-1)/(HMN+1)  
C
C  EACH SI    FOUND USING FORMULA (24)    SUBSTITUTING 
C             COMMON N (NC AS CALCULATED ABOVE)
C      
C  FIND VARIANCE OF USING FORMULA (20)
      SS = 0
      SM = 0
      DO 120 I = 1,N
       SI    = 2 / ( 9 * (NC-1) * ((1-A(I))**QQ) )
       SS = SS + SI   
 120  CONTINUE
      SM = SS / N
C
      CP=0
      DO 130 I=2,N
      DO 130 J=1,I-1
      CI = 2*R(I,J)**2 / ( 9*(NC-1)*((1-A(I))**Q)*((1-A(J))**Q) )
C
       CP = CP + CI
 130  CONTINUE
C
C  FIND COVARIANCE OF ALPHA I & ALPHA J USING FORMULA (25)
      CM = CP / ((N**2-N)/2)
C
C  CALCULATE DEPENDENT SAMPLE CHI-SQUARED       USING FORMULA (26)
      UX = 0      
      DO 335 I = 1,N
       UX = UX +  ( ( ( (1/(1-A(I))**Q) -MU) **2) /(SM-CM)  ) 
 335  CONTINUE     
      DF=N-1
      RDF=N-1
C
      ENDIF
C
C ******************************************************************
C ****************************************************************** 
C  FIND p-VALUE FOR CHI-SQUARED
C
       TDF = 2/(9*RDF) 
       ZCHI = ( (UX/DF)**Q - (1-TDF) ) / DSQRT(TDF)
       PCHI = .5 / (1 + (.196854*ZCHI) + (.115194*(ZCHI)**2) +
     + (.000344*(ZCHI)**3) + (.019527*(ZCHI)**4) ) ** 4
C  PRINT HEADER & INPUT INFORMATION
      WRITE (6,345) INF
 345  FORMAT(' -------------------------------------------------------'/
     +1X,' ------------- TEST FOR ALPHA COEFFICIENTS -------------'/
     +1X,'INPUT FILE FOR THIS RUN WAS:  ',A24)
C
      IF (TYPE.EQ.1) THEN
        WRITE(6,350) N
 350    FORMAT(5X,'# OF INDEPENDENT GROUPS:  ',I3,/)  
      ELSEIF (TYPE.EQ.2) THEN
        WRITE(6,355) N
 355    FORMAT(5X,'# OF DEPENDENT ALPHAS:  ',I3,/)  
      ENDIF
      WRITE(6,400) UX
 400  FORMAT(10X,'CHI-SQUARED VALUE IS :  ',F12.4,/)     
      WRITE(6,410) DF,PCHI 
 410  FORMAT(12X,'df =  ',I5,11X,'p < ',F8.5)
C
C  CLOSE FILES
 500    CLOSE(5)
        CLOSE(6)
        GOTO 900
 700  WRITE(*,701)
 701  FORMAT(' ERROR IN INPUT FILE!!',\)
 900  STOP
      END