/* nag_simple_linear_regression (g02cac) Example Program. * * Copyright 1992 Numerical Algorithms Group. * * Mark 3, 1992. * Mark 8 revised, 2004. */ #include #include #include #include int main(void) { Integer exit_status=0, i, n; NagError fail; Nag_SumSquare mean; char m, w; double a, b, df, err_a, err_b, rsq, rss, *wt=0, *wtptr, *x=0, *y=0; INIT_FAIL(fail); Vprintf("nag_simple_linear_regression (g02cac) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf(" %c %c",&m, &w); Vscanf("%ld", &n); if (n >= (m == 'M' || m == 'm' ? 2 : 1)) { if ( !( x = NAG_ALLOC(n, double)) || !( y = NAG_ALLOC(n, double)) || !( wt = NAG_ALLOC(n, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } } else { Vprintf("Invalid n.\n"); exit_status = 1; return exit_status; } if (m == 'M' || m == 'm') mean = Nag_AboutMean; else mean = Nag_AboutZero; if (w == 'W' || w == 'w') { wtptr = wt; for(i = 0; i < n; ++i) Vscanf("%lf%lf%lf", &x[i], &y[i], &wt[i]); } else { wtptr = (double *)0; for(i = 0; i < n; ++i) Vscanf("%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) { Vprintf("Error from nag_simple_linear_regression (g02cac).\n%s\n", fail.message); exit_status = 1; goto END; } if (mean == Nag_AboutMean) { Vprintf("\nRegression constant a = %6.4f\n\n", a); Vprintf("Standard error of the regression constant a = %6.4f\n\n", err_a); } Vprintf("Regression coefficient b = %6.4f\n\n", b); Vprintf("Standard error of the regression coefficient b = %6.4f\n\n", err_b); Vprintf("The regression coefficient of determination = %6.4f\n\n", rsq); Vprintf("The sum of squares of the residuals about the " "regression = %6.4f\n\n", rss); Vprintf("Number of degrees of freedom about the " "regression = %6.4f\n\n",df); END: if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (wt) NAG_FREE(wt); return exit_status; }