GAMs, GAMMs and other penalized GLMs using mgcv in R

35
GAMs, GAMMs and other penalized GLMs using mgcv in R Simon Wood Mathematical Sciences, University of Bath, U.K.

Transcript of GAMs, GAMMs and other penalized GLMs using mgcv in R

Page 1: GAMs, GAMMs and other penalized GLMs using mgcv in R

GAMs, GAMMs and other penalized GLMsusing mgcv in R

Simon WoodMathematical Sciences, University of Bath, U.K.

Page 2: GAMs, GAMMs and other penalized GLMs using mgcv in R

Simple example

I Consider a very simple dataset relating the timber volumeof cherry trees to their height and trunk girth.

Girth

65 70 75 80 85

812

1620

6575

85

Height

8 10 12 14 16 18 20 10 20 30 40 50 60 70

1030

5070

Volume

Page 3: GAMs, GAMMs and other penalized GLMs using mgcv in R

trees initial gam fit

I A possible model is

log(µi) = f1(Heighti)+f2(Girthi), Volumei ∼ Gamma(µi , φ)

I Which can be estimated using. . .library(mgcv)ct1 <- gam(Volume ~ s(Height) + s(Girth),

family=Gamma(link=log),data=trees)

I gam produces a representation for the smooth functions,and estimates them along with their degree of smoothness.

Page 4: GAMs, GAMMs and other penalized GLMs using mgcv in R

Cherry tree results

I par(mfrow=c(1,2))plot(ct1,various options) ## calls plot.gam

65 70 75 80 85

−0.

3−

0.1

0.1

Height

s(H

eigh

t,1)

8 10 12 14 16 18 20

−0.

50.

00.

51.

0

Girth

s(G

irth

,2.5

1)I This is a simple example of a Generalized Additive Model.I Given the machinery for fitting GAMs a wide variety of

other models can also be estimated.

Page 5: GAMs, GAMMs and other penalized GLMs using mgcv in R

The general model

I Let y be a response variable, fj a smooth function ofpredictors xj and Lij a linear functional. A general model. . .

g(µi) = Aiα +∑

j

Lij fj + Zib, b ∼ N(0,Σθ), yi ∼ EF(µi , φ)

A and Z are model matrices, g is a known link function.I Popular examples are . . .

I g(µi) = Aiα +∑

j fj(xji) (Generalized additive model)I g(µi) = Aiα +

∑j fj(xji) + Zib, b ∼ N(0,Σθ) (GAMM)

I g(µi) =∫

hi(x)f (x)dx (Signal regression model)I g(µi) =

∑j fj(tji)xji (Varying coefficient model)

I The list goes on. . .

Page 6: GAMs, GAMMs and other penalized GLMs using mgcv in R

Representing the fj

I A convenient model representation arises byapproximating each function with a basis expansion

f (x) =K∑

k=1

γkbk (x) (bk known, e.g. spline)

I K is set to something ‘generous’ to avoid bias.I Some way of measuring the ‘wiggliness’ of each f is also

useful, for example∫

f ′′(x)2dx = γTSγ. (S known)

I For many models we will need an identifiability constraint,∑i f (xi) = 0. Automatic re-parameterization handles this.

Page 7: GAMs, GAMMs and other penalized GLMs using mgcv in R

Basis & penalty example

0.0 0.2 0.4 0.6 0.8 1.0

0.0

0.2

0.4

0.6

0.8

1.0

x

y

basis functions

0.0 0.2 0.4 0.6 0.8 1.0

−2

02

46

810

x

y

⌠⌡

f"(x)2dx = 377000

0.0 0.2 0.4 0.6 0.8 1.0

−2

02

46

810

x

y

⌠⌡

f"(x)2dx = 63000

0.0 0.2 0.4 0.6 0.8 1.0

−2

02

46

810

x

y

⌠⌡

f"(x)2dx = 200

