/* nag_all_regsn (g02eac) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include #include int main(void) { /* Scalars */ Integer exit_status, free_vars, i, ii, j, m, n, nmod, pdx; NagError fail; Nag_OrderType order; /* Arrays */ char **model = 0; const char *var_names[] = { "DAY", "BOD", "TKN", "TS", "TVS", "COD" }; double *rss = 0, *x = 0, *y = 0, *wtptr = 0; Integer *sx = 0, *mrank = 0, *nterms = 0; /* For iteration over model */ Integer model_index = 0; #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); exit_status = 0; printf("nag_all_regsn (g02eac) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n] "); scanf("%ld%ld%*[^\n] ", &n, &m); /* Allocate memory */ if (!(x = NAG_ALLOC(n * m, double)) || !(y = NAG_ALLOC(n, double)) || !(sx = NAG_ALLOC(m, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_MAJOR pdx = n; order = Nag_ColMajor; #else pdx = m; order = Nag_RowMajor; #endif for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) scanf("%lf", &X(i, j)); scanf("%lf%*[^\n] ", &y[i - 1]); } free_vars = 1; for (j = 1; j <= m; ++j) { scanf("%ld", &sx[j - 1]); if (sx[j - 1] == 1) { free_vars <<= 1; } } scanf("%*[^\n] "); if (!(model = NAG_ALLOC(free_vars*m, char *)) || !(rss = NAG_ALLOC(free_vars, double)) || !(mrank = NAG_ALLOC(free_vars, Integer)) || !(nterms = NAG_ALLOC(free_vars, Integer))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* nag_all_regsn (g02eac). * Computes residual sums of squares for all possible linear * regressions for a set of independent variables */ nag_all_regsn(order, Nag_MeanInclude, n, m, x, pdx, var_names, sx, y, wtptr, &nmod, (const char **)model, rss, nterms, mrank, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_all_regsn (g02eac).\n%s\n", fail.message); exit_status = 1; goto END; } printf("\n"); printf("Number of Rss Rank Model\n"); printf("parameters\n"); for (i = 1; i <= nmod; ++i) { ii = nterms[i - 1]; printf("%8ld%11.4f%4ld ", ii, rss[i-1], mrank[i-1]); for (j = 1; j <= ii; ++j) printf("%-3.3s %s", model[model_index++], j%5 == 0 || j == 5?"\n":" "); printf("\n"); } END: NAG_FREE(rss); NAG_FREE(x); NAG_FREE(y); NAG_FREE(sx); NAG_FREE(mrank); NAG_FREE(nterms); NAG_FREE(model); return exit_status; }