    PROGRAM nag_fac_analysis_ex01

      ! Example Program Text for nag_fac_analysis
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_fac_analysis, ONLY : nag_prin_comp
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_write_mat, ONLY : nag_write_gen_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, k, m, n
      CHARACTER (1) :: matrix
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: chi_df(:)
      REAL (wp), ALLOCATABLE :: chi_stat(:), data(:,:), loading(:,:), &
       prin_var(:,:), score(:,:), sig_chi_stat(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) &
       'Example Program Results for nag_fac_analysis_ex01'

      ! Skip heading in data file

      READ (nag_std_in,*)
      READ (nag_std_in,*) matrix, n, m
      k = m                        ! all variables are inculded in the
      ! computation
      ALLOCATE (data(n,m),score(n,k),loading(k,k),prin_var(k,3),chi_df(k), &
       chi_stat(k),sig_chi_stat(k)) ! Allocate storage

      READ (nag_std_in,*) (data(i,:),i=1,n)

      CALL nag_prin_comp(data,prin_var,matrix=matrix,score=score, &
       loading=loading,chi_stat=chi_stat,chi_df=chi_df, &
       sig_chi_stat=sig_chi_stat)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       'Eigenvalues   Fractional   Cumulative     Chisq       Sig        DF'
      WRITE (nag_std_out,*) '               variation    variation'

      DO i = 1, k
        WRITE (nag_std_out,'(5f12.4,i8)') prin_var(i,:), chi_stat(i), &
         sig_chi_stat(i), chi_df(i)
      END DO

      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(loading,format='f12.4',title='Loadings')

      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(score,format='f12.4',title= &
       'Principal component scores')

      DEALLOCATE (data,score,loading,prin_var,chi_stat,sig_chi_stat, &
       chi_df)                     ! Deallocate storage

    END PROGRAM nag_fac_analysis_ex01
