Problem

You want to perform a linear curve fit using standard least-squares formulas instead of Excel's linear trendline.

Solution

Use Excel's built-in formulas such as `COUNT`, `SUM`, `SUMSQ`, and `SUMPRODUCT` to make it easy to apply the standard least-squares formulas.

Discussion

The standard equation for a straight line is:

The standard least-squares formulas used to determine the slope, m, and intercept, b, of the fit line are:

In these equations, n is the number of data points. Excel has several built-in functions that make it very easy to compute the various sums that appear in the least-squares equations. These functions include:

COUNT

This function counts the number of cells containing numbers in a range of cells.

SUM

This function adds all the numbers in a range of cells.

SUMSQ

This function returns the sum of squares of the numbers contained in a range of cells.

SUMPRODUCT

This function sums the products of entries in corresponding ranges of cells.

Let's reconsider the data used in the example in Recipe 8.1. Instead of using a chart trendline to determine the best-fit line, we'll use the least-squares equations and built-in Excel functions. Figure 8-4 shows a simple spreadsheet I set up for this example.

The columns labeled `X` and `Y` contain the same data as before. I just renamed them to make it easier to follow the application of the formulas used in this example. Cells B23 through B27 contain formulas to compute the various quantities required by the least-squares formula. The formulas in these cells are as follows:

Cell B23

The formula in this cell, `=COUNT(A4:A21)`, computes the number of data points.

Cell B24

The formula in this cell, `=SUM(A4:A21)`, computes the sum of x-values.

Cell B25

The formula in this cell, `=SUM(B4:B21)`, computes the sum of y-values.

Cell B26

The formula in this cell, `=SUMPRODUCT(A4:A21,B4:B21)`, computes the sum of the products on values in the `X` and `Y` columns.

Cell B27

The formula in this cell, `=SUMSQ(A4:A21)`, computes the sum of squares of x-values.

Figure 8-4. Least-squares fit

These cells contain all of the data we need to apply the least-squares formulas.

Cell F6 computes the slope of the best-fit line. The formula in cell F6 is `=(B23*B26-B24*B25)/(B23*B27-B24^2)`. The formula in cell F7, `=(B25*B27-B24*B26)/(B23*B27-B24^2)`, computes the y-intercept for the best-fit line.

The resulting slope and intercept are 0.80869 and 0.007391, respectively. These values agree very well with those obtained using a trendline as discussed in Recipe 8.1.

To assess how well this fit equation represents the data, you can compute the R-squared value just as Excel does when it computes chart trendlines. The closer R-squared is to 1, the better the fit. Read Recipe 8.7 to learn more about the R-squared value.

For this example, you need to perform a few more calculations in order to compute the R-squared value. Namely, you need to compute the mean of the y-values, the estimated y-values, and the residuals. Figure 8-5 shows these new calculations for this example. Cell F10 computes the mean y-value using Excel's `AVERAGE` function. Column H contains the estimated y-values using the equation for a straight line, along with the computed slope and intercept. Columns I and J are used to calculate the differences between the estimated y-values and the mean and between the actual y-values and the mean, respectively. The sums of the squares of these differences are computed in cells I22 and J22 using Excel's `SUMSQ` function.

Figure 8-5. R-squared computation

The R-squared value is computed in cell F12 by simply taking the ratio of the values in cells I22 and J22. In Figure 8-5, cell F12 is selected and you can see the cell formula for computing R-squared in the formula bar. For this example, R-squared comes out to 0.9985, which indicates a good fit and agrees very well with the value obtained using a chart trendline as discussed in Recipe 8.1.

See Also

Take a look at Recipe 8.7 in this chapter to learn more about R-squared. Also, check out the other recipes in this chapter to learn about alternative methods of performing linear curve fitting .

Using Excel

- Introduction
- Navigating the Interface
- Entering Data
- Setting Cell Data Types
- Selecting More Than a Single Cell
- Entering Formulas
- Exploring the R1C1 Cell Reference Style
- Referring to More Than a Single Cell
- Understanding Operator Precedence
- Using Exponents in Formulas
- Exploring Functions
- Formatting Your Spreadsheets
- Defining Custom Format Styles
- Leveraging Copy, Cut, Paste, and Paste Special
- Using Cell Names (Like Programming Variables)
- Validating Data
- Taking Advantage of Macros
- Adding Comments and Equation Notes
- Getting Help

Getting Acquainted with Visual Basic for Applications

- Introduction
- Navigating the VBA Editor
- Writing Functions and Subroutines
- Working with Data Types
- Defining Variables
- Defining Constants
- Using Arrays
- Commenting Code
- Spanning Long Statements over Multiple Lines
- Using Conditional Statements
- Using Loops
- Debugging VBA Code
- Exploring VBAs Built-in Functions
- Exploring Excel Objects
- Creating Your Own Objects in VBA
- VBA Help

