Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Solved

Posted on 2009-04-08

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

Thanks, BL

6 Comments

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 ]

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\AN

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

Batch/VBscript: import massively chedule task in windows task scheduler | 15 | 44 | |

.Range Set | 25 | 74 | |

Excel Calculate Average - Grouped Values | 7 | 21 | |

Excel. How to get via VBA the last used row in a named dynamic range | 10 | 29 |

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

Connect with top rated Experts

**11** Experts available now in Live!