0% found this document useful (0 votes)
31 views5 pages

Using Excel To Fit Lines

Uploaded by

rosevickymugambi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Using Excel To Fit Lines

Uploaded by

rosevickymugambi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Using Excel to fit lines – good practice

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.

Your graph may well look something like:

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

The Final graph...


2
1.75
1.5
1.25
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

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....

3. The nice thing about Excel is it is possible to copy this


formula down the column. Click on cell B5 and grab
the little square box using the left click of your mouse.
Drag this directly downwards and release. You should
get a bunch of calculated values. Double clicking on
any of the cells shows both the equation and coloured
boxes as to where it is getting data from. as you go
down column B doing this, you will see the reference
x in the equation has moved downwards as you
dragged down the column.

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...

5. In cell B5 the expression should be =B1*A5 + B2*(A5)^2 As before,


drag this equation down the column.

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.

Select the equation in B5.


Click on B1 and press F4,
similarly click on B2 and
press F4. The box right shows what the expression looks like. Accept the
equation by clicking the green tick. Now drag the equation from cell B5 down
the remainder of the column. You should get an error free set of data.

7. A final note on referencing cells. There are four combinations of $ signs....

B1 No fixed references

If the equation is dragged down, then the number will change


If the equation is dragged across, the column will change

Always fixed to column B


$B1
If the equation is dragged down, then the number will change
If the equation is dragged right (across the spreadsheet) the number
will stay as 1

Always fixed to row 1

If the equation is dragged down, the number stays constant


B$1
If the equation is dragged across, the column B will change

Always fixed to cell B1


$B$1

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.

You might also like