|
|
Trendlines |
||
|
|||
Adding a trend line to a graphHave you ever wandered, out of all of Excel's options for the type of trend line to use for your graph, which one would be best? Below, I have drawn a chart illustrating the problem. You can see that the the polynomial or the power trend lines fit the data best, but this was only discovered after adding each of the trend lines in turn, which takes time
Even after looking at all of the trend lines, you are still only making your choice by eye. A more accurate way to determine the correct trend line to use is by looking at its R2 value. Doing this, however, takes even more time. This is where my macro comes in. Once you have selected a chart, it will automatically apply a trend line to the first series, and the type chosen will depend on whichever trend line has the highest R2 value. Using this macro, I have drawn a trend line for the above chart that most appropriately fits the data.
This is the output from the macro. It has chosen the power trend line and named it 'Line of best fit'. How it works
Limitations
If you have a knowledge of VBA, you may wish to overcome these limitations by modifying the code to suit your purposes. The codeSub AddTrendline()
'
' AddTrendline Macro
' Macro recorded 15/05/2005 by Toby Allen
'
'
Dim i
Dim TrendNum
Dim BestSoFarR
Dim BestSoFarTN
Dim myR
With ActiveChart.SeriesCollection(1).Trendlines.Add(xlLinear, 2, , , , , , True, "Line of best fit")
BestSoFarTN = 0
BestSoFarR = 0
For i = 3 To 7
If i > 5 Then TrendNum = i - 4139 Else TrendNum = i
.Type = TrendNum
myR = CDbl(Mid(.DataLabel.Text, 6, Len(.DataLabel.Text) - 5))
If myR >= BestSoFarR Then
BestSoFarR = myR
BestSoFarTN = TrendNum
End If
'Debug.Print Mid(.DataLabel.Text, 6, Len(.DataLabel.Text) - 5)
Next i
'Debug.Print BestSoFarR
'Debug.Print BestSoFarTN
.Type = BestSoFarTN
.DisplayRSquared = False
End With
End Sub
| |||
|
Written by Toby. This work is licensed under a Creative Commons License. Privacy | |||