/* nag_full_step_regsn (g02efc) Example Program. * * Copyright 2004 Numerical Algorithms Group. * * Mark 8, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Scalars */ double fin, fout, rms, rsq, sw, tau; Integer df, exit_status, i, j, m, n, pdx; /* Arrays */ double *b = 0, *c = 0, *se = 0, *wmean = 0, *x = 0; Integer *isx = 0; /* Nag types */ Nag_OrderType order; Nag_SumSquare mean; Nag_Comm comm; NagError fail; #ifdef NAG_COLUMN_ORDER #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; mean = Nag_AboutMean; fprintf(fpout, "nag_full_step_regsn (g02efc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld %lf %lf %lf", &n, &m, &fin, &fout, &tau); fscanf(fpin, "%*[^\n]"); if (n > 1 && m > 1) { /* Allocate memory */ if (!(b = NAG_ALLOC(m+1, double)) || !(c = NAG_ALLOC((m+1)*(m+2)/2, double)) || !(se = NAG_ALLOC(m+1, double)) || !(wmean = NAG_ALLOC(m+1, double)) || !(x = NAG_ALLOC(n * (m+1), double)) || !(isx = NAG_ALLOC(m, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_ORDER pdx = n; #else pdx = m+1; #endif } for (i = 1; i <= n; ++i) { for (j = 1; j <= m+1; ++j) { fscanf(fpin, "%lf", &X(i, j)); } } fscanf(fpin, "%*[^\n]"); for (j = 1; j <= m; ++j) { fscanf(fpin, "%ld", &isx[j-1]); } fscanf(fpin, "%*[^\n]"); /* Compute upper-triangular correlation matrix */ /* nag_sum_sqs (g02buc). * Computes a weighted sum of squares matrix */ nag_sum_sqs(order, mean, n, m+1, x, pdx, 0, &sw, wmean, c, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_sum_sqs (g02buc).\n%s\n.", fail.message); exit_status = 1; goto END; } /* Perform stepwise selection of variables */ /* nag_full_step_regsn (g02efc). * Stepwise linear regression */ nag_full_step_regsn(m, n, wmean, c, sw, isx, fin, fout, tau, b, se, &rsq, &rms, &df, NULLFN, &comm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_full_step_regsn (g02efc).\n%s\n.", fail.message); exit_status = 1; goto END; } /* Display summary information for fitted model */ fprintf(fpout, "\n"); fprintf(fpout, "Fitted Model Summary\n"); fprintf(fpout, "%-13s\t%-9s\t%s\n", "Term", "Estimate", "Standard Error"); fprintf(fpout, "%-13s\t%-9.3e\t%-14.3e\n", "Intercept:", b[0], se[0]); for (i = 1; i <= m; ++i) { j = isx[i-1]; if (j == 1 || j == 2) { fprintf(fpout, "%-10s%3ld\t%12.3e\t%-14.3e\n", "Variable:", i, b[i], se[i]); } } fprintf(fpout, "\n"); fprintf(fpout, "RMS: %-12.3e\n\n", rms); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (b) NAG_FREE(b); if (c) NAG_FREE(c); if (se) NAG_FREE(se); if (wmean) NAG_FREE(wmean); if (x) NAG_FREE(x); if (isx) NAG_FREE(isx); return exit_status; }