• No results found

nonlinear_7.pdf

N/A
N/A
Protected

Academic year: 2020

Share "nonlinear_7.pdf"

Copied!
48
0
0

Loading.... (view fulltext now)

Full text

(1)

Moving Beyond Linearity

The truth is never linear!

Or almost never!

But often the linearity assumption is good enough.

When its not. . .

• polynomials,

• step functions,

• splines,

• local regression, and

• generalized additive models

offer a lot of flexibility, without losing the ease and interpretability of linear models.

(2)

Moving Beyond Linearity

The truth is never linear! Or almost never!

But often the linearity assumption is good enough.

When its not. . .

• polynomials,

• step functions,

• splines,

• local regression, and

• generalized additive models

(3)

Moving Beyond Linearity

The truth is never linear! Or almost never!

But often the linearity assumption is good enough.

When its not. . .

• polynomials,

• step functions,

• splines,

• local regression, and

• generalized additive models

offer a lot of flexibility, without losing the ease and interpretability of linear models.

(4)

Moving Beyond Linearity

The truth is never linear! Or almost never!

But often the linearity assumption is good enough.

When its not. . .

• polynomials,

• step functions,

• splines,

• local regression, and

• generalized additive models

(5)

Polynomial Regression

yi =β0+β1xi+β2x2i +β3x3i +. . .+βdxdi +i

20 30 40 50 60 70 80

50 100 150 200 250 300 Age W age Degree−4 Polynomial

20 30 40 50 60 70 80

0.00 0.05 0.10 0.15 0.20 Age | | | | || ||||||||||| ||||||| | | | | | | || || || | | | | ||| || | | | | | | | | | | | | | | ||| | | || || | | | || | | | | ||| | || ||||| ||| || | | | ||||| |||||||| ||| || || | | | | | | | || | | || | || | || ||| ||||||||||||||||||||||| ||||||||| |||||||||||||||||||||||| | |||| |||||| | | | | |||| |||||| | | || | | | | | | || || | | | | | | | | | | | | | |||||| ||| |||| ||||||||||||||||| ||| | | ||||| || | || ||| |||| || | |||||||||| |||||||| ||| ||||| ||| ||||| |||||||| || | | | | | | | | || || ||| | | || | | | | | | | | | | | | | | | | | | | | | ||||||| ||||||||||||||||||||||||| ||||||||||| ||||| | | | | || || | | |||| || |||||| | || | | | ||| | ||||||||||||||| |||| || ||| | | ||| | ||| | | || | | | | |||| || | | | | | | || | | |||| | || | | | || | | | | | | | | | | | | | | | ||| || | | | || || | | | | | | |||| | | | | || | | | | | | ||| || | ||| | | | | | | | ||| |||||| |||| ||||||||||| |||| |||| | |||| ||||| | | | | | | | | | | | | | | | | | | | | | | | ||| | | | ||| | | | |||| | | ||||||||||| | || | || | | | | | | || | | |||| || ||| | |||||| || ||| | | | | | | || || || | ||| | | || | | | | || || | | | | | | | | | | | | | | || | || | || || |||| | | || || | | | | | | || | | | | | | ||| || || | || | | | | | |||| || | | | | | ||| | | | | | ||| ||||||| ||||||||| || |||||||| |||||||||||||||||||| ||||||| ||| | | | | | | ||||| ||| | | | | | | | | | ||| | | || |||| || | | || | || | | || ||| ||| | || | | | | | | || | | | || | | ||| |||||||| ||||||| ||| ||||||||||||||||||||||| | | | | | | | | ||| || |||| | ||||| || | | |||||||||| |||||||||||| ||||| |||||||||||| ||| | | | | | || | | | | | || | | | ||||| ||| | || | | | |||| | || |||| | | | | | ||||||||||||||||| ||| ||||||||| | | | ||| || | || | | | | | ||| | | || | | | | | || |||||||| | ||||| || || | ||| || ||| |||||||| | |||| | | | | || | | | | | | || | || | | | ||| || | ||| || |||||| || | | | | | | || | || | | | | | | | | | | | || | | | | | | | || | | | | | | | | | | | | | | | | | | | | || | | | | | || ||| |||||||||| || || | | ||| | |||| ||||||| || | | | || | || |||||||||| || | | || | | | | | || | | || |||||||||||||||||||||||||||||||||||||||||||||||||||||| |||| |||||||||||||| |||| |||||| | | | || | | | | | | ||| || |||||||| | ||||||| ||| |||||||||||||||||||||| |||||||||||||||||||||||| |||||| ||||||||||| || | | | | | | | |||| | | | | | ||| | | | | ||| | | | | | | | || | | | ||| | | | | | | | | | | | | | || | | | || |||||| |||||||||| ||||||||| | || | || | || | | | || | | | | | || | | || | ||| ||||| ||||||||| |||||||| ||||| | | | |||||||||||| |||||||||| ||| | | | | || | | | | | | | | ||||||||||| || ||||||||||||| ||||||||||||||||| ||||||||||||||| || | | | | | | | | | | | |||| | | || ||| | || || | | | |||| || | | |||| | ||||| | || | | | | | | | | || | | || | | | | || | | | ||||| ||| || || | || | |||| | | | | | ||| ||| | | | ||||||||||||||||||||||| ||||||||||||||||| |||||||| | | | | | | ||| |||||| |||||| ||||||| | ||||| | | | | || | | | | | | ||||| | || | | | | | | || |||| | ||||||||||||||||||||||||||||||||||||||| | | | | | | | | || | |||| | ||| | | | | | | | | | | || | || | | | | | | || |||| ||| | ||| |||||| | | | | | | | | | | | | | | | | |||||||| | || | | || | | | | | | |||||||||||||||||||||||| |||||||||||| || ||||||||| ||||||||| | | ||| ||||| |||||||||| | || | | | | | | | | | | | | | || ||| | | | | | | | || | | | | || | | || || ||||||| | | | | | | |||| || || | | | | | |||| ||| || |||| || ||| || | | | | | | | || | | | | || | || | | | | || | || | | | || | || | | | | ||| | | | | | | | || ||| | | |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||| | | || | | | | | | | | || ||||||||||||||||||||||||||||||||||||| || | | | | | | | | | | | | | | | | | || | | | || | || | | ||| |||||||||||| ||| || ||| |||||| |||||||||||| |||||||||||||||| ||| |||||||||||||| |||||||| ||||||||||| ||||||||||||||| ||||||| |||||||||||| || | | | | | | | | | | | | | | | | | | | | | | | ||| | | | | || | | | | | | | | || || | | ||||| ||||||| |||||| || || | || || ||| | ||| || |||| |||||||| ||||||||||||||| | | | | | | | | | | || | || || |||| | ||||| || |||| | ||| || ||| | | | | | | | | | ||| || || || ||| | || | |||| | | | | |||| || | || | || | | | | | | | | | | | | | || || | | | | | | |||||| |||| |||| | ||| | ||||||| | | | | | | | | | | ||||| ||| | | ||| | | || | | | | | | | | | | || |||||||||||||||||||||||| |||||||||||||| P r ( W ag e > 25 0 | A ge )

