/* nag_full_step_regsn (g02efc) Example Program. * * Copyright 2004 Numerical Algorithms Group. * * Mark 8, 2004. */ #include #include #include #include int main(void) { /* 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); exit_status = 0; mean = Nag_AboutMean; printf("nag_full_step_regsn (g02efc) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld %ld %lf %lf %lf", &n, &m, &fin, &fout, &tau); scanf("%*[^\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))) { printf("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) { scanf("%lf", &X(i, j)); } } scanf("%*[^\n]"); for (j = 1; j <= m; ++j) { scanf("%ld", &isx[j-1]); } scanf("%*[^\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) { printf("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) { printf("Error from nag_full_step_regsn (g02efc).\n%s\n.", fail.message); exit_status = 1; goto END; } /* Display summary information for fitted model */ printf("\n"); printf("Fitted Model Summary\n"); printf("%-13s\t%-9s\t%s\n", "Term", "Estimate", "Standard Error"); printf("%-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) { printf("%-10s%3ld\t%12.3e\t%-14.3e\n", "Variable:", i, b[i], se[i]); } } printf("\n"); printf("RMS: %-12.3e\n\n", rms); END: 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; }