



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Introduction Consider a set of n measurements of some variable y as a function of another variable x. Typically, y is some measured output as a function of some known input , x. Recall that the linear correlation coefficient is used to determine if there is a trend. If there is a trend, regression analysis is useful. Regression analysis is used to find an equation for y as a function of x that provides the best fit to the data.
Linear regression analysis Linear regression analysis is also called linear least-squares fit analysis. The goal of linear regression analysis is to find the “best fit” straight line through a set of y vs. x data. The technique for deriving equations for this best-fit or least-squares fit line is as follows: o An equation for a straight line that attempts to fit the data pairs is chosen as Y ax b. o In the above equation, a is the slope ( a = dy / dx – most of us are more familiar with the symbol m rather than a for the slope of a line), and b is the y-intercept – the y location where the line crosses the y axis (in other words, the value of Y at x = 0). o An upper case Y is used for the fitted line to distinguish the fitted data from the actual data values, y. o In linear regression analysis, coefficients a and b are optimized for the best possible fit to the data. o The optimization process itself is actually very straightforward: o For each data pair ( xi , yi ), error ei is defined as the difference between the predicted or fitted value and the actual value : ei = error at data pair i , or ei^ ^ Yi^ ^ yi^ ^ axi^ ^ b^ ^ yi. ei is also called the residual. Note : Here, what we call the actual value does not necessarily mean the “correct” value, but rather the value of the actual measured data point. o We define E as the sum of the squared errors of the fit – a global measure of the error associated with
2 2 1 1
i n i n i i i i i
E e ax b y
o It is now assumed that the best fit is the one for which E is the smallest. o In other words, coefficients a and b that minimize E need to be found. These coefficients are the ones that create the best-fit straight line Y = ax + b. o How can a and b be found such that E is minimized? Well, as any good engineer or mathematician knows, to find a minimum (or maximum) of a quantity, that quantity is differentiated , and the derivative is set to zero. o Here, two partial derivatives are required, since E is a function of two variables, a and b. Therefore, we
set 0
a
and 0
b
o After some algebra, which can be verified, the following equations result for coefficients a and b :
1 1 1 2 2 1 1
i n i n i n i i i i i i i i n i n i i i i
n x y x y a n x x
and
2 1 1 1 1 2 2 1 1
i n i n i n i n i i i i i i i i i i n i n i i i i
x y x x y b n x x
Coefficients a and b can easily be calculated in a spreadsheet by the following steps: o Create columns for xi , yi , xiyi , and xi^2. o Sum these columns over all n rows of data pairs. o Using these sums, calculate a and b with the above formulas. Modern spreadsheets and programs like Matlab, MathCad, etc. have built-in regression analysis tools, but it is good to understand what the equations mean and from where they come. In the Excel spreadsheet that accompanies this learning module, coefficients a and b are calculated two ways for each example case – “by hand” using the above equations, and with the built-in regression analysis package. As can be seen, the agreement is excellent, confirming that we have not made any algebra mistakes in the derivation.
Example : Given: 20 data pairs ( y vs. x ) the same data used in a previous example problem in the learning module about correlation and trends. Recall that we calculated the linear correlation coefficient to be rxy = 0.480. The data pairs are listed below, along with a scatter plot of the data.
To do: Find the best linear fit to the data. Solution: o We use the above equations for coefficients a and b with n = 20; we calculate a = 3.241 , and b = 4.082 , to four significant digits. Thus, the best linear fit to the data is Y 3.241 x 4.082. o Alternately, using Excel’s built-in regression analysis macro, the following output is generated: Office 2003 and older: Tools-Data Analysis-Regression Office 2007 and later: Data tab. In Analysis area, Data Analysis-Regression
o With all the data points used, the three stray data points (outliers) have ruined the rest of the fit (solid blue line). For this case, rxy = 0.5745 and S (^) y,x = 4.787. o If these three outliers are removed, the least-squares fit follows the overall trend of the other data points much more accurately (dashed green line). For this case, rxy = 0.9956 and S (^) y,x = 0.5385. The linear correlation coefficient is significantly higher (better correlation), and the standard error is significantly lower (better fit). o In a separate learning module we discuss techniques for properly removing outliers. o To protect against such undesired effects, more complex least-squares methods, such as the robust straight-line fit , are required. Discussion of these methods are beyond the scope of the present course.
Linear regression with multiple variables Linear regression with multiple variables is a feature included with most modern spreadsheets. Consider response, y , which is a function of m independent variables x 1 , x 2 , ..., xm , i.e., y = y ( x 1 , x 2 , ..., xm ). Suppose y is measured at n operating points ( n sets of values of y as a function of each of the other variables). To perform a linear regression on these data using Excel, select the cells for y (in one column as previously), and a range of cells for x 1 , x 2 , ..., xm (in multiple columns), and then run the built-in regression analysis. When there is more than one independent variable, we use a more general equation for the standard error ,
2 1 , (^) df
i n i i i y x
y Y S
, where df = degrees of freedom , df n ( m 1), n is the number of data points or
operating points, and m is the number of independent variables.
Example : Given: In this example, we perform linear regression analysis with multiple variables. o We assume that the measured quantity y is a linear function of three independent variables, x 1 , x 2 , and x 3 , i.e., y^ ^ b^ ^ a x 1 1^ ^ a x 2 2^ ^ a x 3 3. o Nine data points are measured by setting three levels for each parameter, and the data are placed into a simple data array as shown to the right (the image is taken from an Excel spreadsheet). To do: Calculate the y intercept and the three slopes simultaneously , one slope for each independent variable x 1 , x 2 , and x 3. Solution: o We perform a linear regression on these data points to determine the best (least-squares) linear fit to the data. o In Excel, the multiple variable regression analysis procedure is similar to that for a single independent variable, except that we choose several columns of x data instead of just one column: Launch the macro (Data Analysis-Regression). The default options are fine for illustrative purposes. The nine values of y in the y -column are selected for Input Y range. All 27 values of x 1 , x 2 , and x 3 , spanning nine rows and three columns , are selected for Input X range. Output Range is selected, and some suitable cell is selected for placement of the output. OK. Excel generates what it calls a Summary Output. o From Excel’s output, the following information is needed to generate the coefficients of the equation for which we are finding the best fit, y^ ^ b^ ^ a x 1 1^ ^ a x 2 2^ ^ a x 3 3 : The y -intercept , which Excel calls Intercept. For our equation, b^ ^ Intercept. The three slopes , which Excel calls X Variable 1 , X Variable 2 , and X Variable 3. For our equation,
1 1
X Variable 1
y a x
2
X Variable 2
y a x
3
X Variable 3
y a x
, which are the slopes
of y with respect to parameters x 1 , x 2 , and x 3 , respectively. o Note that we use partial derivatives () rather than total derivatives ( d ) here, since y is a function of more than one variable. o A portion of the regression analysis results are shown below (image copied from Excel), with the most important cells highlighted:
Discussion: The fit is pretty good, implying that there is little scatter in the data, and the data fit well with the simple linear equation. We know this is a good fit by looking at the linear correlation coefficient ( Multiple R ), which is greater than 0.99, and the Standard Error , which is only 0.21 for y values ranging from about 4 to about 15. We can claim a successful curve fit. Comments: o In addition to random scatter in the data, there may also be cross-talk between some of the parameters. For example, y may have terms with products like x 1 x 2 , x 2 x 32 , etc., which are clearly nonlinear terms. Nevertheless, a multiple parameter linear regression analysis is often performed only locally , around the operating point, and the linear assumption is reasonably accurate, at least close to the operating point. o In addition, variables x 1 , x 2 , and x 3 may not be totally independent of each other in a real experiment. o Regression analysis with multiple variables becomes quite useful to us later in the course when we discuss optimization techniques such as response surface methodology.
Nonlinear and higher-order polynomial regression analysis Not all data are linear , and a straight line fit may not be appropriate. A good example is thermocouple voltage versus temperature. The relationship is nearly linear, but not quite; that is in fact the very reason for the necessity of thermocouple tables. For nonlinear data, some transformation tricks can be employed, using logarithms or other functions. For some data, a good curve fit can be obtained using a polynomial fit of some appropriate order. The order of a polynomial is defined by m , the maximum exponent in the x data : o zeroth-order ( m = 0) is just a constant: y^ ^ b. o first-order ( m = 1) is a constant plus a linear term: y^ ^ b^ ^ a x 1. (A first-order polynomial fit is the same as a linear least-squares fit, as we have already learned how to do.) o second-order ( m = 2) is a constant plus a linear term plus a quadratic term: y^ ^ b^ ^ a x 1^ ^ a x 2 2. (A second-order polynomial fit is often called a quadratic fit .) o third-order ( m = 3) adds a cubic term: y^ ^ b^ ^ a x 1^ ^ a x 2 2^^ ^ a x 3 3. (A third-order polynomial fit is often called a cubic fit .) o mth-order ( m > 0) adds terms following this pattern up to a (^) mxm : y^ ^ b^ ^ a x 1^ ^ a x 2 2^^ ^ a x 3^^3 ^ ...^ ^ a xm m. Excel can be manipulated to perform least-squares polynomial fits of any order m , since Excel can perform regression analysis on more than one independent variable simultaneously. The procedure is as follows: o To the right of the x column, add new columns for x^2 , x^3 , ... xm. o Perform a multiple variable regression analysis as previously, except choose all the data cells ( x , x^2 , x^3 , ... xm ) as the “Input X Range” in the Regression working window.