68
Generalized Additive Models David L Miller

Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

GeneralizedAdditiveModelsDavidLMiller

Page 2: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

OverviewWhatisaGAM?

Whatissmoothing?

HowdoGAMswork?(Roughly)

Fittingandplottingsimplemodels

Page 3: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

WhatisaGAM?

Page 4: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

GeneralizedAdditiveModelsGeneralized:manyresponsedistributions

Additive:termsaddtogether

Models:well,it'samodel…

Page 5: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

ToGAMsfromGLMsandLMs

Page 6: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

(Generalized)LinearModelsModelsthatlooklike:

(describetheresponse, ,aslinearcombinationofthecovariates, ,withanoffset)

Wecanmake anyexponentialfamilydistribution(Normal,Poisson,etc).

Errorterm isnormallydistributed(usually).

= + + + … +yi β0 x1iβ1 x2iβ2 ϵiyi

xji

∼yi

ϵi

Page 7: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whybotherwithanythingmorecomplicated?!

Page 8: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Isthislinear?

Page 9: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Isthislinear?Maybe?lm(y ~ x1, data=dat)

Page 10: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatcanwedo?

Page 11: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Addingaquadraticterm?lm(y ~ x1 + poly(x1, 2), data=dat)

Page 12: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Isthissustainable?Addinginquadratic(andhigherterms)canmakesense

Thisfeelsabitadhoc

Betterifwehadaframeworktodealwiththeseissues?

Page 13: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

[drumroll]

Page 14: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatdoesamodellooklike?

where , (fornow)

Rememberthatwe'remodellingthemeanofthisdistribution!

Calltheaboveequationthelinearpredictor

= + ( ) +yi β0 ∑jsj xji ϵi

∼ N(0, )ϵi σ2 ∼ Normalyi

Page 15: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Okay,butwhataboutthese"s"things?Think =smooth

Wanttomodelthecovariatesflexibly

Covariatesandresponsenotnecessarilylinearlyrelated!

Wantsome“wiggles”

s

Page 16: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Okay,butwhataboutthese"s"things?Think =smooth

Wanttomodelthecovariatesflexibly

Covariatesandresponsenotnecessarilylinearlyrelated!

Wantsome“wiggles”

s

Page 17: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatissmoothing?

Page 18: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Straightlinesvs.interpolationWantalinethatis“close”toallthedata

Don'twantinterpolation–weknowthereis“error”

Balancebetweeninterpolationand“fit”

Page 19: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

SplinesFunctionsmadeofother,simplerfunctions

Basisfunctions ,estimate

Makesthemath(s)mucheasier

bk βks(x) = (x)∑K

k=1 βkbk

Page 20: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

DesignmatricesWeoftenwritemodelsas

isourdata

areparametersweneedtoestimate

ForaGAMit'sthesame

hascolumnsforeachbasis,evaluatedateachobservation

again,thisisthelinearpredictor

XβXβ

X

Page 21: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

MeasuringwigglynessVisually:

Lotsofwiggles==NOTSMOOTH

Straightline==VERYSMOOTH

Howdowedothismathematically?

Derivatives!

(Calculuswasausefulclassafterall!)

Page 22: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Wigglynessbyderivatives

Page 23: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatwasthatgreybit?

(Takesomederivativesofthesmoothandintegratethemover )

(Turnsoutwecanalwayswritethisas ,sothe isseparatefromthederivatives)

(Call thepenaltymatrix)

dx∫ℝ ( )f (x)∂2

x∂2

2

x

SββT β

S

Page 24: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Makingwigglynessmattermeasureswigglyness

“Likelihood”measuresclosenesstothedata

Penaliseclosenesstothedata…

Useasmoothingparametertodecideonthattrade-off…

Estimatethe termsbutpenaliseobjective

“closenesstodata”+penalty

SββT

λ SββT

βk

Page 25: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Smoothingparameter

Page 26: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

SmoothingparameterselectionManymethods:AIC,Mallow's ,GCV,ML,REML

Recommendation,basedonsimulationandpractice:

UseREMLorML

Reiss&Ogden(2009),Wood(2011)

Cp

Page 27: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

MaximumwigglinessWecansetbasiscomplexityor“size”( )

Maximumwigglyness

Smoothshaveeffectivedegreesoffreedom(EDF)

