    PROGRAM g02dnfe

!      G02DNF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02daf, g02dnf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rss, sestat, stat, t, tol
       INTEGER                         :: i, idf, ifail, ip, irank, ldq, ldx,  &
                                          lwk, lwt, m, n
       LOGICAL                         :: est, svd
       CHARACTER (1)                   :: mean, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), cov(:), f(:), h(:), p(:),      &
                                          q(:,:), res(:), se(:), wk(:), wt(:), &
                                          x(:,:), y(:)
       INTEGER, ALLOCATABLE            :: isx(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count, max
!      .. Executable Statements ..
       WRITE (nout,*) 'G02DNF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) n, m, weight, mean

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

       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)

!      Calculate IP
       ip = count(isx(1:m)>0)
       IF (mean=='M' .OR. mean=='m') THEN
          ip = ip + 1
       END IF

       lwk = max(5*(ip-1)+ip*ip,ip)
       ldq = n
       ALLOCATE (b(ip),se(ip),cov(ip*(ip+1)/2),res(n),h(n),q(ldq,ip+1),p(2*ip+ &
          ip*ip),wk(lwk),f(ip))

!      Use suggested value for tolerance
       tol = 0.000001E0_nag_wp

!      Fit general linear regression model
       ifail = 0
       CALL g02daf(mean,weight,n,x,ldx,m,isx,ip,y,wt,rss,idf,b,se,cov,res,h,q, &
          ldq,svd,irank,p,tol,wk,ifail)

!      Display initial parameter estimates
       WRITE (nout,*) 'Estimates from G02DAF'
       WRITE (nout,*)
       WRITE (nout,99999) 'Residual sum of squares = ', rss
       WRITE (nout,99998) 'Degrees of freedom = ', idf
       WRITE (nout,*)
       WRITE (nout,*) 'Variable   Parameter estimate   Standard error'
       WRITE (nout,*)
       WRITE (nout,99997) (i,b(i),se(i),i=1,ip)
       WRITE (nout,*)

       i = 0
ESTFN_LP: DO
          READ (nin,*,IOSTAT=ifail) f(1:ip)
          IF (ifail/=0) THEN
             EXIT ESTFN_LP
          END IF

          i = i + 1

!         Compute the estimable function
          ifail = -1
          CALL g02dnf(ip,irank,b,cov,p,f,est,stat,sestat,t,tol,wk,ifail)
          IF (ifail/=0) THEN
             IF (ifail/=2) THEN
                GO TO 20
             END IF
          END IF

!         Display results
          WRITE (nout,99996) 'Function ', i
          WRITE (nout,*)
          WRITE (nout,99995) f(1:ip)
          WRITE (nout,*)
          IF (est) THEN
             WRITE (nout,99994) 'STAT = ', stat, ' SE = ', sestat, ' T = ', t
          ELSE
             WRITE (nout,*) 'Function not estimable'
          END IF
          WRITE (nout,*)
       END DO ESTFN_LP

20     CONTINUE

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I4)
99997  FORMAT (1X,I6,2E20.4)
99996  FORMAT (1X,A,I4)
99995  FORMAT (1X,5F8.2)
99994  FORMAT (1X,A,F10.4,A,F10.4,A,F10.4)
    END PROGRAM g02dnfe