(6)

Details

• Create new variables X1 =X,X2 =X2,etc and then treat

as multiple linear regression.

• Not really interested in the coefficients; more interested in

the fitted function values at any value x0:

ˆ

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

• Since ˆf(x0) is a linear function of the ˆβ`, can get a simple

expression for pointwise-variancesVar[ ˆf(x0)] at any

value x0. In the figure we have computed the fit and

pointwise standard errors on a grid of values for x0. We

show ˆf(x0)±2·se[ ˆf(x0)].

• We either fix the degree dat some reasonably low value,

(7)

Details

• Create new variables X1 =X,X2 =X2,etc and then treat

as multiple linear regression.

• Not really interested in the coefficients; more interested in

the fitted function values at any value x0:

ˆ

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

• Since ˆf(x0) is a linear function of the ˆβ`, can get a simple

expression for pointwise-variancesVar[ ˆf(x0)] at any

value x0. In the figure we have computed the fit and

pointwise standard errors on a grid of values for x0. We

show ˆf(x0)±2·se[ ˆf(x0)].

• We either fix the degree dat some reasonably low value,

else use cross-validation to choose d.

(8)

Details

• Create new variables X1 =X,X2 =X2,etc and then treat

as multiple linear regression.

• Not really interested in the coefficients; more interested in

the fitted function values at any value x0:

ˆ

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

