+ All Categories
Home > Documents > COM2023 Mathematics Methods for Computing...

COM2023 Mathematics Methods for Computing...

Date post: 06-Jun-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
66
COM2023 Mathematics Methods for Computing II Lecture 17& 18 Gianne Derks Department of Mathematics (36AA04) http://www.maths.surrey.ac.uk/Modules/COM2023 Autumn 2010 Use channel 04 on your EVS handset
Transcript
Page 1: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

COM2023

Mathematics Methods for

Computing II

Lecture 17& 18

Gianne DerksDepartment of Mathematics (36AA04)

http://www.maths.surrey.ac.uk/Modules/COM2023

Autumn 2010

Use channel 04 on your EVS handset

Page 2: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

OverviewLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Regression modelling

◆ Linear regression

● Recap

● Example

● Predictions

● R and linear regression

◆ Quadratic regression

◆ Other non-linear regression

◆ Multiple regression

Page 3: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Linear regression model (§6.1)

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 4: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Linear regression modelling: recapLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Given a data set {(x1, y1), (x2, y2), . . . , (xn, yn)}, which looks linear. Todetermine the linear regression line:

● Determine the means x = 1n

n∑

i=1

xi and y = 1n

n∑

i=1

yi;

● Determine the quantities

Sxx =

n∑

i=1

(xi − x)2 and Sxy =

n∑

i=1

(xi − x)(yi − y);

● The slope is b =Sxy

Sxx

;

● The intercept is a = y − bx;

● The least squares regression line of y on x is y = a+ bx;

● The residuals are εi = yi − (a+ bxi). Check that they are normallydistributed.

Page 5: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Forbes’ data: linear regression plotLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

22 24 26 28 30

195

200

205

210

atmospheric pressure (inch Hg)

boili

ng p

oint

(F

)

AAAAK

boiling point = 155.3 + 1.9 × atmospheric pressure

Page 6: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Forbes’ data: normal residual checkLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

To check the assumption that the noise in Forbes’ data is normallydistributed, we plot the quantiles of the residuals against the theoreticalquantiles of a normal distribution (QQ plot).

−2 −1 0 1 2

−1.

0−

0.5

0.0

0.5

Theoretical quantiles

For

bes’

dat

a qu

antil

es

Page 7: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Example

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 8: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Example: Properties of Synthetic RubberLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The following table gives the hardness and the abrasion loss of twelve differentspecimens of synthetic rubber.

Specimen Hardness x Abrasion loss y Product x× y

1 45 372 167402 55 206 113303 61 175 106754 68 113 76845 59 249 146916 74 145 107307 81 55 44558 86 45 44559 83 97 8051

10 71 219 1554911 53 221 1171312 88 64 5632

Page 9: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Properties of Synthetic Rubber: plotLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

50 60 70 80

5010

015

020

025

030

035

0

Hardness x

Abr

asio

n lo

ss y

Page 10: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = and Sxy = Txy −

1nTxTy =

andx = and y =

Thus b =Sxy

Sxx= , a = y − bx = and the line is

Page 11: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy =

andx = and y =

Thus b =Sxy

Sxx= , a = y − bx = and the line is

Page 12: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = and y =

Thus b =Sxy

Sxx= , a = y − bx = and the line is

Page 13: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = 68.66667 and y =

Thus b =Sxy

Sxx= , a = y − bx = and the line is

Page 14: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = 68.66667 and y = 163.4167.

Thus b =Sxy

Sxx= , a = y − bx = and the line is

Page 15: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = 68.66667 and y = 163.4167.

Thus b =Sxy

Sxx= −6.123, a = y − bx = and the line is

Page 16: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = 68.66667 and y = 163.4167.

Thus b =Sxy

Sxx= −6.123, a = y − bx = 583.845 and the line is

Page 17: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression lineLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The totals of the data are as follows (n=12):

Tx =

n∑

i=1

xi = 824, Ty =

n∑

i=1

yi = 1961 and Txy =

n∑

i=1

xiyi = 121120.

