introduction to data science - winter semester 2019/20 › mathematik › numa › lehre › ... ·...

47
Introduction to Data Science Winter Semester 2019/20 Oliver Ernst TU Chemnitz, Fakultät für Mathematik, Professur Numerische Mathematik Lecture Slides

Upload: others

Post on 25-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Introduction to Data ScienceWinter Semester 2019/20

Oliver Ernst

TU Chemnitz, Fakultät für Mathematik, Professur Numerische Mathematik

Lecture Slides

Page 2: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents I

1 What is Data Science?

2 Learning Theory2.1 What is Statistical Learning?2.2 Assessing Model Accuracy

3 Linear Regression3.1 Simple Linear Regression3.2 Multiple Linear Regression3.3 Other Considerations in the Regression Model3.4 Revisiting the Marketing Data Questions3.5 Linear Regression vs. K -Nearest Neighbors

4 Classification4.1 Overview of Classification4.2 Why Not Linear Regression?4.3 Logistic Regression4.4 Linear Discriminant Analysis4.5 A Comparison of Classification Methods

5 Resampling MethodsOliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 3 / 463

Page 3: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents II

5.1 Cross Validation5.2 The Bootstrap

6 Linear Model Selection and Regularization6.1 Subset Selection6.2 Shrinkage Methods6.3 Dimension Reduction Methods6.4 Considerations in High Dimensions6.5 Miscellanea

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

8 Tree-Based Methods8.1 Decision Tree Fundamentals8.2 Bagging, Random Forests and Boosting

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 4 / 463

Page 4: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents III

9 Unsupervised Learning9.1 Principal Components Analysis9.2 Clustering Methods

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 5 / 463

Page 5: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 336 / 463

Page 6: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsChapter overview

• Despite the benefits of simplicity and interpretability of the standard linearmodel for regression, it will suffer from large bias if the model generatingthe data depends nonlinearly on the predictors.

• In this chapter we explore methods which make the linear regression modelmore flexible by using linear combinations of nonlinear functions, specifi-cally

1 polynomial and piecewise polynomial functions,2 piecewise constant functions,3 piecewise piecewise polynomial functions with penalty terms and4 generalized additive model functions

of the predictors.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 337 / 463

Page 7: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 338 / 463

Page 8: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

• For univariate models, polynomial regression replaces the simple linearregression model

Y = β0 + β1X + ε

with a polynomial of degree d > 1 in the predictor variable

Y = β0 + β1X + β2X 2 + · · ·+ βdX d + ε.

• High degree polynomials are often difficult to handle due to their oscillato-ry behavior and their unboundedness for large arguments, so that degreeshigher than 4 can become problematic if employed naively.

• Example: Wage data set: income and demographic information for maleswho reside in the central Atlantic region of the United States.Fit response wage [in $ 1000] to predictor age by LS using a polynomial ofdegree d = 4.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 339 / 463

Page 9: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

• For univariate models, polynomial regression replaces the simple linearregression model

Y = β0 + β1X + ε

with a polynomial of degree d > 1 in the predictor variable

Y = β0 + β1X + β2X 2 + · · ·+ βdX d + ε.

• High degree polynomials are often difficult to handle due to their oscillato-ry behavior and their unboundedness for large arguments, so that degreeshigher than 4 can become problematic if employed naively.

• Example: Wage data set: income and demographic information for maleswho reside in the central Atlantic region of the United States.Fit response wage [in $ 1000] to predictor age by LS using a polynomial ofdegree d = 4.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 339 / 463

Page 10: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

• For univariate models, polynomial regression replaces the simple linearregression model

Y = β0 + β1X + ε

with a polynomial of degree d > 1 in the predictor variable

Y = β0 + β1X + β2X 2 + · · ·+ βdX d + ε.

• High degree polynomials are often difficult to handle due to their oscillato-ry behavior and their unboundedness for large arguments, so that degreeshigher than 4 can become problematic if employed naively.

• Example: Wage data set: income and demographic information for maleswho reside in the central Atlantic region of the United States.Fit response wage [in $ 1000] to predictor age by LS using a polynomial ofdegree d = 4.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 339 / 463

Page 11: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Degree−4 Polynomial

20 30 40 50 60 70 80

0.0

00

.05

0.1

00

.15

0.2

0Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Left: Polynomial (d = 4) LS fit of wage against age (solid blue) with 95% confidenceinterval (blue dashed). Right: Model of event {wage > 250} using logistic regressionwith d = 4, fitted posterior probability (solid blue) with 95% confidence interval (bluedashed).

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 340 / 463