Page 8: GAMs, GAMMs and other penalized GLMs using mgcv in R

Representing the model

I The general model is

g(µi) = Aiα +∑

j

Lij fj + Zib, b ∼ N(0,Σθ), yi ∼ EF(µi , φ)

I Given a basis for each fj , we can write∑

j Lij fj = Biγ,where B is determined by the basis functions and Lij , whileγ is a vector containing all the basis coefficients.

I Then the model becomes . . .

g(µi) = Aiα + Biγ + Zib, b ∼ N(0,Σθ), yi ∼ EF(µi , φ)

I Problem: if the bases were rich enough to give negligiblebias, then MLE will overfit.

Page 9: GAMs, GAMMs and other penalized GLMs using mgcv in R

Estimation strategies

1. Control prediction errorI Penalize the model likelihood, using the fj wiggliness

measures,∑

j λjγTSjγ, to control model prediction error.

I Given λ, θ, φ, this yields the maximum penalized likelihoodestimates (MPLE), α̂, γ̂, b̂.

I GCV or similar gives λ̂, θ̂, φ̂.2. Empirical Bayes

I Put exponential priors on function wiggliness so thatγ ∼ N

(0, (

∑j λjSj)

−/φ)

.

I Given λ, θ, φ the MAP estimates α̂, γ̂, b̂, are found byMPLE as under 1.

I Maximize the marginal likelihood (REML) to obtain λ̂, θ̂, φ̂.

Page 10: GAMs, GAMMs and other penalized GLMs using mgcv in R

Maximum Penalized Likelihood Estimation

I Define coefficient vector βT = (αT, γT, bT)

I Deviance is D(β) = 2{lmax − l(β)}I β is estimated by

β̂ = arg minβ

D(β) +∑

j

λjβTSjβ + βTΣ−

θ βφ

I Σ−θ is a zero padded version of Σ−1

θ so thatβTΣ−

θ β = bTΣ−1θ b.

I Similarly the Sj here are zero padded versions of theoriginals.

I In practice this optimization is by Penalized IRLS.

Page 11: GAMs, GAMMs and other penalized GLMs using mgcv in R

Estimating λ etc

I We treat β̂ as a function of λ, θ, φ for GCV/REMLoptimization.

I GCV or REML are optimized numerically, with each steprequiring evaluation of the corresponding β̂.

I Note that computationally the empirical Bayes approachis equivalent to generalized linear mixed modelling.

I However in most applications it is not appropriate to treatthe smooth functions as random quantities to be re-sampled from the prior at each replication of the data set.So the models are not frequentist random effects models.

Page 12: GAMs, GAMMs and other penalized GLMs using mgcv in R

More inference and degrees of freedom

I The Bayesian approach yields the large sample result

β ∼ N(β̂, Vβ)

where Vβ is a penalized version of the information matrixat β̂.

I Via an approach pioneered in Nychka (1988, JASA) it ispossible to show that CI’s based on this result have goodfrequentist properties.

I Effective degrees of freedom can also be computed. Let β̃denote the unpenalized MLE of β,

EDF =∑

j

∂β̂j

∂β̃j

Page 13: GAMs, GAMMs and other penalized GLMs using mgcv in R

Smooths for semi-parametric GLMs

I To build adequate semi-parametric GLMs requires that weuse functions with appropriate properties.

I In one dimension there are several alternatives, and notalot to choose between them.

I In 2 or more dimensions there is a major choice to make.I If the arguments of the smooth function are variables which

all have the same units (e.g. spatial location variables) thenan isotropic smooth may be appropriate. This will tend toexhibit the same degree of flexibility in all directions.

I If the relative scaling of the covariates of the smooth isessentially arbitrary (e.g. they are measured in differentunits), then scale invariant smooths should be used, whichdo not depend on this relative scaling.

Page 14: GAMs, GAMMs and other penalized GLMs using mgcv in R

