    PROGRAM g02bkfe

!      G02BKF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02bkf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, ldrz, ldsspz, ldx, m, n,   &
                                          nvars
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: rz(:,:), sspz(:,:), std(:), x(:,:),  &
                                          xbar(:)
       INTEGER, ALLOCATABLE            :: kvar(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02BKF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) n, m, nvars

       ldrz = nvars
       ldsspz = nvars
       ldx = n
       ALLOCATE (rz(ldrz,nvars),sspz(ldsspz,nvars),std(nvars),x(ldx,m), &
          xbar(nvars),kvar(nvars))

!      Read in data
       READ (nin,*) (x(i,1:m),i=1,n)

!      Read in column IDs
       READ (nin,*) kvar(1:nvars)

!      Display data
       WRITE (nout,99999) 'Number of variables (columns) =', m
       WRITE (nout,99999) 'Number of cases     (rows)    =', n
       WRITE (nout,*)
       WRITE (nout,*) 'Data matrix is:-'
       WRITE (nout,*)
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,x(i,1:m),i=1,n)
       WRITE (nout,*)

!      Calculate summary statistics
       ifail = 0
       CALL g02bkf(n,m,x,ldx,nvars,kvar,xbar,std,sspz,ldsspz,rz,ldrz,ifail)

!      Display results
       WRITE (nout,*) 'Variable   Mean    St. dev.'
       WRITE (nout,99995) (kvar(i),xbar(i),std(i),i=1,nvars)
       WRITE (nout,*)
       WRITE (nout,*) 'Sums of squares and cross-products about' // ' zero'
       WRITE (nout,99998) kvar(1:nvars)
       WRITE (nout,99996) (kvar(i),sspz(i,1:nvars),i=1,nvars)
       WRITE (nout,*)
       WRITE (nout,*) 'Correlation-like coefficients'
       WRITE (nout,99998) kvar(1:nvars)
       WRITE (nout,99996) (kvar(i),rz(i,1:nvars),i=1,nvars)

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,4I12)
99997  FORMAT (1X,I3,4F12.4)
99996  FORMAT (1X,I3,3F12.4)
99995  FORMAT (1X,I5,2F11.4)
    END PROGRAM g02bkfe
