    PROGRAM g04agfe

!      G04AGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g04agf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: gm
       INTEGER                         :: i, ifail, ii, j, k, l, li, n, nhi,   &
                                          nij, nlo, nsub
!      .. Local Arrays ..
       REAL (KIND=nag_wp)              :: f(2), fp(2), ss(4)
       REAL (KIND=nag_wp), ALLOCATABLE :: gbar(:), sgbar(:), y(:)
       INTEGER                         :: idf(4)
       INTEGER, ALLOCATABLE            :: lsub(:), ngp(:), nobs(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          sum
!      .. Executable Statements ..
       WRITE (nout,*) 'G04AGF Example Program Results'
       WRITE (nout,*)

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

!      Read in number of groups
       READ (nin,*) k

       ALLOCATE (lsub(k),ngp(k),gbar(k))

!      Read in number of subgroups
       READ (nin,*) lsub(1:k)

!      Total number of subgroups
       l = sum(lsub(1:k))

       ALLOCATE (nobs(l),sgbar(l))

!      Read in the number of observations
       READ (nin,*) nobs(1:l)

!      Total number of observations
       n = sum(nobs(1:l))

       ALLOCATE (y(n))

!      Read in the data
       READ (nin,*) y(1:n)

!      Display data
       WRITE (nout,*) 'Data values'
       WRITE (nout,*)
       WRITE (nout,*) ' Group  Subgroup  Observations'
       nsub = 0
       nlo = 1
       DO i = 1, k
          li = lsub(i)
          DO j = 1, li
             nsub = nsub + 1
             nij = nobs(nsub)
             nhi = nlo + nij - 1
             WRITE (nout,99999) i, j, y(nlo:nhi)
             nlo = nlo + nij
          END DO
       END DO

!      Perform ANOVA
       ifail = 0
       CALL g04agf(y,n,k,lsub,nobs,l,ngp,gbar,sgbar,gm,ss,idf,f,fp,ifail)

!      Display results
       WRITE (nout,*)
       WRITE (nout,*) 'Subgroup means'
       WRITE (nout,*)
       WRITE (nout,*) '   Group  Subgroup  Mean'
       ii = 0
       DO i = 1, k
          li = lsub(i)
          DO j = 1, li
             ii = ii + 1
             WRITE (nout,99998) i, j, sgbar(ii)
          END DO
       END DO
       WRITE (nout,*)
       WRITE (nout,99997) '    Group 1 mean =', gbar(1), '   (', ngp(1), &
          ' observations)'
       WRITE (nout,99997) '    Group 2 mean =', gbar(2), '   (', ngp(2), &
          ' observations)'
       WRITE (nout,99997) '    Grand mean   =', gm, '   (', n, &
          ' observations)'
       WRITE (nout,*)
       WRITE (nout,*) 'Analysis of variance table'
       WRITE (nout,*)
       WRITE (nout,*) '   Source                SS    DF  F ratio  Sig'
       WRITE (nout,*)
       WRITE (nout,99996) 'Between groups        ', ss(1), idf(1), f(1), fp(1)
       WRITE (nout,99996) 'Bet sbgps within gps  ', ss(2), idf(2), f(2), fp(2)
       WRITE (nout,99996) 'Residual              ', ss(3), idf(3)
       WRITE (nout,*)
       WRITE (nout,99996) 'Total                 ', ss(4), idf(4)

99999  FORMAT (1X,I5,I9,4X,10F4.1)
99998  FORMAT (1X,I6,I8,F10.2)
99997  FORMAT (1X,A,F5.2,A,I2,A)
99996  FORMAT (1X,A,F6.3,I5,F7.2,F8.3)
    END PROGRAM g04agfe
