    PROGRAM g03cafe

!      G03CAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g03caf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, l, ldfl, ldx, liwk, lres,  &
                                          lwk, lwt, m, n, nfac, nvar
       CHARACTER (1)                   :: matrix, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: com(:), e(:), fl(:,:), psi(:),       &
                                          res(:), wk(:), wt(:), x(:,:)
       REAL (KIND=nag_wp)              :: stat(4)
       INTEGER                         :: iop(5)
       INTEGER, ALLOCATABLE            :: isx(:), iwk(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G03CAF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) matrix, weight, n, m, nvar, nfac

       lwk = (5*nvar*nvar+33*nvar-4)/2
       IF (matrix=='C' .OR. matrix=='c') THEN
          lwt = 0
          ldx = m
       ELSE
          IF (weight=='W' .OR. weight=='w') THEN
             lwt = n
          ELSE
             lwt = 0
          END IF
          ldx = n
          lwk = max(lwk,n*nvar+7*nvar+nvar*(nvar-1)/2)
       END IF
       ldfl = nvar
       lres = nvar*(nvar-1)/2
       liwk = 4*nvar + 2
       ALLOCATE (x(ldx,m),isx(m),wt(lwt),e(nvar),com(nvar),psi(nvar), &
          res(lres),fl(ldfl,nfac),iwk(liwk),wk(lwk))

!      Read in the data
       IF (lwt>0) THEN
          READ (nin,*) (x(i,1:m),wt(i),i=1,ldx)
       ELSE
          READ (nin,*) (x(i,1:m),i=1,ldx)
       END IF

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

!      Read in options
       READ (nin,*) iop(1:5)

!      Fit factor analysis model
       ifail = -1
       CALL g03caf(matrix,weight,n,m,x,ldx,nvar,isx,nfac,wt,e,stat,com,psi, &
          res,fl,ldfl,iop,iwk,wk,lwk,ifail)
       IF (ifail/=0) THEN
          IF (ifail<=4) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,*) ' Eigenvalues'
       WRITE (nout,*)
       WRITE (nout,99998) e(1:nvar)
       WRITE (nout,*)
       WRITE (nout,99997) '     Test Statistic = ', stat(2)
       WRITE (nout,99997) '                 df = ', stat(3)
       WRITE (nout,99997) ' Significance level = ', stat(4)
       WRITE (nout,*)
       WRITE (nout,*) ' Residuals'
       WRITE (nout,*)
       l = 1
       DO i = 1, nvar - 1
          WRITE (nout,99999) res(l:(l+i-1))
          l = l + i
       END DO
       WRITE (nout,*)
       WRITE (nout,*) ' Loadings, Communalities and PSI'
       WRITE (nout,*)
       DO i = 1, nvar
          WRITE (nout,99999) fl(i,1:nfac), com(i), psi(i)
       END DO

20     CONTINUE

99999  FORMAT (2X,9F8.3)
99998  FORMAT (2X,6E12.4)
99997  FORMAT (A,F6.3)
    END PROGRAM g03cafe
