I'm writing a program tha calculates linear regression, one of the values that are generated is the r (linear correlation coefficient) value. However, my algorithm doesnt work.
I implemented the r value from the formula located at Click Here
Here is my code:
long double LinearRegression::calculate_r()
{
long double r;
long double sumxy = 0;
long double sumx = 0;
long double sumy = 0;
long double numerator;
long double denominator;
long double sumx2 = 0;
long double sumxto2 = 0;
long double sumy2 = 0;
long double sumyto2 = 0;
long double sqrt1 = 0;
long double sqrt2 = 0;
int arrIt;
// Calcualte the sumation of x, y and x * y.
for (arrIt = 0; arrIt < this->n; arrIt++)
{
sumxy += this->x_list[arrIt] * this->y_list[arrIt];
sumx += this->x_list[arrIt];
sumy += this->y_list[arrIt];
}
numerator = this->n * sumxy - sumx * sumy;
for (arrIt = 0; arrIt < this->n; arrIt++)
{
sumx2 += pow(this->x_list[arrIt], 2);
sumy2 += pow(this->y_list[arrIt], 2);
}
sumxto2 = pow(sumx2, 2);
sumyto2 = pow(sumy2, 2);
sqrt1 = sqrt(this->n * sumx2 - sumxto2);
sqrt2 = sqrt(this->n * sumy2 - sumyto2);
denominator = sqrt1 * sqrt2;
r = numerator / denominator;
this->r = r;
return r;
}
I get a value of: -1.#IND
I am suppose to get a value of: 0.999997556
Whats wrong with my implementation and how do I fix it?