• Since ˆf(x0) is a linear function of the ˆβ`, can get a simple

expression for pointwise-variancesVar[ ˆf(x0)] at any

value x0. In the figure we have computed the fit and

pointwise standard errors on a grid of values for x0. We

show ˆf(x0)±2·se[ ˆf(x0)].

• We either fix the degree dat some reasonably low value,

(9)

Details

• Create new variables X1 =X,X2 =X2,etc and then treat

as multiple linear regression.

• Not really interested in the coefficients; more interested in

the fitted function values at any value x0:

ˆ

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

• Since ˆf(x0) is a linear function of the ˆβ`, can get a simple

expression for pointwise-variancesVar[ ˆf(x0)] at any

value x0. In the figure we have computed the fit and

pointwise standard errors on a grid of values for x0. We

show ˆf(x0)±2·se[ ˆf(x0)].

• We either fix the degree dat some reasonably low value,

else use cross-validation to choose d.

(10)

Details continued

• Logistic regression follows naturally. For example, in figure

we model

Pr(yi >250|xi) =

exp(β0+β1xi+β2x2i +. . .+βdxdi)

1 + exp(β0+β1xi+β2x2i +. . .+βdxdi)

.

• To get confidence intervals, compute upper and lower

bounds on on the logit scale, and then invert to get on

probability scale.

• Can do separately on several variables—just stack the

variables into one matrix, and separate out the pieces afterwards (see GAMs later).

• Caveat: polynomials have notorious tail behavior — very

bad for extrapolation.

(11)

Details continued

• Logistic regression follows naturally. For example, in figure

we model

Pr(yi >250|xi) =

exp(β0+β1xi+β2x2i +. . .+βdxdi)

1 + exp(β0+β1xi+β2x2i +. . .+βdxdi)

.

• To get confidence intervals, compute upper and lower

bounds on on the logit scale, and then invert to get on

probability scale.

• Can do separately on several variables—just stack the

variables into one matrix, and separate out the pieces afterwards (see GAMs later).

• Caveat: polynomials have notorious tail behavior — very

bad for extrapolation.

• Can fit using y∼poly(x,degree=3)in formula.

(12)

Details continued

• Logistic regression follows naturally. For example, in figure

we model

Pr(yi >250|xi) =

exp(β0+β1xi+β2x2i +. . .+βdxdi)

1 + exp(β0+β1xi+β2x2i +. . .+βdxdi)

.

• To get confidence intervals, compute upper and lower

bounds on on the logit scale, and then invert to get on

probability scale.

• Can do separately on several variables—just stack the

variables into one matrix, and separate out the pieces afterwards (see GAMs later).

• Caveat: polynomials have notorious tail behavior — very

bad for extrapolation.

(13)

Step Functions

Another way of creating transformations of a variable — cut the variable into distinct regions.

C1(X) =I(X <35), C2(X) =I(35≤X <50), . . . , C3(X) =I(X≥65)

20 30 40 50 60 70 80

50 100 150 200 250 300 Age W age Piecewise Constant

20 30 40 50 60 70 80

