NAG Library Manual, Mark 30.3
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program g04cafe

!     G04CAF Example Program Text

!     Mark 30.3 Release. nAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: g04caf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: i, ifail, inter, irdf, itotal, k, l, &
                                          maxt, mterm, n, nblock, nfac, ntreat
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: bmean(:), e(:), r(:), semean(:),     &
                                          table(:,:), tmean(:), y(:)
      Integer, Allocatable             :: imean(:), iwk(:), lfac(:)
!     .. Executable Statements ..
      Write (nout,*) 'G04CAF Example Program Results'
      Write (nout,*)

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

!     Read in problem size
      Read (nin,*) n, nblock, nfac, inter

      Allocate (lfac(nfac),iwk(n+3*nfac),y(n),bmean(nblock+1),r(n))

!     Read in the number of levels for each factor
      Read (nin,*) lfac(1:nfac)

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

!     Use standard degrees of freedom
      irdf = 0

!     Using call to G04CAF to calculate required values for MAXT and MTERM ...

!     Setting MAXT to zero ensures it is too small and hence the routine
!     will calculate its correct size, IMEAN needs to be at least 1 element
!     long so set MTERM to 1.
      maxt = 0
      mterm = 1

!     Dummy allocation
      Allocate (tmean(maxt),e(maxt),table(mterm,5),semean(mterm),imean(mterm))

!     Call the routine initially to get MTERM and MAXT
      ifail = 1
      Call g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean,    &
        maxt,e,imean,semean,bmean,r,iwk,ifail)
      If (ifail/=0 .And. ifail/=2) Then
        Write (nout,99996) ' ** G04CAF exited with IFAIL = ', ifail
        Go To 100
      End If

!     Allocate remaining output arrays
      mterm = itotal
      maxt = imean(1)
      Deallocate (tmean,e,table,semean,imean)
      Allocate (tmean(maxt),e(maxt),table(mterm,5),semean(mterm),imean(mterm))

!     Calculate the ANOVA table
      ifail = 0
      Call g04caf(n,y,nfac,lfac,nblock,inter,irdf,mterm,table,itotal,tmean,    &
        maxt,e,imean,semean,bmean,r,iwk,ifail)

!     Display results
      Write (nout,*) ' ANOVA table'
      Write (nout,*)
      Write (nout,*) '  Source      df         SS          MS          F',     &
        '        Prob'
      Write (nout,*)
      k = 0
      If (nblock>1) Then
        k = k + 1
        Write (nout,99998) ' Blocks    ', table(1,1:5)
      End If
      ntreat = itotal - 3
      Do i = 1, ntreat
        Write (nout,99997) ' Effect  ', i, table(k+i,1:5)
      End Do
      Write (nout,99998) ' Residual  ', table(itotal-1,1:3)
      Write (nout,99998) ' Total     ', table(itotal,1:2)
      Write (nout,*)
      Write (nout,*) ' Treatment Means and Standard Errors'
      Write (nout,*)
      k = 1
      Do i = 1, ntreat
        l = imean(i)
        Write (nout,99996) ' Effect ', i
        Write (nout,*)
        Write (nout,99999) tmean(k:l)
        Write (nout,*)
        Write (nout,99995) ' SE of difference in means  = ', semean(i)
        Write (nout,*)
        k = l + 1
      End Do

100   Continue

99999 Format (8F10.2)
99998 Format (A,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99997 Format (A,I2,3X,F3.0,2X,2(F10.0,2X),F10.3,2X,F9.4)
99996 Format (A,I5)
99995 Format (A,F10.2)
    End Program g04cafe