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("lokern-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('lokern') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "glkerns" > > ### * glkerns > > flush(stderr()); flush(stdout()) > > ### Name: glkerns > ### Title: Kernel Regression Smoothing with Adaptive Plug-in Bandwidth > ### Aliases: glkerns > ### Keywords: smooth > > ### ** Examples > > data(xSim)## linear plus an exponential peak, see help(xSim) > n <- length(xSim) > tt <- ((1:n) - 1/2)/n # equidistant x ==> is.rand = FALSE > str(gk <- glkerns(tt, xSim, is.rand = FALSE)) List of 11 $ x : num [1:75] 0.00667 0.02000 0.03333 0.04667 0.06000 ... $ y : num [1:75] 1.97 1.90 1.64 1.43 2.40 ... $ bandwidth: num 0.0485 $ x.out : num [1:300] 0.00667 0.00997 0.01327 0.01657 0.01987 ... $ est : num [1:300] 1.89 1.88 1.87 1.87 1.86 ... $ sig : num 0.145 $ deriv : int 0 $ korder : int 2 $ xl : num 0.067 $ xu : num 0.933 $ s : num [1:76] 4.34e-19 1.33e-02 2.67e-02 4.00e-02 5.33e-02 ... > cat("glkerns() bandwidth:",format(gk$bandwidth, dig=10),"\n") glkerns() bandwidth: 0.04845240731 > ## local bandwidth: fit is very similar : > str(lk <- lokerns(tt, xSim, is.rand = FALSE)) List of 11 $ x : num [1:75] 0.00667 0.02000 0.03333 0.04667 0.06000 ... $ y : num [1:75] 1.97 1.90 1.64 1.43 2.40 ... $ bandwidth: num [1:300] 0.0535 0.0531 0.0527 0.0523 0.0519 ... $ x.out : num [1:300] 0.00667 0.00997 0.01327 0.01657 0.01987 ... $ est : num [1:300] 1.88 1.88 1.87 1.87 1.86 ... $ sig : num 0.145 $ deriv : int 0 $ korder : int 2 $ xl : num 0.067 $ xu : num 0.933 $ s : num [1:76] 4.34e-19 1.33e-02 2.67e-02 4.00e-02 5.33e-02 ... > > cols <- c(gl="PaleGreen", lo="Firebrick") > plot(lk$x.out, lk$bandwidth, axes = FALSE, xlab="", ylab="", + ylim=c(0,max(lk$bandwidth)), type="h", col = "gray90") > axis(4); mtext("bandwidth(s)", side=4) > lines(lk$x.out, lk$bandwidth, col = cols["lo"], lty = 3) > abline( h = gk$bandwidth, col = cols["gl"], lty = 4) > par(new=TRUE) > plot(tt, xSim, main = "global and local bandwidth kernel regression") > lines(gk$x.out, gk$est, col = cols["gl"], lwd = 1.5) > lines(lk$x.out, lk$est, col = cols["lo"]) > # the red curve (local bw) is very slightly better > legend(0.7,4.4, c("global bw","local bw"), col = cols, lwd=1) > > ## This should look > op <- par(mfrow = c(3,1), mar = .1 + c(4,4,2,1), oma = c(0,0,3,0), + mgp = c(1.5, 0.6,0)) > plot(gk, main = expression(paste("Data & ", hat(f)))) > lines(gk$x.out, gk$est, col = "blue", lwd = 1.5) > gk1 <- glkerns(tt, xSim, deriv = 1, is.rand = FALSE) > plot(gk1$x.out, gk1$est, col = "green", lwd = 1.5, type = "l", + main = expression(widehat(paste(f,"'")))) > abline(h=0, col="gray", lty = 3) > gk2 <- glkerns(tt, xSim, deriv = 2, is.rand = FALSE) > plot(gk2$x.out, gk2$est, col = "orange", lwd = 1.5, type = "l", + main = expression(widehat(paste(f,"''")))) > abline(h=0, col="gray", lty = 3) > mtext("Example from www.unizh.ch/biostat/..../kernf77.html",side=3, + outer = TRUE, cex = 1, font = par("font.main")) > > par(op) > data(cars) > plot(dist ~ speed, data = cars, + main = "Global Plug-In Bandwidth") > myfit <- glkerns(cars$ speed, cars$ dist) > lines(myfit$x.out, myfit$est, col=2) > mtext(paste("bandwidth = ", format(myfit$bandwidth, dig = 4))) > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "lokerns" > > ### * lokerns > > flush(stderr()); flush(stdout()) > > ### Name: lokerns > ### Title: Kernel Regression Smoothing with Local Plug-in Bandwidth > ### Aliases: lokerns > ### Keywords: smooth > > ### ** Examples > > data(cars) > lofit <- lokerns(cars$ speed, cars$ dist) > (sb <- summary(lofit$bandwidth)) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.9345 1.3880 1.4650 1.5140 1.8060 1.9490 > op <- par(fg = "gray90", tcl = -0.2, mgp = c(3,.5,0)) > plot(lofit$band, ylim=c(0,3*sb["Max."]), type="h",#col="gray90", + ann = FALSE, axes = FALSE) > > if(R.version$major > 1 || R.version$minor >= 3.0) + boxplot(lofit$bandwidth, add = TRUE, at = 304, boxwex = 8, + col = "gray90",border="gray", pars = list(axes = FALSE)) > axis(4, at = c(0,pretty(sb)), col.axis = "gray") > par(op) > par(new=TRUE) > plot(dist ~ speed, data = cars, + main = "Local Plug-In Bandwidth Vector") > lines(lofit$x.out, lofit$est, col=4) > mtext(paste("bandwidth in [", + paste(format(sb[c(1,6)], dig = 3),collapse=","), + "]; Median b.w.=",formatC(sb["Median"]))) > > > > graphics::par(get("par.postscript", env = .CheckExEnv)) > cleanEx(); ..nameEx <- "varNPreg" > > ### * varNPreg > > flush(stderr()); flush(stdout()) > > ### Name: varNPreg > ### Title: Nonparametric Variance Estimator > ### Aliases: varNPreg > ### Keywords: nonparametric regression > > ### ** Examples > > x <- sort(runif(100)) > y <- sin(pi*x) + rnorm(100)/10 > str(ve <- varNPreg(x,y)) List of 3 $ res : num [1:100] 0.0773 -0.0773 0.1050 -0.1358 0.1080 ... $ snr : num 0.918 $ sigma2: num 0.0088 > > > > cleanEx(); ..nameEx <- "varest" > > ### * varest > > flush(stderr()); flush(stdout()) > > ### Name: varest > ### Title: Nonparametric Variance Estimator > ### Aliases: varest > ### Keywords: nonparametric > > ### ** Examples > > x <- sort(runif(100)) > y <- sin(pi*x) + rnorm(100)/10 > str(ve <- varest(x,y)) List of 3 $ res : num [1:100] 0.0773 -0.0773 0.1050 -0.1358 0.1080 ... $ snr : num 0.918 $ sigma2: num 0.0088 > > > > cleanEx(); ..nameEx <- "xSim" > > ### * xSim > > flush(stderr()); flush(stdout()) > > ### Name: xSim > ### Title: Simulated Linear plus Exponential Peak > ### Aliases: xSim > ### Keywords: datasets > > ### ** Examples > > data(xSim) > plot(xSim, main = "`xSim' - N=75 simulated linear + peak") > > > > ### *