Program g03gbfe
! G03GBF Example Program Text
! Mark 30.1 Release. NAG Copyright 2024.
! .. Use Statements ..
Use nag_library, Only: g03gbf, nag_wp, x04caf
! .. Implicit None Statement ..
Implicit None
! .. Parameters ..
Integer, Parameter :: nin = 5, nout = 6
! .. Local Scalars ..
Real (Kind=nag_wp) :: loglik, tol
Integer :: f1, g1, i, ifail, ldf, ldg, ldprob, &
lds, ldx, m, n, ng, niter, nvar, &
popt, riter, s1, s2, sds, sopt, tds, &
x1
! .. Local Arrays ..
Real (Kind=nag_wp), Allocatable :: f(:,:), g(:,:), prob(:,:), s(:,:,:), &
w(:), x(:,:)
Integer, Allocatable :: isx(:)
! .. Executable Statements ..
Write (nout,*) 'G03GBF Example Program Results'
Write (nout,*)
Flush (nout)
! Skip heading in data file
Read (nin,*)
! Problem size
Read (nin,*) n, m, nvar
! Number of groups
Read (nin,*) ng
! Scaling option
Read (nin,*) sopt
! Initial probabilities option
Read (nin,*) popt
! Maximum number of iterations
Read (nin,*) niter
! Leading dimensions
x1 = n + 1
g1 = nvar + 1
f1 = n + 1
ldx = 2*n
ldprob = n + 3
ldg = 2*nvar
ldf = 2*n
s1 = nvar + 1
s2 = nvar + 1
tds = ng
Select Case (sopt)
Case (1)
Case (2)
Case (3)
s2 = ng + 1
Case (4)
s2 = 2
Case Default
s1 = 2
s2 = 2
End Select
lds = 2*(s1-1)
sds = 2*(s2-1)
Allocate (s(lds,sds,tds))
Allocate (x(ldx,m),prob(ldprob,ng),g(ldg,ng),w(2*ng),isx(m),f(ldf,ng))
! Data matrix X
Read (nin,*)(x(i,1:m),i=1,n)
x(n+1:2*n,1:m) = x(1:n,1:m)
! Included variables
If (nvar/=m) Then
Read (nin,*) isx(1:m)
End If
tol = 0.0E0_nag_wp
riter = 5
! First Call using sopt = 1 (Groupwise covariance) and
! popt = 1 (probabilities set internally)
ifail = 0
Call g03gbf(n,m,x,ldx,isx,nvar,ng,1,prob,ldprob,niter,riter,w,g,ldg,1,s, &
lds,sds,f,ldf,tol,loglik,ifail)
! Optionally read initial probabilities of group membership
If (popt==2) Then
Read (nin,*)(prob(i,1:ng),i=1,n)
End If
! Second Call using supplied sopt and popt.
ifail = 0
Call g03gbf(n,m,x(x1,1),ldx,isx,nvar,ng,popt,prob,ldprob,niter,riter, &
w(ng+1),g(g1,1),ldg,sopt,s(s1,s2,1),lds,sds,f(f1,1),ldf,tol,loglik, &
ifail)
! Results from second call
Write (nout,*)
Flush (nout)
ifail = 0
Call x04caf('g','n',1,ng,w(ng+1),1,'Mixing proportions',ifail)
Write (nout,*)
Flush (nout)
ifail = 0
Call x04caf('g','n',nvar,ng,g(g1,1),ldg,'Group means',ifail)
Write (nout,*)
Flush (nout)
Select Case (sopt)
Case (1)
Do i = 1, ng
ifail = 0
Call x04caf('g','n',nvar,nvar,s(s1,s2,i),lds, &
'Variance-covariance matrix',ifail)
End Do
Case (2)
ifail = 0
Call x04caf('g','n',nvar,nvar,s(s1,s2,1),lds, &
'Pooled Variance-covariance matrix',ifail)
Case (3)
ifail = 0
Call x04caf('g','n',nvar,ng,s(s1,s2,1),lds,'Groupwise Variance',ifail)
Case (4)
ifail = 0
Call x04caf('g','n',nvar,1,s(s1,s2,1),lds,'Pooled Variance',ifail)
Case (5)
ifail = 0
Call x04caf('g','n',1,1,s(s1,s2,1),lds,'Overall Variance',ifail)
End Select
Write (nout,*)
Flush (nout)
ifail = 0
Call x04caf('g','n',n,ng,f(f1,1),ldf,'Densities',ifail)
Write (nout,*)
Flush (nout)
ifail = 0
Call x04caf('g','n',n,ng,prob,ldprob,'Membership probabilities',ifail)
Write (nout,*)
Write (nout,'(1X,A,1X,I16)') 'No. iterations:', niter
Write (nout,'(1X,A,1X,F16.4)') 'Log-likelihood:', loglik
Deallocate (x,prob,g,s,w,isx,f)
End Program g03gbfe