1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
# file nnet/R/lvq.R
# copyright (C) 1994-9 W. N. Venables and B. D. Ripley
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 or 3 of the License
# (at your option).
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# http://www.r-project.org/Licenses/
#
#
lvqinit <- function(x, cl, size, prior, k=5)
{
x <- as.matrix(x)
n <- nrow(x)
p <- ncol(x)
if(length(cl) != n) stop("'x' and 'cl' have different lengths")
g <- as.factor(cl)
if(any(is.na(x)) || any(is.na(g)))
stop("no missing values are allowed")
counts <- tapply(rep(1, length(g)), g, sum)
prop <- counts/n
np <- length(prop)
# allow for supplied prior
if(missing(prior)) prior <- prop
else if(any(prior <0)||round(sum(prior), 5) != 1)
stop("invalid prior")
if(length(prior) != np) stop("'prior' is of incorrect length")
if(missing(size)) size <- min(round(0.4 * np * (np-1+p/2),0), n)
inside <- knn.cv(x, cl, k) == cl
selected <- numeric(0)
for(i in 1L:np){
set <- seq_along(g)[unclass(g)==i & inside]
if(length(set) > 1L)
set <- sample(set, min(length(set), round(size*prior[i])))
selected <- c(selected, set)
}
list(x = x[selected, , drop=FALSE], cl = cl[selected])
}
olvq1 <- function(x, cl, codebk, niter = 40*nrow(codebk$x), alpha = 0.3)
{
x <- as.matrix(x)
if(any(is.na(x)) || any(is.na(cl)))
stop("no missing values are allowed")
n <- nrow(x)
p <- ncol(x)
nc <- dim(codebk$x)[1L]
if(length(cl) != n) stop("'x' and 'cl' have different lengths")
iters <- sample(n, niter, TRUE)
z <- .C(VR_olvq,
as.double(alpha),
as.integer(n),
as.integer(p),
as.double(x),
as.integer(unclass(cl)),
as.integer(nc),
xc = as.double(codebk$x),
as.integer(codebk$cl),
as.integer(niter),
as.integer(iters-1L)
)
xc <- matrix(z$xc,nc,p)
dimnames(xc) <- dimnames(codebk$x)
list(x = xc, cl = codebk$cl)
}
lvq1 <- function(x, cl, codebk, niter = 100*nrow(codebk$x), alpha = 0.03)
{
x <- as.matrix(x)
if(any(is.na(x)) || any(is.na(cl)))
stop("no missing values are allowed")
n <- nrow(x)
p <- ncol(x)
nc <- dim(codebk$x)[1L]
if(length(cl) != n) stop("'x' and 'cl' have different lengths")
iters <- sample(n, niter, TRUE)
z <- .C(VR_lvq1,
as.double(alpha),
as.integer(n),
as.integer(p),
as.double(x),
as.integer(unclass(cl)),
as.integer(nc),
xc = as.double(codebk$x),
as.integer(codebk$cl),
as.integer(niter),
as.integer(iters-1L)
)
xc <- matrix(z$xc,nc,p)
dimnames(xc) <- dimnames(codebk$x)
list(x = xc, cl = codebk$cl)
}
lvq2 <- function(x, cl, codebk, niter = 100*nrow(codebk$x),
alpha = 0.03, win = 0.3)
{
x <- as.matrix(x)
if(any(is.na(x)) || any(is.na(cl)))
stop("no missing values are allowed")
n <- nrow(x)
p <- ncol(x)
nc <- dim(codebk$x)[1L]
if(length(cl) != n) stop("'x' and 'cl' have different lengths")
iters <- sample(n, niter, TRUE)
z <- .C(VR_lvq2,
as.double(alpha),
as.double(win),
as.integer(n),
as.integer(p),
as.double(x),
as.integer(unclass(cl)),
as.integer(nc),
xc = as.double(codebk$x),
as.integer(codebk$cl),
as.integer(niter),
as.integer(iters-1L)
)
xc <- matrix(z$xc,nc,p)
dimnames(xc) <- dimnames(codebk$x)
list(x = xc, cl = codebk$cl)
}
lvq3 <- function(x, cl, codebk, niter = 100*nrow(codebk$x),
alpha = 0.03, win = 0.3, epsilon = 0.1)
{
x <- as.matrix(x)
if(any(is.na(x)) || any(is.na(cl)))
stop("no missing values are allowed")
n <- nrow(x)
p <- ncol(x)
nc <- dim(codebk$x)[1L]
if(length(cl) != n) stop("'x' and 'cl' have different lengths")
iters <- sample(n, niter, TRUE)
z <- .C(VR_lvq3,
as.double(alpha),
as.double(win),
as.double(epsilon),
as.integer(n),
as.integer(p),
as.double(x),
as.integer(unclass(cl)),
as.integer(nc),
xc = as.double(codebk$x),
as.integer(codebk$cl),
as.integer(niter),
as.integer(iters-1L)
)
xc <- matrix(z$xc,nc,p)
dimnames(xc) <- dimnames(codebk$x)
list(x = xc, cl = codebk$cl)
}
lvqtest <- function(codebk, test) knn1(codebk$x, test, codebk$cl)
|