SplinesI Many smooths covered here are based on splines. Here’s

the original underlying idea . . .

1.5 2.0 2.5 3.0

2.0

2.5

3.0

3.5

4.0

4.5

size

wea

r

I Mathematically the red curve is the function minimizing

i

(yi − f (xi))2 + λ

∫f ′′(x)2dx .

Page 15: GAMs, GAMMs and other penalized GLMs using mgcv in R

Splines have variable stiffness

I Varying the flexibility of the strip (i.e. varying λ) changesthe spline function curve.

1.5 2.0 2.5 3.0

2.0

3.0

4.0

size

wea

r

1.5 2.0 2.5 3.0

2.0

3.0

4.0

size

wea

r1.5 2.0 2.5 3.0

2.0

3.0

4.0

size

wea

r

1.5 2.0 2.5 3.02.

03.

04.

0size

wea

r

I But irrespective of λ the spline functions always have thesame basis.

Page 16: GAMs, GAMMs and other penalized GLMs using mgcv in R

Penalized regression splines

I Full splines have one basis function per data point.I This is computationally wasteful, when penalization

ensures that the effective degrees of freedom will be muchsmaller than this.

I Penalized regression splines simply use fewer spline basisfunctions. There are two alternatives:

1. Choose a representative subset of your data (the ‘knots’),and create the spline basis as if smoothing only those data.Once you have the basis, use it to smooth all the data.

2. Choose how many basis functions are to be used and thensolve the problem of finding the set of this many basisfunctions that will optimally approximate a full spline.

I’ll refer to 1 as knot based and 2 as eigen based.

Page 17: GAMs, GAMMs and other penalized GLMs using mgcv in R

Knot based example: "cr"

I In mgcv the "cr" basis is a knot based approximation tothe minimizer of

∑i(yi − f (xi))

2 + λ∫

f ′′(x)2dx — a cubicspline. "cc" is a cyclic version.

0.0 0.2 0.4 0.6 0.8 1.0

−0.

20.

00.

20.

40.

60.

81.

0

full spline basis

x

b i(x

)

0.0 0.2 0.4 0.6 0.8 1.0

0.5

1.0

1.5

2.0

2.5

3.0

data to smooth

x

y

0.0 0.2 0.4 0.6 0.8 1.0

0.5

1.0

1.5

2.0

2.5

3.0

function estimate: full black, regression red

x

s(x)

0.0 0.2 0.4 0.6 0.8 1.0

−0.

20.

00.

20.

40.

60.

81.

0

simple regression spline basis

x

b i(x

)

Page 18: GAMs, GAMMs and other penalized GLMs using mgcv in R

Eigen based example: "tp"I The "tp", thin plate regression spline basis is an eigen

approximation to a thin plate spline (including cubic splinein 1 dimension).

0.0 0.2 0.4 0.6 0.8 1.0

−0.

20.

00.

20.

40.

60.

81.

0

full spline basis

x

b i(x

)

0.0 0.2 0.4 0.6 0.8 1.0

0.5

1.0

1.5

2.0

2.5

3.0

data to smooth

x

y

0.0 0.2 0.4 0.6 0.8 1.0

0.5

1.0

1.5

2.0

2.5

3.0

function estimate: full black, regression red

x

s(x)

0.0 0.2 0.4 0.6 0.8 1.0

−1

01

2

thin plate regression spline basis

x

b i(x

)

Page 19: GAMs, GAMMs and other penalized GLMs using mgcv in R

1 dimensional smoothing in mgcv

I Various 1D smoothers are available in mgcv. . ."cr" knot based cubic regression spline."cc" cyclic version of above."ps" Eilers and Marx style p-splines: Evenly spacedB-spline basis, with discrete penalty on coefficients."ad" adaptive smoother in which strength of penaltyvaries with covariate."tp" thin plate regression spline. Optimal low rank eigenapprox. to a full spline: flexible order penalty derivative.

