    PROGRAM g02cgfe

!      G02CGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02cgf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, k, k1, ldc, ldcoef, ldr,   &
                                          ldrinv, ldssp, ldwkz, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: c(:,:), coef(:,:), r(:,:),           &
                                          rinv(:,:), ssp(:,:), wkz(:,:), xbar(:)
       REAL (KIND=nag_wp)              :: con(3), reslt(13)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02CGF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem size
       READ (nin,*) n, k
       k1 = k + 1
       ldr = k1
       ldssp = k1
       ldc = k
       ldcoef = k
       ldrinv = k
       ldwkz = k
       ALLOCATE (c(ldc,k),coef(ldcoef,3),r(ldr,k1),rinv(ldrinv,k), &
          ssp(ldssp,k1),wkz(ldwkz,k),xbar(k1))

!      Read in data
       READ (nin,*) xbar(1:k1)
       READ (nin,*) (ssp(i,1:k1),i=1,k1)
       READ (nin,*) (r(i,1:k1),i=1,k1)

!      Display data
       WRITE (nout,*) 'Means:'
       WRITE (nout,99999) (i,xbar(i),i=1,k1)
       WRITE (nout,*)
       WRITE (nout,*) 'Sums of squares and cross-products about means:'
       WRITE (nout,99998) (i,i=1,k1)
       WRITE (nout,99997) (i,ssp(i,1:k1),i=1,k1)
       WRITE (nout,*)
       WRITE (nout,*) 'Correlation coefficients:'
       WRITE (nout,99998) (i,i=1,k1)
       WRITE (nout,99997) (i,r(i,1:k1),i=1,k1)
       WRITE (nout,*)

!      Fit multiple linear regression model
       ifail = 0
       CALL g02cgf(n,k1,k,xbar,ssp,ldssp,r,ldr,reslt,coef,ldcoef,con,rinv, &
          ldrinv,c,ldc,wkz,ldwkz,ifail)

!      Display results
       WRITE (nout,*) 'Vble     Coef       Std err      t-value'
       WRITE (nout,99996) (i,coef(i,1:3),i=1,k)
       WRITE (nout,*)
       WRITE (nout,99995) 'Const', con(1:3)
       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,99994) 'Due to regression', reslt(1:4)
       WRITE (nout,99994) 'About  regression', reslt(5:7)
       WRITE (nout,99994) 'Total            ', reslt(8:9)
       WRITE (nout,*)
       WRITE (nout,99993) 'Standard error of estimate =', reslt(10)
       WRITE (nout,99993) 'Multiple correlation (R)   =', reslt(11)
       WRITE (nout,99993) 'Determination (R squared)  =', reslt(12)
       WRITE (nout,99993) 'Corrected R squared        =', reslt(13)
       WRITE (nout,*)
       WRITE (nout,*) &
          'Inverse of correlation matrix of independent variables:'
       WRITE (nout,99992) (i,i=1,k)
       WRITE (nout,99991) (i,rinv(i,1:k),i=1,k)
       WRITE (nout,*)
       WRITE (nout,*) 'Modified inverse matrix:'
       WRITE (nout,99992) (i,i=1,k)
       WRITE (nout,99991) (i,c(i,1:k),i=1,k)

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