EDF<

Set “largeenough”

Penaltydoestherest

Moreonthisinabit…

k

kk

Page 28: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response
Page 29: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

GAMsummaryStraightlinessuck—wewantwiggles

Uselittlefunctions(basisfunctions)tomakebigfunctions(smooths)

Needtomakesureyoursmoothsarewigglyenough

Useapenaltytotradeoffwiggliness/generality

Page 30: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

FittingGAMsinpractice

Page 31: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

TranslatingmathsintoRAsimpleexample:

where

Let'spretendthat

linearpredictor:formula = y ~ s(x) + s(w)responsedistribution:family=gaussian()data:data=some_data_frame

= + s(x) + s(w) +yi β0 ϵi∼ N(0, )ϵi σ2

∼ Normalyi

Page 32: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Puttingthattogether

method="REML"usesREMLforsmoothnessselection(defaultis"GCV.Cp")

my_model <- gam(y ~ s(x) + s(w), family = gaussian(), data = some_data_frame, method = "REML")

Page 33: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whataboutapracticalexample?

Page 34: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

PantropicalspotteddolphinsExampletakenfromMilleretal(2013)

hasabetteranalysis

Simpleexamplehere,ignoringallkindsofimportantstuff!

Paperappendix

Page 35: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

InferentialaimsHowmanydolphinsarethere?

Wherearethedolphins?

Whataretheyinterestedin?

Page 36: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Asimpledolphinmodel

countisafunctionofdepth

off.setistheeffortexpendedwehavecountdata,tryquasi-Poissondistribution

library(mgcv)dolphins_depth <- gam(count ~ s(depth) + offset(off.set), data = mexdolphins, family = quasipoisson(), method = "REML")

Page 37: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatdidthatdo?summary(dolphins_depth)

Family: quasipoisson Link function: log

Formula:count ~ s(depth) + offset(off.set)

Parametric coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -18.2344 0.8949 -20.38 <2e-16 ***---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms: edf Ref.df F p-value s(depth) 6.592 7.534 2.329 0.0224 *---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) = 0.0545 Deviance explained = 26.4%-REML = 948.28 Scale est. = 145.34 n = 387

Page 38: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Plottingplot(dolphins_depth)Dashedlinesindicate+/-2standarderrors

Rugplot

Onthelinkscale

EDFon axisy

Page 39: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

ThinplateregressionsplinesDefaultbasis

Onebasisfunctionperdatapoint

Reduce#basisfunctions(eigendecomposition)

Fittingonreducedproblem

Multidimensional

Wood(2003)

Page 40: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

BivariatetermsAssumedanadditivestructure

Nointeraction

Wecanspecifys(x,y)(ands(x,y,z,...))(Assumingisotropyhere…)

Page 41: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

AddingatermAddasurfaceforlocation( and )

Justuse+foranextratermx y

dolphins_depth_xy <- gam(count ~ s(depth) + s(x, y) + offset(off.set), data = mexdolphins, family=quasipoisson(), method="REML")

Page 42: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Summarysummary(dolphins_depth_xy)

Family: quasipoisson Link function: log

Formula:count ~ s(depth) + s(x, y) + offset(off.set)

Parametric coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -19.1933 0.9468 -20.27 <2e-16 ***---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms: edf Ref.df F p-values(depth) 6.804 7.669 1.461 0.191s(x,y) 23.639 26.544 1.358 0.114

R-sq.(adj) = 0.22 Deviance explained = 49.9%-REML = 923.9 Scale est. = 79.474 n = 387

Page 43: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Plotting

scale=0:eachplotondifferentscalepages=1:plottogether

plot(dolphins_depth_xy, scale=0, pages=1)

Page 44: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Plotting2dterms...erm...

select=pickswhichsmoothtoplot

plot(dolphins_depth_xy, select=2, cex=2, asp=1, lwd=2)

Page 45: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Let'strysomethingdifferent

scheme=2muchbetterforbivariateterms

vis.gam()ismuchmoregeneral

plot(dolphins_depth_xy, select=2, cex=2, asp=1, lwd=2, scheme=2)

Page 46: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Morecomplexplotspar(mfrow=c(1,2))vis.gam(dolphins_depth_xy, view=c("depth","x"), too.far=0.1, phi=30, theta=45)vis.gam(dolphins_depth_xy, view=c("depth","x"), plot.type="contour", too.far=0.1,asp=1/1000)

