    PROGRAM g02dcfe

!      G02DCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02daf, g02dcf, g02ddf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rss, tol, wt, y
       INTEGER                         :: i, idf, ifail, ip, irank, ix, ldq,   &
                                          ldxm, lwt, m, n
       LOGICAL                         :: svd
       CHARACTER (1)                   :: mean, update, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), cov(:), h(:), p(:), q(:,:),    &
                                          res(:), se(:), wk(:), wtm(:), x(:),  &
                                          xm(:,:), ym(:)
       INTEGER, ALLOCATABLE            :: isx(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count
!      .. Executable Statements ..
       WRITE (nout,*) 'G02DCF 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
       ldxm = n
       ALLOCATE (xm(ldxm,m),ym(n),wtm(lwt),isx(m),x(m))

!      Read in data
       IF (lwt>0) THEN
          READ (nin,*) (xm(i,1:m),ym(i),wtm(i),i=1,n)
       ELSE
          READ (nin,*) (xm(i,1:m),ym(i),i=1,n)
       END IF

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

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

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

!      Use suggested value for tolerance
       tol = 0.000001E0_nag_wp

!      Fit initial model using G02DAF
       ifail = 0
       CALL g02daf(mean,weight,n,xm,ldxm,m,isx,ip,ym,wtm,rss,idf,b,se,cov,res, &
          h,q,ldq,svd,irank,p,tol,wk,ifail)

!      Display results from initial model fit
       WRITE (nout,*) 'Results from initial model fit using G02DAF'
       IF (svd) THEN
          WRITE (nout,*)
          WRITE (nout,*) 'Model not of full rank'
       END IF
       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)

!      Updating data is held in X consecutively
       ix = 1

!      Add or delete observations supplied in the data file
U_LP:  DO
          READ (nin,*,IOSTAT=ifail) update
          IF (ifail/=0) THEN
             EXIT U_LP
          END IF

          IF (lwt>0) THEN
             READ (nin,*) x(1:m), y, wt
          ELSE
             READ (nin,*) x(1:m), y
          END IF

!         Update the regression
          ifail = 0
          CALL g02dcf(update,mean,weight,m,isx,q,ldq,ip,x,ix,y,wt,rss,wk, &
             ifail)

!         Display titles and update observation count
          WRITE (nout,*)
          SELECT CASE (update)
          CASE ('a','A')
             WRITE (nout,*) 'Results from adding an observation using G02DCF'
             n = n + 1
          CASE ('d','D')
             WRITE (nout,*) &
                'Results from dropping an observation using G02DCF'
             n = n - 1
          CASE DEFAULT
             WRITE (nout,*) 'Unknown update flag read in from data file'
             GO TO 20
          END SELECT

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

!         Display updated results                        
          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)
       END DO U_LP

20     CONTINUE

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I4)
99997  FORMAT (1X,I6,2E20.4)
    END PROGRAM g02dcfe
