Two-dimensional smooths and spatial data

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Noam Ross

Senior Research Scientist, EcoHealth Alliance

Interactions

$$ \LARGE y = \beta_1 x_1 + \beta_2 x_2 + \red{\beta_3 x_1 x_2} $$

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Interactions in GAMs

$$ \LARGE y = s(x_1, x_2) $$

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Syntax for interactions

gam(y ~ s(x1, x2),                   # <-- 2 variables 
        data = dat, method = "REML")
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Mixing interaction and single terms

gam(y ~ s(x1, x2) + s(x3),
        data = dat, method = "REML")
gam(y ~ s(x1, x2) + x3 + x4,
        data = dat, method = "REML")
Nonlinear Modeling with Generalized Additive Models (GAMs) in R
Family: gaussian 
Link function: identity 

Formula:
y ~ s(x1, x2)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.34256    0.01646   20.82   <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(x1,x2) 10.82   14.9 14.37  <2e-16 *** #<-- Interaction
 ---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.519   Deviance explained = 54.5%
GCV = 0.057564  Scale est. = 0.054161  n = 200
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Spatial data

meuse
         x      y cadmium copper lead zinc   elev       dist   om ffreq ...
1   181072 333611    11.7     85  299 1022  7.909 0.00135803 13.6     1 ...
2   181025 333558     8.6     81  277 1141  6.983 0.01222430 14.0     1 ...
3   181165 333537     6.5     68  199  640  7.800 0.10302900 13.0     1 ...
4   181298 333484     2.6     81  116  257  7.655 0.19009400  8.0     1  ...
5   181307 333330     2.8     48  117  269  7.480 0.27709000  8.7     1  ...
6   181390 333260     3.0     61  137  281  7.791 0.36406700  7.8     1  ...
7   181165 333370     3.2     31  132  346  8.217 0.19009400  9.2     1  ...
8   181027 333363     2.8     29  150  406  8.490 0.09215160  9.5     1  ...
9   181060 333231     2.4     37  133  347  8.668 0.18461400 10.6     1  ...
10  181232 333168     1.6     24   80  183  9.049 0.30970200  6.3     1  ... 
?sp::meuse
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Let's practice!

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Preparing Video For Download...