And,

Txx =n∑

i=1

x2i = 58792 and Tyy =n∑

i=1

y2i = 422597.

Using this, we get

Sxx = Txx −1nT 2x = 2210.667 and Sxy = Txy −

1nTxTy = −13535.33.

andx = 68.66667 and y = 163.4167.

Thus b =Sxy

Sxx= −6.123, a = y − bx = 583.845 and the line is

Abrasion loss = −6.123× Hardness+ 583.845.

Page 18: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: linear regression plotLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

50 60 70 80

5010

015

020

025

030

035

0

Hardness x

Abr

asio

n lo

ss y

�����

abrasion loss = 583.845− 6.123 × hardness

Page 19: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber: normal residual checkLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

−1.5 −1.0 −0.5 0.0 0.5 1.0 1.5

−40

−20

020

4060

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

Page 20: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Predictions (§6.1, page 44-45)

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 21: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Prediction for regression modellingLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● In order to predict a future y-value, y say, at a given x-value, x∗ say, usethe fitted model and take

y = a+ bx∗.

Page 22: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Prediction for regression modellingLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● In order to predict a future y-value, y say, at a given x-value, x∗ say, usethe fitted model and take

y = a+ bx∗.

● The fitted model can usually safely be used for interpolation, i.e., x∗ isan x-value inside the minimum and maximum sample x-values.

Page 23: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Prediction for regression modellingLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● In order to predict a future y-value, y say, at a given x-value, x∗ say, usethe fitted model and take

y = a+ bx∗.

● The fitted model can usually safely be used for interpolation, i.e., x∗ isan x-value inside the minimum and maximum sample x-values.

● Be very cautious about using the fitted straight line model forextrapolation. There is no guarantee that the model will be valid or evenpossible for such an x-value outside the sample x-values.

Page 24: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Prediction for regression modellingLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● In order to predict a future y-value, y say, at a given x-value, x∗ say, usethe fitted model and take

y = a+ bx∗.

● The fitted model can usually safely be used for interpolation, i.e., x∗ isan x-value inside the minimum and maximum sample x-values.

● Be very cautious about using the fitted straight line model forextrapolation. There is no guarantee that the model will be valid or evenpossible for such an x-value outside the sample x-values.

● The prediction is likely to be imprecise if

◆ the variation of the residuals is large;

◆ If the number of data points n is small.

Page 25: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber example, predictionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Item Hardness x Abrasion loss y

1 45 3722 55 2063 61 1754 68 1135 59 2496 74 1457 81 558 86 459 83 9710 71 21911 53 22112 88 64

Linear regression line:

y = 584− 6× x

Predictions:

● What is the expected abrasion lossat hardness 50?

Page 26: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber example, predictionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Item Hardness x Abrasion loss y

1 45 3722 55 2063 61 1754 68 1135 59 2496 74 1457 81 558 86 459 83 9710 71 21911 53 22112 88 64

Linear regression line:

y = 584− 6× x

Predictions:

● What is the expected abrasion lossat hardness 50?Take x = 50 in regression line,then y = 284, thus the expectedabrasion loss is 284.

Page 27: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber example, predictionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Item Hardness x Abrasion loss y

1 45 3722 55 2063 61 1754 68 1135 59 2496 74 1457 81 558 86 459 83 9710 71 21911 53 22112 88 64

Linear regression line:

y = 584− 6× x

Predictions:

● What is the expected abrasion lossat hardness 50?Take x = 50 in regression line,then y = 284, thus the expectedabrasion loss is 284.

● What is the expected abrabsionloss at hardness 100?

Page 28: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Synthetic Rubber example, predictionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Item Hardness x Abrasion loss y

1 45 3722 55 2063 61 1754 68 1135 59 2496 74 1457 81 558 86 459 83 9710 71 21911 53 22112 88 64

Linear regression line:

y = 584− 6× x

Predictions:

● What is the expected abrasion lossat hardness 50?Take x = 50 in regression line,then y = 284, thus the expectedabrasion loss is 284.

