gam.check summary(resid_fit) Randomised quantile residuals Example - - PowerPoint PPT Presentation

gam check summary resid fit randomised quantile residuals
SMART_READER_LITE
LIVE PREVIEW

gam.check summary(resid_fit) Randomised quantile residuals Example - - PowerPoint PPT Presentation

gam.check summary(resid_fit) Randomised quantile residuals Example Fitting to residuals Checking basis size Residual checks A model that converges Sometimes basis size isn't the issue... Response vs. tted values Example of


slide-1
SLIDE 1

"perhaps the most important part of applied statistical "perhaps the most important part of applied statistical modelling" modelling"

Simon Wood Simon Wood

2 / 36 2 / 36

Model checking

As with detection functions, checking is important Checking doesn't mean your model is right Want to know the model conforms to assumptions What assumptions should we check? 3 / 36

Convergence Convergence

4 / 36 4 / 36

Convergence

Fitting the GAM involves an optimization By default this is REstricted Maximum Likelihood (REML) score Sometimes this can go wrong R will warn you! 5 / 36

A model that converges

gam.check(dsm_tw_xy_depth) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-3.456333e-05,1.051004e-05] ## (score 374.7249 & scale 4.172176). ## Hessian positive definite, eigenvalue range [1.179219,301.267]. ## Model rank = 39 / 39 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x,y) 29.00 11.11 0.65 <2e-16 *** ## s(Depth) 9.00 3.84 0.81 0.37 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

6 / 36

A bad model

Error in while (mean(ldxx/(ldxx + ldss)) > 0.4) { : missing value where TRUE/FALSE needed In addition: Warning message: In sqrt(w) : NaNs produced Error in while (mean(ldxx/(ldxx + ldss)) > 0.4) { : missing value where TRUE/FALSE needed

This is rare 7 / 36

The Folk Theorem of Statistical Computing The Folk Theorem of Statistical Computing

"most statistical computational problems are due not to "most statistical computational problems are due not to the algorithm being used but rather the model itself" the algorithm being used but rather the model itself"

Andrew Gelman Andrew Gelman

8 / 36 8 / 36

Folk Theorem anecdata

Often if there are fitting problems, you're asking too much from your data Model is too complicated Too little data Try something simpler, see what happens 9 / 36

Basis size Basis size

10 / 36 10 / 36

Basis size (k)

Set k per term e.g. s(x, k=10) or s(x, y, k=100) Penalty removes "extra" wigglyness up to a point! (But computation is slower with bigger k) 11 / 36

Checking basis size

gam.check(dsm_x_tw) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-3.196351e-06,4.485625e-07] ## (score 409.936 & scale 6.041307). ## Hessian positive definite, eigenvalue range [0.7645492,302.127]. ## Model rank = 10 / 10 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x) 9.00 4.96 0.76 0.38

12 / 36

Increasing basis size

dsm_x_tw_k <- dsm(count~s(x, k=20), ddf.obj=df, segment.data=segs, observation.data=obs, family=tw()) gam.check(dsm_x_tw_k) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-2.30124e-08,3.930703e-09] ## (score 409.9245 & scale 6.033913). ## Hessian positive definite, eigenvalue range [0.7678456,302.0336]. ## Model rank = 20 / 20 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x) 19.00 5.25 0.76 0.35

13 / 36

Sometimes basis size isn't the issue...

Generally, double k and see what happens Didn't increase the EDF much here Other things can cause low "p-value" and "k-index" Increasing k can cause problems (nullspace) 14 / 36

k is a maximum

Don't worry about things being too wiggly k gives the maximum complexity Penalty deals with the rest 15 / 36

Residuals Residuals

16 / 36 16 / 36

What are residuals?

Generally residuals = observed value - fitted value BUT hard to see patterns in these "raw" residuals Need to standardise deviance residuals Expect these residuals

⇒ ∼ 𝑂(0, 1)

17 / 36

Why are residuals important?

Structure in the residuals means your model didn't capture something Maybe a missing covariate Model doesn't describe the data well 18 / 36

Residuals vs. covariates

19 / 36

Residuals vs. covariates (boxplots)

20 / 36

Fitting to residuals

