/* nag_step_regsn (g02eec) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Scalars */ double chrss, f, fin, rss; Integer exit_status, i, idf, ifr, istep, j, m, maxip, n, nterm, pdq, pdx; /* Arrays */ char nag_enum_arg[40]; char *newvar; double *exss = 0, *p = 0, *q = 0, *wt = 0, *x = 0, *y = 0; double *wtptr = 0; Integer *sx = 0; char **free_vars, **model; const char *vname[] = { "DAY", "BOD", "TKN", "TS", "TVS", "COD" }; /* NAG Types */ Nag_OrderType order; Nag_IncludeMean mean; Nag_Boolean addvar = Nag_FALSE, weight; NagError fail; #ifdef NAG_COLUMN_MAJOR #define X(I, J) x[(J-1)*pdx + I - 1] order = Nag_ColMajor; #else #define X(I, J) x[(I-1)*pdx + J - 1] order = Nag_RowMajor; #endif 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); exit_status = 0; fprintf(fpout, "nag_step_regsn (g02eec) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%ld", &n, &m); fscanf(fpin, " %s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ mean = (Nag_IncludeMean) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, " %s", nag_enum_arg); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); maxip = m; /* Allocate memory */ if (!(exss = NAG_ALLOC(maxip, double)) || !(p = NAG_ALLOC(maxip+1, double)) || !(q = NAG_ALLOC(n * (maxip+2), double)) || !(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n * m, double)) || !(y = NAG_ALLOC(n, double)) || !(sx = NAG_ALLOC(m, Integer)) || !(free_vars = NAG_ALLOC(maxip, char *)) || !(model = NAG_ALLOC(maxip, char *)) ) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_MAJOR pdx = n; pdq = n; #else pdx = m; pdq = maxip+2; #endif if (weight) { for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf%lf%*[^\n] ", &y[i - 1], &wt[i - 1]); wtptr = wt; } } else { for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf%*[^\n] ", &y[i - 1]); } } for (j = 1; j <= m; ++j) fscanf(fpin, "%ld", &sx[j - 1]); fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%lf%*[^\n] ", &fin); fprintf(fpout, "\n"); istep = 0; for (i = 1; i <= m; ++i) { /* nag_step_regsn (g02eec). * Fits a linear regression model by forward selection */ nag_step_regsn(order, &istep, mean, n, m, x, pdx, vname, sx, maxip, y, wtptr, fin, &addvar, &newvar, &chrss, &f, model, &nterm, &rss, &idf, &ifr, free_vars, exss, q, pdq, p, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_step_regsn (g02eec).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Step %ld\n", istep); if (!addvar) { fprintf(fpout, "No further variables added maximum F =%7.2f\n", f); fprintf(fpout, "Free variables: "); for (j = 1; j <= ifr; ++j) fprintf(fpout, "%3.3s %s", free_vars[j-1], j%6 == 0 || j == ifr?"\n":" "); fprintf(fpout, "\n"); fprintf(fpout, "Change in residual sums of squares for free variables:\n"); fprintf(fpout, " "); for (j = 1; j <= ifr; ++j) { fprintf(fpout, "%9.4f", exss[j - 1]); fprintf(fpout, "%s", j%6 == 0 || j == ifr?"\n":" "); } goto END; } else { fprintf(fpout, "Added variable is %3s\n", newvar); fprintf(fpout, "Change in residual sum of squares =%13.4e\n", chrss); fprintf(fpout, "F Statistic = %7.2f\n\n", f); fprintf(fpout, "Variables in model: "); for (j = 1; j <= nterm; ++j) fprintf(fpout, "%3s %s", model[j-1], j%6 == 0 || j == nterm?"\n":" "); fprintf(fpout, "Residual sum of squares = %13.4e\n", rss); fprintf(fpout, "Degrees of freedom = %ld\n\n", idf); if (ifr == 0) { fprintf(fpout, "No free variables remaining\n"); goto END; } fprintf(fpout, "%s%6s", "Free variables: ", ""); for (j = 1; j <= ifr; ++j) { fprintf(fpout, "%3.3s ", free_vars[j-1]); fprintf(fpout, j%6 == 0 || j == ifr?"\n":" "); } fprintf(fpout, "Change in residual sums of squares for free variables:\n"); fprintf(fpout, " "); for (j = 1; j <= ifr; ++j) fprintf(fpout, "%9.4f%s", exss[j - 1], j%6 == 0 || j == ifr?"\n":" "); fprintf(fpout, "\n"); } } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (model) NAG_FREE(model); if (free_vars) NAG_FREE(free_vars); if (exss) NAG_FREE(exss); if (p) NAG_FREE(p); if (q) NAG_FREE(q); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (sx) NAG_FREE(sx); return exit_status; }