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("kknn-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('kknn') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "contr.dummy" > > ### * contr.dummy > > flush(stderr()); flush(stdout()) > > ### Name: contr.dummy > ### Title: Contrast Matrices > ### Aliases: contr.dummy contr.metric contr.ordinal > ### Keywords: classif design > > ### ** Examples > > contr.metric(5) [,1] 1 -2 2 -1 3 0 4 1 5 2 > contr.ordinal(5) [,1] [,2] [,3] [,4] 1 0.5 0.5 0.5 0.5 2 -0.5 0.5 0.5 0.5 3 -0.5 -0.5 0.5 0.5 4 -0.5 -0.5 -0.5 0.5 5 -0.5 -0.5 -0.5 -0.5 > contr.dummy(5) 1 2 3 4 5 1 1 0 0 0 0 2 0 1 0 0 0 3 0 0 1 0 0 4 0 0 0 1 0 5 0 0 0 0 1 > > > > cleanEx(); ..nameEx <- "glass" > > ### * glass > > flush(stderr()); flush(stdout()) > > ### Name: glass > ### Title: Glass Identification Database > ### Aliases: glass > ### Keywords: datasets > > ### ** Examples > > data(glass) > str(glass) `data.frame': 214 obs. of 11 variables: $ Id : int 1 2 3 4 5 6 7 8 9 10 ... $ RI : num 1.52 1.52 1.52 1.52 1.52 ... $ Na : num 13.6 13.9 13.5 13.2 13.3 ... $ Mg : num 4.49 3.6 3.55 3.69 3.62 3.61 3.6 3.61 3.58 3.6 ... $ Al : num 1.1 1.36 1.54 1.29 1.24 1.62 1.14 1.05 1.37 1.36 ... $ Si : num 71.8 72.7 73.0 72.6 73.1 ... $ K : num 0.06 0.48 0.39 0.57 0.55 0.64 0.58 0.57 0.56 0.57 ... $ Ca : num 8.75 7.83 7.78 8.22 8.07 8.07 8.17 8.24 8.3 8.4 ... $ Ba : num 0 0 0 0 0 0 0 0 0 0 ... $ Fe : num 0 0 0 0 0 0.26 0 0 0 0.11 ... $ Type: Factor w/ 6 levels "1","2","3","5",..: 1 1 1 1 1 1 1 1 1 1 ... > > > > cleanEx(); ..nameEx <- "ionosphere" > > ### * ionosphere > > flush(stderr()); flush(stdout()) > > ### Name: ionosphere > ### Title: Johns Hopkins University Ionosphere Database > ### Aliases: ionosphere > ### Keywords: datasets > > ### ** Examples > > data(ionosphere) > > > > cleanEx(); ..nameEx <- "kknn" > > ### * kknn > > flush(stderr()); flush(stdout()) > > ### Name: kknn > ### Title: Weighted k-Nearest Neighbor Classifier > ### Aliases: kknn print.kknn summary.kknn predict.kknn > ### Keywords: classif > > ### ** Examples > > library(kknn) > > data(iris) > m <- dim(iris)[1] > val <- sample(1:m, size = round(m/3), replace = FALSE, prob = rep(1/m, m)) > iris.learn <- iris[-val,] > iris.valid <- iris[val,] > iris.kknn <- kknn(Species~., iris.learn, iris.valid, distance = 1, kernel = "triangular") > summary(iris.kknn) Call: kknn(formula = Species ~ ., train = iris.learn, test = iris.valid, distance = 1, kernel = "triangular") Response: "nominal" fit prob.setosa prob.versicolor prob.virginica 1 setosa 1 0.00000000 0.00000000 2 versicolor 0 0.93451091 0.06548909 3 versicolor 0 0.97323876 0.02676124 4 virginica 0 0.08244993 0.91755007 5 setosa 1 0.00000000 0.00000000 6 virginica 0 0.00000000 1.00000000 7 virginica 0 0.00000000 1.00000000 8 versicolor 0 1.00000000 0.00000000 9 versicolor 0 1.00000000 0.00000000 10 setosa 1 0.00000000 0.00000000 11 setosa 1 0.00000000 0.00000000 12 setosa 1 0.00000000 0.00000000 13 virginica 0 0.00000000 1.00000000 14 versicolor 0 1.00000000 0.00000000 15 virginica 0 0.18669408 0.81330592 16 versicolor 0 1.00000000 0.00000000 17 virginica 0 0.00000000 1.00000000 18 virginica 0 0.20744637 0.79255363 19 versicolor 0 0.95435764 0.04564236 20 virginica 0 0.00000000 1.00000000 21 virginica 0 0.00000000 1.00000000 22 setosa 1 0.00000000 0.00000000 23 versicolor 0 1.00000000 0.00000000 24 setosa 1 0.00000000 0.00000000 25 setosa 1 0.00000000 0.00000000 26 versicolor 0 1.00000000 0.00000000 27 setosa 1 0.00000000 0.00000000 28 versicolor 0 0.86324609 0.13675391 29 virginica 0 0.00000000 1.00000000 30 versicolor 0 1.00000000 0.00000000 31 versicolor 0 0.90719769 0.09280231 32 versicolor 0 1.00000000 0.00000000 33 versicolor 0 0.85629936 0.14370064 34 setosa 1 0.00000000 0.00000000 35 virginica 0 0.00000000 1.00000000 36 virginica 0 0.16025830 0.83974170 37 virginica 0 0.00000000 1.00000000 38 setosa 1 0.00000000 0.00000000 39 virginica 0 0.03966491 0.96033509 40 versicolor 0 0.91919268 0.08080732 41 virginica 0 0.15070620 0.84929380 42 versicolor 0 1.00000000 0.00000000 43 virginica 0 0.00000000 1.00000000 44 versicolor 0 1.00000000 0.00000000 45 versicolor 0 0.88586244 0.11413756 46 versicolor 0 0.89490731 0.10509269 47 setosa 1 0.00000000 0.00000000 48 versicolor 0 1.00000000 0.00000000 49 virginica 0 0.15734987 0.84265013 50 virginica 0 0.00000000 1.00000000 > fit <- fitted(iris.kknn) > table(iris.valid$Species, fit) fit setosa versicolor virginica setosa 12 0 0 versicolor 0 19 0 virginica 0 1 18 > pcol <- as.character(as.numeric(iris.valid$Species)) > pairs(iris.valid[1:4], pch = pcol, col = c("green3", "red")[(iris.valid$Species != fit)+1]) > > data(ionosphere) > ionosphere.learn <- ionosphere[1:200,] > ionosphere.valid <- ionosphere[-c(1:200),] > fit.kknn <- kknn(class ~ ., ionosphere.learn, ionosphere.valid) > table(ionosphere.valid$class, fit.kknn$fit) b g b 19 8 g 2 122 > (fit.train1 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 1)) Call: train.kknn(formula = class ~ ., data = ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 1) Type of response variable: nominal Minimal misclassification: 0.125 Best kernel: triangular Best k: 1 > table(predict(fit.train1, ionosphere.valid), ionosphere.valid$class) b g b 25 4 g 2 120 > (fit.train2 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 2)) Call: train.kknn(formula = class ~ ., data = ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 2) Type of response variable: nominal Minimal misclassification: 0.14 Best kernel: triangular Best k: 1 > table(predict(fit.train2, ionosphere.valid), ionosphere.valid$class) b g b 20 5 g 7 119 > > > > cleanEx(); ..nameEx <- "miete" > > ### * miete > > flush(stderr()); flush(stdout()) > > ### Name: miete > ### Title: Munich Rent Standard Database (1994) > ### Aliases: miete > ### Keywords: datasets > > ### ** Examples > > data(miete) > str(miete) `data.frame': 1082 obs. of 17 variables: $ nm : num 693 737 732 1295 395 ... $ wfl : int 50 70 50 55 46 94 28 36 33 57 ... $ bj : num 1972 1972 1972 1893 1957 ... $ bad0 : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ... $ zh : Factor w/ 2 levels "0","1": 2 2 2 2 1 2 2 2 2 2 ... $ ww0 : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ... $ badkach: Factor w/ 2 levels "0","1": 1 1 1 1 1 2 1 1 1 2 ... $ fenster: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 1 1 1 ... $ kueche : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 2 2 1 2 ... $ mvdauer: int 2 26 1 0 27 2 9 3 1 9 ... $ bjkat : Ord.factor w/ 6 levels "1"<"2"<"3"<"4"<..: 4 4 4 1 3 4 4 4 4 6 ... $ wflkat : Ord.factor w/ 3 levels "1"<"2"<"3": 1 2 1 2 1 3 1 1 1 2 ... $ nmqm : num 13.87 10.52 14.64 23.55 8.59 ... $ rooms : int 1 3 1 3 3 4 1 1 1 2 ... $ nmkat : Ord.factor w/ 5 levels "1"<"2"<"3"<"4"<..: 3 3 3 5 1 5 1 3 4 2 ... $ adr : Ord.factor w/ 3 levels "1"<"2"<"3": 2 2 2 2 2 2 2 2 2 2 ... $ wohn : Ord.factor w/ 3 levels "1"<"2"<"3": 2 2 2 2 2 2 2 2 2 2 ... > > > > cleanEx(); ..nameEx <- "simulation" > > ### * simulation > > flush(stderr()); flush(stdout()) > > ### Name: simulation > ### Title: Crossvalidation procedure to test prediction accuracy > ### Aliases: simulation > ### Keywords: classif > > ### ** Examples > > library(kknn) > data(miete) > simulation(nmqm ~ wfl + bjkat + zh, data = miete, runs = 5, kernel = "triangular", k = 15) absolute distance squared distance mean 3.5385374 19.964232 sd 0.1031885 1.557433 > simulation(wflkat ~ nm + bjkat + zh, data = miete, runs = 5) misclassification absolute distance squared distance Mean 0.46814404 0.49584488 0.55124654 sd 0.02026141 0.01385042 0.02216066 > simulation(zh ~ wfl + bjkat + nmqm, data = miete, runs = 5) misclassification mean 0.17008310 sd 0.01379490 > > > > > cleanEx(); ..nameEx <- "train.kknn" > > ### * train.kknn > > flush(stderr()); flush(stdout()) > > ### Name: train.kknn > ### Title: Training kknn > ### Aliases: train.kknn plot.train.kknn print.train.kknn predict.train.kknn > ### summary.train.kknn > ### Keywords: classif > > ### ** Examples > > library(kknn) > > data(miete) > (train.con <- train.kknn(nmqm ~ wfl + bjkat + zh, data = miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank"))) Call: train.kknn(formula = nmqm ~ wfl + bjkat + zh, data = miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank")) Type of response variable: continuous minimal mean absolute error: 3.52243 Minimal mean squared error: 19.97185 Best kernel: rectangular Best k: 25 > plot(train.con) > (train.ord <- train.kknn(wflkat ~ nm + bjkat + zh, miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank"))) Call: train.kknn(formula = wflkat ~ nm + bjkat + zh, data = miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank")) Type of response variable: ordinal minimal mean absolute error: 0.4731978 Minimal mean squared error: 0.5138632 Minimal misclassification: 0.4528651 Best kernel: epanechnikov Best k: 10 > plot(train.ord) > (train.nom <- train.kknn(zh ~ wfl + bjkat + nmqm, miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank"))) Call: train.kknn(formula = zh ~ wfl + bjkat + nmqm, data = miete, kmax = 25, kernel = c("rectangular", "triangular", "epanechnikov", "gaussian", "rank")) Type of response variable: nominal Minimal misclassification: 0.1506470 Best kernel: rectangular Best k: 23 > plot(train.nom) > > data(glass) > glass <- glass[,-1] > (fit.glass1 <- train.kknn(Type ~ ., glass, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 1)) Call: train.kknn(formula = Type ~ ., data = glass, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 1) Type of response variable: nominal Minimal misclassification: 0.2523364 Best kernel: triangular Best k: 5 > (fit.glass2 <- train.kknn(Type ~ ., glass, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 2)) Call: train.kknn(formula = Type ~ ., data = glass, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 2) Type of response variable: nominal Minimal misclassification: 0.2710280 Best kernel: rectangular Best k: 3 > plot(fit.glass1) > plot(fit.glass2) > > > > > ### *