● What is the expected abrabsionloss at hardness 100?We are not sure if the model isvalid for hardness less than 45 orlarger than 88. The answer fromthe regression line is y = 584 −600 = −16, hence not realistic.

Page 29: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Regression and Matlab (§6.1, p.

41-43)

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 30: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

“Women” data set in Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Weight and height of a sample group of women:

58 60 62 64 66 68 70 72

120

130

140

150

160

height

wei

ght

Page 31: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Analysis of data sets in Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● The weight vector is such that weight[i] lbs is the average weight ofwomen who are height[i] inches tall.

55 60 65 70 75100

120

140

160

180W

eigh

t (lb

)

Height (in)

Page 32: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Analysis of data sets in Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● The weight vector is such that weight[i] lbs is the average weight ofwomen who are height[i] inches tall.

● Check the correlation and rank correlation:

◆ corr(height,weight)=0.9955

◆ corr(height,weight,’Type’,’Spearman’)=1.

Page 33: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Analysis of data sets in Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● The weight vector is such that weight[i] lbs is the average weight ofwomen who are height[i] inches tall.

● Check the correlation and rank correlation:

◆ corr(height,weight)=0.9955

◆ corr(height,weight,’Type’,’Spearman’)=1.

● Next we fit a linear model y = a+ bx with x the height, y the weight.

◆ myfit = polyfit(height,weight,1)

◆ This gives myfit = 3.4500 -87.5167,hence the coefficients a and b with a = −87.52 and b = 3.45.

◆ Thus the linear regression line is “weight = 3.45 × height - 87.52”

Page 34: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Analysis of data sets in Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● The weight vector is such that weight[i] lbs is the average weight ofwomen who are height[i] inches tall.

● Check the correlation and rank correlation:

◆ corr(height,weight)=0.9955

◆ corr(height,weight,’Type’,’Spearman’)=1.

● Next we fit a linear model y = a+ bx with x the height, y the weight.

◆ myfit = polyfit(height,weight,1)

◆ This gives myfit = 3.4500 -87.5167,hence the coefficients a and b with a = −87.52 and b = 3.45.

◆ Thus the linear regression line is “weight = 3.45 × height - 87.52”

● To get analyse the residuals, use

◆ weight approx = polyval(myfit,height);the model y-values.

◆ residuals = weight-weight approx; gives the residuals.

◆ mean(residuals) gives 2.8422e-14 and var(residuals)=2.1595

Page 35: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Plot for the “women” dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

scatter(height,weight); hold on; plot(height,weight approx)

title(’American women aged 30-39’); xlabel(’Height (in)’);

ylabel(’Weight (lb)’);

58 60 62 64 66 68 70 72

120

130

140

150

160

American women aged 30−39

Height (in)

Wei

ght (

lb)

Page 36: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Plot for the residuals in “women” dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

figure; scatter(height,residuals); hold on;

plot(height,zeros(length(height))); title(’Residual Plot’);

xlabel(’Height (in)’); ylabel(’Residual (lb)’);

58 60 62 64 66 68 70 72

−1

01

23

Residual Plot

Height (in)

Res

idua

l (lb

)

Page 37: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Normal check of residuals “women” dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

figure; qqplot(residuals)

−1 0 1

−1

01

23

normal Q−Q plot for women data

Theoretical Quantiles

Qua

ntile

s w

omen

dat

a

Page 38: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Quadratic regression (§6.2)

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 39: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Non-linear regression and women dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Looking at the plots obtained from fitting a straight line to the women data,it appears that a non-linear relationship may be more appropriate.

Assume that the appropriate relationship between the variables “weight” and“height” is:

weight = α+ β × height + γ × (height)2 + noise

We will try to fit a quadratic of the form:

weight = a+ b× height + c× (height)2

to the data, where a, b and c have to be found by using the observations.This will be done by using procedures in Matlab.

Page 40: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Using Matlab to fit a quadraticLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Using Matlab, we fit a quadratic of the form

weight = a+ b× height + c× (height)2

