/* nag_regsn_mult_linear_upd_model (g02ddc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * Mark 8 revised, 2004. */ #include #include #include #include #include #define X(I, J) x[(I) *tdx + J] #define Q(I, J) q[(I) *tdq + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, ip, ipmax, j, m, n, rank, tdq, tdx; double *b = 0, *cov = 0, df, *p = 0, *q = 0, rss, *se = 0, tol, *wt = 0; double *wtptr, *x = 0, *xe = 0; char nag_enum_arg[40]; Nag_Boolean svd, weight; NagError fail; 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); fprintf(fpout, "nag_regsn_mult_linear_upd_model (g02ddc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld %s", &n, &m, nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); ipmax = 4; if (n >= 1 && m >= 1) { if (!(b = NAG_ALLOC(ipmax, double)) || !(cov = NAG_ALLOC(ipmax*(ipmax+1)/2, double)) || !(p = NAG_ALLOC(ipmax*(ipmax+2), double)) || !(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n*m, double)) || !(xe = NAG_ALLOC(n, double)) || !(se = NAG_ALLOC(ipmax, double)) || !(q = NAG_ALLOC(n*(ipmax+1), double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdx = m; tdq = ipmax+1; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } if (weight) wtptr = wt; else wtptr = (double *) 0; if (wtptr) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf%lf", &Q(i, 0), &wt[i]); } } else { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &X(i, j)); fscanf(fpin, "%lf", &Q(i, 0)); } } /* Set tolerance */ tol = 0.000001e0; ip = 0; for (j = 0; j < m; ++j) { /* * Fit model using g02dec */ for (i = 0; i < n; i++) xe[i] = X(i, j); /* nag_regsn_mult_linear_add_var (g02dec). * Add a new independent variable to a general linear * regression model */ nag_regsn_mult_linear_add_var(n, ip, q, tdq, p, wtptr, xe, &rss, tol, &fail); if (fail.code == NE_NOERROR) ip += 1; else if (fail.code == NE_NVAR_NOT_IND) fprintf(fpout, " * New variable not added * \n"); else { fprintf(fpout, "Error from nag_regsn_mult_linear_add_var (g02dec).\n%s\n", fail.message); exit_status = 1; goto END; } } rss = 0.0; /* nag_regsn_mult_linear_upd_model (g02ddc). * Estimates of regression parameters from an updated model */ nag_regsn_mult_linear_upd_model(n, ip, q, tdq, &rss, &df, b, se, cov, &svd, &rank, p, tol, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_regsn_mult_linear_upd_model (g02ddc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (svd) fprintf(fpout, "Model not of full rank\n\n"); fprintf(fpout, "Residual sum of squares = %13.4e\n", rss); fprintf(fpout, "Degrees of freedom = %3.1f\n\n", df); fprintf(fpout, "Variable Parameter estimate Standard error\n\n"); for (j = 0; j < ip; j++) fprintf(fpout, "%6ld%20.4e%20.4e\n", j+1, b[j], se[j]); fprintf(fpout, "\n"); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (b) NAG_FREE(b); if (cov) NAG_FREE(cov); if (p) NAG_FREE(p); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); if (xe) NAG_FREE(xe); if (se) NAG_FREE(se); if (q) NAG_FREE(q); return exit_status; }