/* nag_regsn_mult_linear_newyvar (g02dgc) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 2 revised, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include #define XM(I, J) xm[(I) *tdxm + J] #define Q(I, J) q[(I) *tdq + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; Nag_Boolean svd; Integer exit_status = 0, i, ip, j, m, n, rank, *sx = 0, tdq, tdxm; NagError fail; Nag_IncludeMean mean; Nag_Boolean weight; char nag_enum_arg[40]; double df, rss, tol; double *b = 0, *com_ar = 0, *cov = 0, *h = 0, *newy = 0, *p = 0; double *q = 0, *res = 0, *se = 0, *wt = 0, *wtptr, *xm = 0, *y = 0; 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_newyvar (g02dgc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld %ld", &n, &m); fscanf(fpin, "%s", 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); fscanf(fpin, "%s%*[^\n] ", nag_enum_arg); mean = (Nag_IncludeMean) nag_enum_name_to_value(nag_enum_arg); if (n >= 2 && m >= 1) { if (!(h = NAG_ALLOC(n, double)) || !(newy = NAG_ALLOC(n, double)) || !(res = NAG_ALLOC(n, double)) || !(wt = NAG_ALLOC(n, double)) || !(xm = NAG_ALLOC(n*m, double)) || !(y = NAG_ALLOC(n, double)) || !(sx = NAG_ALLOC(m, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdxm = m; } else { fprintf(fpout, "Invalid n or m.\n"); exit_status = 1; return exit_status; } if (weight) { wtptr = wt; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &XM(i, j)); fscanf(fpin, "%lf%lf%lf", &y[i], &wt[i], &newy[i]); } } else { wtptr = (double *) 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) fscanf(fpin, "%lf", &XM(i, j)); fscanf(fpin, "%lf%lf", &y[i], &newy[i]); } } for (j = 0; j < m; j++) fscanf(fpin, "%ld", &sx[j]); fscanf(fpin, "%ld", &ip); if (!(b = NAG_ALLOC(ip, double)) || !(cov = NAG_ALLOC(ip*(ip+1)/2, double)) || !(p = NAG_ALLOC(ip*(ip+2), double)) || !(q = NAG_ALLOC(n*(ip+1), double)) || !(se = NAG_ALLOC(ip, double)) || !(com_ar = NAG_ALLOC(5*(ip-1)+ip*ip, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tdq = ip+1; /* Set tolerance */ tol = 0.00001e0; /* Fit initial model using nag_regsn_mult_linear (g02dac) */ /* nag_regsn_mult_linear (g02dac). * Fits a general (multiple) linear regression model */ nag_regsn_mult_linear(mean, n, xm, tdxm, m, sx, ip, y, wtptr, &rss, &df, b, se, cov, res, h, q, tdq, &svd, &rank, p, tol, com_ar, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_regsn_mult_linear (g02dac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Results from g02dac\n\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"); /* nag_regsn_mult_linear_newyvar (g02dgc). * Fits a general linear regression model to new dependent * variable */ nag_regsn_mult_linear_newyvar(n, wtptr, &rss, ip, rank, cov, q, tdq, svd, p, newy, b, se, res, com_ar, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_regsn_mult_linear_newyvar (g02dgc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); fprintf(fpout, "Results for second y-variable using " "nag_regsn_mult_linear_newyvar (g02dgc)\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 (h) NAG_FREE(h); if (newy) NAG_FREE(newy); if (res) NAG_FREE(res); if (wt) NAG_FREE(wt); if (xm) NAG_FREE(xm); if (y) NAG_FREE(y); if (sx) NAG_FREE(sx); if (b) NAG_FREE(b); if (cov) NAG_FREE(cov); if (p) NAG_FREE(p); if (q) NAG_FREE(q); if (se) NAG_FREE(se); if (com_ar) NAG_FREE(com_ar); return exit_status; }