I Smooth classes can be added (?smooth.construct).

Page 20: GAMs, GAMMs and other penalized GLMs using mgcv in R

1D smooths compared

10 20 30 40 50

−10

0−

500

50

times

acce

l

tp

cr

ps

cc

ad

Page 21: GAMs, GAMMs and other penalized GLMs using mgcv in R

Isotropic smooths

I One way of generalizing splines from 1D to several D is toturn the flexible strip into a flexible sheet (hyper sheet).

I This results in a thin plate spline. It is an isotropic smooth.I Isotropy may be appropriate when different covariates are

naturally on the same scale.I In mgcv terms like s(x,z) generate such smooths.

x

0.2

0.4

0.6

0.8

z

0.2

0.4

0.6

0.8

linear predictor

0.0

0.2

0.4

0.6

0.8

x

0.2

0.4

0.6

0.8

z

0.2

0.4

0.6

0.8

linear predictor0.0

0.2

0.4

0.6

0.8

x

0.2

0.4

0.6

0.8

z

0.2

0.4

0.6

0.8

linear predictor

0.0

0.2

0.4

0.6

0.8

Page 22: GAMs, GAMMs and other penalized GLMs using mgcv in R

Thin plate spline details

I In 2 dimensions a thin plate spline is the functionminimizing

i

{yi − f (xi , zi)}2 + λ

∫f 2xx + 2f 2

xz + f 2zzdxdz

I This generalizes to any number of dimensions, d , and anyorder of differential, m, such that 2m > d + 1.

I Full thin plate spline has n unknown coefficients.I An optimal low rank eigen approximation is a thin plate

regression spline.I A t.p.r.s uses far fewer coefficients than a full spline, and is

therefore computationally efficient, while losing littlestatistical performance.

Page 23: GAMs, GAMMs and other penalized GLMs using mgcv in R

Scale invariant smoothing: tensor product smoothsI Isotropic smooths assume that a unit change in one

variable is equivalent to a unit change in another variable,in terms of function variability.

I When this is not the case, isotropic smooths can be poor.I Tensor product smooths generalize from 1D to several D

using a lattice of bendy strips, with different flexibility indifferent directions.

xzf(x,z)

Page 24: GAMs, GAMMs and other penalized GLMs using mgcv in R

Tensor product smooths

I Carefully constructed tensor product smooths are scaleinvariant.

I Consider constructing a smooth of x , z.I Start by choosing marginal bases and penalties, as if

constructing 1-D smooths of x and z. e.g.

fx(x) =∑

αiai(x), fz(z) =∑

βjbj(z),

Jx(fx) =

∫f ′′x (x)2dx = αTSxα & Jz(fz) = BTSzB

Page 25: GAMs, GAMMs and other penalized GLMs using mgcv in R

Marginal reparameterization

I Suppose we start with fz(z) =∑6

i=1 βjbj(z), on the left.

0.0 0.2 0.4 0.6 0.8 1.0

0.1

0.3

0.5

z

f z(z

)

0.0 0.2 0.4 0.6 0.8 1.0

0.1

0.3

0.5

z

f z( z

)I We can always re-parameterize so that its coefficients are

functions heights, at knots (right). Do same for fx .

Page 26: GAMs, GAMMs and other penalized GLMs using mgcv in R

Making fz depend on xI Can make fz a function of x by letting its coefficients vary

smoothly with x

xz

f(z)

xz

f(x,z)

Page 27: GAMs, GAMMs and other penalized GLMs using mgcv in R

The complete tensor product smoothI Use fx basis to let fz coefficients vary smoothly (left).I Construct in symmetric (see right).

xz

f(x,z)

xz

f(x,z)

Page 28: GAMs, GAMMs and other penalized GLMs using mgcv in R

Tensor product penalties - one per dimensionI x-wiggliness: sum marginal x penalties over red curves.I z-wiggliness: sum marginal z penalties over green curves.