0.00 0.05 0.10 0.15 0.20 Age | | | | ||| | ||||||||| ||||||| | | | | | | || || || | | | | ||| || | | | | | | | | | | | | | | ||| | | || || | | | || | | | | ||| | || ||||| ||| || | | | ||||| |||||||| ||| || || | | | | | | | || | | || | || | || |||||||||||||||||||||||||| |||||||||||||||||||||||||||||||| |||||||||||| | | | | |||| |||||| | | || | | | | | | || || | | | | | | | | | || | || |||||||||||||||| |||| ||||||||||||||||||||||||||| ||||| | | |||||||||| |||||||| ||| |||| |||| ||||| |||||||| || | | | | | | | | || || ||| | | || | | | | | | | | | | || | | | | | | | | | ||||||| ||||||||||||||||||||||||| ||||||||||| ||||| | | | | || || | | |||| || |||||| | || | | | ||| | ||||||||||||||| |||| || ||| | | ||| | ||| | | || | | | | |||| || | | | | | | || | | |||| | || | | | || | | | | | | | | | | | | | || ||| || | | | || || | | | | | | |||| | | | | || | | | | | | ||| || | ||| | | | | | | | |||||||| || ||| ||||||||||| ||||||||| |||| ||||| | | | | | | | | | | | | | | | | | | | | | | | ||| | | | ||| | | | |||| | | ||||||||||| | || || | ||| | | | || | | |||| || ||| ||||||| || ||| | | | | | ||| || || | ||| | | || | | || || || | | | || | | | | | | | | | | | | || | || || |||| | | || || | | | | | | || | | | | | | ||| || || | || | | | | | |||| || | | | | | ||| | | | | |||| ||||||| |||||||| ||| |||||||| |||||||||||||||||||| ||||||| ||| | | | | | | ||||| |||| | | | | | | | | ||| | | || |||| || | | || | || | | || |||||| | || | | | | | | || | | | || | | ||| |||||||| ||||||| ||| ||||||||||||||||||||||| | | | | | | | | ||| || |||| | ||||| || | | ||||||||||| ||||||||||| ||||| |||||||||||| ||| | | || | || | | | | | || | | | ||||| ||| | || | | | |||| | || |||| | | | | | ||||||||||||||||| ||| ||||||||| | | | ||| || | ||| | | | | ||| | | || | | | | | || |||||||| |||||| || || | ||| || ||| |||||||| | |||| | | | | || | | | | | | || | || | | | ||| || | ||| | ||||||| || | | | | | | | || | | | | | | | | | | | | | || | | | | | | | || | | | | | | | | | | | | | | | | | | | | || || | | | || ||| ||||||||| | || || | | |||| |||| ||||||| || | | | || | || |||||||||| || | | ||| | | | | || | || | |||||||||||||||||||||||||||||||||||||||||||||||||||||| ||| ||||||||||||||| |||| |||||| | | | || | | | | | | ||| || |||||||| | || | |||| ||| || ||||||||||||||||||||| || || || ||| | ||| | |||||||| ||| |||||||||| |||||| | | | | | | | | |||| | | | | | ||| | | | | ||| | | | | | | | || | | | ||| | | | | | | | | | | | | | || | | | || |||||| |||||||||| ||||||||| | || | || | || | | | || | | | | | || | | || | | || ||||| ||||||||| |||||||| ||||| || | |||||||||||| |||||||||| ||| | | | | || | | | | | | | | ||||||||||| || ||||||||||||| ||||||||||||||||| ||||||||||||||| || | | | | | | | | | | | || || | | || ||| | || || | | || |||||| ||| | | ||||||||| |||| | | | | || | | ||| | | | || | | | ||||| ||| || || | || | |||| | | | | | ||| | || | | | ||||||||||||||||||||||| ||||||||||||||||| |||||||| | | | | | | | |||||||| |||||| |||| | |||||||| | | | | || | | | | | | ||||| | || | | | | | | || |||| |||||||||||||||||||||||||||||||||||||||| | | | | | | | | || | |||| | ||| | | | | | | | | | | || | || | | | | | | || |||| ||| | ||||||||| | | | | | | | | | | | | | | | | |||||||| | || | | || | | | | | | ||||||||||||||||||||| ||||||||||||||| || ||||||||| ||||||||| | | |||||||| |||||||||| | || | | | | | | | | | | | | | || ||| | | | | | | | || | | | | || | | || || ||||||| | | | | | | |||| || || | | | | | |||| ||| || |||| ||||| || | | | | | | | || | | | | || | || | | | | || | || | | | || | || | | | | ||| | | | | | | | || ||| | | |||||||| |||||||||||||||||||||||||||||||||||||||||||||||||||||| | ||| | | | | | | | | || ||||||||||||||||||||||||||||||||||||| || | | | | | | | | | | | | | | | | | || | | | || | || | | ||| |||||||||||| ||| || ||| | ||||| |||||||||||| |||||||||||||||| ||| |||||||||||||| ||||||| |||||||||||| ||||||||||||||||||||| ||||||||||||| || | | | | | | | | | | | | | | | | | | | | | | | ||| | | | | || | | | | | | | | || || | | ||||| ||||||| |||||| || || | | | || ||| | ||| || |||| |||||||| ||||||||||||||| | | | | | || | | | || | || || |||| | ||||| | ||||| | ||| || ||| | | | | | | | | | ||| || || || ||| | || | |||| | | | | |||| || | || | || | | | | | | | | | | | | | || || | | | | | | |||||| |||||||| | ||| | ||||||| | | | | | | | | || ||||| ||| | | ||| | | || | | | | | | | | | | || |||||||||||||||||||||||| |||||||||||||| P r ( W ag e > 25 0 | A ge )

(14)

Step functions continued

• Easy to work with. Creates a series of dummy variables

representing each group.

• Useful way of creating interactions that are easy to

interpret. For example, interaction effect of Yearand Age:

I(Year<2005)·Age, I(Year≥2005)·Age

