## Load gam
library(gam)
## Loading required package: splines
## Loaded gam 1.09
## Kyphosis fit 1
data(kyphosis)
fitKyphosis1 <- gam(Kyphosis ~ s(Age,4) + Number, family = binomial, data=kyphosis,trace=TRUE)
## GAM s.wam loop 1: deviance = 66.42
## GAM s.wam loop 2: deviance = 63.77
## GAM s.wam loop 3: deviance = 63.25
## GAM s.wam loop 4: deviance = 63.13
## GAM s.wam loop 5: deviance = 63.11
## GAM s.wam loop 6: deviance = 63.11
## GAM s.wam loop 7: deviance = 63.11
## GAM s.wam loop 8: deviance = 63.11
## GAM s.wam loop 9: deviance = 63.11
summary(fitKyphosis1)
##
## Call: gam(formula = Kyphosis ~ s(Age, 4) + Number, family = binomial,
## data = kyphosis, trace = TRUE)
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.784 -0.719 -0.359 -0.120 2.320
##
## (Dispersion Parameter for binomial family taken to be 1)
##
## Null Deviance: 83.23 on 80 degrees of freedom
## Residual Deviance: 63.11 on 75 degrees of freedom
## AIC: 75.11
##
## Number of Local Scoring Iterations: 9
##
## Anova for Parametric Effects
## Df Sum Sq Mean Sq F value Pr(>F)
## s(Age, 4) 1 0.4 0.38 0.45 0.5063
## Number 1 7.4 7.44 8.69 0.0043 **
## Residuals 75 64.2 0.86
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Anova for Nonparametric Effects
## Npar Df Npar Chisq P(Chi)
## (Intercept)
## s(Age, 4) 3 5.9 0.12
## Number
plot(fitKyphosis1)
## Kyphosis fit 2
fitKyphosis2 <- gam(Kyphosis ~ poly(Age,2) + s(Start), data=kyphosis, family=binomial, subset=Number>2)
summary(fitKyphosis2)
##
## Call: gam(formula = Kyphosis ~ poly(Age, 2) + s(Start), family = binomial,
## data = kyphosis, subset = Number > 2)
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.7969 -0.4169 -0.1952 -0.0233 2.0983
##
## (Dispersion Parameter for binomial family taken to be 1)
##
## Null Deviance: 77.05 on 68 degrees of freedom
## Residual Deviance: 44.56 on 62 degrees of freedom
## AIC: 58.56
##
## Number of Local Scoring Iterations: 10
##
## Anova for Parametric Effects
## Df Sum Sq Mean Sq F value Pr(>F)
## poly(Age, 2) 2 6.4 3.18 4.57 0.0141 *
## s(Start) 1 5.2 5.18 7.45 0.0083 **
## Residuals 62 43.1 0.70
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Anova for Nonparametric Effects
## Npar Df Npar Chisq P(Chi)
## (Intercept)
## poly(Age, 2)
## s(Start) 3 6.96 0.073 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(fitKyphosis2)
## Air quality fit
data(airquality)
gam(Ozone^(1/3) ~ lo(Solar.R) + lo(Wind, Temp), data=airquality, na=na.gam.replace)
## Warning: 37 observations omitted due to missing values in the response
## Call:
## gam(formula = Ozone^(1/3) ~ lo(Solar.R) + lo(Wind, Temp), data = airquality,
## na.action = na.gam.replace)
##
## Degrees of Freedom: 115 total; 102.7 Residual
## Residual Deviance: 20.5
## gam.data fit
data(gam.data)
gam.object <- gam(y ~ s(x,6) + z,data=gam.data)
summary(gam.object)
##
## Call: gam(formula = y ~ s(x, 6) + z, data = gam.data)
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.5918 -0.1805 0.0129 0.2394 0.5141
##
## (Dispersion Parameter for gaussian family taken to be 0.082)
##
## Null Deviance: 57.75 on 99 degrees of freedom
## Residual Deviance: 7.575 on 92 degrees of freedom
## AIC: 43.75
##
## Number of Local Scoring Iterations: 2
##
## Anova for Parametric Effects
## Df Sum Sq Mean Sq F value Pr(>F)
## s(x, 6) 1 38.2 38.2 464.10 <2e-16 ***
## z 1 0.1 0.1 1.02 0.31
## Residuals 92 7.6 0.1
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Anova for Nonparametric Effects
## Npar Df Npar F Pr(F)
## (Intercept)
## s(x, 6) 5 28.7 <2e-16 ***
## z
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(gam.object,se=TRUE)
## gam.data fit
data(gam.newdata)
predict(gam.object,type="terms",newdata=gam.newdata)
## s(x, 6) z
## 1 0.44379 0.039428
## 2 0.75361 0.028568
## 3 0.85468 0.017707
## 4 0.80513 0.006847
## 5 0.51279 -0.004013
## 6 0.07524 -0.014873
## 7 -0.42623 -0.025734
## 8 -0.80006 -0.036594
## 9 -0.96322 -0.047454
## 10 -1.01595 -0.058315
## attr(,"constant")
## [1] 0.6349