Nonlinear Modeling with Generalized Additive Models (GAMs) in R
Noam Ross
Senior Research Scientist, EcoHealth Alliance
mpg
symbol loss make fuel aspir doors ... eng.loc wb length width
1 3 NA alfa-romero gas std two ... front 88.6 168.8 64.1
2 3 NA alfa-romero gas std two ... front 88.6 168.8 64.1
3 1 NA alfa-romero gas std two ... front 94.5 171.2 65.5
4 2 164 audi gas std four ... front 99.8 176.6 66.2
5 2 164 audi gas std four ... front 99.4 176.6 66.4
6 2 NA audi gas std two ... front 99.8 177.3 66.3
7 1 158 audi gas std four ... front 105.8 192.7 71.4
8 1 NA audi gas std four ... front 105.8 192.7 71.4
9 1 158 audi gas turbo four ... front 105.8 192.7 71.4
10 0 NA audi gas turbo two ... front 99.5 178.2 67.9
...
model <- gam(hw.mpg ~ s(weight), data = mpg,
method = "REML")
model <- gam(hw.mpg ~ s(weight), data = mpg,
method = "REML")
model2 <- gam(hw.mpg ~ s(weight) + s(length), data = mpg,
method = "REML")
model2 <- gam(hw.mpg ~ s(weight) + s(length), data = mpg,
method = "REML")
model2 <- gam(hw.mpg ~ s(weight) + length, data = mpg,
method = "REML")
model2b <- gam(hw.mpg ~ s(weight) + s(length, sp = 1000),
data = mpg, method = "REML")
model3 <- gam(hw.mpg ~ s(weight) + fuel, data = mpg,
method = "REML")
model4 <- gam(hw.mpg ~ s(weight, by = fuel), data = mpg,
method = "REML")
model4b <- gam(hw.mpg ~ s(weight, by = fuel) + fuel,
data = mpg, method = "REML")
Nonlinear Modeling with Generalized Additive Models (GAMs) in R