    PROGRAM g12zafe

!      G12ZAF Example Program Text.

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g11caf, g12zaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: dev, tol
       INTEGER                         :: cm, i, ifail, ip, iprint, ldz, lisi, &
                                          lwk, m, maxit, mxn, n, nd, ns, num,  &
                                          nxs
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), cov(:), sc(:), se(:), t(:),    &
                                          tp(:), wk(:), x(:,:), z(:,:)
       INTEGER, ALLOCATABLE            :: cnt(:), ic(:), id(:), irs(:),        &
                                          isi(:), isz(:), ixs(:), nca(:), nct(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count, maxval
!      .. Executable Statements ..
       WRITE (nout,*) 'G12ZAF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem size
       READ (nin,*) n, m, ns, maxit, iprint

       IF (ns>0) THEN
          lisi = n
       ELSE
          lisi = 0
       END IF

       ldz = n
       ALLOCATE (z(ldz,m),isz(m),t(n),ic(n),isi(lisi),tp(n),irs(n))

!      Read in the data
       IF (ns>0) THEN
          READ (nin,*) (t(i),z(i,1:m),ic(i),isi(i),i=1,n)
       ELSE
          READ (nin,*) (t(i),z(i,1:m),ic(i),i=1,n)
       END IF

!      Read in the variable indicator
       READ (nin,*) isz(1:m)

!      Calculate number of parameters in the model
       ip = count(isz(1:m)>0)

!      Call the routine once to calculate size of MXN ...

!      Dummy allocation
       mxn = 0
       ALLOCATE (x(mxn,ip),id(mxn),ixs(mxn))

!      Call G12ZAF to calculate MXN
       ifail = 1
       CALL g12zaf(n,m,ns,z,ldz,isz,ip,t,ic,isi,num,ixs,nxs,x,mxn,id,nd,tp, &
          irs,ifail)
       IF (ifail/=0 .AND. ifail/=3) THEN
          GO TO 20
       END IF

!      Required size for MXN is returned in NUM, so reallocate memory
       mxn = num
       DEALLOCATE (x,id,ixs)
       ALLOCATE (x(mxn,ip),id(mxn),ixs(mxn))

!      Create risk set
       ifail = 0
       CALL g12zaf(n,m,ns,z,ldz,isz,ip,t,ic,isi,num,ixs,nxs,x,mxn,id,nd,tp, &
          irs,ifail)

       ALLOCATE (cnt(nxs),b(ip),se(ip),sc(ip),nca(nxs),nct(nxs),cov(ip*(ip+ &
          1)/2))

!      Set tolerance
       tol = 1.0E-5_nag_wp

!      Read in initial parameter estimates
       READ (nin,*) b(1:ip)

!      Count the number of observations in each stratum
       cnt(1:nxs) = 0
       DO i = 1, num
          cnt(ixs(i)) = cnt(ixs(i)) + 1
       END DO
       cm = maxval(cnt(1:nxs))

       lwk = ip*num + (cm+1)*(ip+1)*(ip+2)/2 + cm
       ALLOCATE (wk(lwk))

!      Get parameter estimates from conditional logistic analysis
       ifail = 0
       CALL g11caf(num,ip,nxs,x,mxn,isz,ip,id,ixs,dev,b,se,sc,cov,nca,nct,tol, &
          maxit,iprint,wk,lwk,ifail)

!      Display results
       WRITE (nout,*) ' Parameter      Estimate', '       Standard Error'
       WRITE (nout,*)
       WRITE (nout,99999) (i,b(i),se(i),i=1,ip)

20     CONTINUE

99999  FORMAT (I6,10X,F8.4,10X,F8.4)
    END PROGRAM g12zafe
