/* nag_mv_cluster (g03ejc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. */ #include #include #include #include #define NMAX 10 #define X(I,J) x[(I)*tdx + J] int main(void) { Integer exit_status=0, i, *ic=0, *ilc=0, int_method, *iord=0, *isx=0, *iuc=0; Integer j, k, m, n, nsym, tdx; NagError fail; Nag_ClusterMethod method; Nag_DistanceType dist; Nag_MatUpdate update; Nag_VarScaleType scale; char **c=0, char_dist[2], char_scale[2], char_update[2], name[NMAX][3]; double *cd=0, *d=0, dlevel, dmin_, *dord=0, dstep, *s=0, *x=0, ydist; INIT_FAIL(fail); Vprintf("nag_mv_cluster_indicator (g03ejc) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf("%ld",&n); Vscanf("%ld",&m); if (n>=2 && m>=1) { if ( !( cd = NAG_ALLOC(n-1, double)) || !( d = NAG_ALLOC(n*(n-1)/2, double)) || !( dord = NAG_ALLOC(n, double)) || !( s = NAG_ALLOC(m, double)) || !( x = NAG_ALLOC((n)*(m), double)) || !( ic = NAG_ALLOC(n, Integer)) || !( ilc = NAG_ALLOC(n-1, Integer)) || !( iord = NAG_ALLOC(n, Integer)) || !( isx = NAG_ALLOC(m, Integer)) || !( iuc = NAG_ALLOC(n-1, Integer)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } tdx=m; } else { Vprintf("Invalid n or m.\n"); exit_status = 1; return exit_status; } Vscanf("%ld",&int_method); if (int_method == 1) method = Nag_SingleLink; else if (int_method == 2) method = Nag_CompleteLink; else if (int_method == 3) method = Nag_GroupAverage; else if (int_method == 4) method = Nag_Centroid; else if (int_method == 5) method = Nag_Median; else method = Nag_MinVariance; Vscanf("%s",char_update); if (*char_update == 'U') update = Nag_MatUp; else update = Nag_NoMatUp; Vscanf("%s",char_dist); if (*char_dist == 'A') dist = Nag_DistAbs; else if (*char_dist == 'E') dist = Nag_DistEuclid; else dist = Nag_DistSquared; Vscanf("%s",char_scale); if (*char_scale == 'S') scale = Nag_VarScaleStd; else if (*char_scale == 'R') scale = Nag_VarScaleRange; else if (*char_scale == 'G') scale = Nag_VarScaleUser; else scale = Nag_NoVarScale; for (j = 0; j < n; ++j) { for (i = 0; i < m; ++i) Vscanf("%lf",&X(j,i)); Vscanf("%s",name[j]); } for (i = 0; i < m; ++i) Vscanf("%ld",&isx[i]); for (i = 0; i < m; ++i) Vscanf("%lf",&s[i]); Vscanf("%ld",&k); Vscanf("%lf",&dlevel); /* Compute the distance matrix */ /* nag_mv_distance_mat (g03eac). * Compute distance (dissimilarity) matrix */ nag_mv_distance_mat(update, dist, scale, n, m, x, tdx, isx, s, d, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_distance_mat (g03eac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Perform clustering */ /* nag_mv_hierar_cluster_analysis (g03ecc). * Hierarchical cluster analysis */ nag_mv_hierar_cluster_analysis(method, n, d, ilc, iuc, cd, iord, dord, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_cluster_indicator (g03ejc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\nDistance Clusters Joined\n\n"); for (i = 0; i < n-1; ++i) { Vprintf("%10.3f ",cd[i]); Vprintf("%3s",name[ilc[i]-1]); Vprintf("%3s",name[iuc[i]-1]); Vprintf("\n"); } /* Produce dendrogram */ nsym = 20; dmin_ = 0.0; dstep = cd[n - 2] / (double) nsym; /* nag_mv_dendrogram (g03ehc). * Construct dendogram following * nag_mv_hierar_cluster_analysis (g03ecc) */ nag_mv_dendrogram(Nag_DendSouth, n, dord, dmin_, dstep, nsym, &c, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_dendrogram (g03ehc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); Vprintf("Dendrogram "); Vprintf("\n"); Vprintf("\n"); ydist = cd[n - 2]; for (i = 0; i < nsym; ++i) { if ((i+1) % 3 == 1) { Vprintf("%10.3f%6s",ydist,""); Vprintf("%s",c[i]); Vprintf("\n"); } else { Vprintf("%16s%s","", c[i]); Vprintf("\n"); } ydist -= dstep; } Vprintf("\n"); Vprintf("%14s",""); for (i = 0; i < n; ++i) { Vprintf("%3s",name[iord[i]-1]); } Vprintf("\n"); /* nag_mv_dend_free (g03xzc). * Frees memory allocated to the dendrogram array in * nag_mv_dendrogram (g03ehc) */ nag_mv_dend_free(&c); /* nag_mv_cluster_indicator (g03ejc). * Construct clusters following * nag_mv_hierar_cluster_analysis (g03ecc) */ nag_mv_cluster_indicator(n, cd, iord, dord, &k, &dlevel, ic, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_mv_cluster_indicator (g03ejc).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n%s%2ld%s\n\n","Allocation to ",k," clusters"); Vprintf("Object Cluster\n\n"); for (i = 0; i < n; ++i) { Vprintf("%5s%s%5s","",name[i],""); Vprintf("%ld ",ic[i]); Vprintf("\n"); } END: if (cd) NAG_FREE(cd); if (d) NAG_FREE(d); if (dord) NAG_FREE(dord); if (s) NAG_FREE(s); if (x) NAG_FREE(x); if (ic) NAG_FREE(ic); if (ilc) NAG_FREE(ilc); if (iord) NAG_FREE(iord); if (isx) NAG_FREE(isx); if (iuc) NAG_FREE(iuc); return exit_status; }