    PROGRAM g02bgfe

!      G02BGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

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

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

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

       ldr = nvars
       ldssp = nvars
       ldx = n
       ALLOCATE (r(ldr,nvars),ssp(ldssp,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 g02bgf(n,m,x,ldx,nvars,kvar,xbar,std,ssp,ldssp,r,ldr,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 of deviations'
       WRITE (nout,99998) kvar(1:nvars)
       WRITE (nout,99996) (kvar(i),ssp(i,1:nvars),i=1,nvars)
       WRITE (nout,*)
       WRITE (nout,*) 'Correlation coefficients'
       WRITE (nout,99998) kvar(1:nvars)
       WRITE (nout,99996) (kvar(i),r(i,1:nvars),i=1,nvars)

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