Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to create a Linear Regression line but for some reason, I can't get my code to open the text file. Can anyone help

I'm trying to create a Linear Regression line but for some reason, I can't get my code to open the text file. Can anyone help me?

Here are the cpp file:

#include "linearregression.h"

#include

#include

using namespace std;

LinearRegression::LinearRegression(Point2D* p, long size)

{

long i;

a = b = sumX = sumY = sumXsquared = sumYsquared = sumXY = 0.0;

n = 0L;

if (size > 0L) // if size greater than zero there are data arrays

for (n = 0, i = 0L; i < size; i++)

addPoint(p[i]);

}

LinearRegression::LinearRegression(double* x, double* y, long size)

{

long i;

a = b = sumX = sumY = sumXsquared = sumYsquared = sumXY = 0.0;

n = 0L;

if (size > 0L) // if size greater than zero there are data arrays

for (n = 0, i = 0L; i < size; i++)

addXY(x[i], y[i]);

}

void LinearRegression::addXY(const double& x, const double& y)

{

n++;

sumX += x;

sumY += y;

sumXsquared += x * x;

sumYsquared += y * y;

sumXY += x * y;

Calculate();

}

void LinearRegression::Calculate()

{

if (haveData())

{

if (fabs(double(n) * sumXsquared - sumX * sumX) > DBL_EPSILON)

{

b = (double(n) * sumXY - sumY * sumX) /

(double(n) * sumXsquared - sumX * sumX);

a = (sumY - b * sumX) / double(n);

double sx = b * (sumXY - sumX * sumY / double(n));

double sy2 = sumYsquared - sumY * sumY / double(n);

double sy = sy2 - sx;

coefD = sx / sy2;

coefC = sqrt(coefD);

stdError = sqrt(sy / double(n - 2));

}

else

{

a = b = coefD = coefC = stdError = 0.0;

}

}

}

ostream& operator<<(ostream& out, LinearRegression& lr)

{

if (lr.haveData())

out << "f(x) = " << lr.getA()

<< " + ( " << lr.getB()

<< " * x )";

return out;

}

here is the header file

#pragma once

#ifndef _LINREG_H_

#define _LINREG_H_

class Point2D

{

public:

Point2D(double X = 0.0, double Y = 0.0) : x(X), y(Y) { }

void setPoint(double X, double Y) { x = X; y = Y; }

void setX(double X) { x = X; }

void setY(double Y) { y = Y; }

double getX() const { return x; }

double getY() const { return y; }

private:

double x, y;

};

class LinearRegression

{

friend ofstream& operator<<(ofstream& out, LinearRegression& lr);

public:

// Constructor using an array of Point2D objects

// This is also the default constructor

LinearRegression(Point2D* p = 0, long size = 0);

// Constructor using arrays of x values and y values

LinearRegression(double* x, double* y, long size = 0);

virtual void addXY(const double& x, const double& y);

void addPoint(const Point2D& p) { addXY(p.getX(), p.getY()); }

// Must have at least 3 points to calculate

// standard error of estimate. Do we have enough data?

int haveData() const { return (n > 2 ? 1 : 0); }

long items() const { return n; }

virtual double getA() const { return a; }

virtual double getB() const { return b; }

double getCoefDeterm() const { return coefD; }

double getCoefCorrel() const { return coefC; }

double getStdErrorEst() const { return stdError; }

virtual double estimateY(double x) const { return (a + b * x); }

protected:

long n; // number of data points input so far

double sumX, sumY; // sums of x and y

double sumXsquared, // sum of x squares

sumYsquared; // sum y squares

double sumXY; // sum of x*y

double a, b; // coefficients of f(x) = a + b*x

double coefD, // coefficient of determination

coefC, // coefficient of correlation

stdError; // standard error of estimate

void Calculate(); // calculate coefficients

};

#endif // end of linreg.h

here is the text file

X. Y

1 4

5 2

8. 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago