Regression Equation / Statistics in VS 2005

Is there an easy way to create a regression equation based on certain data located in variables Or do I have to get out the 'ole stats book and write the code manually I couldn't find anything in the help file about this, so I may be out of luck!

Thank you!

-Corey


Answer this question

Regression Equation / Statistics in VS 2005

  • Rachel J

    Try this...makes it more flexible:

    Public Class Point

    Public Sub New(ByVal X As Single, ByVal Y As Single)

    pX = X

    pY = Y

    End Sub

    Public pX, pY As Single

    End Class

    Module Module1

    Sub Main()

    Dim Points As New List(Of Point)

    Points.Add(New Point(1, 5))

    Points.Add(New Point(2, 10))

    Points.Add(New Point(3, 15))

    Points.Add(New Point(4, 20))

    Dim B0, B1, xGiven, yHat As Single

    xGiven = 5

    regression(Points, B0, B1)

    yHat = B0 + (B1 * xGiven)

    MsgBox(yHat)

    End Sub

    Sub regression(ByVal Points As List(Of Point), ByRef B0 As Single, ByRef B1 As Single)

    Dim xBar As Single = 0

    Dim yBar As Single = 0

    Dim b1Num As Single = 0

    Dim b1Den As Single = 0

    For Each pt As Point In Points

    xBar += pt.pX

    yBar += pt.pY

    Next

    xBar /= Points.Count

    yBar /= Points.Count

    For Each pt As Point In Points

    b1Num += (pt.pX - xBar) * (pt.pY - yBar)

    b1Den += (pt.pX - xBar) ^ 2

    Next

    B1 = b1Num / b1Den

    B0 = yBar - (B1 * xBar)

    End Sub

    End Module


  • gteddy

    Hi,

    You welcome.

    I've realised you could do this with string manipulation etcetera.

    See string functions.>> http://search.msdn.microsoft.com/search/default.aspx __VIEWSTATE=&query=string&siteid=0&tab=0

    Regards,

    S_DS



  • itsRashmi

    "This involves comparing all the X values and the Y's, not easy."

    This is what I was responding too. The OP wanted to know how to do linear regression. As you can see, it's not a difficult problem.



  • tlc660

    Hi,

     It depends i guess on the nature and complexity of the data.

     If you are looking for a program that simplifies formulae then that is not an easy task.

     Two programs come to mind. Mathematica is one of them from.>> http://www.wolfram.com

    Trial it.>> http://www.wolfram.com/products/mathematica/trial.cgi

     Can you give a simple example of what your trying to achieve please

     

    Regards,

    S_DS

     

     



  • PremZ

    If you want to fit to say

    y=A exp( B x)

    where A and B are constants, and y and x are data points, you can do this:

    ln y = ln [A exp (Bx)]
    ln y = ln A + ln [exp(Bx)]
    ln y = ln A + Bx

    so transform your data, take ln of y, call this new set u; let C = ln A, you have

    u = C + Bx

    find your coefficients, problem solved.


  • S_R_M

     

     

    Sidermans Dark side, sometime you highly misnformed in the things you say.

    I'n 1970 I was doing linear regression equations on Mini-Computers in Focal a VB predecessor.. Linear regression is very,very  EASY to on a computer.

    It's pretty much like a correlation coeffiient. Unfortunately I am asked to do this once every fifteen years of so and I've forgotten the approach.

    I remember y = mx + b after to get the coefficient.  But the point is.... it's a really easy thing to do.

    I see you saying a lot of theings here are just aren't so.



  • rxg

    Hi,

    This involves comparing all the X values and the Y's, not easy.

    If X's were 1,3,5,7 then it is obviously linear in respect to the X's but what if the Y's aren't linear

    like 1,2,4,9,16 which is the the 1st four numbers ^ 2

    So if you had

     Point1(1,1)

    Point2(3,2)

    Point3(5,4)

    Point4(7,9)

    What would Y be if you had a point where X=5.2

    My only thought on this is you could open Excel minimised and do the Forecast function in a cell and get the result back into your program.

    Whether you can edit the formula of a cell in VBA or VB.Net is another matter.

    See these EXCEL threads in these VB.Net forums.>>

    http://forums.microsoft.com/MSDN/Search/Search.aspx words=Excel&localechoice=9&SiteID=1&searchscope=forumgroupscope&ForumGroupID=10

     

     

    Regards,

    S_DS

     

     



  • colin leversuch-roberts

    Great suggestions, thank you for taking the time to help me. I will be working on this tonight and (hopefully) I will have it figured out and into my code by the morning! I will defenately post my code on here when I get done incase anyone else needs this in the future.

    In answer to your question, I am not sure if I will need it on a regular basis, but I may. I will be writing similar programs in the future, so I'd like to figure out the best way to do it with code...

    Thanks again,

    -Corey


  • heyram

    Thanks for your reply...

    This will be used as part of a larger program. Basically, I will be taking four data points (x, y) that I have stored in variables. I need to get the linear regression coefficient and y-intercept so I can predict y, given x. I can just get out my stats book and lookup the mathematical procedure to do this and then write code to calcualte it, but i thought maybe there is a built-in function in VS that can do it for me. You can do this in excel using =Forecast(x, known y's, known x's). Is there something similar in VS

    Thanks!

    -Corey


  • tarzan123_us

    RenneC,

    Another thing is, i was working nights Saturday night and didn't get much sleep Sunday day time.

     I did not read right through the 2nd post by cmsmith81 in this thread properly, like you didn't read right through my posts before raising a statement. It appears I helped Him in the end to answer His question so why not leave this thread and answer in another, move on, like we all do, next....

    Even the original post user admitted that non-linear regression isn't easy so you've obviously not read through all of this thread have you Come on, admit it.

    Just read the 4th post by the originator cmsmith81, enough said.

     

     

    Regards,

    S_DS

     



  • Vladox

    You are right about needing to figure out whether it is a linear, exponential, logarithmic, etc relationship. But luckily in my case, it is stipulated that with the data I will be using, the relationship will be linear. So I don't have to worry about that. (Actually, I did not know that the forecast function in Excel took this into account... I assumed it treated it as linear.)

    So anyway, I am looking for a function that will create the regression equation (or the components of it) rather than me having to write the code to calculate the regression equation. In all honesty, it won't be too hard to write the code. But if there is something already built in, it'll probably save me some time.

    Thank you for you help thus far!!

    -Corey


  • Mohan1

    Hello again,

    Not that i can think of but you could maybe use the method of simutaneoues equation to find your multipliers

    Add x and y.

    Say

    x1+y1= 6 <---formula 1

    x1+y2=12 <---formula2

    Cancel or make equal one formula with the other.>>

    (x1+y1) * (12/6) = 6 * (12/6) <--- I have multiplied both sides by ( 12 / 6 ) on formula1.

    ---> (x1+y1)*2=6*2

    ---> Substitute x for x1 and y for y1 you the get 2x+2y=12

    Equation for x becomes>>x=(12-2y) / 2

    and y becomes y=(12-2x) / 2

    How you would do this in code The only way i can think of is to resort to OOP, object orientated programming as you are moving mathematical terms around.

    Regards,

    S_DS

    P.S. Is this something that you would need on a regular basis

    If you don't follow what i've done above then give me two pairs of your point values.



  • Derek Sanderson

    I just wrote the code to calculate it instead of trying to find a built-in function to do it for me.

    Note: Since in my particular case there will always be 4 data points and only 4 data points, this code is fairly simple. If you have a varying number of data points, you could add loops to accommodate this.

    Here is my code:

    Dim VO1 as integer 'these are the Y variables
    Dim VO2 as integer
    Dim VO3 as integer
    Dim VO4 as integer

    Dim HRf1 as integer 'these are the X variables
    Dim HRf2 as integer
    Dim HRf3 as integer
    Dim HRf4 as integer

    VO1 = 5
    VO2 = 10
    VO3 = 15
    VO4 = 20

    HRf1 = 1
    HRf2 = 2
    HRf3 = 3
    HRf4 = 4

    Dim xBar 'average of the x values
    Dim yBar 'average of the y values
    Dim B1
    Dim B0
    Dim xGiven = 5 'to predict y, given that x = 5
    Dim yHat
    Dim B1Numerator
    Dim B1Denominator

    xBar = (HRf1 + HRf2 + HRf3 + HRf4) / 4
    yBar = (VO1 + VO2 + VO3 + VO4) / 4

    B1Numerator = ((HRf1 - xBar) * (VO1 - yBar)) + ((HRf2 - xBar) * (VO2 - yBar)) + ((HRf3 - xBar) * (VO3 - yBar)) + ((HRf4 - xBar) * (VO4 - yBar))

    B1Denominator = ((HRf1 - xBar) ^ 2) + ((HRf2 - xBar) ^ 2) + ((HRf3 - xBar) ^ 2) + ((HRf4 - xBar) ^ 2)

    B1 = B1Numerator / B1Denominator

    B0 = yBar - (B1 * xBar)

    yHat = B0 + (B1 * xGiven) 'this is the regression equation, y = b + m*x

    msgbox (yHat)


    This will predict y based on what the variable 'xGiven' is set to. In the case that xGiven=5, yHat (predicted y) is 25 for these data points.

    -Corey


  • Simpson474

     ReneeC wrote:

     

     

    Spidermans_DarkSide, sometime you highly misnformed in the things you say.

    I'n 1970 I was doing linear regression equations on Mini-Computers in Focal a VB predecessor.. Linear regression is very,very  EASY to on a computer.

    It's pretty much like a correlation coeffiient. Unfortunately I am asked to do this once every fifteen years of so and I've forgotten the approach.

    I remember y = mx + b after to get the coefficient.  But the point is.... it's a really easy thing to do.

    I see you saying a lot of theings here are just aren't so.

     

    RenneC,

     

    Linear regression is easy if you read properly what i've typed i was also referring to regression which is non-linear also in this thread.

    By the way please keep criticisms to yourself and leave it out of all forum posts as it is not relevant, i never criticise anyone in any forum area other than this post highlighting the fact that you have.

     

    Regards,

    S_DS

     



  • Regression Equation / Statistics in VS 2005