Collecting and Cleaning Up Data

- Introduction
- Importing Data from Text Files
- Importing Data from Delimited Text Files
- Importing Data Using Drag-and-Drop
- Importing Data from Access Databases
- Importing Data from Web Pages
- Parsing Data
- Removing Weird Characters from Imported Text
- Converting Units
- Sorting Data
- Filtering Data
- Looking Up Values in Tables
- Retrieving Data from XML Files

Charting

- Introduction
- Creating Simple Charts
- Exploring Chart Styles
- Formatting Charts
- Customizing Chart Axes
- Setting Log or Semilog Scales
- Using Multiple Axes
- Changing the Type of an Existing Chart
- Combining Chart Types
- Building 3D Surface Plots
- Preparing Contour Plots
- Annotating Charts
- Saving Custom Chart Types
- Copying Charts to Word
- Recipe 4-14. Displaying Error Bars

Statistical Analysis

- Introduction
- Computing Summary Statistics
- Plotting Frequency Distributions
- Calculating Confidence Intervals
- Correlating Data
- Ranking and Percentiles
- Performing Statistical Tests
- Conducting ANOVA
- Generating Random Numbers
- Sampling Data

Time Series Analysis

- Introduction
- Plotting Time Series Data
- Adding Trendlines
- Computing Moving Averages
- Smoothing Data Using Weighted Averages
- Centering Data
- Detrending a Time Series
- Estimating Seasonal Indices
- Deseasonalization of a Time Series
- Forecasting
- Applying Discrete Fourier Transforms

Mathematical Functions

- Introduction
- Using Summation Functions
- Delving into Division
- Mastering Multiplication
- Exploring Exponential and Logarithmic Functions
- Using Trigonometry Functions
- Seeing Signs
- Getting to the Root of Things
- Rounding and Truncating Numbers
- Converting Between Number Systems
- Manipulating Matrices
- Building Support for Vectors
- Using Spreadsheet Functions in VBA Code
- Dealing with Complex Numbers

Curve Fitting and Regression

- Introduction
- Performing Linear Curve Fitting Using Excel Charts
- Constructing Your Own Linear Fit Using Spreadsheet Functions
- Using a Single Spreadsheet Function for Linear Curve Fitting
- Performing Multiple Linear Regression
- Generating Nonlinear Curve Fits Using Excel Charts
- Fitting Nonlinear Curves Using Solver
- Assessing Goodness of Fit
- Computing Confidence Intervals

Solving Equations

- Introduction
- Finding Roots Graphically
- Solving Nonlinear Equations Iteratively
- Automating Tedious Problems with VBA
- Solving Linear Systems
- Tackling Nonlinear Systems of Equations
- Using Classical Methods for Solving Equations

Numerical Integration and Differentiation

- Introduction
- Integrating a Definite Integral
- Implementing the Trapezoidal Rule in VBA
- Computing the Center of an Area Using Numerical Integration
- Calculating the Second Moment of an Area
- Dealing with Double Integrals
- Numerical Differentiation

Solving Ordinary Differential Equations

- Introduction
- Solving First-Order Initial Value Problems
- Applying the Runge-Kutta Method to Second-Order Initial Value Problems
- Tackling Coupled Equations
- Shooting Boundary Value Problems

Solving Partial Differential Equations

- Introduction
- Leveraging Excel to Directly Solve Finite Difference Equations
- Recruiting Solver to Iteratively Solve Finite Difference Equations
- Solving Initial Value Problems
- Using Excel to Help Solve Problems Formulated Using the Finite Element Method

Performing Optimization Analyses in Excel

- Introduction
- Using Excel for Traditional Linear Programming
- Exploring Resource Allocation Optimization Problems
- Getting More Realistic Results with Integer Constraints
- Tackling Troublesome Problems
- Optimizing Engineering Design Problems
- Understanding Solver Reports
- Programming a Genetic Algorithm for Optimization

Introduction to Financial Calculations

- Introduction
- Computing Present Value
- Calculating Future Value
- Figuring Out Required Rate of Return
- Doubling Your Money
- Determining Monthly Payments
- Considering Cash Flow Alternatives
- Achieving a Certain Future Value
- Assessing Net Present Worth
- Estimating Rate of Return
- Solving Inverse Problems
- Figuring a Break-Even Point

Index

Excel Scientific and Engineering Cookbook (Cookbooks (OReilly))

ISBN: 0596008791

EAN: 2147483647

EAN: 2147483647

Year: N/A

Pages: 206

Pages: 206

Authors: David M Bourg

Flylib.com © 2008-2020.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net