would allow for different linear functions in each age category.

• In R: I(year<2005) orcut(age,c(18,25,40,65,90)).

• Choice of cutpoints or knotscan be problematic. For

creating nonlinearities, smoother alternatives such as

(15)

Step functions continued

• Easy to work with. Creates a series of dummy variables

representing each group.

• Useful way of creating interactions that are easy to

interpret. For example, interaction effect of Yearand Age:

I(Year<2005)·Age, I(Year≥2005)·Age

would allow for different linear functions in each age category.

• In R: I(year<2005) orcut(age,c(18,25,40,65,90)).

• Choice of cutpoints or knotscan be problematic. For

creating nonlinearities, smoother alternatives such as

splinesare available.

(16)

Step functions continued

• Easy to work with. Creates a series of dummy variables

representing each group.

• Useful way of creating interactions that are easy to

interpret. For example, interaction effect of Yearand Age:

I(Year<2005)·Age, I(Year≥2005)·Age

would allow for different linear functions in each age category.

• In R: I(year<2005) orcut(age,c(18,25,40,65,90)).

• Choice of cutpoints or knotscan be problematic. For

creating nonlinearities, smoother alternatives such as

(17)

Step functions continued

• Easy to work with. Creates a series of dummy variables

representing each group.

• Useful way of creating interactions that are easy to

interpret. For example, interaction effect of Yearand Age:

I(Year<2005)·Age, I(Year≥2005)·Age

would allow for different linear functions in each age category.

• In R: I(year<2005) orcut(age,c(18,25,40,65,90)).

• Choice of cutpoints orknots can be problematic. For

creating nonlinearities, smoother alternatives such as

splinesare available.

(18)

Piecewise Polynomials

• Instead of a single polynomial in X over its whole domain,

we can rather use different polynomials in regions defined by knots. E.g. (see figure)

yi =

