//******************************************************************************************************************************* // // L I N R E G // // Program: LINREG // // Programmer: Dr. David G. Simpson // Department of Physical Science // Prince George's Community College // Largo, Maryland 20774 // // Date: January 21, 2002 // // Language: C++ // // Description: This program performs a linear regression analysis for a set of data given as (x,y) pairs. The output from // the program is the slope and y-intercept of the least-squares best fit straight line through the data points. // //******************************************************************************************************************************* //******************************************************************************************************************************* // #includes //******************************************************************************************************************************* #include // standard i/o #include // string functions #include // math functions //******************************************************************************************************************************* // function prototypes //******************************************************************************************************************************* void chop (char *str); // remove \n from end of string double sqr (double x); // compute the square of a number //****************************************************************************************************************************** // global variables //****************************************************************************************************************************** double b; // y-intercpt of least-squares best fit line double m; // slope of least-squares best fit line double n = 0.0; // number of data points double r; // squared correlation coefficient char str[81]; // input string double sumx = 0.0; // sum of x double sumx2 = 0.0; // sum of x**2 double sumxy = 0.0; // sum of x * y double sumy = 0.0; // sum of y double sumy2 = 0.0; // sum of y**2 double x; // input x data double y; // input y data //******************************************************************************************************************************* // main() //******************************************************************************************************************************* int main (void) { printf("LINREG - Perform linear regression\n"); // print introductory message printf(" (Enter END to stop data entry and compute linear regression.)\n\n"); for (;;) // loop for all data points { printf ("Enter x y: "); fgets (str, 80, stdin); chop (str); if (!strcmp(str,"end") || !strcmp(str,"END")) break; sscanf (str, "%lf %lf", &x, &y); n += 1.0; // increment number of data points by 1 sumx += x; // compute sum of x sumx2 += x * x; // compute sum of x**2 sumxy += x * y; // compute sum of x * y sumy += y; // compute sum of y sumy2 += y * y; // compute sum of y**2 } m = (n * sumxy - sumx * sumy) / (n * sumx2 - sqr(sumx)); // compute slope b = (sumy * sumx2 - sumx * sumxy) / (n * sumx2 - sqr(sumx)); // compute y-intercept r = (sumxy - sumx * sumy / n) / // compute correlation coefficient sqrt((sumx2 - sqr(sumx)/n) * (sumy2 - sqr(sumy)/n)); printf ("\nSlope m = %15.6e\n", m); // print results printf ("y-intercept b = %15.6e\n", b); printf ("Correlation r = %15.6e\n", r); return 0; // return to operating system } //******************************************************************************************************************************* // chop() // // Remove final \n from end of string. //******************************************************************************************************************************* void chop (char *str) { int len; len = strlen (str); if (str[len-1] == '\n') str[len-1] = '\0'; } //******************************************************************************************************************************* // sqr() // // Returns the square of a number. //******************************************************************************************************************************* double sqr (double x) { return x * x; // compute square of argument }