    PROGRAM g04eafe

!      G04EAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02daf, g04eaf, 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, j, ldq,    &
                                          ldx, levels, lv, lwt, m, n, tdx
       LOGICAL                         :: svd
       CHARACTER (1)                   :: mean, typ, weight
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:), cov(:), h(:), p(:), q(:,:),    &
                                          rep(:), res(:), se(:), v(:), wk(:),  &
                                          wt(:), x(:,:), y(:)
       INTEGER, ALLOCATABLE            :: ifact(:), isx(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G04EAF Example Program Results'
       WRITE (nout,*)

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

!      Read in problem information
       READ (nin,*) n, levels, typ, weight, mean

       IF (typ=='P' .OR. typ=='p') THEN
          lv = levels
       ELSE
          lv = 1
       END IF
       IF (typ=='C' .OR. typ=='c') THEN
          tdx = levels
       ELSE
          tdx = levels - 1
       END IF
       IF (weight=='w' .OR. weight=='W') THEN
          lwt = n
       ELSE
          lwt = 1
       END IF
       ldx = n
       ALLOCATE (x(ldx,tdx),ifact(n),v(lv),rep(levels),y(n),wt(lwt))

!      Read in data
       IF (weight=='W' .OR. weight=='w') THEN
          READ (nin,*) (ifact(i),y(i),wt(i),i=1,n)
       ELSE
          READ (nin,*) (ifact(i),y(i),i=1,n)
       END IF
       IF (typ=='P' .OR. typ=='p') THEN
          READ (nin,*) v(1:levels)
       END IF

!      Calculate dummy variables
       ifail = 0
       CALL g04eaf(typ,n,levels,ifact,x,ldx,v,rep,ifail)

       IF (typ=='C' .OR. typ=='c') THEN
          m = levels
       ELSE
          m = levels - 1
       END IF
       ip = m
       IF (mean=='M' .OR. mean=='m') THEN
          ip = ip + 1
       END IF

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

!      Use all the variables in the regression
       isx(1:m) = 1

!      Use the suggested value for tolerance
       tol = 0.00001E0_nag_wp

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

!      Display the results of the regression
       IF (svd) THEN
          WRITE (nout,99999) 'Model not of full rank, rank = ', irank
          WRITE (nout,*)
       END IF
       WRITE (nout,99998) 'Residual sum of squares = ', rss
       WRITE (nout,99999) 'Degrees of freedom = ', idf
       WRITE (nout,*)
       WRITE (nout,*) 'Variable   Parameter estimate   Standard error'
       WRITE (nout,*)
       WRITE (nout,99997) (j,b(j),se(j),j=1,ip)

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