    PROGRAM g02gkfe

!      G02GKF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02gcf, g02gkf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: a, dev, eps, tol
       INTEGER                         :: i, iconst, idf, ifail, ip, iprint,   &
                                          irank, ldc, ldv, ldx, lwk, lwt, m,   &
                                          maxit, n
       CHARACTER (1)                   :: link, mean, offset, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), c(:,:), cov(:), se(:), v(:,:), &
                                          wk(:), wt(:), x(:,:), y(:)
       INTEGER, ALLOCATABLE            :: isx(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count
!      .. Executable Statements ..
       WRITE (nout,*) 'G02GKF Example Program Results'
       WRITE (nout,*)

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

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

       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))

!      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)

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

!      Read in power for exponential link
       IF (link=='E' .OR. link=='e') THEN
          READ (nin,*) a
       END IF

       ldv = n
       lwk = (ip*ip+3*ip+22)/2
       ALLOCATE (b(ip),se(ip),cov(ip*(ip+1)/2),v(ldv,ip+7),wk(lwk))

!      Read in the offset
       IF (offset=='Y' .OR. offset=='y') THEN
          READ (nin,*) v(1:n,7)
       END IF

!      Read in control parameters
       READ (nin,*) iprint, eps, tol, maxit

!      Fit generalized linear model with Poisson errors
       ifail = -1
       CALL g02gcf(link,mean,offset,weight,n,x,ldx,m,isx,ip,y,wt,a,dev,idf,b, &
          irank,se,cov,v,ldv,tol,maxit,iprint,eps,wk,ifail)
       IF (ifail/=0) THEN
          IF (ifail<7) THEN
             GO TO 20
          END IF
       END IF

!      Display initial results
       WRITE (nout,99999) 'Deviance = ', dev
       WRITE (nout,99998) 'Degrees of freedom = ', idf
       WRITE (nout,*)

!      Calculate the number of constraints required
       iconst = ip - irank

!      Going to reallocate workspace, so deallocate it
       DEALLOCATE (wk)
       lwk = 2*ip*ip + ip*iconst + 2*iconst*iconst + 4*iconst

       ldc = ip
       ALLOCATE (c(ldc,iconst),wk(lwk))

!      Read in constraints
       READ (nin,*,IOSTAT=ifail) (c(i,1:iconst),i=1,ip)
       IF (ifail/=0) THEN
          WRITE (nout,99996) &
             ' ** Insufficient constraints supplied, was expecting ', iconst
          GO TO 20
       END IF

!      Re-estimate the model given the constraints
       ifail = 0
       CALL g02gkf(ip,iconst,v,ldv,c,ldc,b,1.0E0_nag_wp,se,cov,wk,ifail)

!      Display the constrained parameter estimates
       WRITE (nout,*) '      Estimate     Standard error'
       WRITE (nout,*)
       WRITE (nout,99997) (b(i),se(i),i=1,ip)

20     CONTINUE

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I2)
99997  FORMAT (1X,2F14.4)
99996  FORMAT (1X,A,I5)
    END PROGRAM g02gkfe
