+ Reply to Thread
Results 1 to 10 of 10

Regression formula

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2010
    Location
    Rome, Italy
    MS-Off Ver
    Excel 2003
    Posts
    36

    Cool Regression formula

    I often use regression in data analysis and produce relevant graph.
    Here's the code:

    Sub Previsioni()
    '
    If Range("D30") = 42 Then
    MsgBox "Attenzione!!! Zona dati vuota"
    End
    End If
    
    Dim Codice, Tipo, equaz
    equaz = ""
    Codice = Range("f5").Value
    Codice = Left(Codice, 1)
    
    If Codice = 1 Then
    Tipo = xlLinear
    End If
    
    If Codice = 2 Then
    Tipo = xlExponential
    End If
    
    If Codice = 3 Then
    Tipo = xlLogarithmic
    End If
    
    If Codice = 4 Then
    Tipo = xlPolynomial
    End If
    
    ActiveSheet.ChartObjects("Chart 31").Activate
    ActiveChart.ChartArea.Select
    
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=Tipo, Forward:=0, _
    Backward:=0, DisplayEquation:=True, DisplayRSquared:=True).Select
    
    ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
    'equaz = ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel
    Selection.Left = 440
    Selection.Top = 273
    ActiveWindow.Visible = False
    
    'Range("Equazione").Value = ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel
    'Selection.Paste
    End Sub
    As you can see I've tried to "catch" automatically in some way the formula given by the trendlines in the graph area in order to use it for other calculations, but unsuccessful.
    Have you any hint on how this can be achieved?
    Thanks in advance for your usual precious help.
    Last edited by Romoluzzi; 01-02-2011 at 09:46 AM.

  2. #2
    Registered User
    Join Date
    09-21-2010
    Location
    Rome, Italy
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Regression formula

    No suggestions? not even for the new year?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regression formula

    You can use LINEST to get the parameters of the regression.

    If you post a workbook with an example, I or someone else will show you how.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,266

    Re: Regression formula

    Does the link below help in resolving your desire?

    http://zimmer.csufresno.edu/~davidz/...INESTfull.html
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    09-21-2010
    Location
    Rome, Italy
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Regression formula

    Here's is another evidence that (almost) always the simplest solution is the hardest to find out..... I hadn't thought about using Excel's LINEST function, my fault.
    But at this point I have another question: is there any way to use the 10 parameters generated by LINEST in VBA code? In other words, is it possible in a macro / UDF to set a variable to get the value of the slope, the intercept, etc and to use it in further calculations, WITHOUT actually inputting the array formula in a ten-cell area of the worksheet and/or WITHOUT set the calculation in the VBA procedure?
    For example, if I use the macro recorder and input the LINEST formula in the worksheet, I get:

    Sub Macro2()
        Range("K41:L45").Select
        Selection.FormulaArray = _
            "=LINEST(R[-33]C[-8]:R[-26]C[-8],R[-33]C[-10]:R[-26]C[-10],TRUE,TRUE)"
    End Sub
    I see that there is a VBA instruction to calculate the parameters; is it possible to put some of the 10 values DIRECTLY in variables WITHOUT using the corresponding cells values in the worksheet?

    I hope my question is clear and I thank you all for your support.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Regression formula

    Do you mean along the lines of:

    Dim vResults As Variant
    With Sheets("Sheet1").Range("A8:A15")
        vResults = .Parent.Evaluate("=LINEST(" & .Offset(, 2).Address & "," & .Address & ",TRUE,TRUE)")
    End With
    vResults would be a 2d Array of the results.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regression formula

    Or

        Dim v As Variant
    
        v = WorksheetFunction.LinEst(Range("B1:B10").Value, Range("A1:A10").Value, , True)
        MsgBox "Slope:=" & v(1, 1) & vbLf & _
               "Intercept:=" & v(1, 2)

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Regression formula

    oops... I do like to over engineer every now and then !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1