Solved

How to automate the LINEST function in vba to find the best fit for a second order or higher polynomial?

Posted on 2009-04-08
6
1,183 Views
Last Modified: 2012-08-13
I have hundreds of data sets showing change (y) over time (t) at different locations.  I am attempting to plot the (y,t) data for each location and fit a second order polynomial line of regression to each of the data sets.  I would like to automate the LINEST function in VBA to accomplish this by finding the constants C1, C2, and b; i.e. y = C1*t^2 + C2*t + b.  I would also like the statistical data that LINEST provides.

Thanks, BL
0
Comment
Question by:brandonlampe
6 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 24102106
Are you familiar with matrices?

Compose the Vandermonde matrix for the t_i data, that is:

      [ 1  t_1  t_1^2 ]
      [ 1  t_2  t_2^2 ]
X = [   .       .        .  ]
      [   .          .     .  ]
      [ 1  t_n  t_n^2 ]

Then compute:

  (X^t X)^-1 X

and multiply this (from the left of course) by:

      [ y_1 ]
Y = [   .    ]
      [ y_n ]

then the answer will be a vector consisting of the coefficients:

  [  b  ]
  [ C2 ]
  [ C1 ]

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24105766
The trendline formulas are here if it helps: http://spreadsheetpage.com/index.php/tip/chart_trendline_formulas/
 
0
 

Author Comment

by:brandonlampe
ID: 24112709
I guess I was looking for a little more help with writing the VBA code.
0
 
LVL 3

Expert Comment

by:jtm111
ID: 24205677
brandonlampe -- I hope you still need help on this because I know the answer.

You have to install the Analysis Toolpak - VBA add-in, which I assume you know how to do since you're a VBA programmer.

Make sure you choose both the Analysis Toolpak AND the Analysis Toolpak - VBA

The resource for the Analysis Toolpak - VBA is something like

C:\Program Files\Microsoft Office\Office12\LIBRARY\ANALYSIS\ATPVBAEN.XLAM

Then, when you're in the VBA editor in Excel, you can call the data analysis functions. The native LINEST is actually surpassed by the Regress function which gives all the output you want.

One the Analysis Toolpak add-ins are installed correctly, go to your VBA editor and add a Reference to ATPVBAEN.XLS

Here's a little snippet:

Sub test()
    Dim Res As Variant
    Res = [atpvbaen.xls].Regress( ARGS = X AND Y RANGE,  DESIRED OUTPUT STATISTICS )
    Debug.Print Res
End Sub

What you want to do is totally possible.

Good luck, hope this helps!

 
0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 25847814
Question PAQ'd, 250 points not refunded, and stored in the solution database.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now