/* nag_ken_spe_corr_coeff (g02brc) Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1993. * Mark 8 revised, 2004. */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define CORR(I, J) corr[(I) *tdcorr + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, m, n, *sobs = 0, *sobsptr, *svar = 0; Integer *svarptr; Integer tdcorr, tdx; NagError fail; char s, w; double *corr = 0, *x = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_ken_spe_corr_coeff (g02brc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, " %*[^\n]"); /* Read data */ fscanf(fpin, "%ld%ld\n", &m, &n); if (m >= 2 && n >= 2) { if (!(x = NAG_ALLOC(n*m, double)) || !(corr = NAG_ALLOC(m*m, double)) || !(svar = NAG_ALLOC(m, Integer)) || !(sobs = NAG_ALLOC(n, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; tdcorr = m; } else { fprintf(fpout, "Invalid m or n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; i++) for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); /* Read flag specifying if svar is to be supplied */ fscanf(fpin, " %c", &s); if (s == 'S' || s == 's') { /* Assign pointer to svar and read in values for svar */ svarptr = svar; for (i = 0; i < m; i++) fscanf(fpin, "%ld", &svar[i]); } else { /* Assign pointer to NULL and discard rest of line */ svarptr = (Integer *) 0; /* skip rest of line */ fscanf(fpin, " %*[^\n]"); } /* Read flag specifying if sobs is to be supllied */ fscanf(fpin, " %c", &w); if (w == 'W' || w == 'w') { /* Assign pointer to sobs and read in values for sobs */ sobsptr = sobs; for (i = 0; i < n; i++) fscanf(fpin, "%ld", &sobs[i]); } else { /* Assign pointer to NULL and discard rest of line */ sobsptr = (Integer *) 0; /* skip rest of line */ fscanf(fpin, " %*[^\n]"); } /* Calculate the Kendall and Spearman coefficients */ /* nag_ken_spe_corr_coeff (g02brc). * Kendall and/or Spearman non-parametric rank correlation * coefficients, allows variables and observations to be * selectively disregarded */ nag_ken_spe_corr_coeff(n, m, x, tdx, svarptr, sobsptr, corr, tdcorr, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_ken_spe_corr_coeff (g02brc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nCorrelation coefficients:\n\n"); for (i = 0; i < m; i++) { for (j = 0; j < m; j++) fprintf(fpout, "%8.5f ", CORR(i, j)); fprintf(fpout, "\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (corr) NAG_FREE(corr); if (svar) NAG_FREE(svar); if (sobs) NAG_FREE(sobs); return exit_status; }