to the women data.

Page 41: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Using Matlab to fit a quadraticLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Using Matlab, we fit a quadratic of the form

weight = a+ b× height + c× (height)2

to the women data.

Matlab can fit any order polynomial with polyfit, so now we just use theoption “2” for a second order polynomial:

myfit2 = polyfit(height,weight,2)

This gives c = 0.0831, b = −7.3483 and a = 261.8782.

Page 42: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Using Matlab to fit a quadraticLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Using Matlab, we fit a quadratic of the form

weight = a+ b× height + c× (height)2

to the women data.

Matlab can fit any order polynomial with polyfit, so now we just use theoption “2” for a second order polynomial:

myfit2 = polyfit(height,weight,2)

This gives c = 0.0831, b = −7.3483 and a = 261.8782.

Thus the approximate quadratic is

weight = 261.878184− 7.348319× height+ 0.083064× (height)2

Page 43: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Quadratic & linear fit for women dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

58 60 62 64 66 68 70 72

120

130

140

150

160

Height (in)

Wei

ght (

lb)

Fitted quadratic modelFitted linear model

Page 44: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Residuals of quadratic fit for women dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

58 60 62 64 66 68 70 72

−0.

4−

0.2

0.0

0.2

0.4

0.6

Residual plot

height

resi

dual

s

−1 0 1

−0.

4−

0.2

0.0

0.2

0.4

0.6

Normal Q−Q Plot

Theoretical Quantiles

quan

tiles

of r

esid

uals

of q

uadr

atic

fit

Page 45: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Another quadratic fitting exampleLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

We “polute” the quadratic y = (x−6)2

4 = 9− 3x+ x2

4 by adding noise and getthe following dataset:

x 1 2 3 4 5 6 7 8 9 10 11 12 13 14

y 7.1 4.2 0.3 3.0 -1.0 -1.5 0.3 -1.0 1.5 1.4 6.3 8.5 13.5 15.4

y 6.3 4.0 2.3 1.0 0.3 0.0 0.3 1.0 2.3 4.0 6.3 9.0 12.3 16.0

Here y is the “poluted” data and y are the “pure” values.

This data is obtained in Matlab by using the following commands:

x = 1:14;

y = 0.25*(x-6).^2 + normrnd(0,1,1,14);

yhat = 0.25*(x-6).^2

Page 46: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Fitting the quadratic dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Next we fit a quadratic by using:

myfit = polyfit(x,y,2)

approx y = polyval(myfit,x)

This gives the fitted curve y = 10.3− 3.6x+ 0.29x2 and a set of values y(x),called approx y.

Page 47: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Fitting the quadratic dataLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Next we fit a quadratic by using:

myfit = polyfit(x,y,2)

approx y = polyval(myfit,x)

This gives the fitted curve y = 10.3− 3.6x+ 0.29x2 and a set of values y(x),called approx y.

To plot this, we use

scatter(x,y); hold on; plot(x,approx y);

plot(x,yhat,’--’);

title(’Quadratic regression example’);legend(’Data points’,’Fitted quadratic’,’Original

quadratic’,’Location’, ’West’)

Page 48: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Fitted & original plot of quadraticLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

2 4 6 8 10 12 14

05

1015

Quadratic regression example

x

y

Fitted quadraticOriginal quadratic

Page 49: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Residuals in quadratic regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Finally we analyse the residuals:residuals = y - approx y;

subplot(1,2,1); scatter(x,residuals); plot(x,zeros(14));

title(’Residual plot’);

subplot(1,2,2);

qqplot(residuals)

2 4 6 8 10 12 14

−1

01

2

Residual plot

x

Res

idua

ls

−1 0 1

−1

01

2

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

Page 50: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Regression and Matlab

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Given a data set {(x1, y1), (x2, y2), . . . , (xn, yn)}. Using Matlab:

● The coefficients of a polynomial approximation of order n is obtained by

myfit = polyfit(x,y,n)

● The approximate y values are obtained by

