    PROGRAM g11aafe

!      G11AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g11aaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: chi, df, g, prob
       INTEGER                         :: i, ifail, ldnobs, ncol, nrow
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: chist(:,:), expt(:,:)
       INTEGER, ALLOCATABLE            :: nobs(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) ' G11AAF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

!      Read in the problem size
       READ (nin,*) nrow, ncol

       ldnobs = nrow
       ALLOCATE (nobs(ldnobs,ncol),expt(ldnobs,ncol),chist(ldnobs,ncol))

!      Read in data
       READ (nin,*) (nobs(i,1:ncol),i=1,nrow)

!      Perform chi-squared test
       ifail = -1
       CALL g11aaf(nrow,ncol,nobs,ldnobs,expt,chist,prob,chi,g,df,ifail)
       IF (ifail/=0) THEN
          IF (ifail/=3) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,99999) ' Probability =', prob
       WRITE (nout,99998) ' Pearson Chi-square statistic = ', chi
       WRITE (nout,99998) ' Likelihood ratio test statistic = ', g
       WRITE (nout,99997) ' Degrees of freedom = ', df

20     CONTINUE

99999  FORMAT (A,F7.4)
99998  FORMAT (A,F8.3)
99997  FORMAT (A,F4.0)
    END PROGRAM g11aafe
