CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Regression Line Calculation (SLOPE)

    In Microsoft Excel there is a SLOPE function. What I need to do is translate the formula of this SLOPE function to work in a Visual C++ Application. Can anyone out there show me how I would write the function or functions that will produce the same output as the Excel SLOPE function! I can provide more information as needed, but would appreciate as detailed information as possible! PLEASE HELP!

    Many thanks!
    Charlie

    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  2. #2
    Join Date
    Aug 1999
    Location
    Canada
    Posts
    2,076

    Re: Regression Line Calculation (SLOPE)

    Linear line regression algorithm is very easy to implement. I never use Excel SLOPE function. I suppose, that parameters are array of x,y points, the slope of a line( steepness of the line) and intercept.

    But I'm not sure, how is this algorithm implemented in Excel. Let me detailed information, if you want.


  3. #3
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Re: Regression Line Calculation (SLOPE)

    I need the details if you can provide please do so!
    Thanks

    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  4. #4
    Join Date
    Aug 1999
    Location
    Canada
    Posts
    2,076

    Re: Regression Line Calculation (SLOPE)

    In this WEB site you can find explanation.

    http://www.ruf.rice.edu/~lane/hyperstat/A115370.html

    I never used Excel, so I need to know, what kind of input data you use.


  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Regression Line Calculation (SLOPE)

    I don't know what SLOPE does, but to find the
    "least squares regression line" , y = Mx + B
    for the points (x(i),y(i)) for i = 1,N

    you would do the following :

    s0 = N+1

    s1 = sum_of(x(i)) for i = 1,N

    s2 = sum_of(x(i)*x(i)) for i = 1,N

    t0 = sum_of(y(i)) for i = 1,N

    t1 = sum_of(x(i)*y(i)) for i = 1,N


    then

    M = ( s0*t1 - s1*t0 ) / (s0*s2 - s1*s1)

    B = ( s2*t0 - s1*t1 ) / (s0*s2 - s1*s1)

    and the regression line is given by

    y = Mx + B




  6. #6
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Re: Regression Line Calculation (SLOPE)


    A B Slope
    1.05 0.016632 0.015338
    1.05 0.016817
    1.05 0.016662
    3.05 0.047281
    3.05 0.047469
    3.05 0.047494
    5.05 0.079046
    5.05 0.078907
    5.05 0.078804
    10.05 0.155113
    10.05 0.154559
    10.05 0.154475



    If you open Excel and add the data (not the slope value) The in a column off to the right add the slope function and it should look like this
    SLOPE(B2:B13,A2:A13)
    If you need more info please let me know.

    Thanks!
    Charlie



    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  7. #7
    Join Date
    Aug 1999
    Location
    Canada
    Posts
    2,076

    Re: Regression Line Calculation (SLOPE)

    Thank you, I take a look on this through the weekend. I'm very busy during working week....

    Can you send me your email adress? My is in my profile on codeguru.


  8. #8
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Re: Regression Line Calculation (SLOPE)

    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Regression Line Calculation (SLOPE)


    f.y.i. the algorithm that I gave you calculated the same slope that
    you provided in your sample data set.

    Here is a code snippet to do the calculation :


    // values needed prior to code snippet
    //
    // int n = number of (x,y) points
    // double x[ i ] for i = 0 to n-1 ... the x points
    // double y[ i ] for i = 0 to n-1 ... the y points
    //

    double s0 = 0;
    double s1 = 0;
    double s2 = 0;
    double t0 = 0;
    double t1 = 0;

    for (int i=0; i<n; i++)
    {
    s0++;
    s1 = s1 + x[ i ];
    s2 = s2 + x[ i ]*x[ i ];
    t0 = t0 + y[ i ];
    t1 = t1 + x[ i ]*y[ i ];
    }

    double M = ( s0*t1 - s1*t0 ) / (s0*s2 - s1*s1) ; // slope

    double B = ( s2*t0 - s1*t1 ) / (s0*s2 - s1*s1) ; // y-intercept








  10. #10
    Join Date
    May 1999
    Location
    Arizona, U.S.A.
    Posts
    101

    Re: Regression Line Calculation (SLOPE)

    SORRY! I didn't understand your original message, but this is exactly what I was looking for! So I will give it a try and I appreciate your extra effort in providing the information in a more understandable format!

    Thanks!
    Charlie

    Everything is Free Until You Have to Pay for it....

    Platform is Windows 2000/XP Professional, Visual C++ 6.0

  11. #11
    Join Date
    Feb 2013
    Posts
    7

    Re: Regression Line Calculation (SLOPE)

    Quote Originally Posted by Philip Nicoletti View Post
    f.y.i. the algorithm that I gave you calculated the same slope that
    you provided in your sample data set.

    Here is a code snippet to do the calculation :


    // values needed prior to code snippet
    //
    // int n = number of (x,y) points
    // double x[ i ] for i = 0 to n-1 ... the x points
    // double y[ i ] for i = 0 to n-1 ... the y points
    //

    double s0 = 0;
    double s1 = 0;
    double s2 = 0;
    double t0 = 0;
    double t1 = 0;

    for (int i=0; i<n; i++)
    {
    s0++;
    s1 = s1 + x[ i ];
    s2 = s2 + x[ i ]*x[ i ];
    t0 = t0 + y[ i ];
    t1 = t1 + x[ i ]*y[ i ];
    }

    double M = ( s0*t1 - s1*t0 ) / (s0*s2 - s1*s1) ; // slope

    double B = ( s2*t0 - s1*t1 ) / (s0*s2 - s1*s1) ; // y-intercept
    Hi thanks for this code,but can you help me to get the regression..y=mx+b? what is the value of x? is it the iteration of data or the total absorbance? or conc? Please email me at jotyme@rocketmail.com. Thanks bro.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured