36
Section 4.2 Fitting Curves and Surfaces by Least Squares Surfaces by Least Squares

Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Section 4.2 Fitting Curves and Surfaces by Least SquaresSurfaces by Least Squares

Page 2: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

4 2 1 Curve Fitting4.2.1 Curve Fitting

In many cases the relationship of y to x is not a straight lineg

Page 3: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

To fit a curve to the data one can

• Fit a nonlinear function directly to the data. .Fit a nonlinear function directly to the data. .

• Rescale, transform x or y to make the l i hi lirelationship linear.

• Fit a polynomial function to the data.p y

Page 4: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

For a uniform fluid – light should decay as an exponential function of g y p

depth

– Given data on depth x and light intensity yGiven data on depth, x, and light intensity, y,

h d h ld l k lik i l f iThe data should look like an exponential function.

xb1xbaey 1≈

Page 5: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

1. Direct Fitting1. Direct Fitting

We could fit the equation directly by i i i i ( )∑ 2xbminimizing

If the residuals are normal with equal variance

( )∑ − 1 ixbi aey

qthen

this is an optimal way to go– this is an optimal way to go.

We could use a "nonlinear regression" program to accomplish this.p g p

Page 6: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

This can also be accomplish with the "Solver" add‐in in Excel.

We won't be doing such curves this way in Stat 3411.

But very often in situations such as exponentially y p ycurved data the variances are not constant.

• One way to handle nonconstant variance is "weighted regression".– We give larger weight to residuals where variance is small

and we are more sure of where the fitted line should go.

• We won't be doing this in Stat 3411.

Page 7: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

2. Transforming

Often with an exponential relationship th l l h i d– the larger values have more variance and

– the smaller values have smaller variances.

