问题
We are using a programming language that does not have a linear regression function in it. We have already implemented a single variable linear equation:
y = Ax + B
and have simply calculated the A and B coefficents from the data using a solution similar to this Stack Overflow answer.
I know this problem gets geometrically harder as variables are added, but for our purposes, we only need to add one more:
z = Ax + By + C
Does anyone have the closed form equations, or code in any language that can solve for A, B and C given an array of x, y, and z's?
回答1:
so you have three linear equations
k = aX1 + bY1 + cZ1
k = aX2 + bY2 + cZ2
k = aX3 + bY3 + cZ3
What you can do is rewrite it as matriz
| x1 y1 z1 | | a | | k |
| x2 y2 z2 | | b | = | k |
| x3 y3 y3 | | c | | k |
to work out [a b c ]
do the following matrix operation
| a | | x1 y1 z1 | | k |
| b | = inverse( | x2 y2 z2 | ) | k |
| c | | x3 y3 y3 | | k |
The formula for a 3x3 matrix inverse can be found here
回答2:
Yes, it's an easy linear algebra problem if you think of it the way Gil Strang does it. Here's a written explanation.
回答3:
Can you use MatLab or does the calculation have to occur inside your software?
MatLab instructions on multiple regression analysis.
Integrating MatLab with C#.
来源:https://stackoverflow.com/questions/459480/equations-for-2-variable-linear-regression