| 1 |
|
| 2 |
R Under development (unstable) (2019-04-24 r76419) -- "Unsuffered Consequences" |
| 3 |
Copyright (C) 2019 The R Foundation for Statistical Computing |
| 4 |
Platform: x86_64-pc-linux-gnu (64-bit) |
| 5 |
|
| 6 |
R is free software and comes with ABSOLUTELY NO WARRANTY. |
| 7 |
You are welcome to redistribute it under certain conditions. |
| 8 |
Type 'license()' or 'licence()' for distribution details. |
| 9 |
|
| 10 |
R is a collaborative project with many contributors. |
| 11 |
Type 'contributors()' for more information and |
| 12 |
'citation()' on how to cite R or R packages in publications. |
| 13 |
|
| 14 |
Type 'demo()' for some demos, 'help()' for on-line help, or |
| 15 |
'help.start()' for an HTML browser interface to help. |
| 16 |
Type 'q()' to quit R. |
| 17 |
|
| 18 |
> library(sfsmisc) |
| 19 |
> |
| 20 |
> ###--------------- "Iris Example for ever" ---------------------------- |
| 21 |
> data(iris) |
| 22 |
> cl.true <- as.integer(iris[,"Species"]) |
| 23 |
> n <- length(cl.true) |
| 24 |
> stopifnot(cl.true == rep(1:3, each = 50)) |
| 25 |
> m.iris <- data.matrix(iris[, 1:4]) |
| 26 |
> |
| 27 |
> .proctime00 <- proc.time() |
| 28 |
> |
| 29 |
> ## Self Prediction: Not too good (2+4 and 3+3 misclass.) |
| 30 |
> table(diagDA(m.iris, cl.true, m.iris), cl.true) |
| 31 |
cl.true |
| 32 |
1 2 3 |
| 33 |
1 50 0 0 |
| 34 |
2 0 48 4 |
| 35 |
3 0 2 46 |
| 36 |
> table(diagDA(m.iris, cl.true, m.iris, pool=FALSE), cl.true) |
| 37 |
cl.true |
| 38 |
1 2 3 |
| 39 |
1 50 0 0 |
| 40 |
2 0 47 3 |
| 41 |
3 0 3 47 |
| 42 |
> |
| 43 |
> ## Crossvalidation: The same example as knn() & knn1() from "class" : |
| 44 |
> data(iris3) |
| 45 |
> train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3]) |
| 46 |
> test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3]) |
| 47 |
> cl <- rep(1:3, each = 25) |
| 48 |
> |
| 49 |
> pcl <- diagDA(train, cl, test) |
| 50 |
> table(pcl, cl)## 0 + 1 + 2 misclassified |
| 51 |
cl |
| 52 |
pcl 1 2 3 |
| 53 |
1 25 0 0 |
| 54 |
2 0 24 2 |
| 55 |
3 0 1 23 |
| 56 |
> ## knn ( k=1) has 0 + 1 + 3 |
| 57 |
> ## knn ( *, k=3) has 0 + 2 + 3 ==> ``diagDA() is best ..'' |
| 58 |
> |
| 59 |
> stopifnot(pcl == diagDA(train,cl, test, pool = FALSE)) |
| 60 |
> # i.e. quadratic identical here |
| 61 |
> |
| 62 |
> ### Test 'NA' in predict dat.fr |
| 63 |
> RNGversion("3.5.0")# -- so w/ sample() still stays unchanged: |
| 64 |
Warning message: |
| 65 |
In RNGkind("Mersenne-Twister", "Inversion", "Rounding") : |
| 66 |
non-uniform 'Rounding' sampler used |
| 67 |
> set.seed(753) |
| 68 |
> itr <- sample(n, 0.9 * n) |
| 69 |
> lrn <- m.iris[ itr,] |
| 70 |
> tst <- m.iris[-itr,] |
| 71 |
> dd <- dDA(lrn, cl.true[itr]) |
| 72 |
> pd0 <- predict(dd, tst) |
| 73 |
> |
| 74 |
> i.NA <- c(3:5,7,11) |
| 75 |
> j.NA <- sample(1:ncol(tst), size=length(i.NA), replace=TRUE) |
| 76 |
> tst[cbind(i.NA, j.NA)] <- NA |
| 77 |
> pdd <- predict(dd, tst) |
| 78 |
> pcl <- diagDA(lrn, cl.true[itr], tst) |
| 79 |
> stopifnot(length(pdd) == nrow(tst), |
| 80 |
+ identical(pdd, pcl), |
| 81 |
+ pdd[-i.NA] == pd0[-i.NA], |
| 82 |
+ which(is.na(pdd)) == i.NA) |
| 83 |
> |
| 84 |
> ## Now do some (randomized) CV : |
| 85 |
> ## for each observation, count how often it's misclassified |
| 86 |
> M <- 200 |
| 87 |
> set.seed(234) |
| 88 |
> missCl <- integer(n) |
| 89 |
> for(m in 1:M) { |
| 90 |
+ itr <- sample(n, 0.9 * n) |
| 91 |
+ lrn <- m.iris[ itr,] |
| 92 |
+ tst <- m.iris[-itr,] |
| 93 |
+ pcl <- diagDA(lrn, cl.true[itr], tst) |
| 94 |
+ stopifnot(pcl == predict(dDA(lrn, cl.true[itr]), tst)) |
| 95 |
+ missCl <- missCl + as.integer(pcl != cl.true[ - itr]) |
| 96 |
+ } |
| 97 |
> missCl ; mean(missCl) / M |
| 98 |
[1] 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 |
| 99 |
[26] 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 |
| 100 |
[51] 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 |
| 101 |
[76] 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 |
| 102 |
[101] 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 |
| 103 |
[126] 3 7 15 12 13 5 16 18 14 5 0 0 2 1 2 3 7 15 12 13 5 16 18 14 5 |
| 104 |
[1] 0.03766667 |
| 105 |
> |
| 106 |
> ## The "same" with 'pool=FALSE' : |
| 107 |
> missCl <- integer(n) |
| 108 |
> for(m in 1:M) { |
| 109 |
+ itr <- sample(n, 0.9 * n) |
| 110 |
+ lrn <- m.iris[ itr,] |
| 111 |
+ tst <- m.iris[-itr,] |
| 112 |
+ pcl <- diagDA(lrn, cl.true[itr], tst, pool=FALSE) |
| 113 |
+ stopifnot(pcl == predict(dDA(lrn, cl.true[itr], pool=FALSE), tst)) |
| 114 |
+ missCl <- missCl + as.integer(pcl != cl.true[ - itr]) |
| 115 |
+ } |
| 116 |
> missCl ; mean(missCl) / M ## here somewhat worse than linear |
| 117 |
[1] 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 |
| 118 |
[26] 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 |
| 119 |
[51] 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 |
| 120 |
[76] 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 |
| 121 |
[101] 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 |
| 122 |
[126] 11 13 8 14 12 12 11 14 15 7 1 2 1 3 9 11 13 8 14 12 12 11 14 15 7 |
| 123 |
[1] 0.04433333 |
| 124 |
> |
| 125 |
> cat('Time elapsed: ', proc.time() - .proctime00,'\n') |
| 126 |
Time elapsed: 0.424 0.072 0.5 0 0 |
| 127 |
> |
| 128 |
> |
| 129 |
> proc.time() |
| 130 |
user system elapsed |
| 131 |
0.546 0.091 0.630 |