approx = polyval(myfit,x)

Page 51: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Multiple regression (§6.2)

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 52: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Data with three componentsLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

The trees data set gives the relation between the height, volume and girth(diameter) of 31 cherry trees.

4.1 4.2 4.3 4.4 4.5 4.62

2.5

3

3.5

4

4.5

5

log(height)

log(

volu

me)

2 2.5 3 3.52

2.5

3

3.5

4

4.5

5

log(girth)

log(

volu

me)

2 2.5 3 3.54

4.2

4.4

4.6

4.8

log(girth)

log(

heig

ht)

corr(Volume’,Height’) = 0.5982

corr(Girth’,Volume’) = 0.9671

corr(Girth’,Height’) = 0.5193

corr(log(Volume)’,log(Height)’)

= 0.6486

corr(log(Girth)’,log(Volume)’)

= 0.9767

corr(log(Girth)’,log(Height)’)

= 0.5302

Page 53: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Linear regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

First linear regression on the data concerning the girth and volume, usinglog(girth) and log(volume), ignoring any information about the height.

2.2 2.4 2.6 2.8 3.0

2.5

3.0

3.5

4.0

log.g

log.

v

Page 54: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Multiple regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Next multiple regression, writing log(volume) as a linear combination oflog(girth) and log(height). Shown are the observed volume against the fittedvolume

10 20 30 40 50 60 70

1020

3040

5060

7080

observed volume

fitte

d vo

lum

e

Page 55: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Comparing residuals (1)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

A plot of the residuals of the linear (left) and multiple regression (right)models

2.5 3.0 3.5 4.0

−0.

2−

0.1

0.0

0.1

0.2

Residuals Linear Regression

log(volume)

resi

dual

s

2.5 3.0 3.5 4.0

−0.

15−

0.10

−0.

050.

000.

050.

10

Residuals Multiple Regression

log(volume)

resi

dual

s

The residuals for the linear regression are mainly between −0.2 and +0.2 andthe residuals for the multiple regression are mainly between −0.1 and +0.1.

Page 56: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Comparing residuals (2)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Finally we compare the residuals of the linear regression (left) and multipleregression (right) in normal qqplots:

−2 −1 0 1 2

−0.

2−

0.1

0.0

0.1

0.2

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

−2 −1 0 1 2

−0.

15−

0.10

−0.

050.

000.

050.

10

Normal QQ plot multiple regression

Theoretical Quantiles

Sam

ple

Qua

ntile

s

Page 57: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Matlab commands used (1)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Overview plots:scatter(log(Girth),log(Height));

scatter(log(Height),log(Volume));

scatter(log(Girth),log(Volume));

4.1 4.2 4.3 4.4 4.5 4.62

2.5

3

3.5

4

4.5

5

log(height)

log(

volu

me)

2 2.5 3 3.52

2.5

3

3.5

4

4.5

5

log(girth)

log(

volu

me)

2 2.5 3 3.54

4.2

4.4

4.6

4.8

log(girth)

log(

heig

ht)

Page 58: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Matlab commands used (1)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Overview plots:scatter(log(Girth),log(Height));

scatter(log(Height),log(Volume));

scatter(log(Girth),log(Volume));

● Linear regression:myfit lin = polyfit(log(Girth),log(Volume),1);

approx lin = polyval(myfit lin,Girth)

scatter(log(Girth),log(Volume)); plot(Girth,approx lin);

2.2 2.4 2.6 2.8 3.0

2.5

3.0

3.5

4.0

log.g

log.

v

Page 59: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Matlab commands used (2)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Multiple regression:X = [ones(length(Girth),1),Girth,Height];

mul = regress(Volume,X);

approx mul = mul(1) + mul(2)*Girth + mul(3)*Height;

scatter(volume,exp(approx mul))

plot([Volume(1),Volume(1)],[Volume(end),Volume(end)])

10 20 30 40 50 60 70

1020

3040

5060

7080

observed volume

fitte

d vo

lum

e