(

β01+β11xi+β21x2i +β31x3i +i ifxi< c;

β02+β12xi+β22x2i +β32x3i +i ifxi≥c.

• Better to add constraints to the polynomials, e.g.

continuity.

(19)

20 30 40 50 60 70

50

100

150

200

250

Age

W

age

Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

W

age

Continuous Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

W

age

Cubic Spline

20 30 40 50 60 70

50

100

150

200

250

Age

W

age

Linear Spline

(20)

Linear Splines

A linear spline with knots at ξk, k= 1, . . . , K is a piecewise linear polynomial continuous at each knot.

We can represent this model as

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

where thebk arebasis functions.

b1(xi) = xi

bk+1(xi) = (xi−ξk)+, k= 1, . . . , K

Here the ()+ meanspositive part; i.e.

(xi−ξk)+=

xi−ξk ifxi > ξk

(21)

Linear Splines

A linear spline with knots at ξk, k= 1, . . . , K is a piecewise linear polynomial continuous at each knot.

We can represent this model as

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

where thebk arebasis functions.

b1(xi) = xi

bk+1(xi) = (xi−ξk)+, k= 1, . . . , K

Here the ()+ meanspositive part; i.e.

(xi−ξk)+=

xi−ξk ifxi > ξk

0 otherwise

(22)

0.0 0.2 0.4 0.6 0.8 1.0

0.3

0.5

0.7

0.9

x

f(x)

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

x

(23)

Cubic Splines

A cubic spline with knots atξk, k= 1, . . . , K is a piecewise cubic polynomial with continuous derivatives up to order 2 at each knot.

Again we can represent this model with truncated power basis functions

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

b1(xi) = xi

b2(xi) = x2i

b3(xi) = x3i

bk+3(xi) = (xi−ξk)3+, k= 1, . . . , K

where

(xi−ξk)3+=

(xi−ξk)3 ifxi > ξk

0 otherwise

(24)

0.0 0.2 0.4 0.6 0.8 1.0

1.0

1.2

1.4

1.6

x

f(x)

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

x

(25)

Natural Cubic Splines

A natural cubic spline extrapolates linearly beyond the

boundary knots. This adds 4 = 2×2 extra constraints, and

allows us to put more internal knots for the same degrees of freedom as a regular cubic spline.

20 30 40 50 60 70

50

100

150

200

250

Age

W

age

Natural Cubic Spline Cubic Spline

(26)

Fitting splines in R is easy: bs(x, ...) for any degree splines,

andns(x, ...) for natural cubic splines, in package splines.

20 30 40 50 60 70 80

50 100 150 200 250 300 Age W age

Natural Cubic Spline

20 30 40 50 60 70 80

(27)

Knot placement

• One strategy is to decide K, the number of knots, and then

place them at appropriate quantiles of the observed X.

• A cubic spline withK knots hasK+ 4 parameters or

degrees of freedom.

• A natural spline with K knots hasK degrees of freedom.

20 30 40 50 60 70 80

50

100

150

200

250

300

Age

W

age

Natural Cubic Spline

Polynomial Comparison of a

degree-14

polyno-mial and a natural cubic spline, each with 15df.

ns(age, df=14)

poly(age, deg=14)

(28)

Knot placement

• One strategy is to decide K, the number of knots, and then

place them at appropriate quantiles of the observed X.

• A cubic spline withK knots hasK+ 4 parameters or

degrees of freedom.

• A natural spline with K knots hasK degrees of freedom.

20 30 40 50 60 70 80

50

100

150

200

250

300

W

age

Natural Cubic Spline

Polynomial Comparison of a

degree-14

polyno-mial and a natural cubic spline, each with 15df.

ns(age, df=14)

(29)

Smoothing Splines

This section is a little bit mathematical

Consider this criterion for fitting a smooth functiong(x) to

some data:

minimize

g∈S

n

X

i=1

(yi−g(xi))2+λ

Z

g00(t)2dt

• The first term is RSS, and tries to make g(x) match the

data at eachxi.

• The second term is a roughness penalty and controls how

wiggly g(x) is. It is modulated by thetuning parameter

λ0.

• The smallerλ, the more wiggly the function, eventually interpolatingyi whenλ= 0.

• Asλ→ ∞, the functiong(x) becomes linear.

(30)

Smoothing Splines

This section is a little bit mathematical

Consider this criterion for fitting a smooth functiong(x) to

some data:

minimize

g∈S

n

X

i=1

(yi−g(xi))2+λ

Z

g00(t)2dt

• The first term is RSS, and tries to make g(x) match the

data at eachxi.

• The second term is a roughness penalty and controls how

wiggly g(x) is. It is modulated by thetuning parameter

λ0.

• The smallerλ, the more wiggly the function, eventually interpolatingyi whenλ= 0.

(31)

Smoothing Splines

This section is a little bit mathematical

Consider this criterion for fitting a smooth functiong(x) to

some data:

minimize

g∈S

n

X

i=1

(yi−g(xi))2+λ

Z

g00(t)2dt

• The first term is RSS, and tries to make g(x) match the

data at eachxi.

• The second term is a roughness penalty and controls how

wiggly g(x) is. It is modulated by thetuning parameter

λ0.

• The smallerλ, the more wiggly the function, eventually interpolatingyi whenλ= 0.

• Asλ→ ∞, the functiong(x) becomes linear.

(32)

Smoothing Splines

This section is a little bit mathematical

Consider this criterion for fitting a smooth functiong(x) to

some data:

minimize

g∈S

n

X

i=1

(yi−g(xi))2+λ

Z

g00(t)2dt

• The first term is RSS, and tries to make g(x) match the

data at eachxi.

• The second term is a roughness penalty and controls how

wiggly g(x) is. It is modulated by thetuning parameter

λ0.

• The smallerλ, the more wiggly the function, eventually interpolatingyi whenλ= 0.

(33)

Smoothing Splines

This section is a little bit mathematical

Consider this criterion for fitting a smooth functiong(x) to

some data:

minimize

g∈S

n

X

i=1

(yi−g(xi))2+λ

Z

g00(t)2dt

• The first term is RSS, and tries to make g(x) match the

data at eachxi.

• The second term is a roughness penalty and controls how

wiggly g(x) is. It is modulated by thetuning parameter

λ0.

• The smallerλ, the more wiggly the function, eventually interpolatingyi whenλ= 0.

• Asλ→ ∞, the functiong(x) becomes linear.

(34)

Smoothing Splines continued

The solution is a natural cubic spline, with a knot at every

unique value ofxi. The roughness penalty still controls the

roughness viaλ.

Some details

• Smoothing splines avoid the knot-selection issue, leaving a

single λto be chosen.

• The algorithmic details are too complex to describe here.

In R, the function smooth.spline()will fit a smoothing

spline.

• The vector of nfitted values can be written as ˆgλ=Sλy,

where Sλ is an×nmatrix (determined by thexi and λ).

• The effective degrees of freedom are given by

dfλ = n

X

i=1

(35)

Smoothing Splines continued

The solution is a natural cubic spline, with a knot at every

unique value ofxi. The roughness penalty still controls the

roughness viaλ.

Some details

• Smoothing splines avoid the knot-selection issue, leaving a

single λto be chosen.

• The algorithmic details are too complex to describe here.

In R, the function smooth.spline()will fit a smoothing

spline.

• The vector of nfitted values can be written as ˆgλ =Sλy,

where Sλ is an×nmatrix (determined by thexi and λ).

• The effective degrees of freedom are given by

dfλ = n

X

i=1

{Sλ}ii.

(36)

Smoothing Splines continued

The solution is a natural cubic spline, with a knot at every

unique value ofxi. The roughness penalty still controls the

roughness viaλ.

Some details

• Smoothing splines avoid the knot-selection issue, leaving a

single λto be chosen.

• The algorithmic details are too complex to describe here.

In R, the function smooth.spline()will fit a smoothing

spline.

• The vector of nfitted values can be written as ˆgλ =Sλy,

where Sλ is an×nmatrix (determined by thexi and λ).

• The effective degrees of freedom are given by

dfλ = n

X

i=1

(37)

Smoothing Splines continued

The solution is a natural cubic spline, with a knot at every

unique value ofxi. The roughness penalty still controls the

roughness viaλ.

Some details

• Smoothing splines avoid the knot-selection issue, leaving a

single λto be chosen.

• The algorithmic details are too complex to describe here.

In R, the function smooth.spline()will fit a smoothing

spline.

• The vector of nfitted values can be written as ˆgλ =Sλy,

where Sλ is an×nmatrix (determined by thexi and λ).

• The effective degrees of freedom are given by

dfλ = n

X

i=1

{Sλ}ii.

(38)

Smoothing Splines continued — choosing

λ

• We can specify df rather thanλ!

In R: smooth.spline(age,wage,df=10)

• The leave-one-out (LOO) cross-validated error is given by

RSScv(λ) =

n

X

i=1

(yi−ˆgλ(−i)(xi))2=

n

X

i=1

yi−gˆλ(xi)

1− {Sλ}ii

2

.

(39)

Smoothing Splines continued — choosing

λ

• We can specify df rather thanλ!

In R: smooth.spline(age,wage,df=10)

• The leave-one-out (LOO) cross-validated error is given by

RSScv(λ) =

n

X

i=1

(yi−ˆgλ(−i)(xi))2=

n

X

i=1

yi−gˆλ(xi)

1− {Sλ}ii

2

.

In R: smooth.spline(age,wage)

(40)

20 30 40 50 60 70 80

0

50

100

200

300

Age

W

age

Smoothing Spline

(41)

Local Regression

0.0 0.2 0.4 0.6 0.8 1.0

−1.0 −0.5 0.0 0.5 1.0 1.5 O O O O O O O O O O O O O O O OO O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O OO O O O O O O O O O O O OO O O O O O O O OO O O O O O O O O O O O O OO O O O O OO O O O O O O O O O O O O O O O O O O O O O OO O O O O O

0.0 0.2 0.4 0.6 0.8 1.0

−1.0 −0.5 0.0 0.5 1.0 1.5 O O O O O O O O O O O O O O O OO O O O O O O O O O O O O O O O O O O O O O OO O O O O O O O O O O OO O O O O O O O O O O O OO O O OO O O O OO O O O O O O O O O O O O OO O O O O OO O O O O O O O O O O O O O O O O O O O O O O O OO O O O O O O O O O O OO O O O O O O O O O O O OO O O OO O O Local Regression

With a sliding weight function, we fit separate linear fits over

the range ofX by weighted least squares.

See text for more details, andloess()function in R.

(42)

Generalized Additive Models

Allows for flexible nonlinearities in several variables, but retains the additive structure of linear models.

yi=β0+f1(xi1) +f2(xi2) +· · ·+fp(xip) +i.

2003 2005 2007 2009

−30

−20

−10

0

10

20

30

20 30 40 50 60 70 80

−50

−40

−30

−20

−10

0

10

20

−30

−20

−10

0

10

20

30

40

<HS HS <Coll Coll >Coll

f1

(

ye

ar

)

f2

(

ag

e

)

f3

(

ed

uc

at

io

n

)

(43)

GAM details

• Can fit a GAM simply using, e.g. natural splines:

lm(wagens(year,df=5) +ns(age,df=5) +education)

• Coefficients not that interesting; fitted functions are. The

previous plot was produced using plot.gam.

• Can mix terms — some linear, some nonlinear — and use

anova() to compare models.

• Can use smoothing splines or local regression as well:

gam(wages(year,df=5) +lo(age,span=.5) +education)

• GAMs are additive, although low-order interactions can be

included in a natural way using, e.g. bivariate smoothers or

interactions of the form ns(age,df=5):ns(year,df=5).

(44)

GAM details

• Can fit a GAM simply using, e.g. natural splines:

lm(wagens(year,df=5) +ns(age,df=5) +education)

• Coefficients not that interesting; fitted functions are. The

previous plot was produced using plot.gam.

• Can mix terms — some linear, some nonlinear — and use

anova() to compare models.

• Can use smoothing splines or local regression as well:

gam(wages(year,df=5) +lo(age,span=.5) +education)

• GAMs are additive, although low-order interactions can be

included in a natural way using, e.g. bivariate smoothers or

(45)

GAM details

• Can fit a GAM simply using, e.g. natural splines:

lm(wagens(year,df=5) +ns(age,df=5) +education)

• Coefficients not that interesting; fitted functions are. The

previous plot was produced using plot.gam.

• Can mix terms — some linear, some nonlinear — and use

anova() to compare models.

• Can use smoothing splines or local regression as well:

gam(wages(year,df=5) +lo(age,span=.5) +education)

• GAMs are additive, although low-order interactions can be

included in a natural way using, e.g. bivariate smoothers or

interactions of the form ns(age,df=5):ns(year,df=5).

(46)

GAM details

• Can fit a GAM simply using, e.g. natural splines:

lm(wagens(year,df=5) +ns(age,df=5) +education)

• Coefficients not that interesting; fitted functions are. The

previous plot was produced using plot.gam.

• Can mix terms — some linear, some nonlinear — and use

anova() to compare models.

• Can use smoothing splines or local regression as well:

gam(wages(year,df=5) +lo(age,span=.5) +education)

• GAMs are additive, although low-order interactions can be

included in a natural way using, e.g. bivariate smoothers or

(47)

GAM details

• Can fit a GAM simply using, e.g. natural splines:

lm(wagens(year,df=5) +ns(age,df=5) +education)

• Coefficients not that interesting; fitted functions are. The

previous plot was produced using plot.gam.

• Can mix terms — some linear, some nonlinear — and use

anova() to compare models.

• Can use smoothing splines or local regression as well:

gam(wages(year,df=5) +lo(age,span=.5) +education)

• GAMs are additive, although low-order interactions can be

included in a natural way using, e.g. bivariate smoothers or

interactions of the form ns(age,df=5):ns(year,df=5).

(48)

GAMs for classification

log

p(X)

1p(X)

=β0+f1(X1) +f2(X2) +· · ·+fp(Xp).

2003 2005 2007 2009

−4

−2

0

2

4

20 30 40 50 60 70 80

−8

−6

−4

−2

0

2

−4

−2

0

2

4

HS <Coll Coll >Coll

f1

(

ye

ar

)

f2

(

ag

e

)

f3

(

ed

uc

at

io

n

)

year age education

References

Related documents

Abstract In present paper we considered if temperature effects the growth of microcracks in a broken femur with AMBI. We studied three particularly points of fracture. We

The main objective of this research is to develop the KMS that provides the dynamic and personalized web services for cluster members by proposing the model of double spiral

2 David Drown Boulevard, Fairhaven, MA 02719, 28 miles, 30 min drive Plimoth Plantation, Mayflower and Plymouth Rock.. One of New England’s best living museums is

The Culinary Institute Switzerland is a member of SEG, Swiss Education Group, a network of Switzerland’s leading hotel and hospitality management schools. A recent survey

We evaluated for an association between iron homeostasis and both smoking and a diagnosis of chronic obstructive pulmonary disease ( COPD ) by measuring metal concentrations in

To validate educational credentials as predictors of first-term completion, so that a refined screening tool can be developed, a new model was constructed so that only the

Consumers often have little knowledge about the extent of privacy risks taken when using mobile applications (apps) on their smartphones, and are unlikely to be informed by

Get out to events and activities and speak to people about your project. Network whenever you meet opinion-formers, politicians, non-governmental organisations