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("GenKern-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('GenKern') Loading required package: KernSmooth KernSmooth 2.22 installed Copyright M. P. Wand 1997 Loading GenKern version 1.1 Copyright Lucy and Aykroyd 2000 last update October 2004 requires KernSmooth Package GenKern installed > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "KernSec" > > ### * KernSec > > flush(stderr()); flush(stdout()) > > ### Name: KernSec > ### Title: Univariate kernel density estimate > ### Aliases: KernSec > ### Keywords: distribution smooth > > ### ** Examples > > x <- c(2,4,6,8,10) > > z <- KernSec(x) # simplest invocation > plot(z$xords, z$yden, type="l") > > z <- KernSec(x, xbandwidth=2, range.x=c(0,8)) > plot(z$xords, z$yden, type="l") > > # local bandwidths > ords <- seq(from=0, to=14, length=100) > bands <- x/15 > z <- KernSec(x, xbandwidth=bands, range.x=ords) > plot(z$xords, z$yden, type="l") # should plot a wriggly line > > bands <- seq(from=1, to=4, length=100) # improvise a pilot estimate > z <- KernSec(x, xbandwidth=bands, range.x=ords) > plot(z$xords, z$yden, type="l") > > > > cleanEx(); ..nameEx <- "KernSur" > > ### * KernSur > > flush(stderr()); flush(stdout()) > > ### Name: KernSur > ### Title: Bivariate kernel density estimation > ### Aliases: KernSur > ### Keywords: distribution smooth > > ### ** Examples > > x <- c(2,4,6,8,10) # make up some x-y data > y <- x > > # calculate and plot a surface with zero correlation based on above data > op <- KernSur(x,y, xgridsize=50, ygridsize=50, correlation=0, + xbandwidth=1, ybandwidth=1, range.x=c(0,13), range.y=c(0,13)) > image(op$xords, op$yords, op$zden, col=terrain.colors(100), axes=TRUE) > contour(op$xords, op$yords, op$zden, add=TRUE) > box() > > # re-calculate and re-plot the above using a 0.8 correlation > op <- KernSur(x,y, xgridsize=50, ygridsize=50, correlation=0.8, + xbandwidth=1, ybandwidth=1, range.x=c(0,13), range.y=c(0,13)) > image(op$xords, op$yords, op$zden, col=terrain.colors(100), axes=TRUE) > contour(op$xords, op$yords, op$zden, add=TRUE) > box() > > # calculate and plot a surface of the above data with an ascending > # correlation and bandwidths and a vector of equally spaced ordinates > bands <- c(1,1.1,1.2,1.3,1.0) > cors <- c(0,-0.2,-0.4,-0.6, -0.7) > rnge.x <- seq(from=0, to=13, length=100) > > op <- KernSur(x,y, xgridsize=50, ygridsize=50, correlation=cors, + xbandwidth=bands, ybandwidth=bands, range.x=rnge.x, range.y=c(0,13)) > image(op$xords, op$yords, op$zden, col=terrain.colors(100), axes=TRUE) > contour(op$xords, op$yords, op$zden, add=TRUE) > box() > > > > > cleanEx(); ..nameEx <- "nearest" > > ### * nearest > > flush(stderr()); flush(stdout()) > > ### Name: nearest > ### Title: Index of a vector nearest in value to a supplied value > ### Aliases: nearest > ### Keywords: arith > > ### ** Examples > > # make up a vector > x <- c(1,2,2,2,2,2,3,4,5,6,7,8,9,10) > # conventional useage - xval within range should return 9 > nearest(x, 4.7) [1] 9 > # xval - outside the range of x should return 14 > nearest(x, 12.7, outside=TRUE) [1] 14 > # many 'nearest' values in x - should return - 2 3 4 5 6 > nearest(x, 1.7) [1] 2 3 4 5 6 > # make x[3] an NA > x[3] <- NA > # returns - 2 4 5 6 - by enabling na.rm > nearest(x, 1.7, na.rm=TRUE) [1] 2 4 5 6 > > > > cleanEx(); ..nameEx <- "per" > > ### * per > > flush(stderr()); flush(stdout()) > > ### Name: per > ### Title: Locate value for ith percentage point in a binned distribution > ### Aliases: per > ### Keywords: nonparametric > > ### ** Examples > > # make up some x-y data > x <- seq(1,100) > y <- dnorm(x, mean=40, sd=10) > plot(x,y) > # mark the median, 0.1 and 0.9 positions with vertical lines > abline(v=per(y,x,0.5)) > abline(v=per(y,x,0.9)) > abline(v=per(y,x,0.1)) > # for a bimodal distribution which doesn't sum to one > x <- c(1:5) > y <- c(2,3,4,3,4) > per(y,x,0.5) # should return 3.25 [1] 3.25 > # change the previous example to bin extremes > x <- c(1:6) > per(y,x,0.5) # should return 3.75 [1] 3.75 > > > > ### *