Introduction to Generalized Additive Models Noam Ross Senior - - PowerPoint PPT Presentation

introduction to generalized additive models
SMART_READER_LITE
LIVE PREVIEW

Introduction to Generalized Additive Models Noam Ross Senior - - PowerPoint PPT Presentation

DataCamp Nonlinear Modeling in R with GAMs NONLINEAR MODELING IN R WITH GAMS Introduction to Generalized Additive Models Noam Ross Senior Research Scientist, EcoHealth Alliance DataCamp Nonlinear Modeling in R with GAMs Trade-offs in Model


slide-1
SLIDE 1

DataCamp Nonlinear Modeling in R with GAMs

Introduction to Generalized Additive Models

NONLINEAR MODELING IN R WITH GAMS

Noam Ross

Senior Research Scientist, EcoHealth Alliance

slide-2
SLIDE 2

DataCamp Nonlinear Modeling in R with GAMs

Trade-offs in Model Building

slide-3
SLIDE 3

DataCamp Nonlinear Modeling in R with GAMs

Non-linear Relationships

slide-4
SLIDE 4

DataCamp Nonlinear Modeling in R with GAMs

Nonlinear Relationships (2)

linear_mod <- lm(y ~ x, data = my_data)

slide-5
SLIDE 5

DataCamp Nonlinear Modeling in R with GAMs

Nonlinear Relationships (3)

library(mgcv) gam_mod <- gam(y ~ s(x), data = my_data)

slide-6
SLIDE 6

DataCamp Nonlinear Modeling in R with GAMs

Basis Functions

slide-7
SLIDE 7

DataCamp Nonlinear Modeling in R with GAMs

Basis Functions (2)

gam_mod <- gam(y ~ s(x), data = my_data) coef(gam_mod) (Intercept) s(x2).1 s(x2).2 7.814448 5.272290 5.104941 s(x2).3 s(x2).4 s(x2).5 1.271135 1.720561 -1.180613 s(x2).6

  • 2.676133
slide-8
SLIDE 8

DataCamp Nonlinear Modeling in R with GAMs

Let's practice!

NONLINEAR MODELING IN R WITH GAMS

slide-9
SLIDE 9

DataCamp Nonlinear Modeling in R with GAMs

Basis Functions and Smoothing

NONLINEAR MODELING IN R WITH GAMS

Noam Ross

Senior Research Scientist, EcoHealth Alliance

slide-10
SLIDE 10

DataCamp Nonlinear Modeling in R with GAMs

Getting the right fit

Close to the data (avoiding under- fitting) Not fitting the noise (avoiding over- fitting)

slide-11
SLIDE 11

DataCamp Nonlinear Modeling in R with GAMs

Balancing Wiggliness

Fit = Likelihood − λ × Wiggliness

slide-12
SLIDE 12

DataCamp Nonlinear Modeling in R with GAMs

Choosing the Right Smoothing Parameter

slide-13
SLIDE 13

DataCamp Nonlinear Modeling in R with GAMs

Smoothing Syntax

Setting a fixed smoothing parameter Smoothing via restricted maximum likelihood

gam(y ~ s(x), data = dat, sp = 0.1) gam(y ~ s(x, sp = 0.1), data = dat) gam(y ~ s(x), data = dat, method = "REML")

slide-14
SLIDE 14

DataCamp Nonlinear Modeling in R with GAMs

Number of basis functions

slide-15
SLIDE 15

DataCamp Nonlinear Modeling in R with GAMs

Basis Function Syntax

Setting number of basis functions Use the defaults

gam(y ~ s(x, k = 3), data = dat, method = "REML") gam(y ~ s(x, k = 10), data = dat, method = "REML") gam(y ~ s(x), data = dat, method = "REML")

slide-16
SLIDE 16

DataCamp Nonlinear Modeling in R with GAMs

Let's practice!

NONLINEAR MODELING IN R WITH GAMS

slide-17
SLIDE 17

DataCamp Nonlinear Modeling in R with GAMs

Multiple Regression with GAMs

NONLINEAR MODELING IN R WITH GAMS

Noam Ross

Senior Research Scientist, EcoHealth Alliance

slide-18
SLIDE 18

DataCamp Nonlinear Modeling in R with GAMs

Our Working Dataset: mpg

mpg symbol loss make fuel aspir doors style drive eng.loc wb 1 3 NA alfa-romero gas std two convertible rwd front 88.6 2 3 NA alfa-romero gas std two convertible rwd front 88.6 3 1 NA alfa-romero gas std two hatchback rwd front 94.5 4 2 164 audi gas std four sedan fwd front 99.8 5 2 164 audi gas std four sedan 4wd front 99.4 6 2 NA audi gas std two sedan fwd front 99.8 7 1 158 audi gas std four sedan fwd front 105.8 8 1 NA audi gas std four wagon fwd front 105.8 9 1 158 audi gas turbo four sedan fwd front 105.8 10 0 NA audi gas turbo two hatchback 4wd front 99.5 ...

slide-19
SLIDE 19

DataCamp Nonlinear Modeling in R with GAMs

Multiple Smooths (1)

model <- gam(hw.mpg ~ s(weight), data = mpg, method = "REML")

slide-20
SLIDE 20

DataCamp Nonlinear Modeling in R with GAMs

Multiple Smooths (2)

model <- gam(hw.mpg ~ s(weight), data = mpg, method = "REML") model2 <- gam(hw.mpg ~ s(weight) + s(length), data = mpg, method = "REML")

slide-21
SLIDE 21

DataCamp Nonlinear Modeling in R with GAMs

Multiple Smooths (3)

model2 <- gam(hw.mpg ~ s(weight) + s(length), data = mpg, method = "REML")

slide-22
SLIDE 22

DataCamp Nonlinear Modeling in R with GAMs

Linear terms

model2 <- gam(hw.mpg ~ s(weight) + length, data = mpg, method = "REML")

slide-23
SLIDE 23

DataCamp Nonlinear Modeling in R with GAMs

Linear Terms (2)

model2b <- gam(hw.mpg ~ s(weight) + s(length, sp = 1000), data = mpg, method = "REML")

slide-24
SLIDE 24

DataCamp Nonlinear Modeling in R with GAMs

Categorical Terms (1)

model3 <- gam(hw.mpg ~ s(weight) + fuel, data = mpg, method = "REML")

slide-25
SLIDE 25

DataCamp Nonlinear Modeling in R with GAMs

Categorical Terms (2)

model4 <- gam(hw.mpg ~ s(weight, by = fuel), data = mpg, method = "REML")

slide-26
SLIDE 26

DataCamp Nonlinear Modeling in R with GAMs

Categorical Terms (3)

model4b <- gam(hw.mpg ~ s(weight, by = fuel) + fuel, data = mpg, method = "REML")

slide-27
SLIDE 27

DataCamp Nonlinear Modeling in R with GAMs

Let's practice!

NONLINEAR MODELING IN R WITH GAMS