Visualizing categorical-continuous interactions

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Noam Ross

Senior Research Scientist, EcoHealth Alliance

Categorical-continuous interactions

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

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Factor-smooths

model4c <- gam(hw.mpg ~ s(weight, fuel, bs = "fs"),
               data = mpg,
               method = "REML")
Nonlinear Modeling with Generalized Additive Models (GAMs) in R
summary(model4c)
Family: gaussian 
Link function: identity 
Formula:
hw.mpg ~ s(weight, fuel, bs = "fs")

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   28.644      7.615   3.761 0.000223 ***
 ---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Approximate significance of smooth terms:
                edf Ref.df     F p-value    
s(weight,fuel) 7.71     19 53.12  <2e-16 ***
 ---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R-sq.(adj) =  0.832   Deviance explained = 83.8%
-REML = 518.54  Scale est. = 7.9735    n = 205
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Plotting factor-smooths

plot(model4c)
vis.gam(model4c, theta = 125, plot.type = "persp")

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...