In this case it can work better to rescale the data

)l ()l (

1

baey xb≈

11)ln()ln(xbbyyxbay

new +=′=

+=

Often values in the log scale have fairly constant variances.110 xbbyy +==

Page 8: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Original Scale

30

10

15

20

25

Y

0

5

10

0 2 4 6 8 10 12 14 16 18 20

X

A residual plot gives us a magnified view of the increasing variance and curvature.

Original Scale

6

8

10

This residual plot indicates 2 problems with this linear least squares fit.The relationship is not linear

d d b h h

-2

0

2

4

0 2 4 6 8 10 12 14 16 18

Res

idua

l

‐Indicated by the curvature in the residual plot

The variance is not constantS l t i 't th b t

-6

-4

2

Predicted

‐So least squares isn't the best approach even if we handle the nonlinearity.

Page 9: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Let us try to transform the data.

(A log transformation)(A log transformation)

Page 10: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Ln Scale

3

3.5

1 5

2

2.5

3

Ln(Y)

0

0.5

1

1.5

00 5 10 15 20

X

Fit a line to these data. Here it makes sense for all residuals to count equally.q y

Page 11: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Then back‐transform to the original scale

Original Scale

25

30

15

20

Y

5

10

00 2 4 6 8 10 12 14 16 18 20

X

Page 12: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

ExtrapolationExtrapolation

• Predicted values are less reliable if you are extrapolating outside the range of x values in p g gthe data set.

Page 13: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

• Extrapolations are more reliable if one has an equation from a physical model.q p y

• For example physical models predict that in a uniform fluid light will decay exponentiallyuniform fluid light will decay exponentially with depth.

Page 14: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

• CE4505SurfaceWaterQuality Engineering

• http://www.cee.mtu.edu/~mtauer/classes/ce4505/lecture3.htm

Page 15: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

3. PolynomialsWhen we have no theory to guide us, we can often fit the curve in the range of observed x values with a polynomial function.

For example a cubic polynomial would be22 bbbb +++

Thi i li f ti f th th i bl

23

2210 xbxbxbby +++≈

• This is linear function for the three variables3

32

11 xxxxxx ===

• Excel and other programs fit these sorts of3322110 xbxbxbby +++≈

Excel and other programs fit these sorts of polynomial models.

Page 16: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

In Excel

• Add new columns for x2 and x3Add new columns for x and x

• Run a regression using 3 columns for the x i blvariables

– x , x2 , and x3

– All three columns need to be next to each other in Excel.Excel.

Page 17: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Gi h fi d f i h k fGiven the fitted function, we want to check for an adequate fit by plotting the data along with the fitted function.

Notice that replication is useful for assessingis useful for assessing how well the functional form fits the data.

With replication here we can tell that the quadratic polynomial is under‐fitting the y values at x = 2.

Page 18: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Residual PlotsResidual Plots

ld l id l i hWe would plot residuals in the same ways suggested in section 4.1

– Versus predicted values – Versus x– In run order– Versus other potentially influential variables, e.g.

technician– Normal Plot of residuals

Page 19: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Section 4.2.2 Surface Fitting by Least Squares

In many situations the response variable, y, is affected by more than one x variable.y

For example we could have (see problem 21 inFor example we could have (see problem 21 in the Exercises)

y = armor strengthy gxl = thicknessx2 = Brinell hardnessx2 Brinell hardness

The simplest model to fit in this case is a linearThe simplest model to fit in this case is a linear model in both variables

xbxbby ++≈ 22110 xbxbby ++≈

Page 20: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

• In this case we are fitting a plane to the 3‐D points.

This sort of linear model with more than one x variable is called "multiple linear regression"

Page 21: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

For this function fitted y values for a fixedFor this function fitted y values for a fixed value of x1 follow parallel lines when plotted

i tagainst x2.

Page 22: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

xbxbby ++≈ iii xbxbby 22110 ++≈

Again, we find b0 , b1 , b2 to minimize

• the sum of squared deviations of the fitted qvalues,

• the residual or error sum of squares• the residual or error sum of squares.

• ( )222110min∑ ++− iii xbxbbyThis minimization can be solved with explicit matrix

formulas.

Many programs including Excel have the capability.

Page 23: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

There can be any number of x variables, for examplep

xbxbxbby +++ 3322110 xbxbxbby +++≈

Page 24: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

It is also possible to introduce curvature into f th i blone or more of the variables

For example p

326235

2243322110 xxbxbxbxbxbxbby ++++++≈

Page 25: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

• Product terms (called interactions) in the model gi e nonparallel conto rsmodel give nonparallel contours.

Page 26: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

ResidualsResiduals are as always .ˆi i ie y y= −

R2 is still the percent of variation explained by the regression.

• How better do we do – using the regression equation versus– using the mean to predict all values– using the mean to predict all values

( ) ( )100

ˆ 222 ∗

−−− ∑∑ yyyyR iii

( )1002 ∗

−=

∑ yyR

i

R2 is also the square of the correlation between .ˆand i iy y

Page 27: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

To check how well the model fits we • plot residuals in the same ways as earlier

– Versus predicted values – Versus x– In run order– Versus other potentially influential variables, e.g.

technicianNormal Plot of residuals– Normal Plot of residuals

• In addition we would plot the residualsIn addition we would plot the residuals– Versus each x variable

• Versus x11• Versus x2• Versus x3• An so on• An so on

Page 28: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Final Choice of ModelFinal Choice of Model

We have many choices for our final model including

• Transformations of many kindsy

• Polynomials

Our final choice is guided by 3 considerations

• Sensible engineering models and experience

• How well the model fits the data• How well the model fits the data.

• The simplicity of the model.

Page 29: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

1. Sensible engineering models

– Whenever possible use models that make intuitiveWhenever possible use models that make intuitive physical sense

Good mathematical models based on physics– Good mathematical models based on physics makes extrapolation much less dangerous.

H i h i ll i t t bl ti t d– Having physically interpretable estimated parameters

Page 30: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

2. Obviously, the model needs to fit the data reasonably well.y

Al l t d t d id l i i f ti– Always plot data and residuals in informative ways.

Page 31: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

h i li i f h d l3. The simplicity of the model.

A very complex model relative to the amount of dataof data– Can fit the data well

But not predict new points well particularly– But not predict new points well, particularly extrapolated points."Overfitting" the model– "Overfitting" the model

Learn more in Stat 5511 Regression Analysis

Page 32: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Overfitting Polynomial

100

120

60

80

Y

0

20

40

The fourth order polynomial in pink

0 1 2 3 4 5 6

X

p y p• fits the data exactly, • but likely would not work well for predicting for x=0.5 or x=1.5

The linear fit in blue• likely predicts new points better.

f f l l ll l d l f hOften transforming to a log scale allows simpler models to fit the data reasonably well.

Page 33: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

ExtrapolatingExtrapolating

With multiple x variables we need to be careful about extrapolating beyond the region p g y gof x variables for the observed data.

We can't necessarily tell that a combination ofWe can t necessarily tell that a combination of X1 and X2 is unusual just by seeing – separately

– where the new value of Xl falls amongst the l gmeasured Xl values

– where the new value of X2 falls amongst thewhere the new value of X2 falls amongst the measured X2 values

Page 34: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the
Page 35: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

Some Study Questions:Some Study Questions:

• You run a regression model fitting y with x1, x2, and x3. What five residual plots would you need to check in order see if the data areresidual plots would you need to check in order see if the data are fit well by the model?

Wh t i t l ti ? Wh i t l ti t ti ll i k ?• What is extrapolation? Why is extrapolation potentially risky?

• What can we do to reduce potential risks in extrapolating?p p g

• We run a regression predicting y = armor strength from xl = thickness and x = Brinell hardness and obtain R2 = 80% Explain inthickness and x2 = Brinell hardness and obtain R = 80%. Explain in words for a manager who has not seen R2 values what it means for R2 to be 80%.

• What is the problem with fitting a complex model to a small data set in order to fit the data points very exactly?

Page 36: Section 4.2 Fitting Curves and Surfaces by LeastSurfaces ...mduan/stat3411/ch4_2.pdf · Surfaces by LeastSurfaces by Least Squares . ... quadratic polynomial is under‐fitting the

The following residual plot is for predicting product lifetimes from using x1 and x2

100

60

80

0

20

40

Res

idua

l

-40

-20

-40 -20 0 20 40 60 80 100 120

PredictedPredicted

What 3 problems with our fitting method and results are indicated by this residual plot?What might you do to try finding a better fit to the data?What might you do to try finding a better fit to the data?