Using Excel To Fit Lines
Using Excel To Fit Lines
Excel can be used to plot scatter graphs and fit expressions. It’s certainly not the best
statistics package (see SPSS, Minitab, R, stata) or graphing package (see Origin,
Matlab), but for a simple linear plot it is relatively quick and easy to use. The following
set of screen shots show you how to plot graphs in Excel 2007.
Here you can see the two columns of data (X,Y) to be plotted on a scatter graph. To
select the x set, hold down the left mouse key over cell A2 and drag down to A9. To
select the y data, hold down the Ctrl key on the keyboard (this prevents you un-
selecting the x-data) and drag from cell C2 down to C9. The two columns will be
highlighted in grey.
From the insert menu, then Chart menu, select Scatter and the ‘Scatter with only
markers’ as shown in the next figure. Note unless you have good reason to believe to
rather wavy curves Excel loves to fit, you should avoid them now and forever more.
The points are the data, if you need to fit a trend line, fit one you choose rather than
Excel.
Once clicked, this will bring up a chart. Format it to black and white, with clearly
different symbols for each set of y-data (only one set here), sort out axis labels, titles
and grid lines. This shows the before with how to change properties:
1.5
1
Series1
0.5
0
0 0.5 1 1.5
To change each of the properties of the graph, left click within one of the circles
indicated above to ‘select’, then right click, to bring up a menu.
For titles, axis labels etc, the easiest way to alter these is to select the chart with a left
mouse-click, then bring up the Chart tools: layout menu.
Finally, to fit data, select the dataset (left-
click), then right click to bring up the format
trendline box. Important options in here are
the type of fit (e.g. linear), and the option to
display the equation on the chart (so you
can get the gradient and intercept) and the
R2 parameter, which measures how well the
data fits.
Almost there...
2
1.75
1.5 y = 1.1916x - 0.1927
1.25 R² = 0.9259
Some y-data
1
0.75
0.5
0.25
0
-0.25 0 0.25 0.5 0.75 1 1.25 1.5
-0.5
Some x-data
1
0.75
0.5
0.25
0
-0.25 0 0.25 0.5 0.75 1 1.25 1.5
-0.5
Some x-data
A final thing to tidy up is to remove the best fit equation shown on the graph, and write
this into your report (with the corresponding R2 value). You can then comment on the
significance of the equation (e.g. what does the slope, the intercept, the correlation
coefficient tell you ?). Depending what you are writing, it is sometimes common to also
remove the title, and display this in the figure caption. For this report, include a sensible
title on the graph.
If there’s one thing you should never ever ever EVER do, is to fit an awful wiggly
line to your data (unless you are fitting the shape of a worm).
A very quick guide to processing data in Excel
This shows how to take some data in a column and process it subject to a given
equation. This example will make the transformation:
𝑦 = 𝑧1 𝑥 + 𝑧2 𝑥 2
The values of z1 and z2 are constant, but the example will allow these to be set on
the spreadsheet.
1. Open Excel. In Column A, paste the numbers shown
right. You may want to leave a gap above this data
(ie start with x in cell A4), alternatively you can add a
blank line above the data by clicking on the
appropriate number in the very left hand column to
select the whole row, then right click and select the
‘insert’ option.
2. Now for the equation. Lets use example values of z1=1.0 and z2=0.5. Select
cell B5 and in the function box type: =1.0*A5 + 0.5*(A5)^2
(you can click on the relevant cell A5 with your mouse at the appropriate place
in your formula). Check your answer....
4. One last thing. Rather than putting constants z1 and z2 explicitly in the
equation, lets make space for them on the spreadsheet so its possible to alter
them. First identify a space for z1 and z2 on the spreadsheet. A suitable place
would be in column B cells 1 and 2. You should label the data – its also
possible to draw borders to tidy things up...
6. You should see a whole load of errors in the box. Lets look why. Double click,
in turn, in boxes B5, B6, B7. As you do so, you will see coloured boxes that
indicate the source of the data (note press escape after highlighting the
coloured boxes to return to the normal mode of operation). When you dragged
the equation down the chart it has correctly moved the reference to x, but it
has also moved the reference it makes to B1 and B2. Double clicking on B7
shows it thinks it is getting the value for z1 from cell B3 and z2 from cell B4.
Since the values z1 and z2 are constant, you want to ‘lock’ the equation to
these values. To do this you make use of $ symbols around B1 and B2. A
quick way to do this when editing the equation, is to press F4 on the
keyboard.
B1 No fixed references
There is a lot more you can do with data in Excel. It is a useful tool with which you
can collect information together (from experiment or computations) that you can then
go on and process.