R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("gam-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('gam') Loading required package: splines > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "anova.gam" > > ### * anova.gam > > flush(stderr()); flush(stdout()) > > ### Name: anova.gam > ### Title: Analysis of Deviance for a Generalized Additive Model > ### Aliases: anova.gam summary.gam > ### Keywords: models regression nonparametric > > ### ** Examples > > data(gam.data) > gam.object <- gam(y~s(x,6)+z,data=gam.data) > anova(gam.object) DF for Terms and F-values for Nonparametric Effects Df Npar Df Npar F Pr(F) (Intercept) 1 s(x, 6) 1 5 28.695 < 2.2e-16 *** z 1 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > gam.object2 <- update(gam.object, ~.-z) > anova(gam.object, gam.object2, test="Chisq") Analysis of Deviance Table Model 1: y ~ s(x, 6) + z Model 2: y ~ s(x, 6) Resid. Df Resid. Dev Df Deviance P(>|Chi|) 1 92 7.5745 2 93 7.6444 -1 -0.0699 0.3568 > > > > cleanEx(); ..nameEx <- "gam" > > ### * gam > > flush(stderr()); flush(stdout()) > > ### Name: gam > ### Title: Fitting Generalized Additive Models > ### Aliases: gam gam.fit > ### Keywords: models regression nonparametric > > ### ** Examples > > data(kyphosis) > gam(Kyphosis ~ s(Age,4) + Number, family = binomial, data=kyphosis, + trace=TRUE) GAM s.wam loop 1: deviance = 66.42083 GAM s.wam loop 2: deviance = 63.77248 GAM s.wam loop 3: deviance = 63.2519 GAM s.wam loop 4: deviance = 63.13396 GAM s.wam loop 5: deviance = 63.11019 GAM s.wam loop 6: deviance = 63.10754 GAM s.wam loop 7: deviance = 63.10733 GAM s.wam loop 8: deviance = 63.10732 GAM s.wam loop 9: deviance = 63.10732 Call: gam(formula = Kyphosis ~ s(Age, 4) + Number, family = binomial, data = kyphosis, trace = TRUE) Degrees of Freedom: 80 total; 75.00015 Residual Residual Deviance: 63.10732 > data(airquality) > gam(Ozone^(1/3) ~ lo(Solar.R) + lo(Wind, Temp), data=airquality, na=na.gam.replace) Warning in function (frame) : 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.6761 Residual Residual Deviance: 20.50298 > gam(Kyphosis ~ poly(Age,2) + s(Start), data=kyphosis, family=binomial, subset=Number>2) Call: gam(formula = Kyphosis ~ poly(Age, 2) + s(Start), family = binomial, data = kyphosis, subset = Number > 2) Degrees of Freedom: 68 total; 61.9999 Residual Residual Deviance: 44.56368 > 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.59177 -0.18050 0.01291 0.23940 0.51412 (Dispersion Parameter for gaussian family taken to be 0.0823) Null Deviance: 57.7496 on 99 degrees of freedom Residual Deviance: 7.5745 on 92 degrees of freedom AIC: 43.7497 Number of Local Scoring Iterations: 2 DF for Terms and F-values for Nonparametric Effects Df Npar Df Npar F Pr(F) (Intercept) 1 s(x, 6) 1 5 28.695 < 2.2e-16 *** z 1 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > plot(gam.object,se=TRUE) > data(gam.newdata) > predict(gam.object,type="terms",newdata=gam.newdata) s(x, 6) z 1 0.44378432 0.039427729 2 0.75359604 0.028567521 3 0.85469236 0.017707313 4 0.80512152 0.006847105 5 0.51279083 -0.004013103 6 0.07523078 -0.014873311 7 -0.42621357 -0.025733519 8 -0.80004353 -0.036593727 9 -0.96322154 -0.047453935 10 -1.01599595 -0.058314143 attr(,"constant") [1] 0.634896 > > > > cleanEx(); ..nameEx <- "gam.control" > > ### * gam.control > > flush(stderr()); flush(stdout()) > > ### Name: gam.control > ### Title: Auxilliary for controlling GAM fitting > ### Aliases: gam.control > ### Keywords: models regression nonparametric > > ### ** Examples > > ## Not run: gam(formula, family, control = gam.control(bf.maxit=15)) > ## Not run: gam(formula, family, bf.maxit = 15) # these are equivalent > > > > cleanEx(); ..nameEx <- "gam.data" > > ### * gam.data > > flush(stderr()); flush(stdout()) > > ### Name: gam.data > ### Title: Simulated dataset for gam > ### Aliases: gam.data gam.newdata > ### Keywords: datasets > > ### ** Examples > > data(gam.data) > gam(y ~ s(x) + z, data=gam.data) Call: gam(formula = y ~ s(x) + z, data = gam.data) Degrees of Freedom: 99 total; 93.99996 Residual Residual Deviance: 7.90771 > > > > cleanEx(); ..nameEx <- "lo" > > ### * lo > > flush(stderr()); flush(stdout()) > > ### Name: lo > ### Title: Specify a loess fit in a GAM formula > ### Aliases: lo gam.lo > ### Keywords: models regression nonparametric > > ### ** Examples > > y ~ Age + lo(Start, span=.5) y ~ Age + lo(Start, span = 0.5) > # fit Start using a loess smooth with a span of 0.5. > y ~ lo(Age) + lo(Start, Number) y ~ lo(Age) + lo(Start, Number) > y ~ lo(Age, 0.5) # the argument name for span is not needed. y ~ lo(Age, 0.5) > > > > cleanEx(); ..nameEx <- "na.gam.replace" > > ### * na.gam.replace > > flush(stderr()); flush(stdout()) > > ### Name: na.gam.replace > ### Title: Missing Data Filter for GAMs > ### Aliases: na.gam.replace > ### Keywords: models regression nonparametric > > ### ** Examples > > data(airquality) > gam(Ozone^(1/3) ~ lo(Solar.R) + lo(Wind, Temp), data=airquality, na=na.gam.replace) Warning in function (frame) : 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.6761 Residual Residual Deviance: 20.50298 > > > > cleanEx(); ..nameEx <- "plot.gam" > > ### * plot.gam > > flush(stderr()); flush(stdout()) > > ### Name: plot.gam > ### Title: Plot Components of a GAM Object > ### Aliases: plot.gam preplot.gam plot.preplot.gam > ### Keywords: models regression nonparametric > > ### ** Examples > > data(gam.data) > gam.object <- gam(y ~ s(x,6) + z,data=gam.data) > plot(gam.object,se=TRUE) > data(gam.newdata) > preplot(gam.object,newdata=gam.newdata) Warning in newdata.predict.gam(object, newdata, type, dispersion, se.fit, : No standard errors (currently) for gam predictions with newdata $"s(x, 6)" $x [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 $y 1 2 3 4 5 6 0.44378432 0.75359604 0.85469236 0.80512152 0.51279083 0.07523078 7 8 9 10 -0.42621357 -0.80004353 -0.96322154 -1.01599595 $se.y NULL $xlab [1] "x" $ylab [1] "s(x, 6)" attr(,"class") [1] "preplot.gam" $z $x [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 $y 1 2 3 4 5 6 0.039427729 0.028567521 0.017707313 0.006847105 -0.004013103 -0.014873311 7 8 9 10 -0.025733519 -0.036593727 -0.047453935 -0.058314143 $se.y NULL $xlab [1] "z" $ylab [1] "partial for z" attr(,"class") [1] "preplot.gam" attr(,"class") [1] "preplot.gam" > > > > cleanEx(); ..nameEx <- "predict.gam" > > ### * predict.gam > > flush(stderr()); flush(stdout()) > > ### Name: predict.gam > ### Title: Predict method for GAM fits > ### Aliases: predict.gam > ### Keywords: models regression nonparametric > > ### ** Examples > > data(gam.data) > gam.object <- gam(y ~ s(x,6) + z, data=gam.data) > predict(gam.object) # extract the additive predictors 1 2 3 4 5 6 0.80174236 1.08594720 1.31185931 -0.06023088 1.03175402 1.18776824 7 8 9 10 11 12 -0.34196089 0.91816180 1.14185041 -0.29130792 -0.39471934 1.44862019 13 14 15 16 17 18 -0.10965211 0.47271279 0.59043489 1.42774901 1.35225538 1.28011864 19 20 21 22 23 24 -0.37702822 1.47743529 1.38816587 1.14343124 -0.15894245 -0.25786206 25 26 27 28 29 30 1.41672592 -0.26040124 0.16411710 -0.27613061 1.48808923 -0.01668390 31 32 33 34 35 36 -0.08602763 1.51644195 -0.32403266 -0.18498080 1.30911786 -0.13643300 37 38 39 40 41 42 -0.26455800 1.46929420 0.86226736 0.15782227 0.13535744 0.01135680 43 44 45 46 47 48 0.51406338 1.06271839 -0.29758233 1.45362893 1.03180497 1.49352792 49 50 51 52 53 54 0.77868272 1.46033254 -0.30039613 -0.33160621 0.73381631 1.47103493 55 56 57 58 59 60 -0.32700989 1.43849166 -0.31925805 1.50499770 -0.24600726 0.43842092 61 62 63 64 65 66 1.44918383 -0.16657598 0.74918250 0.60880911 1.34655176 -0.33211608 67 68 69 70 71 72 -0.34074073 0.12581265 0.38596596 0.56651408 -0.32542086 1.34193342 73 74 75 76 77 78 0.87463872 0.89007891 1.39620365 1.49045042 -0.27479459 1.38673785 79 80 81 82 83 84 1.41351374 1.45097308 -0.29389320 0.96967205 1.44159356 1.38500100 85 86 87 88 89 90 0.67737547 0.92695518 -0.33725594 1.52324210 -0.32514876 0.56679832 91 92 93 94 95 96 1.32211422 0.85794848 0.78036577 1.20135834 1.37958726 1.02704183 97 98 99 100 0.55290083 1.45672096 -0.32176147 0.05277387 > data(gam.newdata) > predict(gam.object, gam.newdata, type="terms") s(x, 6) z 1 0.44378432 0.039427729 2 0.75359604 0.028567521 3 0.85469236 0.017707313 4 0.80512152 0.006847105 5 0.51279083 -0.004013103 6 0.07523078 -0.014873311 7 -0.42621357 -0.025733519 8 -0.80004353 -0.036593727 9 -0.96322154 -0.047453935 10 -1.01599595 -0.058314143 attr(,"constant") [1] 0.634896 > > > > cleanEx(); ..nameEx <- "s" > > ### * s > > flush(stderr()); flush(stdout()) > > ### Name: s > ### Title: Specify a Smoothing Spline Fit in a GAM Formula > ### Aliases: s gam.s > ### Keywords: models regression nonparametric > > ### ** Examples > > # fit Start using a smoothing spline with 4 df. > y ~ Age + s(Start, 4) y ~ Age + s(Start, 4) > # fit log(Start) using a smoothing spline with 5 df. > y ~ Age + s(log(Start), df=5) y ~ Age + s(log(Start), df = 5) > > > > cleanEx(); ..nameEx <- "step.gam" > > ### * step.gam > > flush(stderr()); flush(stdout()) > > ### Name: step.gam > ### Title: Stepwise model builder for GAM > ### Aliases: step.gam > ### Keywords: models regression nonparametric > > ### ** Examples > > data(gam.data) > gam.object <- gam(y~x+z, data=gam.data) > step.object <-step.gam(gam.object, scope=list("x"=~1+x+s(x,4)+s(x,6)+s(x,12),"z"=~1+z+s(z,4))) Start: y ~ x + z; AIC= 127.7316 Trial: y ~ 1 + z; AIC= 234.8821 Trial: y ~ s(x, 4) + z; AIC= 44.0546 Trial: y ~ x + 1; AIC= 126.5148 Trial: y ~ x + s(z, 4); AIC= 131.0277 Step : y ~ s(x, 4) + z ; AIC= 44.0546 Trial: y ~ s(x, 6) + z; AIC= 43.7497 Trial: y ~ s(x, 4) + 1; AIC= 43.1801 Trial: y ~ s(x, 4) + s(z, 4); AIC= 47.1022 Step : y ~ s(x, 4) ; AIC= 43.1801 Trial: y ~ s(x, 6) + 1; AIC= 42.6683 Step : y ~ s(x, 6) ; AIC= 42.6683 Trial: y ~ s(x, 12) + 1; AIC= 45.5601 > > > > ### *