Refit our model but with the residuals as response Response is normal (for deviance residuals) What pattern is left in the residuals? 21 / 36

Example

Example model with NPP and Depth

# get data refit_dat <- dsm_depth_npp$data # make residuals column refit_dat$resid <- residuals(dsm_depth_npp) # fit a model (same model) resid_fit <- gam(resid~s(Depth, bs="ts", k=20) + s(NPP, bs="ts", k=20), family=gaussian(), data=refit_dat, method="REML")

22 / 36

summary(resid_fit)

## ## Family: gaussian ## Link function: identity ## ## Formula: ## resid ~ s(Depth, bs = "ts", k = 20) + s(NPP, bs = "ts", k = 20) ## ## Parametric coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -0.49454 0.03274 -15.1 <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) 2.56621 19 1.230 4.9e-06 *** ## s(NPP) 0.03322 19 0.002 0.316 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## R-sq.(adj) = 0.0241 Deviance explained = 2.67% ## -REML = 1362 Scale est. = 1.0174 n = 949

23 / 36

What's going on there?

Something unexplained going on? Maybe Depth + NPP is not enough? Add other smooths (s(x, y)? ) Increase k? 24 / 36

Other residual checking Other residual checking

25 / 36 25 / 36

gam.check

26 / 36

Shortcomings

gam.check can be helpful "Resids vs. linear pred" is victim of artifacts Need an alternative "Randomised quanitle residuals" rqgam.check Exactly normal residuals 27 / 36

Randomised quantile residuals

28 / 36

Example of "bad" plots

29 / 36

Example of "bad" plots

30 / 36

Residual checks

Looking for patterns (not artifacts) This can be tricky Need to use a mixture of techniques Cycle through checks, make changes recheck 31 / 36

Observed vs. expected

class: inverse, middle, center 32 / 36

Response vs. tted values

gam.check "response vs. fitted values" BUT smooths are "wrong" everywhere in particular 33 / 36

Summarize over covariate chunks

On average the smooth is right Check aggregations of count Here detection function has Beaufort as factor

  • bs_exp(dsm_bad, "Beaufort_f")

## [0,1] (1,2] (2,3] (3,4] (4,5] ## Observed 1.00000 95.45000 103.5500 34.70000 4.000000 ## Expected 20.28781 54.57573 136.3581 53.98742 5.949304

  • bs_exp(dsm_good, "Beaufort_f")

## [0,1] (1,2] (2,3] (3,4] (4,5] ## Observed 1.0000 95.45000 103.5500 34.70000 4.000000 ## Expected 6.8887 45.18587 118.5747 53.81458 4.909644

34 / 36

Observed vs. expected for environmental covariates

Just need to specify the cutpoints

  • bs_exp(dsm_bad, "Depth", c(0, 1000, 2000, 3000, 4000, 6000))

## (0,1e+03] (1e+03,2e+03] (2e+03,3e+03] (3e+03,4e+03] (4e+03,6e+03] ## Observed 4.00000 52.53333 139.16667 35.00000 8.00000 ## Expected 85.65231 37.98341 63.40892 53.78726 30.32642

  • bs_exp(dsm_good, "Depth", c(0, 1000, 2000, 3000, 4000, 6000))

## (0,1e+03] (1e+03,2e+03] (2e+03,3e+03] (3e+03,4e+03] (4e+03,6e+03] ## Observed 4.000000 52.53333 139.1667 35.00000 8.000000 ## Expected 5.308628 48.14915 128.7962 38.76013 8.359456

35 / 36

Summary

Convergence Rarely an issue Basis size k is a maximum Double and see what happens Residuals Deviance and randomised quantile check for artifacts Observed vs. expected Compare aggregate information 36 / 36

Lecture 4: Model checking Lecture 4: Model checking

1 / 36 1 / 36

slide-2
SLIDE 2

"perhaps the most important part of applied statistical "perhaps the most important part of applied statistical modelling" modelling"

Simon Wood Simon Wood

2 / 36 2 / 36

slide-3
SLIDE 3

Model checking

As with detection functions, checking is important Checking doesn't mean your model is right Want to know the model conforms to assumptions What assumptions should we check? 3 / 36

slide-4
SLIDE 4

