Visualizing a generalized additive model
In this recipe, we demonstrate how to add a gam
fitted regression line to a scatter plot. In addition, we visualize the gam
fit using the plot
function.
Getting ready
Complete the previous recipe by assigning a gam
fitted model to the fit
variable.
How to do it...
Perform the following steps to visualize the generalized additive model:
- Generate a scatter plot using the
nox
anddis
variables:
> plot(nox, dis)

Scatter plot of variable nox against dis
- Add the regression to the scatter plot:
> x = seq(0, 1, length = 500) > y = predict(fit, data.frame(nox = x)) > lines(x, y, col = "red", lwd = 2)

Fitted regression of gam on a scatter plot
- Alternatively, you can plot the fitted model using the
plot
function:
> plot(fit)

Plot of fitted gam
How it works...
To visualize the fitted regression, we first generate a scatter plot using the dis
and nox
variables. Then, we generate the sequence of x-axis, and respond y...