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("caTools-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('caTools') Loading required package: bitops > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "ENVI" > > ### * ENVI > > flush(stderr()); flush(stdout()) > > ### Name: ENVI > ### Title: Read and write binary data in ENVI format > ### Aliases: read.ENVI write.ENVI > ### Keywords: file > > ### ** Examples > > X = array(1:60, 3:5) > write.ENVI(X, "temp.nvi") > Y = read.ENVI("temp.nvi") > stopifnot(X == Y) > readLines("temp.nvi.hdr") [1] "ENVI" "description = { R-language data }" [3] "samples = 4" "lines = 3" [5] "bands = 5" "data type = 3" [7] "header offset = 0" "interleave = bsq" [9] "byte order = 1" > > d = c(20,30,40) > X = array(runif(prod(d)), d) > write.ENVI(X, "temp.nvi", interleave="bil") > Y = read.ENVI("temp.nvi") > stopifnot(X == Y) > readLines("temp.nvi.hdr") [1] "ENVI" "description = { R-language data }" [3] "samples = 30" "lines = 20" [5] "bands = 40" "data type = 5" [7] "header offset = 0" "interleave = bil" [9] "byte order = 1" > > file.remove("temp.nvi") [1] TRUE > file.remove("temp.nvi.hdr") [1] TRUE > > > > cleanEx(); ..nameEx <- "LogitBoost" > > ### * LogitBoost > > flush(stderr()); flush(stdout()) > > ### Name: LogitBoost > ### Title: LogitBoost Classification Algorithm > ### Aliases: LogitBoost > ### Keywords: classif > > ### ** Examples > > data(iris) > Data = iris[,-5] > Label = iris[, 5] > > # basic interface > model = LogitBoost(Data, Label, nIter=20) > Lab = predict(model, Data) > Prob = predict(model, Data, type="raw") > t = cbind(Lab, Prob) > t[1:10, ] Lab setosa versicolor virginica [1,] 1 1 0.0024726232 1.522998e-08 [2,] 1 1 0.0003353501 3.353501e-04 [3,] 1 1 0.0024726232 8.315280e-07 [4,] 1 1 0.0003353501 4.539787e-05 [5,] 1 1 0.0024726232 1.522998e-08 [6,] 1 1 0.0179862100 1.522998e-08 [7,] 1 1 0.0024726232 8.315280e-07 [8,] 1 1 0.0024726232 1.522998e-08 [9,] 1 1 0.0024726232 3.353501e-04 [10,] 1 1 0.0003353501 4.539787e-05 > > # two alternative call syntax > p=predict(model,Data) > q=predict.LogitBoost(model,Data) > pp=p[!is.na(p)]; qq=q[!is.na(q)] > stopifnot(pp == qq) > > # accuracy increases with nIter (at least for train set) > table(predict(model, Data, nIter= 2), Label) Label setosa versicolor virginica setosa 48 0 0 versicolor 0 45 2 virginica 0 2 44 > table(predict(model, Data, nIter=10), Label) Label setosa versicolor virginica setosa 50 0 0 versicolor 0 47 2 virginica 0 0 45 > table(predict(model, Data), Label) Label setosa versicolor virginica setosa 50 0 0 versicolor 0 49 0 virginica 0 1 50 > > # example of spliting the data into train and test set > mask = sample.split(Label) > model = LogitBoost(Data[mask,], Label[mask], nIter=10) > table(predict(model, Data[!mask,], nIter=2), Label[!mask]) setosa versicolor virginica setosa 17 0 0 versicolor 0 17 1 virginica 0 0 13 > table(predict(model, Data[!mask,]), Label[!mask]) setosa versicolor virginica setosa 17 0 0 versicolor 0 17 2 virginica 0 0 14 > > > > cleanEx(); ..nameEx <- "base64" > > ### * base64 > > flush(stderr()); flush(stdout()) > > ### Name: base64 > ### Title: Convert R vectors to/from the Base64 format > ### Aliases: base64encode base64decode > ### Keywords: file > > ### ** Examples > > x = (10*runif(10)>5) # logical > for (i in c(NA, 1, 2, 4)) { + y = base64encode(x, size=i) + z = base64decode(y,typeof(x), size=i) + stopifnot(x==z) + } > print("Checked base64 for encode/decode logical type") [1] "Checked base64 for encode/decode logical type" > > x = as.integer(1:10) # integer > for (i in c(NA, 1, 2, 4)) { + y = base64encode(x, size=i) + z = base64decode(y,typeof(x), size=i) + stopifnot(x==z) + } > print("Checked base64 encode/decode for integer type") [1] "Checked base64 encode/decode for integer type" > > x = (1:10)*pi # double > for (i in c(NA, 4, 8)) { + y = base64encode(x, size=i) + z = base64decode(y,typeof(x), size=i) + stopifnot(mean(abs(x-z))<1e-5) + } > print("Checked base64 for encode/decode double type") [1] "Checked base64 for encode/decode double type" > > x = log(as.complex(-(1:10)*pi)) # complex > y = base64encode(x) > z = base64decode(y,typeof(x)) > stopifnot(x==z) > print("Checked base64 for encode/decode complex type") [1] "Checked base64 for encode/decode complex type" > > x = "Chance favors the prepared mind" # character > y = base64encode(x) > z = base64decode(y,typeof(x)) > stopifnot(x==z) > print("Checked base64 for encode/decode character type") [1] "Checked base64 for encode/decode character type" > > > > cleanEx(); ..nameEx <- "bin2raw" > > ### * bin2raw > > flush(stderr()); flush(stdout()) > > ### Name: bin2raw > ### Title: Convert R vectors to/from the raw binary format > ### Aliases: bin2raw raw2bin > ### Keywords: file > > ### ** Examples > > x = (10*runif(10)>5) # logical > for (i in c(NA, 1, 2, 4)) { + y = bin2raw(x, size=i) + z = raw2bin(y,typeof(x), size=i) + stopifnot(x==z) + } > print("Checked bin2raw and raw2bin conversion for logical type") [1] "Checked bin2raw and raw2bin conversion for logical type" > > x = as.integer(1:10) # integer > for (i in c(NA, 1, 2, 4)) { + y = bin2raw(x, size=i) + z = raw2bin(y,typeof(x), size=i) + stopifnot(x==z) + } > print("Checked bin2raw and raw2bin conversion for integer type") [1] "Checked bin2raw and raw2bin conversion for integer type" > > x = (1:10)*pi # double > for (i in c(NA, 4, 8)) { + y = bin2raw(x, size=i) + z = raw2bin(y,typeof(x), size=i) + stopifnot(mean(abs(x-z))<1e-5) + } > print("Checked bin2raw and raw2bin conversion for double type") [1] "Checked bin2raw and raw2bin conversion for double type" > > x = log(as.complex(-(1:10)*pi)) # complex > y = bin2raw(x) > z = raw2bin(y,typeof(x)) > stopifnot(x==z) > print("Checked bin2raw and raw2bin conversion for complex type") [1] "Checked bin2raw and raw2bin conversion for complex type" > > x = "Chance favors the prepared mind" # character > y = bin2raw(x) > z = raw2bin(y,typeof(x)) > stopifnot(x==z) > print("Checked bin2raw and raw2bin conversion for character type") [1] "Checked bin2raw and raw2bin conversion for character type" > > > > cleanEx(); ..nameEx <- "colAUC" > > ### * colAUC > > flush(stderr()); flush(stdout()) > > ### Name: colAUC > ### Title: Columnwise Area Under ROC Curve (AUC) > ### Aliases: colAUC > ### Keywords: univar > > ### ** Examples > > # load MASS library with "cats" data set that have following columns: sex, > # body weight, hart weight > library(MASS) > data(cats) > colAUC(cats[,2:3], cats[,1]) Bwt Hwt F vs. M 0.8338451 0.759048 > > # compare with examples from roc.area function: using Data from Mason and Graham (2002). > a<- (1981:1995) > b<- c(0,0,0,1,1,1,0,1,1,0,0,0,0,1,1) > c<- c(.8, .8, 0, 1,1,.6, .4, .8, 0, 0, .2, 0, 0, 1,1) > d<- c(.928,.576, .008, .944, .832, .816, .136, .584, .032, .016, .28, .024, 0, .984, .952) > A<- data.frame(a,b,c,d) > names(A)<- c("year", "event", "p1", "p2") > if (library(verification, logical.return=TRUE)) { + roc.area(A$event, A$p1) # for model with ties + roc.area(A$event, A$p2) # for model without ties + } Loading required package: fields fields is loaded use help(fields) for an overview of this library Loading required package: waveslim Package verify: Please send comments or suggestions to pocernic@(Remove this)ucar.edu $A.tilda [1] 0.875 $n.total [1] 15 $n.events [1] 7 $n.noevents [1] 8 $U [1] 7 $p [1] 0.007543628 $p.adj [1] 0.007543628 > wilcox.test(p2~event, data=A) Wilcoxon rank sum test data: p2 by event W = 7, p-value = 0.01399 alternative hypothesis: true mu is not equal to 0 > # colAUC output is the same as roc.area's A.tilda values > colAUC(A[,3:4], A$event) p1 p2 0 vs. 1 0.8392857 0.875 > # colAUC output is the same as roc.area's and wilcox.test's p values > colAUC(A[,3:4], A$event, p.val=TRUE) p1 p2 0 vs. 1 0.01394526 0.007543628 > > # example of 3-class data > data(iris) > colAUC(iris[,-5], iris[,5]) Sepal.Length Sepal.Width Petal.Length Petal.Width setosa vs. versicolor 0.9326 0.9248 1.0000 1.0000 setosa vs. virginica 0.9846 0.8344 1.0000 1.0000 versicolor vs. virginica 0.7896 0.6636 0.9822 0.9804 > > > > cleanEx(); ..nameEx <- "combs" Warning in detach("package:fields") : package:fields is required by package:verification (still attached) Warning in detach("package:waveslim") : package:waveslim is required by package:verification (still attached) > > ### * combs > > flush(stderr()); flush(stdout()) > > ### Name: combs > ### Title: All Combinations of k Elements from Vector v > ### Aliases: combs > ### Keywords: models > > ### ** Examples > > #example: combs(1:3,2) returns matrix with following rows (1 2), (1 3), (2 3) > combs(1:3,2) [,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 2 3 > > > > cleanEx(); ..nameEx <- "predict.LogitBoost" > > ### * predict.LogitBoost > > flush(stderr()); flush(stdout()) > > ### Name: predict.LogitBoost > ### Title: Prediction Based on LogitBoost Classification Algorithm > ### Aliases: predict.LogitBoost > ### Keywords: classif > > ### ** Examples > # See LogitBoost example > > > cleanEx(); ..nameEx <- "runfunc" > > ### * runfunc > > flush(stderr()); flush(stdout()) > > ### Name: runfunc > ### Title: Moving Window Analysis of a Vector > ### Aliases: runmean runmin runmax runmad runquantile EndRule > ### Keywords: ts smooth array utilities > > ### ** Examples > > # test runmin, runmax and runmed > k=15; n=200; > x = rnorm(n,sd=30) + abs(seq(n)-n/4) > col = c("black", "red", "green", "blue", "magenta", "cyan") > plot(x, col=col[1], main = "Moving Window Analysis Functions") > lines(runmin(x,k), col=col[2]) > lines(runmed(x,k), col=col[3]) > lines(runmax(x,k), col=col[4]) > legend(0,.9*n, c("data", "runmin", "runmed", "runmax"), col=col, lty=1 ) > > #test runmean and runquantile > y=runquantile(x, k, probs=c(0, 0.5, 1, 0.25, 0.75), endrule="constant") > plot(x, col=col[1], main = "Moving Window Quantile") > lines(runmean(y[,1],k), col=col[2]) > lines(y[,2], col=col[3]) > lines(runmean(y[,3],k), col=col[4]) > lines(y[,4], col=col[5]) > lines(y[,5], col=col[6]) > lab = c("data", "runmean(runquantile(0))", "runquantile(0.5)", + "runmean(runquantile(1))", "runquantile(.25)", "runquantile(.75)") > legend(0,0.9*n, lab, col=col, lty=1 ) > > #test runmean and runquantile > k =25 > m=runmed(x, k) > y=runmad(x, k, center=m) > plot(x, col=col[1], main = "Moving Window Analysis Functions") > lines(m , col=col[2]) > lines(m-y/2, col=col[3]) > lines(m+y/2, col=col[4]) > lab = c("data", "runmed", "runmed-runmad/2", "runmed+runmad/2") > legend(0,1.8*n, lab, col=col, lty=1 ) > > # numeric comparison between different algorithms > numeric.test = function (n, k) { + eps = .Machine$double.eps ^ 0.5 + x = rnorm(n,sd=30) + abs(seq(n)-n/4) + # numeric comparison : runmean + a = runmean(x,k) + b = runmean(x,k, alg="R") + d = runmean(x,k, alg="exact") + e = filter(x, rep(1/k,k)) + stopifnot(all(abs(a-b) numeric.test(50, 3) # test different window size vs. vector ... > numeric.test(50,15) # ... length combinations > numeric.test(50,49) > numeric.test(49,49) > > # speed comparison > x=runif(100000); k=991; > system.time(runmean(x,k)) [1] 0.01 0.01 0.01 0.00 0.00 > system.time(runmean(x,k, alg="R")) [1] 0.03 0.02 0.06 0.00 0.00 > system.time(runmean(x,k, alg="exact")) [1] 0.09 0.01 0.10 0.00 0.00 > system.time(filter(x, rep(1/k,k), sides=2)) #the fastest alternative I know [1] 3.61 0.03 3.65 0.00 0.00 > k=91; > system.time(runmad(x,k)) [1] 0.23 0.02 0.25 0.00 0.00 > system.time(apply(embed(x,k), 1, mad)) #the fastest alternative I know [1] 29.73 1.64 31.56 0.00 0.00 > > # numerical comparison of round-off error handling > test.runmean = function (x, k) { + a = k*runmean(x,k, alg="exact") + b = k*runmean(x,k, alg="C") + d = k*runmean(x,k, alg="R") + e = k*filter(x, rep(1/k,k)) + f = k* c(NA, NA, apply(embed(x,k), 1, mean), NA, NA) + x = cbind(x, a, b, d, e, f) + colnames(x) = c("x","runmean(alg=exact)","runmean(alg=C)", + "runmean(alg=R)","filter","apply") + return(x) + } > a = rep( c(1, 10, -10, -1, 0, 0, 0), 3) # nice-behaving array > b = rep( c(1, 10^20, -10^20, -1, 0, 0, 0), 3) # round-off error prone array > d = rep( c(1, 10^20, 10^40, -10^40, -10^20, -1, 0), 3) > test.runmean(a, 5) #all runmean algorithms give the same result Time Series: Start = 1 End = 21 Frequency = 1 x runmean(alg=exact) runmean(alg=C) runmean(alg=R) filter apply 1 1 NA NA NA NA NA 2 10 NA NA NA NA NA 3 -10 0 0 0 -2.775558e-16 0 4 -1 -1 -1 -1 -1.000000e+00 -1 5 0 -11 -11 -11 -1.100000e+01 -11 6 0 0 0 0 0.000000e+00 0 7 0 11 11 11 1.100000e+01 11 8 1 1 1 1 1.000000e+00 1 9 10 0 0 0 -2.775558e-16 0 10 -10 0 0 0 -2.775558e-16 0 11 -1 -1 -1 -1 -1.000000e+00 -1 12 0 -11 -11 -11 -1.100000e+01 -11 13 0 0 0 0 0.000000e+00 0 14 0 11 11 11 1.100000e+01 11 15 1 1 1 1 1.000000e+00 1 16 10 0 0 0 -2.775558e-16 0 17 -10 0 0 0 -2.775558e-16 0 18 -1 -1 -1 -1 -1.000000e+00 -1 19 0 -11 -11 -11 -1.100000e+01 -11 20 0 NA NA NA NA NA 21 0 NA NA NA NA NA > test.runmean(b, 5) #runmean(alg=R) gives wrong result Time Series: Start = 1 End = 21 Frequency = 1 x runmean(alg=exact) runmean(alg=C) runmean(alg=R) filter apply 1 1e+00 NA NA NA NA NA 2 1e+20 NA NA NA NA NA 3 -1e+20 0e+00 0e+00 -1e+00 5.552115e+03 1e+00 4 -1e+00 -1e+00 -1e+00 -2e+00 5.551115e+03 0e+00 5 0e+00 -1e+20 -1e+20 -1e+20 -1.000000e+20 -1e+20 6 0e+00 0e+00 0e+00 0e+00 0.000000e+00 0e+00 7 0e+00 1e+20 1e+20 1e+20 1.000000e+20 1e+20 8 1e+00 1e+00 1e+00 0e+00 5.552115e+03 1e+00 9 1e+20 0e+00 0e+00 -1e+00 5.552115e+03 1e+00 10 -1e+20 0e+00 0e+00 -1e+00 5.552115e+03 1e+00 11 -1e+00 -1e+00 -1e+00 -2e+00 5.551115e+03 0e+00 12 0e+00 -1e+20 -1e+20 -1e+20 -1.000000e+20 -1e+20 13 0e+00 0e+00 0e+00 0e+00 0.000000e+00 0e+00 14 0e+00 1e+20 1e+20 1e+20 1.000000e+20 1e+20 15 1e+00 1e+00 1e+00 0e+00 5.552115e+03 1e+00 16 1e+20 0e+00 0e+00 -1e+00 5.552115e+03 1e+00 17 -1e+20 0e+00 0e+00 -1e+00 5.552115e+03 1e+00 18 -1e+00 -1e+00 -1e+00 -2e+00 5.551115e+03 0e+00 19 0e+00 -1e+20 -1e+20 -1e+20 -1.000000e+20 -1e+20 20 0e+00 NA NA NA NA NA 21 0e+00 NA NA NA NA NA > test.runmean(d, 5) #only runmean(alg=exact) gives correct result Time Series: Start = 1 End = 21 Frequency = 1 x runmean(alg=exact) runmean(alg=C) runmean(alg=R) filter apply 1 1e+00 NA NA NA NA NA 2 1e+20 NA NA NA NA NA 3 1e+40 1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 4 -1e+40 -1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 5 -1e+20 -1e+20 -1e+20 -2e+20 -4.935140e+22 0e+00 6 -1e+00 -1e+40 -1e+40 -1e+40 -1.000000e+40 -1e+40 7 0e+00 0e+00 0e+00 0e+00 -5.551115e+03 0e+00 8 1e+00 1e+40 1e+40 1e+40 1.000000e+40 1e+40 9 1e+20 1e+20 1e+20 0e+00 -4.925140e+22 1e+20 10 1e+40 1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 11 -1e+40 -1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 12 -1e+20 -1e+20 -1e+20 -2e+20 -4.935140e+22 0e+00 13 -1e+00 -1e+40 -1e+40 -1e+40 -1.000000e+40 -1e+40 14 0e+00 0e+00 0e+00 0e+00 -5.551115e+03 0e+00 15 1e+00 1e+40 1e+40 1e+40 1.000000e+40 1e+40 16 1e+20 1e+20 1e+20 0e+00 -4.925140e+22 1e+20 17 1e+40 1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 18 -1e+40 -1e+00 0e+00 -1e+20 -4.925140e+22 1e+20 19 -1e+20 -1e+20 -1e+20 -2e+20 -4.935140e+22 0e+00 20 -1e+00 NA NA NA NA NA 21 0e+00 NA NA NA NA NA > > > > cleanEx(); ..nameEx <- "sample.split" > > ### * sample.split > > flush(stderr()); flush(stdout()) > > ### Name: sample.split > ### Title: Split Data into Test and Train Set > ### Aliases: sample.split msc.sample.split > ### Keywords: classif > > ### ** Examples > > library(MASS) > data(cats) # load cats data > Y = cats[,1] # extract labels from the data > msk = sample.split(Y, SplitRatio=3/4) > table(Y,msk) msk Y FALSE TRUE F 12 35 M 24 73 > t=sum( msk) # number of elements in one class > f=sum(!msk) # number of elements in the other class > stopifnot( round((t+f)*3/4) == t ) # test ratios > > # example of using group variable > g = rep(seq(length(Y)/4), each=4); g[48]=12; > msk = sample.split(Y, SplitRatio=1/2, group=g) > table(Y,msk) # try to get correct split ratios ... msk Y FALSE TRUE F 23 24 M 49 48 > split(msk,g) # ... while keeping samples with the same group label together $"1" [1] TRUE TRUE TRUE TRUE $"2" [1] TRUE TRUE TRUE TRUE $"3" [1] TRUE TRUE TRUE TRUE $"4" [1] TRUE TRUE TRUE TRUE $"5" [1] FALSE FALSE FALSE FALSE $"6" [1] TRUE TRUE TRUE TRUE $"7" [1] FALSE FALSE FALSE FALSE $"8" [1] FALSE FALSE FALSE FALSE $"9" [1] FALSE FALSE FALSE FALSE $"10" [1] TRUE TRUE TRUE TRUE $"11" [1] FALSE FALSE FALSE FALSE $"12" [1] FALSE FALSE FALSE TRUE $"13" [1] FALSE FALSE FALSE FALSE $"14" [1] FALSE FALSE FALSE FALSE $"15" [1] TRUE TRUE TRUE TRUE $"16" [1] TRUE TRUE TRUE TRUE $"17" [1] FALSE FALSE FALSE FALSE $"18" [1] FALSE FALSE FALSE FALSE $"19" [1] FALSE FALSE FALSE FALSE $"20" [1] TRUE TRUE TRUE TRUE $"21" [1] TRUE TRUE TRUE TRUE $"22" [1] TRUE TRUE TRUE FALSE $"23" [1] FALSE FALSE FALSE FALSE $"24" [1] FALSE FALSE FALSE FALSE $"25" [1] FALSE FALSE FALSE FALSE $"26" [1] TRUE TRUE TRUE TRUE $"27" [1] FALSE FALSE FALSE FALSE $"28" [1] TRUE TRUE TRUE TRUE $"29" [1] FALSE FALSE FALSE FALSE $"30" [1] FALSE FALSE FALSE FALSE $"31" [1] TRUE TRUE TRUE TRUE $"32" [1] TRUE TRUE TRUE TRUE $"33" [1] TRUE TRUE TRUE TRUE $"34" [1] TRUE TRUE TRUE TRUE $"35" [1] FALSE FALSE FALSE FALSE $"36" [1] TRUE TRUE TRUE TRUE > > # test results > print(paste( "All Labels numbers: total=",t+f,", train=",t,", test=",f, + ", ratio=", t/(t+f) ) ) [1] "All Labels numbers: total= 144 , train= 108 , test= 36 , ratio= 0.75" > U = unique(Y) # extract all unique labels > for( i in 1:length(U)) { # check for all labels + lab = (Y==U[i]) # mask elements that have label U[i] + t=sum( msk[lab]) # number of elements with label U[i] in one class + f=sum(!msk[lab]) # number of elements with label U[i] in the other class + print(paste( "Label",U[i],"numbers: total=",t+f,", train=",t,", test=",f, + ", ratio=", t/(t+f) ) ) + } [1] "Label F numbers: total= 47 , train= 24 , test= 23 , ratio= 0.51063829787234" [1] "Label M numbers: total= 97 , train= 48 , test= 49 , ratio= 0.494845360824742" > > # use results > train = cats[ msk,2:3] # use output of sample.split to ... > test = cats[!msk,2:3] # create train and test subsets > z = lda(train, Y[msk]) # perform classification > table(predict(z, test)$class, Y[!msk]) # predicted & true labels F M F 13 10 M 10 39 > > # see also LogitBoost example > > > > cleanEx(); ..nameEx <- "sum.exact" > > ### * sum.exact > > flush(stderr()); flush(stdout()) > > ### Name: sum.exact > ### Title: Basic Sum Operations without Round-off Errors > ### Aliases: sum.exact cumsum.exact runsum.exact > ### Keywords: ts smooth array utilities > > ### ** Examples > > x = c(1, 1e20, 1e40, -1e40, -1e20, -1) > sum(x) [1] -1e+20 > sum.exact(x) [1] 0 > cumsum(x) [1] 1e+00 1e+20 1e+40 0e+00 -1e+20 -1e+20 > cumsum.exact(x) [1] 1e+00 1e+20 1e+40 1e+20 1e+00 0e+00 > > > > cleanEx(); ..nameEx <- "trapz" > > ### * trapz > > flush(stderr()); flush(stdout()) > > ### Name: trapz > ### Title: Trapezoid Rule Numerical Integration > ### Aliases: trapz > ### Keywords: math > > ### ** Examples > > # integral of sine function in [0, pi] range suppose to be exactly 2. > # lets calculate it using 10 samples: > x = (1:10)*pi/10 > trapz(x, sin(x)) [1] 1.934983 > # now lets calculate it using 1000 samples: > x = (1:1000)*pi/1000 > trapz(x, sin(x)) [1] 1.999993 > > > > ### *