Convergence Convergence

4 / 36 4 / 36

slide-5
SLIDE 5

Convergence

Fitting the GAM involves an optimization By default this is REstricted Maximum Likelihood (REML) score Sometimes this can go wrong R will warn you! 5 / 36

slide-6
SLIDE 6

A model that converges

gam.check(dsm_tw_xy_depth) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-3.456333e-05,1.051004e-05] ## (score 374.7249 & scale 4.172176). ## Hessian positive definite, eigenvalue range [1.179219,301.267]. ## Model rank = 39 / 39 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x,y) 29.00 11.11 0.65 <2e-16 *** ## s(Depth) 9.00 3.84 0.81 0.37 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

6 / 36

slide-7
SLIDE 7

A bad model

Error in while (mean(ldxx/(ldxx + ldss)) > 0.4) { : missing value where TRUE/FALSE needed In addition: Warning message: In sqrt(w) : NaNs produced Error in while (mean(ldxx/(ldxx + ldss)) > 0.4) { : missing value where TRUE/FALSE needed

This is rare 7 / 36

slide-8
SLIDE 8

The Folk Theorem of Statistical Computing The Folk Theorem of Statistical Computing

"most statistical computational problems are due not to "most statistical computational problems are due not to the algorithm being used but rather the model itself" the algorithm being used but rather the model itself"

Andrew Gelman Andrew Gelman

8 / 36 8 / 36

slide-9
SLIDE 9

Folk Theorem anecdata

Often if there are fitting problems, you're asking too much from your data Model is too complicated Too little data Try something simpler, see what happens 9 / 36

slide-10
SLIDE 10

Basis size Basis size

10 / 36 10 / 36

slide-11
SLIDE 11

Basis size (k)

Set k per term e.g. s(x, k=10) or s(x, y, k=100) Penalty removes "extra" wigglyness up to a point! (But computation is slower with bigger k) 11 / 36

slide-12
SLIDE 12

Checking basis size

gam.check(dsm_x_tw) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-3.196351e-06,4.485625e-07] ## (score 409.936 & scale 6.041307). ## Hessian positive definite, eigenvalue range [0.7645492,302.127]. ## Model rank = 10 / 10 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x) 9.00 4.96 0.76 0.38

12 / 36

slide-13
SLIDE 13

Increasing basis size

dsm_x_tw_k <- dsm(count~s(x, k=20), ddf.obj=df, segment.data=segs, observation.data=obs, family=tw()) gam.check(dsm_x_tw_k) ## ## Method: REML Optimizer: outer newton ## full convergence after 7 iterations. ## Gradient range [-2.30124e-08,3.930703e-09] ## (score 409.9245 & scale 6.033913). ## Hessian positive definite, eigenvalue range [0.7678456,302.0336]. ## Model rank = 20 / 20 ## ## Basis dimension (k) checking results. Low p-value (k-index<1) may ## indicate that k is too low, especially if edf is close to k'. ## ## k' edf k-index p-value ## s(x) 19.00 5.25 0.76 0.35

13 / 36

slide-14
SLIDE 14

Sometimes basis size isn't the issue...

Generally, double k and see what happens Didn't increase the EDF much here Other things can cause low "p-value" and "k-index" Increasing k can cause problems (nullspace) 14 / 36

slide-15
SLIDE 15

k is a maximum

Don't worry about things being too wiggly k gives the maximum complexity Penalty deals with the rest 15 / 36

slide-16
SLIDE 16

Residuals Residuals

16 / 36 16 / 36

slide-17
SLIDE 17

What are residuals?

Generally residuals = observed value - fitted value BUT hard to see patterns in these "raw" residuals Need to standardise deviance residuals Expect these residuals

⇒ ∼ 𝑂(0, 1)

17 / 36

slide-18
SLIDE 18

Why are residuals important?

Structure in the residuals means your model didn't capture something Maybe a missing covariate Model doesn't describe the data well 18 / 36

slide-19
SLIDE 19

Residuals vs. covariates

19 / 36

slide-20
SLIDE 20

Residuals vs. covariates (boxplots)

20 / 36

slide-21
SLIDE 21

Fitting to residuals

Refit our model but with the residuals as response Response is normal (for deviance residuals) What pattern is left in the residuals? 21 / 36

slide-22
SLIDE 22

Example

Example model with NPP and Depth

# get data refit_dat <- dsm_depth_npp$data # make residuals column refit_dat$resid <- residuals(dsm_depth_npp) # fit a model (same model) resid_fit <- gam(resid~s(Depth, bs="ts", k=20) + s(NPP, bs="ts", k=20), family=gaussian(), data=refit_dat, method="REML")

22 / 36

slide-23
SLIDE 23

summary(resid_fit)

## ## Family: gaussian ## Link function: identity ## ## Formula: ## resid ~ s(Depth, bs = "ts", k = 20) + s(NPP, bs = "ts", k = 20) ## ## Parametric coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -0.49454 0.03274 -15.1 <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) 2.56621 19 1.230 4.9e-06 *** ## s(NPP) 0.03322 19 0.002 0.316 ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## R-sq.(adj) = 0.0241 Deviance explained = 2.67% ## -REML = 1362 Scale est. = 1.0174 n = 949

23 / 36

slide-24
SLIDE 24

What's going on there?

Something unexplained going on? Maybe Depth + NPP is not enough? Add other smooths (s(x, y)? ) Increase k? 24 / 36

slide-25
SLIDE 25

Other residual checking Other residual checking

25 / 36 25 / 36

slide-26
SLIDE 26

gam.check

26 / 36

slide-27
SLIDE 27

Shortcomings

gam.check can be helpful "Resids vs. linear pred" is victim of artifacts Need an alternative "Randomised quanitle residuals" rqgam.check Exactly normal residuals 27 / 36

slide-28
SLIDE 28

Randomised quantile residuals

28 / 36

slide-29
SLIDE 29

Example of "bad" plots

29 / 36

slide-30
SLIDE 30

Example of "bad" plots

30 / 36

slide-31
SLIDE 31

Residual checks

Looking for patterns (not artifacts) This can be tricky Need to use a mixture of techniques Cycle through checks, make changes recheck 31 / 36

slide-32
SLIDE 32

Observed vs. expected

class: inverse, middle, center 32 / 36

slide-33
SLIDE 33

Response vs. tted values

gam.check "response vs. fitted values" BUT smooths are "wrong" everywhere in particular 33 / 36

slide-34
SLIDE 34

Summarize over covariate chunks

On average the smooth is right Check aggregations of count Here detection function has Beaufort as factor

  • bs_exp(dsm_bad, "Beaufort_f")

## [0,1] (1,2] (2,3] (3,4] (4,5] ## Observed 1.00000 95.45000 103.5500 34.70000 4.000000 ## Expected 20.28781 54.57573 136.3581 53.98742 5.949304

  • bs_exp(dsm_good, "Beaufort_f")

## [0,1] (1,2] (2,3] (3,4] (4,5] ## Observed 1.0000 95.45000 103.5500 34.70000 4.000000 ## Expected 6.8887 45.18587 118.5747 53.81458 4.909644

34 / 36

slide-35
SLIDE 35

Observed vs. expected for environmental covariates

Just need to specify the cutpoints

  • bs_exp(dsm_bad, "Depth", c(0, 1000, 2000, 3000, 4000, 6000))

## (0,1e+03] (1e+03,2e+03] (2e+03,3e+03] (3e+03,4e+03] (4e+03,6e+03] ## Observed 4.00000 52.53333 139.16667 35.00000 8.00000 ## Expected 85.65231 37.98341 63.40892 53.78726 30.32642

  • bs_exp(dsm_good, "Depth", c(0, 1000, 2000, 3000, 4000, 6000))

## (0,1e+03] (1e+03,2e+03] (2e+03,3e+03] (3e+03,4e+03] (4e+03,6e+03] ## Observed 4.000000 52.53333 139.1667 35.00000 8.000000 ## Expected 5.308628 48.14915 128.7962 38.76013 8.359456

35 / 36

slide-36
SLIDE 36

Summary

Convergence Rarely an issue Basis size k is a maximum Double and see what happens Residuals Deviance and randomised quantile check for artifacts Observed vs. expected Compare aggregate information 36 / 36