Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2009, 07:29 AM
Registered User
 
Join Date: May 2009
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default Calculate Regression/Slope in VB 2008

Thanks in advance.

I need to calculate the slope of a regression line through several points within VB 2008.

I have been doing it in VBA with the Excel function, Application.WorksheetFunction.Slope....but can't find anything in VB 2008.

Is there an internal function or something that i can import to do this kind of calculation?

best regards,

VBtyler
 
Old May 21st, 2009, 07:42 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Nothing built in, but wouldn't be hard to create your own function to do it.

It's a simple enough formula.

What do your points look like?? That is, are they in array of Point objects or what??

This is TOTALLY off the top of my head:
Code:
Function Slope( pointArray() As Point ) AS Double
    Dim ix As Integer
    Dim avgX As Double = 0
    Dim avgY As Double = 0
    Dim pointCount As Integer = UBound(pointArray)+1
    ' first get the average X and average Y:
    For ix = 0 To pointCount-1
        avgX += pointArray(ix).X
        avgY += pointArray(ix).Y
    Next
    avgX /= pointCount
    avgY /= pointCount
    ' now get the top and bottom sums:
    Dim topSum As Double = 0.0
    Dim bottomSum As Double = 0.0
    For ix = 0 To pointCount-1
        Dim xdiff As Double = pointArray(ix).X - avgX
        topSum += ( xdiff * ( pointArray(ix).Y - avgY ) )
        bottomSum += ( xdiff * xdiff )
    Next
    Return topSum / bottomSum
End Function
That assumes that a Point is described as basically
Code:
Class Point
    public X as Double
    public Y as Double
End Class
Could have other methods, etc., of course.
The Following User Says Thank You to Old Pedant For This Useful Post:
VBtyler (May 23rd, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB 2008 Output WillMartinez Visual Basic 2008 Essentials 0 February 16th, 2009 12:20 PM
VB.NET 2003 Appendix B convert to VB 2008 Express Edition brucechess BOOK: Beginning VB.NET Databases 10 February 5th, 2009 12:52 PM
vb 2008 exsp Dracitus Visual Basic 2008 Professionals 2 September 27th, 2008 10:56 AM
why buy vs-2008 instead of vb-2008 jerryham VB.NET 7 September 2nd, 2008 02:39 PM
How do I write this vb 6 code to work in vb 2008? sanderson Visual Basic 2008 Essentials 3 June 10th, 2008 01:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.