Page 12: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

Left panel in previous figure:• Given fit of particular age value x0

f (x0) = β0 + β1x0 + β2x20 + β3x30 + β4x40 ,

use variance/covariance estimates of βj to estimate variance of f (x0).

• If C ∈ R5×5 is the estimated covariance matrix of the βj , then

Var f (x0) = `>0 C`0, where `0 = (1, x0, x20 , . . . , x40 )>.

• Estimated pointwise standard error of f (x0) is the square root of thisvariance.

• Repeat calculation for all x0, plotting ±2× standard error (corresponds to≈ 95% confidence interval for normally distributed errors) yields dashedlines.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 341 / 463

Page 13: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

Left panel in previous figure:• Given fit of particular age value x0

f (x0) = β0 + β1x0 + β2x20 + β3x30 + β4x40 ,

use variance/covariance estimates of βj to estimate variance of f (x0).• If C ∈ R5×5 is the estimated covariance matrix of the βj , then

Var f (x0) = `>0 C`0, where `0 = (1, x0, x20 , . . . , x40 )>.

• Estimated pointwise standard error of f (x0) is the square root of thisvariance.

• Repeat calculation for all x0, plotting ±2× standard error (corresponds to≈ 95% confidence interval for normally distributed errors) yields dashedlines.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 341 / 463

Page 14: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

Left panel in previous figure:• Given fit of particular age value x0

f (x0) = β0 + β1x0 + β2x20 + β3x30 + β4x40 ,

use variance/covariance estimates of βj to estimate variance of f (x0).• If C ∈ R5×5 is the estimated covariance matrix of the βj , then

