/* 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; 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; Vprintf("nag_all_regsn (g02eac) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%ld%*[^\n] ", &n, &m); /* Allocate memory */ if ( !(x = NAG_ALLOC(n * m, double)) || !(y = NAG_ALLOC(n, double)) || !(sx = NAG_ALLOC(m, Integer)) ) { Vprintf("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) Vscanf("%lf", &X(i,j)); Vscanf("%lf%*[^\n] ", &y[i - 1]); } free_vars = 1; for (j = 1; j <= m; ++j) { Vscanf("%ld", &sx[j - 1]); if (sx[j - 1] == 1) { free_vars <<= 1; } } Vscanf("%*[^\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)) ) { Vprintf("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, model, rss, nterms, mrank, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_all_regsn (g02eac).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); Vprintf("Number of Rss Rank Model\n"); Vprintf("parameters\n"); for (i = 1; i <= nmod; ++i) { ii = nterms[i - 1]; Vprintf("%8ld%11.4f%4ld ", ii, rss[i-1], mrank[i-1]); for (j = 1; j <= ii; ++j) Vprintf("%-3.3s %s", model[model_index++], j%5 == 0 || j == 5 ?"\n":" "); Vprintf("\n"); } END: if (rss) NAG_FREE(rss); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (sx) NAG_FREE(sx); if (mrank) NAG_FREE(mrank); if (nterms) NAG_FREE(nterms); if ( model) NAG_FREE(model); return exit_status; }