    Program g04agfe

!     G04AGF Example Program Text

!     Mark 24 Release. NAG Copyright 2012.

!     .. 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 Procedures ..
      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
