# Linear Regression Program (C++)

*“The subject of this handout is something I like to call linear regression. That is because that is what everyone else calls it.”*

*-Dr. Brian Harral*

This is a program that I originally wrote in the Fall of ’09 to solve all my linear regression problems. Now the average ~~student~~ human being would have forgotten about it, but because of this obsession problem I have, I wound up revisiting it in the summer of ’10 and expanded it to roughly what I consider v.2.0. I recently came back one final time to create version 3.0. Or so I thought. I have now created what I consider the perfect version, v 4.0, even though I know that one day, years from now, I will return once again. For the time being, feel free to use, enjoy, and expand upon it in any way!

// Linear Regression version 4.0

#include “stdafx.h”

#include <new>

# include <iostream>

using std::cout;

using std::endl;

using std::cin;

void linearregression ();

int main ()

{

cout << “Welcome to the Linear Regression Program!” << endl << endl;

do

{

char continue = Y;

void linearregression ();

cout << “Would you like to continue? (Y/N)”

cin >> continue;

}

while (continue != N)

cout << “Have a nice day! Or, enter more variables for more linear regression!” << endl << “Original Program by Noah Diekemper of Binary Hangman Comics.” << endl << endl << endl;

int end;

cin >> end;

return 0;

}

void linearregression ()

{

int number, xnumvar, ynumvar;

cout << “Please enter the number of x and y variables and press enter: “;

cin >> number;

xnumvar = ynumvar = number;

cout << endl << endl;

float xval [1000];

float yval [1000];

float xxval [1000];

float yyval [1000];

float xyval [1000];

float xbar, ybar, xxbar, yybar, xybar;

float xwhile = 0, ywhile = 0;

while (xnumvar > 0) {cout << “Please enter x variable number ” << (xwhile + 1) << “: “; cin >> xval[xnumvar – 1]; cout << endl; xnumvar–; xwhile++;}

while (ynumvar > 0) {cout << “Please enter y variable number ” << (ywhile + 1) << “: “; cin >> yval[ynumvar – 1]; cout << endl; ynumvar–; ywhile++;}

float xbardiv = 0, ybardiv = 0;

while (xnumvar < number) {xbardiv = xbardiv + (xval[xnumvar]); xnumvar++;}

while (ynumvar < number) {ybardiv = ybardiv + yval[ynumvar]; ynumvar++;}

xbar = xbardiv/number;

ybar = ybardiv/number;

int xxnumvar = 0, yynumvar = 0, xynumvar = 0;

while (xxnumvar < number) {xxval[xxnumvar] = xval[xxnumvar]*xval[xxnumvar]; xxnumvar++;}

while (yynumvar < number) {yyval[yynumvar] = yval[yynumvar]*yval[yynumvar]; yynumvar++;}

while (xynumvar < number) {xyval[xynumvar] = xval[xynumvar]*yval[xynumvar]; xynumvar++;}

xxnumvar = yynumvar = xynumvar = 0;

float xxbardiv = 0, yybardiv = 0, xybardiv = 0;

while (xxnumvar < number) {xxbardiv = xxbardiv + xxval[xxnumvar]; xxnumvar++;}

while (yynumvar < number) {yybardiv = yybardiv + yyval[yynumvar]; yynumvar++;}

while (xynumvar < number) {xybardiv = xybardiv + xyval[xynumvar]; xynumvar++;}

xxbar = xxbardiv/number;

yybar = yybardiv/number;

xybar = xybardiv/number;

//Creating variables

float varx, vary, covxy, slope, yint, xdivy, GOF;

//Calculating the following values

varx = xxbar – (xbar * xbar);

vary = yybar – (ybar * ybar);

covxy = xybar – (xbar * ybar);

slope = covxy/varx;

yint = ybar – (slope * xbar);

xdivy = varx/vary;

GOF = slope * slope * xdivy;

//The following code prints out the results to the end user

cout << “x bar is ” << xbar << endl;

cout << “y bar is ” << ybar << endl;

cout << “x squared bar is ” << xxbar << endl;

cout << “y squared bar is ” << yybar << endl;

cout << “xy bar is ” << xybar << endl << endl;

cout << “The variance of x is ” << varx << endl;

cout << “The variance of y is ” << vary << endl;

cout << “The covariance of x and y is ” << covxy << endl;

cout << “The slope is ” << slope << endl;

cout << “The y-intercept is ” << yint << endl;

cout << “The Goodness of Fit is ” << GOF << endl << endl;

/*-Creating some test Cartesian Coordinate graph points/ordered pairs, */including material for x- and y-intercepts

float x1 = 0, x2 = 1, x3, x4, y1, y2, y3 = 0, y4 = 1;

y1 = slope*x1 + yint;

y2 = slope*x2 + yint;

x3 = (y3 – yint)/slope;

x4 = (y4 – yint)/slope;

cout << “Some ordered pairs to graph are: (” << x1 << “, ” << y1 << “), ” << endl;

cout << “(” << x2 << “, ” << y2 << “), ” << endl;

cout << “(” << x3 << “, ” << y3 << “), ” << endl;

cout << “and (” << x4 << “, ” << y4 << “). ” << endl << endl;

}

Pingback: Comic Archive and Sonic Advance Review « Binary Hangman Comics