7

I have searched stack overflow and have not found any question that really is the same as mine because none really have more than one independent variable. Basically I have an array of datapoints and I want to be able to find a regression equation for those data points. The code I have so far looks like this: (w,x,z are the independent variables and y is the dependent variable)

var dataPoints = [{
 "w" : 1, "x" : 2, "z" : 1, "y" : 7
}, {
 "w" : 2, "x" : 1, "z" : 4, "y" : 5
}, {
 "w" : 1, "x" : 5, "z" : 3, "y" : 2
}, {
 "w" : 4, "x" : 3, "z" : 5, "y" : 15
}];

I would like a function that would return a formula object like this:

var regressionEquation = [{
 "var" : "w", "power" : 1, "coeff" : "1.5"
}, {
 "var" : "x", "power" : 1, "coeff" : "2"
}, {
 "var" : "z", "power" : 1, "coeff" : "1"
}];

Is there a way to come up with a regression equation like this without using a loop to step and plug in the values? Is there a way to come up with the regression equation for powers that are more than 1? Thanks in advance.

EDIT

Many people have suggested solving a system of equations made by plugging in the powers. The problem I have with this is when there is more than enough data points to solve for a system of equations. In the examples in the question, I have 3 variables in order to solve the system of equations that people are suggesting, I would need 3 datapoints but I have 4. This leads to a problem because there is more than one solution. There are 4 possible solutions because there are 4 ways to combine the 4 equations into different groups of 3. This would leave me with 4 answers with possibly none of them the best fit to all 4 points.

4

3 回答 3

1

您所说的问题在转换下等效于线性回归问题。你在评论中说你有固定的指数k_1,k_2k_3。转换需要一个 tuple{w, x, z ,y}到 tuple {w^k_1, x^k_2, z^k_2, y} = {w', x', z' ,y}。对引物变量使用线性回归来获得系数。

例如,如果k_1 = 2k_2 = 3k_3 = 1,那么这里是一个变换的例子:

{"w" : 4, "x" : 3, "z" : 5, "y" : 15} 
==> {"w*" : 16, "x*" : 27, "z*" : 5, "y" : 15}

这只是将多项式回归问题转换为线性回归问题的一种特殊情况。在您的情况下,您正在考虑的多项式形式特别简单。

使用任何你喜欢的 JavaScript 库来解决线性回归问题;其中有很多。

于 2012-11-22T01:55:18.693 回答
1

我认为如果是有四个方程并且只有 3 个变量的情况(因为你已经确定了幂、插件并使它成为一个线性方程),那么线性方程就已经完成了,并且不存在一个确切的答案可以满足所有四个方程。

您可以做的是最小化残差并获得最佳近似值。

假设你有 wx 和 z 的系数 ab 和 c,

定义矩阵

M=[w1,x1,z1;w2,x2,z2;w3,x3,z3;w4,x4,z4]. 

并定义向量

v=[a;b;c], 

定义向量

r=[y1;y2;y3;y4]. 

那么问题来了

M*v=r solve v. 

1.如果rank(M)>variable number,你必须最小化残差

||M*v-r||_2. 

由于这是凸的,因此对其求导并使其为零:

M^T*M*v-M^T*r=0 => v=(M^T*M)\M^T*r. 

(M^T*M)\M^T 是 M 的 MP-inverse,如果 rank(M)> 变量数,则 (M^T*M) 是可逆的。

2.如果rank(M)<=变量数,你可以得到方程的无限多个精确解。

M*v=r. 

让 M 的奇异值分解:

M=U*S*V^T, 

然后

v=V*S^-1*U^T*r 

是解决方案之一。

V*S^-1*U^T 是 M 的伪逆。

如果您使用线性代数库,则无需迭代就很容易获得封闭形式的解决方案。http://sylvester.jcoglan.com/

于 2012-11-25T06:29:22.340 回答
0

我建议使用最小二乘法来获得线性方程。此外,您可以使用非线性最小二乘法,前提是您事先知道要拟合的函数。

(http://en.wikipedia.org/wiki/Least_squares)

在 javascript 中有几个线性 LS 的链接,您可以将它们调整为 3 维(例如,来自快速 Google 搜索的http://dracoblue.net/dev/linear-least-squares-in-javascript/159/ )。对于非线性情况,它需要更多的工作。

于 2012-11-25T11:10:35.077 回答