Var f (x0) = `>0 C`0, where `0 = (1, x0, x20 , . . . , x40 )>.

• Estimated pointwise standard error of f (x0) is the square root of thisvariance.

• Repeat calculation for all x0, plotting ±2× standard error (corresponds to≈ 95% confidence interval for normally distributed errors) yields dashedlines.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 341 / 463

Page 15: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPolynomial Regression

Right panel in previous figure:• Observations seem to fall into 2 classes: high earners (> $250K) and low

earners; treat wage as binary response variable with these two groups.• Using logistic regression, can predict this binary response using polynomialfunctions of predictor age.

• This corresponds to fitting

P(yi > 250|xi ) =exp(β0 + β1xi + · · ·+ βdxd

i )

1+ exp(β0 + β1xi + · · ·+ βdxdi ).

• Gray marks in figure denote ages of high and low earners.• Solid blue: fitted probabilities of being high/low earner given age, dashedblue gives 95% confidence interval (very wide).

• Only 79 high earners of n = 3000 observations, results in high variance ofcoefficients and therefore wide confidence intervals.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 342 / 463

Page 16: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 343 / 463

Page 17: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

Idea:• Polynomials are globally defined on the domain of the predictor(s) X .

• To model more locally varied response behavior, divide domain of X intosubdomains and use different response model on each.

• Simplest case: different constant function on each subinterval.• Amounts to converting a continuous variable into an unordered categori-cal variable.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 344 / 463

Page 18: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

Idea:• Polynomials are globally defined on the domain of the predictor(s) X .• To model more locally varied response behavior, divide domain of X intosubdomains and use different response model on each.

• Simplest case: different constant function on each subinterval.• Amounts to converting a continuous variable into an unordered categori-cal variable.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 344 / 463

Page 19: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

• Introduce “cut points” c1 < c2 < · · · < cK in range of X , construct K + 1new (dummy) variables with indicator function 1(·)

C0(X ) = 1(X < c1),

C1(X ) = 1(c1 ≤ X < c2),...

CK−1(X ) = 1(cK−2 ≤ X < cK−1),

CK (X ) = 1(cK ≤ X ).

(7.1)

• Since events exhaustive and mutually exclusive we have∑K

k=0 Ck(X ) ≡ 1.• Now fit LS model using C1(X ), . . . ,CK (X ) as predictors9:

yi = β0 + β1C1(X ) + β2C2(X ) + · · ·+ βKCK (X ) + εi .

βj (j > 0): average increase in response for X ∈ [cj , cj+1) relative to X < c1.9Omit C0 as this is redundant with the intercept.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 345 / 463

Page 20: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Piecewise Constant

20 30 40 50 60 70 800

.00

0.0

50

.10

0.1

50

.20

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Left: piecewise constant fit of wage against age (solid) with 95% confidence band(dashed). Right: modeling event {wage > 250} using logistic regression (solid) with95% confidence band (dashed).

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 346 / 463

Page 21: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

Previous figure:• Left: capturing response behavior requires choosing the cut points appro-priately. Increasing trend of wage with age clearly missed in first bin.

• Right: logistic regression fits

P(yi > 250|xi ) =exp(β0 + β1C1(X ) + · · ·+ βKCK (X )

1+ exp(β0 + β1C1(X ) + · · ·+ βKCK (X )

to predict probability of being high earner given age.

Piecewise constant approximation popular in biostatistics and epidemiology,where bins often correspond to 5-year age groups.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 347 / 463

Page 22: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsStep Functions

Previous figure:• Left: capturing response behavior requires choosing the cut points appro-priately. Increasing trend of wage with age clearly missed in first bin.

• Right: logistic regression fits

P(yi > 250|xi ) =exp(β0 + β1C1(X ) + · · ·+ βKCK (X )

1+ exp(β0 + β1C1(X ) + · · ·+ βKCK (X )

to predict probability of being high earner given age.

Piecewise constant approximation popular in biostatistics and epidemiology,where bins often correspond to 5-year age groups.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 347 / 463

Page 23: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGeneral regression functions

• Polynomial, piecewise constant regression examples of basis function ap-proach, where linear combination of transformations {bk(X )}Kk=1 of predic-tor variables used for fitting:

yi = β0 + β1b1(xi ) + β2b2(xi ) + · · ·+ βKbK (xi ) + εi

• Basis functions bk chosen a priori. Examples:

bk(xi ) =

{xki polynomial regression,

1(ck ≤ xi < ck+1) piecewise constant regression.

• Model still linear in the coefficients, hence all inferential methods of linearLS still applicable (standard errors for coefficient estimates, F-statistics formodel significance etc. ).

• Many possible choices: wavelets, Fourier modes, splines, etc.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 348 / 463

Page 24: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 349 / 463

Page 25: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPiecewise polynomials

• As in piecewise constant modes, introduce partition of X domain into sub-intervals.

• Fit a different low-degree polynomial in each subinterval.• E.g. cubic:

yi = β0 + β1xi + β2x2i + β3x3i + εi , (7.2)

with separate coefficients β0, β1, β2, β3 in each subinterval.• Spline termionology: cut points called knots.• Piecewise cubic with single knot at X = c :

yi =

{β0,1 + β1,1xi + β2,1x2i + β3,1x3i + εi , if xi < c ,

β0,2 + β1,2xi + β2,2x2i + β3,2x3i + εi , if xi ≥ c .

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 350 / 463

Page 26: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPiecewise polynomials

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Continuous Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Cubic Spline

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Linear Spline

A piecewise cubic fit of wage against age for the Wage data set. Note the discontinuityat the (single) knot c = 50. Model has 8 = 2× 4 degrees of freedom.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 351 / 463

Page 27: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPiecewise polynomials with constraints

20 30 40 50 60 70

50

10

01

50

20

02

50

Age

Wa

ge

Piecewise Cubic

20 30 40 50 60 70

50

10

01

50

20

02

50

Age

Wa

ge

Continuous Piecewise Cubic

20 30 40 50 60 70

50

10

01

50

20

02

50

Age

Wa

ge

Cubic Spline

20 30 40 50 60 70

50

10

01

50

20

02

50

Age

Wa

ge

Linear Spline

A piecewise cubic fit of the same data, now with the added constraint that the twopolynomials should agree at the knot. This still leaves a ‘kink’ at the knot, i.e., a dis-continuity of the first derivative.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 352 / 463

Page 28: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPiecewise polynomials with constraints

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Continuous Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Cubic Spline

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Linear Spline

A piecewise cubic fit of the same data, now with the added constraint that the twopolynomials as well as their first derivatives should agree at the knot.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 353 / 463

Page 29: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsPiecewise polynomials with constraints

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Continuous Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Cubic Spline

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Linear Spline

A piecewise linear fit of the same data with continuity constraint.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 354 / 463

Page 30: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsSplines

• Cubic spline with K knots: 4+ K degrees of freedom.• General definition of (univariate) spline: piecewise polynomial of degree dwith continuity of derivatives of orders 0, 1, 2 . . . , d − 1.

• Cubic spline model with K knots can be modeled as

yi = β0 + β1b1(xi ) + β2b2(xi ) + · · ·+ βK+3bK+3(xi ) + εi

using appropriate basis functions.• One possible basis (cubic case): start off with monomials x , x2, x3, thenadd for each knot ξ one truncated monomial

h(x , ξ) := (x − ξ)3+ :=

{(x − ξ)3 if x > ξ,

0 otherwise.

• Adding single basis function h(x , ξ) to model (7.2) will introduce disconti-nuity only in third derivative at x = ξ.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 355 / 463

Page 31: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsLS regression with splines

To fit LS regression model with cubic splines using K knots {ξk}Kk=1, use K + 3predictor variables X ,X 2,X 3, h(X , ξ1), . . . , h(X , ξK ).

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Natural Cubic SplineCubic Spline

Cubic and natural (linear beyond boundary knots) spline fit using 3 knots to fit a sub-set of the Wage data. Note the large variance near the endpoints.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 356 / 463

Page 32: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsChoice of spline knots

• Spline most flexible near knots, place these where most variability expected.• Common practice: space knots uniformly, choose # degrees of freedom,have software place knots at uniform quantiles.

20 30 40 50 60 70 80

50

100

150

200

250

300

Age

Wage

Natural Cubic Spline

20 30 40 50 60 70 80

0.0

00.0

50.1

00.1

50.2

0

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 357 / 463

Page 33: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsChoice of spline knots

Previous figure:• Fit natural cubic spline to Wage data. Three knots, chosen automatically at25th, 50th and 75th percentiles of age.

• Requested 4 DOF, leading to 3 interior knots. Actually: 5 knots including2 boundary knots. Corresponds to 9 = 5 + 4 DOF for cubic spline. Twonatural constraints at boundary knots to enforce linearity, leaving 5 = 9− 4DOF. One DOF absorbed in intercept, leaves 4 DOF.

• Right panel: Logistic regression modeling binary event {wage > 250}.Shown: fitted posterior probability.

• Choosing # knots: trial and error or cross-validation.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 358 / 463

Page 34: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsChoice of spline knots

2 4 6 8 10

16

00

16

20

16

40

16

60

16

80

Degrees of Freedom of Natural Spline

Mean S

quare

d E

rror

2 4 6 8 10

16

00

16

20

16

40

16

60

16

80

Degrees of Freedom of Cubic Spline

Mean S

quare

d E

rror

Ten-fold CV MSE for selecting DOF when fitting splines to Wage data.Clear result: 1-DOF not adequate.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 359 / 463

Page 35: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsComparison with polynomials

• Spline regression often superior to polynomial.• More stable as flexibility comes from variation of coefficients of low-degreepolynomials and knot placement.

20 30 40 50 60 70 80

50

100

150

200

250

300

Age

Wage

Natural Cubic Spline

Polynomial

For Wage data: comparison of natural cubic spline with 15 DOF to polynomial of de-gree 15. Latter shows spurious variation near endpoints.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 360 / 463

Page 36: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 361 / 463

Page 37: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsSmoothing splines

• Fitting data with smooth function g: want small RSS =∑n

i=1(yi − g(xi ))2.

• With no constraints on g, can always attain RSS = 0 by interpolating thedata, leading to overfitting.

• Ensure smoothness by adding penalty term: minimize

n∑i=1

(yi − g(xi ))2 + λ

∫g′′(t)2 dt (7.3)

with tuning parameter λ controlling weight assigned to smoothness.• Limiting values: λ = 0 corresponds to no smoothing, leading to interpolati-on for sufficiently many DOF; λ→∞ tends to linear LS fit.

• λ controls bias-variance tradeoff of smoothing spline.• Can show: minimizer of (7.3) is natural cubic spline with knots at x1, . . . , xn.Not the natural cubic spline of the basis function approach, but a shrunkenversion, degree of shrinkage controlled by λ.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 362 / 463

Page 38: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsSmoothing splines: effective DOF

• Smoothing spline: natural cubic spline with knots at x1, . . . , xn, i.e., n DOF.• Can show: as λ → ∞, effective degrees of freedom dfλ decrease from nto 2.

• Smoothing spline has nominally n DOF, these are heavily constrained, i.e.,they are “shrunk” by higher weighting of the penalty term.

• Measure of flexibility of smoothing splines: dfλ.• Mapping from observation vector y ∈ Rn to vector gλ of n coefficientsdefining the smoothing spline with penalty parameter λ is linear, i.e.,

Sλgλ = y , Sλ ∈ Rn×n.

Effective DOF defined by

dfλ := trSλ =

n∑i=1

[Sλ]i ,i .

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 363 / 463

Page 39: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsSmoothing splines: choosing λ

• For smoothing splines no need to choose knot number and locations, eachpredictor observation xi is a knot.

• Remaining problem is choice of smoothing parameter λ.• Obvious option: choose λ to minimize CV estimates of RSS.• For smoothing splines LOOCV error can be computed at nearly the cost ofsingle fit:

RSSCV (λ) =

n∑i=1

(yi − g(−i)λ (xi ))

2 =

n∑i=1

[yi − gλ(xi )

1− [Sλ]i ,i

]2,

g(−i)λ (xi ) : value of smoothing spline fitted with all but i-th observation,

gλ(xi ) : value of smoothing spline using all observations.

• Similar “magic formula” in (5.1) for LS regression.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 364 / 463

Page 40: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsSmoothing splines: choosing λ

20 30 40 50 60 70 80

050

100

200

300

Age

Wage

Smoothing Spline

16 Degrees of Freedom

6.8 Degrees of Freedom (LOOCV)

Smoothing spline fit to Wage data. Red: specified 16 effective DOF.Blue: λ determined by LOOCV, resulting in dfλ = 6.8.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 365 / 463

Page 41: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Contents

7 Nonlinear Regression Models7.1 Polynomial Regression7.2 Step Functions7.3 Regression Splines7.4 Smoothing Splines7.5 Generalized Additive Models

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 366 / 463

Page 42: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGeneralized additive models

• Up to now: single predictor X , extensions of simple linear regression.• Here: consider extensions of multiple linear regression of response Y onpredictors X1, . . . ,Xp.

• Framework: generalized additive models (GAMs).• Allow nonlinear functions of Xj while maintaining additivity.• Can be applied with quantitative and qualitative responses.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 367 / 463

Page 43: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGAMs for regression

• Extend standard multiple linear regression model

yi = β0 + β1xi ,1 + β2xi ,2 + · · ·+ βpxi ,p + εi

to

yi = β0 +

p∑j=1

fj(xi ,j ) + εi .

• Additive: separate fj for each Xj , then add.• Example: Consider natural splines and task of fitting model

wage = β0 + f1(year) + f2(age) + f3(education) + ε (7.4)

from Wage data set, with quantitative variables year, age and qualitati-ve variable education ∈ {<HS, HS, <Coll, Coll, >Coll}. Fit f1, f2 usingnatural splines, f3 using separate constant for each value (dummy variableapproach).

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 368 / 463

Page 44: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGAMs for regression

• Fit entire model (7.4) using LS, expand each function in natural spline ba-sis or dummy variables, resulting in single large regression matrix.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 369 / 463

Page 45: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGAMs for regression

2003 2005 2007 2009

−30

−20

−10

010

20

30

20 30 40 50 60 70 80

−50

−40

−30

−20

−10

010

20

−30

−20

−10

010

20

30

40

<HS HS <Coll Coll >Coll

f 1(year)

f 2(age)

f 3(edu

cation)

year ageeducation

Relationship of each feature and response (wage). f1 and f2 are natural splines in yearand age with 4 and 5 DOF, respectively. f3 is a step function fit to qualitative predic-tor education.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 370 / 463

Page 46: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGAMs for regression

2003 2005 2007 2009

−30

−20

−10

010

20

30

20 30 40 50 60 70 80

−50

−40

−30

−20

−10

010

20

−30

−20

−10

010

20

30

40

<HS HS <Coll Coll >Coll

f 1(year)

f 2(age)

f 3(edu

cation)

year ageeducation

Same as before except f1 and f2 smoothing splines with 4 and 5 DOF, respectively. Fitof smoothinmg splines more difficult than for natural splines, standard software solvesan optimization problem via an algorithm known as backfitting.

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 371 / 463

Page 47: Introduction to Data Science - Winter Semester 2019/20 › mathematik › numa › lehre › ... · ContentsIII 9 UnsupervisedLearning 9.1 PrincipalComponentsAnalysis 9.2 ClusteringMethods

Nonlinear Regression ModelsGAMs: benefits and shortcomings

+ GAMs allow fitting nonlinear fj to each Xj in order to capturenonlinear dependencies.

+ Potentially more accurate predictions of response Y .

+ Model still additive, effect of each Xj can be examoned separate-ly, useful for inference.

+ Smoothness of each fj can be summarized via (effective) DOF.

- Additivity is a restriction, interactions can be missed. Can addinteraction terms manually by adding predictors Xj × Xk or lowdegree interaction functions fj ,k(Xj ,Xk).

GAMs are a useful compromise between linear and fully nonparametric methodssuch as random forests and boosting (later).

Oliver Ernst (NM) Introduction to Data Science Winter Semester 2018/19 372 / 463