    PROGRAM g02dgfe

!      G02DGF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02daf, g02dgf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rss, tol
       INTEGER                         :: i, idf, ifail, ip, irank, ldq, ldx,  &
                                          lwk, lwt, m, n
       LOGICAL                         :: svd
       CHARACTER (1)                   :: mean, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), cov(:), h(:), oy(:), p(:),     &
                                          q(:,:), res(:), se(:), wk(:), wt(:), &
                                          x(:,:), y(:)
       INTEGER, ALLOCATABLE            :: isx(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          count
!      .. Executable Statements ..
       WRITE (nout,*) 'G02DGF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)
       READ (nin,*) n, m, weight, mean

       IF (weight=='W' .OR. weight=='w') THEN
          lwt = n
       ELSE
          lwt = 0
       END IF
       ldx = n
       ALLOCATE (x(ldx,m),isx(m),oy(n),y(n),wt(lwt))

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

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

!      Calculate IP
       ip = count(isx(1:m)>0)
       IF (mean=='M' .OR. mean=='m') THEN
          ip = ip + 1
       END IF

       lwk = 5*(ip-1) + ip*ip
       ldq = n
       ALLOCATE (b(ip),se(ip),cov(ip*(ip+1)/2),res(n),h(n),q(ldq,ip+1),p(2*ip+ &
          ip*ip),wk(lwk))

!      Use suggested value for tolerance
       tol = 0.000001E0_nag_wp

!      Fit general linear regression model to first dependent variable
       ifail = 0
       CALL g02daf(mean,weight,n,x,ldx,m,isx,ip,oy,wt,rss,idf,b,se,cov,res,h, &
          q,ldq,svd,irank,p,tol,wk,ifail)

!      Display results for model fit to original dependent variable
       WRITE (nout,*) 'Results for original y-variable using G02DAF'
       WRITE (nout,*)
       IF (svd) THEN
          WRITE (nout,*) 'Model not of full rank'
          WRITE (nout,*)
       END IF
       WRITE (nout,99999) 'Residual sum of squares = ', rss
       WRITE (nout,99998) 'Degrees of freedom = ', idf
       WRITE (nout,*)
       WRITE (nout,*) 'Variable   Parameter estimate   Standard error'
       WRITE (nout,*)
       WRITE (nout,99997) (i,b(i),se(i),i=1,ip)
       WRITE (nout,*)

!      Read in the new dependent variable
       READ (nin,*) y(1:n)

!      Fit same model to different dependent variable
       ifail = 0
       CALL g02dgf(weight,n,wt,rss,ip,irank,cov,q,ldq,svd,p,y,b,se,res,wk, &
          ifail)

!      Display results for model fit to new dependent variable
       WRITE (nout,*) 'Results for second y-variable using G02DGF'
       WRITE (nout,*)
       WRITE (nout,99999) 'Residual sum of squares = ', rss
       WRITE (nout,99998) 'Degrees of freedom = ', idf
       WRITE (nout,*)
       WRITE (nout,*) 'Variable   Parameter estimate   ', 'Standard error'
       WRITE (nout,*)
       WRITE (nout,99997) (i,b(i),se(i),i=1,ip)

99999  FORMAT (1X,A,E12.4)
99998  FORMAT (1X,A,I4)
99997  FORMAT (1X,I6,2E20.4)
    END PROGRAM g02dgfe