Page 60: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Matlab commands used (3)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Residuals:res lin = log(Volume)-log(approx lin);

res mul = log(Volume)-log(approx mul);

subplot(1,2,1); plot(log(Volume),res lin);

subplot(1,2,2); plot(log(Volume),res mul);

2.5 3.0 3.5 4.0

−0.

2−

0.1

0.0

0.1

0.2

Residuals Linear Regression

log(volume)

resi

dual

s

2.5 3.0 3.5 4.0

−0.

15−

0.10

−0.

050.

000.

050.

10

Residuals Multiple Regression

log(volume)

resi

dual

s

Page 61: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Matlab commands used (3)Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

● Residuals:res lin = log(Volume)-log(approx lin);

res mul = log(Volume)-log(approx mul);

subplot(1,2,1); plot(log(Volume),res lin);

subplot(1,2,2); plot(log(Volume),res mul);

● Quantum-quantum plots:subplot(1,2,1); qqplot(res lin);

subplot(1,2,1); qqplot(res mul);

−2 −1 0 1 2

−0.

2−

0.1

0.0

0.1

0.2

Normal Q−Q Plot

Theoretical Quantiles

Sam

ple

Qua

ntile

s

−2 −1 0 1 2

−0.

15−

0.10

−0.

050.

000.

050.

10

Normal QQ plot multiple regression

Theoretical Quantiles

Sam

ple

Qua

ntile

s

Page 62: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Other nonlinear regression

Linear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Page 63: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Nonlinear regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Some nonlinear regression can be done by introducing new variables, whichmake the relation linear (and hence we can use the simple lm command in Ras before). For example:

● The exponential regression model: Y = exp(α+ βX).Note that log Y = α+ βX, hence with U = log Y , we have a linearrelation between U and X. This is U = α+ βX;

Page 64: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Nonlinear regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Some nonlinear regression can be done by introducing new variables, whichmake the relation linear (and hence we can use the simple lm command in Ras before). For example:

● The exponential regression model: Y = exp(α+ βX).Note that log Y = α+ βX, hence with U = log Y , we have a linearrelation between U and X. This is U = α+ βX;

● The logarithmic regression model: Y = log(α+ βX).Note that expY = α+ βX, hence with V = expY , we have a linearrelation between V and X. This is V = α+ βX;

Page 65: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Nonlinear regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Some nonlinear regression can be done by introducing new variables, whichmake the relation linear (and hence we can use the simple lm command in Ras before). For example:

● The exponential regression model: Y = exp(α+ βX).Note that log Y = α+ βX, hence with U = log Y , we have a linearrelation between U and X. This is U = α+ βX;

● The logarithmic regression model: Y = log(α+ βX).Note that expY = α+ βX, hence with V = expY , we have a linearrelation between V and X. This is V = α+ βX;

● The power regression model: Y = αXβ .Note that log Y = logα+ β logX, hence with W = log Y andZ = logX, we have a linear relation between W and Z. This isW = logα+ βZ.

Page 66: COM2023 Mathematics Methods for Computing IIpersonal.maths.surrey.ac.uk/st/G.Derks/COM2023/lecture17.pdf · Linearregression Example Predictions Matlab Quadraticregression Multipleregression

Nonlinear regressionLinear regression Example Predictions Matlab Quadratic regression Multiple regression Other nonlinear regression

Some nonlinear regression can be done by introducing new variables, whichmake the relation linear (and hence we can use the simple lm command in Ras before). For example:

● The exponential regression model: Y = exp(α+ βX).Note that log Y = α+ βX, hence with U = log Y , we have a linearrelation between U and X. This is U = α+ βX;

● The logarithmic regression model: Y = log(α+ βX).Note that expY = α+ βX, hence with V = expY , we have a linearrelation between V and X. This is V = α+ βX;

● The power regression model: Y = αXβ .Note that log Y = logα+ β logX, hence with W = log Y andZ = logX, we have a linear relation between W and Z. This isW = logα+ βZ.

However, most nonlinear regression models are more complicated.


Recommended