/* nag_simple_linear_regression (g02cac) Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1992. * Mark 8 revised, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, n; Nag_SumSquare mean; Nag_Boolean weight; char nag_enum_arg[40]; double a, b, df, err_a, err_b, rsq, rss; double *wt = 0, *wtptr, *x = 0, *y = 0; 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_simple_linear_regression (g02cac) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, " %s", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ mean = (Nag_SumSquare) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, " %s", nag_enum_arg); weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg); fscanf(fpin, "%ld", &n); if (n >= (mean == Nag_AboutMean?2:1)) { if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(wt = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } if (weight) { wtptr = wt; for (i = 0; i < n; ++i) fscanf(fpin, "%lf%lf%lf", &x[i], &y[i], &wt[i]); } else { wtptr = (double *) 0; for (i = 0; i < n; ++i) fscanf(fpin, "%lf%lf", &x[i], &y[i]); } /* nag_simple_linear_regression (g02cac). * Simple linear regression with or without a constant term, * data may be weighted */ nag_simple_linear_regression(mean, n, x, y, wtptr, &a, &b, &err_a, &err_b, &rsq, &rss, &df, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_simple_linear_regression (g02cac).\n%s\n", fail.message); exit_status = 1; goto END; } if (mean == Nag_AboutMean) { fprintf(fpout, "\nRegression constant a = %6.4f\n\n", a); fprintf(fpout, "Standard error of the regression constant a = %6.4f\n\n", err_a); } fprintf(fpout, "Regression coefficient b = %6.4f\n\n", b); fprintf(fpout, "Standard error of the regression coefficient b = %6.4f\n\n", err_b); fprintf(fpout, "The regression coefficient of determination = %6.4f\n\n", rsq); fprintf(fpout, "The sum of squares of the residuals about the " "regression = %6.4f\n\n", rss); fprintf(fpout, "Number of degrees of freedom about the " "regression = %6.4f\n\n", df); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (wt) NAG_FREE(wt); return exit_status; }