Perform the following steps to fit a generalized additive model into data:
- First, load the mgcv package, which contains the gam function:
> install.packages("mgcv") > library(mgcv)
- Then, install the MASS package and load the Boston dataset:
> install.packages("MASS") > library(MASS) > attach(Boston) > str(Boston)
- Fit the regression using gam:
> fit = gam(dis ~ s(nox))
- Get the summary information of the fitted model:
> summary(fit)
Output:
Family: gaussian
Link function: identity
Formula:
dis ~ s(nox)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.79504 0.04507 84.21 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Approximate significance of smooth terms:
edf Ref.df F p-value
s(nox) 8.434 8.893 189 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
R-sq.(adj) = 0.768 Deviance explained = 77.2%
GCV = 1.0472 Scale est. = 1.0277 n = 506