xz

f(x,z)

xz

f(x,z)

Page 29: GAMs, GAMMs and other penalized GLMs using mgcv in R

Isotropic vs. tensor product comparison

x

24

6

8

z

2

4

6

8

z

0.0

0.2

0.4

Isotropic Thin Plate Spline

x

24

6

8

z

2

4

6

8

z

0.0

0.2

0.4

Tensor Product Spline

x

0.5

1.0

1.5

z

2

4

6

8

z

0.0

0.2

0.4

0.6

x

0.5

1.0

1.5

z

2

4

6

8

z

0.0

0.2

0.4

. . . each figure smooths the same data. The only modification isthat x has been divided by 5 in the bottom row.

Page 30: GAMs, GAMMs and other penalized GLMs using mgcv in R

Soap film smoothing

I Sometimes we require a smoother defined over a boundedregion, where it is important not to smooth acrossboundary features.

I A useful smoother can be constructed by considering adistorted soap film suspended from a flexible boundarywire.

−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y

−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y

−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y

−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y

−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y−1 0 1 2 3

−1.

0−

0.5

0.0

0.5

1.0

x

y

Page 31: GAMs, GAMMs and other penalized GLMs using mgcv in R

Markov Random fields

I Sometimes data are associated with discrete geographicregions.

I A neighbourhood structure on these regions can be usedto define a Markov random field, which induces a simplepenalty structure on region specific coefficients.

−4

−2

02

46

810

s(di

stric

t,2.7

1)

Page 32: GAMs, GAMMs and other penalized GLMs using mgcv in R

0 Dimensional smooths

I Having considered smoothing with respect to 1, 2 andseveral covariates, or a neighbourhood structure, considerthe case where we want to smooth without reference to acovariate (or graph).

I This amounts to simply shrinking some model coefficientstowards zero, as in ridge regression.

I In the context of penalized GLMs such zero dimensionalsmooths are equivalent to simple Gaussian random effects.

Page 33: GAMs, GAMMs and other penalized GLMs using mgcv in R

Specifying GAMs in R with mgcv

I library(mgcv) loads a semi-parametric GLM package.I gam(formula,family) is quite like glm.I The family argument specifies the distribution and link

function. e.g. Gamma(log).I The response variable and linear predictor structure are

specified in the model formula.I Response and parametric terms exactly as for lm or glm.I Smooth functions are specified by s or te terms. e.g.

log{E(yi)} = α + βxi + f (zi), yi ∼ Gamma,

is specified by. . .gam(y ~ x + s(z),family=Gamma(link=log))

Page 34: GAMs, GAMMs and other penalized GLMs using mgcv in R

More specification: by variables

I Smooth terms can accept a by variable argument, whichallows Lij fj terms other than just fj(xi).

I e.g. E(yi) = f1(zi)wi + f2(vi), yi ∼ Gaussian, becomesgam(y ~ s(z,by=w) + f(v))

i.e. f1(zi) is multiplied by wi in the linear predictor.I e.g. E(yi) = fj(xi , zi) if factor gi is of level j , becomes

gam(y ~ s(x,z,by=g) + g)

i.e. there is one smooth function for each level of factorvariable g, with each yi depending on just one of thesefunctions.

Page 35: GAMs, GAMMs and other penalized GLMs using mgcv in R

Yet more specification: a summation convention

I s and te smooth terms accept matrix arguments and byvariables to implement general Lij fj terms.

I If X and L are n × p matrices thens(X,by=L)

evaluates Lij fj =∑

k f (Xik )Lik for all i .I For example, consider data yi ∼ Poi where

log{E(yi)} =

∫ki(x)f (x)dx ' 1

h

p∑

k=1

ki(xk )f (xk )

(the xk are evenly spaced points).I Let Xik = xk ∀ i and Lik = ki(xk )/h. The model is fit by

gam(y ~ s(X,by=L),poisson)