Page 47: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Fitting/plottingGAMssummarygamdoesalltheworkverysimilartoglmsindicatesasmoothterm

plotcangivesimpleplots

vis.gamformoreadvancedstuff

Page 48: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Prediction

Page 49: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatisaprediction?Evaluatethemodel,ataparticularcovariatecombination

Answering(e.g.)thequestion“atagivendepth,howmanydolphins?”

Steps:

1. evaluatethe terms

2. movetotheresponsescale(exponentiate?Donothing?)

3. (multiplyanyoffsetetc)

s(…)

Page 50: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Exampleofpredictioninmaths:

Model:

Dropinthevaluesof (and )

inR:

buildadata.framewithusepredict()

(se.fit=TRUEgivesastandarderrorforeachprediction)

= exp( + s( , ) + s( ))counti Ai β0 xi yi Depthix, y, Depth A

x, y, Depth, A

preds <- predict(my_model, newdat=my_data, type="response")

Page 51: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Backtothedolphins...

Page 52: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Wherearethedolphins?

(ggplot2codeincludedintheslidesource)

dolphin_preds <- predict(dolphins_depth_xy, newdata=preddata, type="response")

Page 53: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

PredictionsummaryEvaluatethefittedmodelatagivenpoint

Canevaluatemanyatonce(data.frame)Don'tforgetthetype=...argument!

Obtainper-predictionstandarderrorwithse.fit

Page 54: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whataboutuncertainty?

Page 55: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Withoutuncertainty,we'renotdoingstatistics

Page 56: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Wheredoesuncertaintycomefrom?:uncertaintyinthesplineparameters

:uncertaintyinthesmoothingparameter

(Traditionallywe'veonlyaddressedtheformer)

(Newtoolsletusaddressthelatter…)

βλ

Page 57: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

ParameteruncertaintyFromtheory:

(caveat:thenormalityisonlyapproximatefornon-normalresponse)

Whatdoesthismean?Varianceforeachparameter.

Inmgcv:vcov(model)returns .

β ∼ N( , )β̂ Vβ

Page 58: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Whatcanwedothisthis?confidenceintervalsinplotstandarderrorsusingse.fitderivedquantities?(seebibliography)

Page 59: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response
Page 60: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Thelpmatrix,magic,etcForregularpredictions:

form usingthepredictiondata,evaluatingbasisfunctionsaswego.

(Needtoapplythelinkfunctionto )

Butthe fundoesn'tstopthere…

=η̂p Lpβ̂

Lp

η̂p

Lp

Page 61: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

[[mathematicsintensifies]]

Page 62: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

VarianceandlpmatrixTogetvarianceonthescaleofthelinearpredictor:

pre-/post-multiplicationshiftsthevariancematrixfromparameterspacetolinearpredictor-space.

(Canthenpre-/post-multiplybyderivativesofthelinktoputvarianceonresponsescale)

=Vη̂ LTpVβ̂Lp

Page 63: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Simulatingparametershasadistribution,wecansimulateβ

Page 64: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

UncertaintyinsmoothingparameterRecentworkbySimonWood

“smoothingparameteruncertaintycorrected”versionof

Inafittedmodel,wehave:

$Vpwhatwegotwithvcov$Vcthecorrectedversion

Stillexperimental

Vβ̂

Page 65: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

VariancesummaryEverythingcomesfromvarianceofparameters

Needtore-project/scalethemtogetthequantitiesweneed

mgcvdoesmostofthehardworkforus

Fancystuffpossiblewithalittlemaths

Canincludeuncertaintyinthesmoothingparametertoo

Page 66: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

Okay,thatwasalotofinformation

Page 67: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

SummaryGAMsareGLMsplussomeextrawiggles

Needtomakesurethingsarejustwigglyenough

Basis+penaltyisthewaytodothis

Fittinglookslikeglmwithextras()terms

Moststuffcomesdowntomatrixalgebra,thatmgcvsheildsyoufrom

Todofancystuff,getinsidethematrices

Page 68: Generalized Additive Models · What is smoothing? How do GAMs work? (Roughly) Fitting and plotting simple models. What is a GAM? Generalized Additive Models Generalized: many response

COFFEE