    PROGRAM g02eefe

!      G02EEF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02eef, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6, vnlen = 3
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: chrss, f, fin, rss
       INTEGER                         :: i, idf, ifail, ifr, istep, ldq, ldx, &
                                          lwt, m, maxip, n, nterm
       LOGICAL                         :: addvar
       CHARACTER (1)                   :: mean, weight
       CHARACTER (3)                   :: newvar
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: exss(:), p(:), q(:,:), wk(:), wt(:), &
                                          x(:,:), y(:)
       INTEGER, ALLOCATABLE            :: isx(:)
       CHARACTER (vnlen), ALLOCATABLE  :: free(:), model(:), vname(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count
!      .. Executable Statements ..
       WRITE (nout,*) 'G02EEF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size and various control parameters
       READ (nin,*) n, m, mean, weight, fin

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldx = n
       ALLOCATE (x(ldx,m),y(n),wt(lwt),isx(m),vname(m))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (x(i,1:m),y(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (x(i,1:m),y(i),i=1,n)
       END IF

!      Read in variable inclusion flags
       READ (nin,*) isx(1:m)

!      Read in first VNLEN characters of the variable names
       READ (nin,*) vname(1:m)

!      Calculate the maximum number of parameters in the model
       maxip = count(isx(1:m)>0)
       IF (mean=='M' .OR. mean=='m') THEN
          maxip = maxip + 1
       END IF

       ldq = n
       ALLOCATE (model(maxip),free(maxip),exss(maxip),q(ldq,maxip+2), &
          p(maxip+1),wk(2*maxip))

!      Loop over each variable, attempting to add each in turn
       istep = 0
       DO i = 1, m
!         Fit the linear regression model
          ifail = 0
          CALL g02eef(istep,mean,weight,n,m,x,ldx,vname,isx,maxip,y,wt,fin, &
             addvar,newvar,chrss,f,model,nterm,rss,idf,ifr,free,exss,q,ldq,p, &
             wk,ifail)

!         Display the results at each step
          WRITE (nout,99999) 'Step ', istep
          IF ( .NOT. addvar) THEN
             WRITE (nout,99998) 'No further variables added maximum F =', f
             WRITE (nout,99993) 'Free variables:  ', free(1:ifr)
             WRITE (nout,*) &
                'Change in residual sums of squares for free variables:'
             WRITE (nout,99992) '                 ', exss(1:ifr)
             GO TO 20
          ELSE
             WRITE (nout,99997) 'Added variable is ', newvar
             WRITE (nout,99996) 'Change in residual sum of squares =', chrss
             WRITE (nout,99998) 'F Statistic = ', f
             WRITE (nout,*)
             WRITE (nout,99995) 'Variables in model:', model(1:nterm)
             WRITE (nout,*)
             WRITE (nout,99994) 'Residual sum of squares = ', rss
             WRITE (nout,99999) 'Degrees of freedom = ', idf
             WRITE (nout,*)
             IF (ifr==0) THEN
                WRITE (nout,*) 'No free variables remaining'
                GO TO 20
             END IF
             WRITE (nout,99993) 'Free variables:  ', free(1:ifr)
             WRITE (nout,*) &
                'Change in residual sums of squares for free variables:'
             WRITE (nout,99992) '                 ', exss(1:ifr)
             WRITE (nout,*)
          END IF
       END DO

20     CONTINUE

99999  FORMAT (1X,A,I2)
99998  FORMAT (1X,A,F7.2)
99997  FORMAT (1X,2A)
99996  FORMAT (1X,A,E13.4)
99995  FORMAT (1X,A,6(1X,A))
99994  FORMAT (1X,A,E13.4)
99993  FORMAT (1X,A,6(6X,A))
99992  FORMAT (1X,A,6(F9.4))
    END PROGRAM g02eefe
