/* nag_correg_linregm_rssq (g02eac) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
#include <string.h>
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]
#else
#define X(I, J) x[(I - 1) * pdx + J - 1]
#endif
INIT_FAIL(fail);
exit_status = 0;
printf("nag_correg_linregm_rssq (g02eac) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\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("%" NAG_IFMT "", &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_correg_linregm_rssq (g02eac).
* Computes residual sums of squares for all possible linear
* regressions for a set of independent variables
*/
nag_correg_linregm_rssq(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_correg_linregm_rssq (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("%8" NAG_IFMT "%11.4f%4" NAG_IFMT " ", 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;
}