Home » excel » How do I get the polynomial regression coefficients using Excel VBA?

How do I get the polynomial regression coefficients using Excel VBA?

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

I’m a rookie in Excel VBA and have no clue what to do.

For example-

x y
-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

a= -.008571
b= 0.99555
c= 1.648439. 

I got the coefficients using THIS website

How to&Answers:

In terms of using code for this, use LINEST, one way below

Sub Quaddy()
Dim X
X = Application.Evaluate("=linest(b1:B8,A1:A8^{1,2})")
MsgBox "Equation is y=" & Format(X(1), "0.

Answer:

") & "x2+" & Format(X(2), "0.

Answer:

") & "x+" & Format(X(3), "0.

Answer:

") End Sub

This is shown in the Excel chart below
enter image description here