如何在javascript中找到多变量回归方程 [英] How to find multivariable regression equation in javascript

查看:109
本文介绍了如何在javascript中找到多变量回归方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了堆栈溢出,并且没有发现任何与我的相同的问题,因为没有一个真正有多个自变量。基本上我有一组数据点,我希望能够找到这些数据点的回归方程。到目前为止我的代码看起来像这样:(w,x,z是自变量,y是因变量)

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"
}];

有没有办法在没有使用循环步进和插入的情况下提出这样的回归方程式在价值观?有没有办法提出超过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.

编辑

许多人建议通过插入权力来解决一个方程组。我遇到的问题是,当一个方程组有足够的数据点来解决时。在问题的例子中,我有3个变量,以解决人们建议的方程组,我需要3个数据点,但我有4个。这导致了一个问题,因为有多个解决方案。有4种可能的解决方案,因为有4种方法可以将4个方程组合成3个不同的组。这将给我留下4个答案,可能没有一个最适合所有4个点。

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.

推荐答案

我认为如果是四个方程只有3个变量(因为你已经确定了权力,插件)并且使它成为一个线性方程式,线性方程式完整,并且不存在满足所有四个方程的精确答案。

I think if it is the case that there are four equations and only 3 variables (As you already determined the powers, plugin and make it a linear equation), the linear equation is over complete, and there does not exist an exact answer that will satisfy all four equations.

你能做的是最小化残差并获得最佳近似值。

What you can do is to minimize the residual error and get a best approximation.

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

Assume you have coefficients a b and c for the w x and z,

定义矩阵

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)>变量数,则必须最小化残差

1. If rank(M)>variable number, you have to minimize the residual error

||M*v-r||_2. 

由于这是凸的,取其导数并将其设为零:

Since this is convex, take derivative on it and make it zero:

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

(M ^ T * M)\ M ^ T是M的MP-inverse,如果等级(M)>可变数,然后(M ^ T * M)是不可逆的。

(M^T*M)\M^T is MP-inverse of M, if rank(M)>variable number, then (M^T*M) is inversible.

2。如果等级(M)<1。 =变量数,你可以得到无数多个精确解的方程式。

2. If the rank(M)<=variable number, you can get infinitely many exact solution to the equation.

M*v=r. 

让M的奇异值分解:

M=U*S*V^T, 

然后

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

是其中一个解决方案。

is one of the solutions.

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

V*S^-1*U^T is pseudo inverse of M.

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

If you use a linear algebra library, it is very easy to get closed form solution without iterating. http://sylvester.jcoglan.com/

这篇关于如何在javascript中找到多变量回归方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