如何使用 Excel VBA 获得多项式回归系数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21133620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the polynomial regression coefficients using Excel VBA?
提问by Raul
I have two columns, say x
and f(x)
. I want to get the coefficients of the second degree equation after doing the polynomial regression in Excel VBA.
我有两列,比如说x
和f(x)
。我想在 Excel VBA 中进行多项式回归后得到二次方程的系数。
I'm a rookie in Excel VBA and have no clue what to do.
我是 Excel VBA 的新手,不知道该怎么做。
For example-
例如-
x y
-1 -1
0 3
1 2.5
2 5
3 4
5 2
7 5
9 4
xy
-1 -1
0 3
1 2.5
2 5
3 4
5 2
7 5
9 4
The coefficients for the equation f(x)=a*x^2+b*x+c
will be
方程的系数f(x)=a*x^2+b*x+c
将是
a= -.008571
b= 0.99555
c= 1.648439.
I got the coefficients using THISwebsite
我使用这个网站得到了系数
回答by brettdj
In terms of using code for this, use LINEST
, one way below
在为此使用代码方面,请使用LINEST
下面的一种方式
Sub Quaddy()
Dim X
X = Application.Evaluate("=linest(b1:B8,A1:A8^{1,2})")
MsgBox "Equation is y=" & Format(X(1), "0.###") & "x2+" & Format(X(2), "0.###") & "x+" & Format(X(3), "0.###")
End Sub
This is shown in the Excel chart below
这显示在下面的 Excel 图表中