/* nag_chi_sq_2_way_table (g11aac) Example Program. * * Copyright 1996 Numerical Algorithms Group. * * Mark 4, 1996. * Mark 8 revised, 2004. * */ #include #include #include #include #include #define EXPT(I, J) expt[(I) *tdnobs + J] #define CHIST(I, J) chist[(I) *tdnobs + J] #define NOBST(I, J) nobst[(I) *tdnobs + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, ncol, *nobst = 0, nrow, tdnobs; NagError fail; double chi, *chist = 0, df, *expt = 0, g, prob; 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_chi_sq_2_way_table (g11aac) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld %ld %*[^\n] ", &nrow, &ncol); if (nrow >= 2 && ncol >= 2) { if (!(expt = NAG_ALLOC((nrow)*(ncol), double)) || !(chist = NAG_ALLOC((nrow)*(ncol), double)) || !(nobst = NAG_ALLOC((nrow)*(ncol), Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdnobs = ncol; } else { fprintf(fpout, "Invalid nrow or ncol.\n"); exit_status = 1; return exit_status; } for (i = 0; i < nrow; ++i) { for (j = 0; j < ncol; ++j) fscanf(fpin, "%ld", &NOBST(i, j)); fscanf(fpin, "%*[^\n]"); } /* nag_chi_sq_2_way_table (g11aac). * chi^2 statistics for two-way contingency table */ nag_chi_sq_2_way_table(nrow, ncol, nobst, tdnobs, expt, chist, &prob, &chi, &g, &df, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_chi_sq_2_way_table (g11aac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Probability = %6.4f\n", prob); fprintf(fpout, "Pearson Chi-square statistic = %8.3f\n", chi); fprintf(fpout, "Likelihood ratio test statistic = %8.3f\n", g); fprintf(fpout, "Degrees of freedom = %4.0f\n", df); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (expt) NAG_FREE(expt); if (chist) NAG_FREE(chist); if (nobst) NAG_FREE(nobst); return exit_status; }