How to do it...

Perform the following steps to fit a generalized additive model into data:

  1. First, load the mgcv package, which contains the gam function:
        > install.packages("mgcv")
        > library(mgcv)  
  1. Then, install the MASS package and load the Boston dataset:
        > install.packages("MASS")
        > library(MASS)
        > attach(Boston)
        > str(Boston)  
  1. Fit the regression using gam:
        > fit = gam(dis ~ s(nox))  
  1. 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset