so far we have used void functions....not we will be using functions that return
ID: 3798123 • Letter: S
Question
so far we have used void functions....not we will be using functions that return values. Using the list of numbers provided,,,,,,find the best fit of a streight line through it (regression analysis): to generate the equation y = mx + b.
data linear regression:
0 0
1 6.88
1.5 15.48
2 27.52
2.5 43
3 61.92
3.27 73.56
3.496 84.08
3.5 84.28
3.524 85.44
3.732 95.82
4 110.1
4.5 139.3
5 172
So this was the question....I answered Like below:
/*
Dweep Dey Program 5
02/20/17
This program will return values, y=mx+b, regression coefficient
*/
using namespace std;
#include <iostream>
#include <fstream>
// global variable declerations
float x[50], y[50];
int NumOfDataPoints,n,i;
float m, b;
// function declerations
float sumX();
float sumY();
float sumXY();
float sumXsq();
void getData();
int main()
{
getData();//this function gets data and puts it in arrays x and y
NumOfDataPoints = n; // number of data points is the n
// computing m and b using function calls
m = ((n*sumXY()) - (sumX()*sumY())) / ((n*sumXsq()) - (sumX()*sumX()));
b = ((sumY() - (m*sumX())) / (n));
cout<<"y = "<<m<<"x"<<"+ "<<b<<endl;
system("pause"); //so we can see the equation printed on the screen above
return 0;
}
void getData() //get Data begins
{ int i=0;
ifstream datfile; //the data buffer
datfile.open("thedata.txt"); //opens the data file on the disk
while (!datfile.eof())//while the file is not empty keep reading
{
datfile>>x[i]; datfile>>y[i]; //put the X and Y points in arrays
cout<<x[i]<<" "<<y[i]<<endl;//show data on screen
i++; //get ready for next data point
}
datfile.close();//all done with disk file
n = i-1; //store the number of data pairs in a place called "n"
}
float sumX()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++)// sum of all x
ans = ans + x[i];
return ans;
}
float sumY()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++) // sum of all y
ans = ans +y[i];
return ans;
}
float sumXY()
{
float ans = 0.0;
for(int i = 0;i<=n-1 ;i++) // sum of all (xy)
ans = ans + x[i]*y[i];
return ans;
}
float sumXsq()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++) // sum of all x^2
ans = ans +x[i]*x[i];
return ans;
}
** Professor said, this program is good. This program give the right value of m and b....He gave me 45 out of 50...as he want me to calculate r....the program above is good, can i just get help to calculate r....let me know what you add ....please do not say need more information :( FYI: The values of m= 34.5349, b= -29.808, ( My program output this values) , need to calculate r which value will be 0.91789***
*** Do not change any format or add any new header please ( professor instruct)
Thank you so much!
Explanation / Answer
// C++ code
using namespace std;
#include <iostream>
#include <fstream>
#include <math.h>
// global variable declerations
float x[50], y[50];
int NumOfDataPoints,n,i;
float m, b;
// function declerations
float sumX();
float sumY();
float sumXY();
float sumXsq();
void getData();
int main()
{
getData();//this function gets data and puts it in arrays x and y
NumOfDataPoints = n; // number of data points is the n
// computing m and b using function calls
m = ((n*sumXY()) - (sumX()*sumY())) / ((n*sumXsq()) - (sumX()*sumX()));
b = ((sumY() - (m*sumX())) / (n));
cout<<"y = "<<m<<"x"<<"+ "<<b<<endl;
float averageY = sumY()/NumOfDataPoints;
float sum_Y_residue = 0, sum_residue = 0, resiue = 0, y_residue;
for (int i = 0; i < NumOfDataPoints; i++)
{
y_residue = pow((y[i] - b - (m * x[i])), 2);
sum_Y_residue += y_residue;
//current residue squared (y[i] - averageY)^2
resiue = pow(y[i] - averageY, 2);
sum_residue += resiue;
}
printf("r = %0.6lf ", (sum_residue - sum_Y_residue)/sum_residue);
return 0;
}
void getData() //get Data begins
{ int i=0;
ifstream datfile; //the data buffer
datfile.open("thedata.txt"); //opens the data file on the disk
while (!datfile.eof())//while the file is not empty keep reading
{
datfile>>x[i]; datfile>>y[i]; //put the X and Y points in arrays
cout<<x[i]<<" "<<y[i]<<endl;//show data on screen
i++; //get ready for next data point
}
datfile.close();//all done with disk file
n = i-1; //store the number of data pairs in a place called "n"
}
float sumX()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++)// sum of all x
ans = ans + x[i];
return ans;
}
float sumY()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++) // sum of all y
ans = ans +y[i];
return ans;
}
float sumXY()
{
float ans = 0.0;
for(int i = 0;i<=n-1 ;i++) // sum of all (xy)
ans = ans + x[i]*y[i];
return ans;
}
float sumXsq()
{
float ans = 0.0;
for(int i = 0;i<=n-1;i++) // sum of all x^2
ans = ans +x[i]*x[i];
return ans;
}
/*
output:
0 0
1 6.88
1.5 15.48
2 27.52
2.5 43
3 61.92
3.27 73.56
3.496 84.08
3.5 84.28
3.524 85.44
3.732 95.82
4 110.1
4.5 139.3
5 172
y = 31.3589x+ -23.2485
r = 0.928284
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.