    PROGRAM g02chfe

!      G02CHF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02chf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, k, k1, ldcoef, ldcz, ldrz, &
                                          ldrznv, ldsspz, ldwkz, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: coef(:,:), cz(:,:), rz(:,:),         &
                                          rznv(:,:), sspz(:,:), wkz(:,:)
       REAL (KIND=nag_wp)              :: reslt(13)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02CHF Example Program Results'
       WRITE (nout,*)

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

!      Read in the problem size
       READ (nin,*) n, k
       k1 = k + 1
       ldcoef = k
       ldcz = k
       ldrz = k1
       ldrznv = k
       ldsspz = k1
       ldwkz = k
       ALLOCATE (coef(ldcoef,3),cz(ldcz,k),rz(ldrz,k1),rznv(ldrznv,k), &
          sspz(ldsspz,k1),wkz(ldwkz,k))

!      Read in data
       READ (nin,*) (sspz(i,1:k1),i=1,k1)
       READ (nin,*) (rz(i,1:k1),i=1,k1)

!      Display data
       WRITE (nout,*) 'Sums of squares and cross-products about zero:'
       WRITE (nout,99999) (i,i=1,k1)
       WRITE (nout,99998) (i,sspz(i,1:k1),i=1,k1)
       WRITE (nout,*)
       WRITE (nout,*) 'Correlation-like coefficients:'
       WRITE (nout,99999) (i,i=1,k1)
       WRITE (nout,99998) (i,rz(i,1:k1),i=1,k1)
       WRITE (nout,*)

!      Fit multiple linear regression model
       ifail = 0
       CALL g02chf(n,k1,k,sspz,ldsspz,rz,ldrz,reslt,coef,ldcoef,rznv,ldrznv, &
          cz,ldcz,wkz,ldwkz,ifail)

!      Display results
       WRITE (nout,*) 'Vble     Coef       Std err      t-value'
       WRITE (nout,99997) (i,coef(i,1:3),i=1,k)
       WRITE (nout,*)
       WRITE (nout,*) 'Analysis of regression table :-'
       WRITE (nout,*)
       WRITE (nout,*) '      Source        Sum of squares  D.F. &
          &   Mean square     F-value'
       WRITE (nout,*)
       WRITE (nout,99996) 'Due to regression', reslt(1:4)
       WRITE (nout,99996) 'About  regression', reslt(5:7)
       WRITE (nout,99996) 'Total            ', reslt(8:9)
       WRITE (nout,*)
       WRITE (nout,99995) 'Standard error of estimate =', reslt(10)
       WRITE (nout,99995) 'Multiple correlation (R)   =', reslt(11)
       WRITE (nout,99995) 'Determination (R squared)  =', reslt(12)
       WRITE (nout,99995) 'Corrected R squared        =', reslt(13)
       WRITE (nout,*)
       WRITE (nout,*) &
          'Inverse of correlation matrix of independent variables:'
       WRITE (nout,99994) (i,i=1,k)
       WRITE (nout,99993) (i,rznv(i,1:k),i=1,k)
       WRITE (nout,*)
       WRITE (nout,*) 'Modified inverse matrix:'
       WRITE (nout,99994) (i,i=1,k)
       WRITE (nout,99993) (i,cz(i,1:k),i=1,k)

99999  FORMAT (1X,3I10)
99998  FORMAT (1X,I4,3F10.4)
99997  FORMAT (1X,I3,3F12.4)
99996  FORMAT (1X,A,F14.4,F8.0,2F14.4)
99995  FORMAT (1X,A,F8.4)
99994  FORMAT (1X,2I10)
99993  FORMAT (1X,I4,2F10.4)
    END PROGRAM g02chfe
