    PROGRAM g02dffe

!      G02DFF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

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

!      Skip heading in data file
       READ (nin,*)
       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))

!      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

!      Include all variables in the model
       isx(1:m) = 1
       ip = m
       IF (mean=='M' .OR. mean=='m') THEN
          ip = ip + 1
       END IF

       lwk = max(5*(ip-1)+ip*ip,2*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))

!      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 results from G02DAF
       WRITE (nout,*) 'Results from full model'
       IF (svd) THEN
          WRITE (nout,*) 'Model not of full rank'
          WRITE (nout,*)
       END IF
       WRITE (nout,99999) 'Residual sum of squares = ', rss
       WRITE (nout,99998) 'Degrees of freedom = ', idf
       WRITE (nout,*)

!      Loop over list of variables to drop
U_LP:  DO
          READ (nin,*,IOSTAT=ifail) indx
          IF (ifail/=0) THEN
             EXIT U_LP
          END IF

          IF (ip<=0) THEN
             WRITE (nout,*) 'No terms left in model'
             EXIT U_LP
          END IF

!         Drop variable INDX from the model
          ifail = 0
          CALL g02dff(ip,q,ldq,indx,rss,wk,ifail)

          ip = ip - 1
          WRITE (nout,99998) 'Variable', indx, ' dropped'

!         Calculate parameter estimates etc
          ifail = 0
          CALL g02ddf(n,ip,q,ldq,rss,idf,b,se,cov,svd,irank,p,tol,wk,ifail)

!         Display the results for model with variable INDX droped
          WRITE (nout,99999) 'Residual sum of squares = ', rss
          WRITE (nout,99998) 'Degrees of freedom = ', idf
          WRITE (nout,*)
          WRITE (nout,*) 'Parameter estimate   Standard error'
          WRITE (nout,*)
          WRITE (nout,99997) (b(i),se(i),i=1,ip)
       END DO U_LP

99999  FORMAT (1X,A,E13.4)
99998  FORMAT (1X,A,I4,A)
99997  FORMAT (1X,E15.4,E20.4)
    END PROGRAM g02dffe
