I have an over-determined system of 2D data. I am using the Eigen library to compute the linear regression line. The data is in the form of A x = b, where A is an nx1 matrix and b is an n-sized vector.
When I run the SVD, I calculate a slope, and the line passes through the origin (i.e., there is no Y-intercept). For data which has a trend line that does not pass through the origin, this doesn't result in the line I'm looking for.
Here is an example:
//Eigen
#include <Eigen/Dense>
//std
#include <cstdlib>
#include <iostream>
int main() {
Eigen::MatrixXd A(15,1);
Eigen::VectorXd b(15);
A << -4, -4, -4, -3, -3, -2, -2, -1, -1, -1, 0, 1, 1, 2, 2;
b << 11.8, 10.9, 11.5, 9.6, 8.4, 7.4, 6.2, 4.8, 5.4, 4.5, 3.5, 1.5, 0.1, -0.5, -2;
//Calculate the SVD and solve for the regression line
Eigen::MatrixXd x = A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);
Eigen::MatrixXd b_reg = A*x;
std::cout << x << std::endl;
std::cout << b_reg << std::endl;
return EXIT_SUCCESS;
}
The trend line I expect is y = -2.079x + 2.907. My program above reports x to be -2.714, and the line passing through the origin.
Is there an easy way to use the SVD to recover the "offset" regression line? Or should I be using something other than the SVD? in which case, what?