GAM model

This example is using material from Noam Ross available at the following url: https://noamross.github.io/gams-in-r-course/chapter1.

The data

source("mpg.R")
head(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
##   length width height weight eng.type cylinders eng.cc fuel.sys bore stroke
## 1  168.8  64.1   48.8   2548     dohc      four    130     mpfi 3.47   2.68
## 2  168.8  64.1   48.8   2548     dohc      four    130     mpfi 3.47   2.68
## 3  171.2  65.5   52.4   2823     ohcv       six    152     mpfi 2.68   3.47
## 4  176.6  66.2   54.3   2337      ohc      four    109     mpfi 3.19   3.40
## 5  176.6  66.4   54.3   2824      ohc      five    136     mpfi 3.19   3.40
## 6  177.3  66.3   53.1   2507      ohc      five    136     mpfi 3.19   3.40
##   comp.ratio  hp  rpm city.mpg hw.mpg price
## 1        9.0 111 5000       21     27 13495
## 2        9.0 111 5000       21     27 16500
## 3        9.0 154 5000       19     26 16500
## 4       10.0 102 5500       24     30 13950
## 5        8.0 115 5500       18     22 17450
## 6        8.5 110 5500       19     25 15250

The GAM model

mod_city3 <- gam(
  city.mpg ~ s(weight, by = drive) + s(length, by = drive) + s(price, by = drive) + drive,
  data = mpg, method = "REML")

The flextable

ft <- as_flextable(mod_city3)
ft