Fitting a generalized additive model to data
Generalized Additive Model (GAM), which is used to fit generalized additive models, can be viewed as a semiparametric extension of GLM. While GLM holds the assumption that there is a linear relationship between dependent and independent variables, GAM fits the model on account of the local behavior of data. As a result, GAM has the ability to deal with highly nonlinear relationships between dependent and independent variables. In the following recipe, we will introduce how to fit regression using a generalized additive model.
Getting ready
We need to prepare a data frame containing variables, where one of the variables is a response variable and the others may be predictor variables.
How to do it...
Perform the following steps to fit a generalized additive model into data:
- First, load the
mgcv
package, which contains thegam
function:
> install.packages("mgcv") > library(mgcv)
- Then, install the
MASS
package and load theBoston...