Part 4
Chapter
14&15
Linear
Regression
PowerPoints organized by Dr. Michael R. Gustafson II, Duke University
All images copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter Objectives
• Familiarizing yourself with some basic descriptive
statistics and the normal distribution.
• Knowing how to compute the slope and intercept of a
best fit straight line with linear regression.
• Knowing how to compute and understand the meaning
of the coefficient of determination and the standard
error of the estimate.
• Understanding how to use transformations to linearize
nonlinear equations so that they can be fit with linear
regression.
• Knowing how to implement linear regression with
MATLAB.
• Knowing how to implement multiple linear regression.
Statistics Review
Measure of Location
• Arithmetic mean: the sum of the individual
data points (yi) divided by the number of
points n:
y
yi
n
• Median: the midpoint of a group of data.
• Mode: the value
that occurs most frequently
in a group of data.
Statistics Review
Measures of Spread
• Standard deviation:
St
sy
n 1
where St is the sum of the squares of the data residuals:
St yi y
2
and n-1 is referred to as the degrees of freedom.
• Variance:
yi y yi yi / n
2
2
2
sy
2
n 1 n 1
• Coefficient of variation:
sy
c.v. 100%
y
Normal Distribution
Descriptive Statistics in MATLAB
• MATLAB has several built-in commands to
compute and display descriptive statistics.
Assuming some column vector s:
– mean(s), median(s), mode(s)
• Calculate the mean, median, and mode of s. mode is a part of
the statistics toolbox.
– min(s), max(s)
• Calculate the minimum and maximum value in s.
– var(s), std(s)
• Calculate the variance and standard deviation of s
• Note - if a matrix is given, the statistics will be
returned for each column.
Histograms in MATLAB
• [n, x] = hist(s)
– Determine the number of elements in each bin of data in
s. x is a vector containing the center values of the bins.
• [n, x] = hist(s, m)
– Determine the number of elements in each bin of data in
s using m bins. x will contain the centers of the bins.
The default case is m=10
• hist(s, x) or hist(s, m) or hist(s)
– With no output arguments, hist will actually produce a
histogram.
Histogram Example
Linear Least-Squares Regression
• Linear least-squares regression is a method to
determine the “best” coefficients in a linear model
for given data set.
• “Best” for least-squares regression means
minimizing the sum of the squares of the estimate
residuals. For a straight line model, this gives:
n n
Sr e yi a0 a1 xi
2 2
i
i1 i1
• This method will yield a unique line for a given set
of data.
Least-Squares Fit of a Straight Line
• Using the model:
y a0 a1x
the slope and intercept producing the best fit
can be found using:
n xi yi xi yi
a1
n x x
2
2
i i
a0 y a1 x
Example
V F n xi yi xi yi 8312850 3605135
a1 19.47024
(m/s) (N)
n x x 820400 360
2 2
2
i i
i xi yi (xi)2 x iy i
a0 y a1 x 641.875 19.47024 45 234.2857
1 10 25 100 250
2 20 70 400 1400
3 30 380 900 11400 Fest 234.2857 19.47024v
4 40 550 1600 22000
5 50 610 2500 30500
6 60 1220 3600 73200
7 70 830 4900 58100
8 80 1450 6400 116000
360 5135 20400 312850
Quantification of Error
• Recall for a straight line, the sum of the
squares of the estimate residuals:
n n
Sr e yi a0 a1 xi
2 2
i
i1 i1
• Standard error of the estimate:
Sr
s y/ x
n2
Standard Error of the Estimate
• Regression data showing (a) the spread of data around the
mean of the dependent data and (b) the spread of the data
around the best fit line:
• The reduction in spread represents the improvement due to
linear regression.
Coefficient of Determination
• The coefficient of determination r2 is the difference between
the sum of the squares of the data residuals and the sum of
the squares of the estimate residuals, normalized by the
sum of the squares of the data residuals:
St Sr
r
2
St
• r2 represents the percentage of the original uncertainty
explained by the model.
• For a perfectfit, Sr=0 and r2=1.
• If r2=0, there is no improvement over simply picking the
mean.
• If r2<0, the model is worse than simply picking the mean!
Example
V F
(m/s) (N)
Fest 234.2857 19.47024v
St yi y 1808297
2
i xi yi a0+a1xi (yi- ȳ)2 (yi-a0-a1xi)2
Sr yi a0 a1 xi 216118
1 10 25 -39.58 380535 4171 2
2 20 70 155.12 327041 7245
1808297
3 30 380 349.82 68579 911 sy 508.26
8 1
4 40 550 544.52 8441 30 216118
s y/ x 189.79
5 50 610 739.23 1016 16699 82
6 60 1220 933.93 334229 81837 1808297 216118
r2 0.8805
7 70 830 1128.63 35391 89180 1808297
8 80 1450 1323.33 653066 16044 88.05% of the original uncertainty
has been explained by the
360 5135 1808297 216118
linear model
Nonlinear Relationships
• Linear regression is predicated on the fact
that the relationship between the dependent
and independent variables is linear - this is
not always the case.
• Three common examples are:
exponential : y 1e1 x
power : y 2 x 2
x
saturation- growth - rate : y 3
3 x
Linearization of Nonlinear
Relationships
• One option for finding the coefficients for a
nonlinear fit is to linearize it. For the three
common models, this may involve taking
logarithms or inversion:
M odel Nonlinear Linearized
exponential : y 1e1 x ln y ln 1 1 x
power : y 2 x 2 log y log 2 2 log x
x 1 1 3 1
saturation- growth - rate : y 3
3 x y 3 3 x
Transformation Examples
Linear Regression Program
MATLAB Functions
• MATLAB has a built-in function polyfit that fits a
least-squares nth order polynomial to data:
– p = polyfit(x, y, n)
• x: independent data
• y: dependent data
• n: order of polynomial to fit
• p: coefficients of polynomial
f(x)=p1xn+p2xn-1+…+pnx+pn+1
• MATLAB’s polyval command can be used to
compute a value using the coefficients.
– y = polyval(p, x)
Multiple Linear Regression
• Another useful extension of
linear regression is the case
where y is a linear function of
two or more independent
variables:
y a0 a1x1 a2 x2 am xm
• Again, the best fit is obtained by
minimizing the sum of the
squares of the estimate
residuals:
n n
Sr ei2 yi a0 a1 x1,i a2 x2,i am xm,i
2
i1 i1