|
a |
|
b/diag.disc.R |
|
|
1 |
diag.disc <- |
|
|
2 |
function (x, centroids, prior, weight) |
|
|
3 |
{ |
|
|
4 |
if (!missing(weight)) { |
|
|
5 |
posid <- (weight > 0) |
|
|
6 |
if (any(posid)) { |
|
|
7 |
weight <- sqrt(weight[posid]) |
|
|
8 |
centroids <- centroids[posid, , drop = FALSE] * weight |
|
|
9 |
x <- x[posid, , drop = FALSE] * weight |
|
|
10 |
} |
|
|
11 |
else { |
|
|
12 |
mat <- outer(rep(1, ncol(x)), log(prior), "*") |
|
|
13 |
dimnames(mat) <- list(NULL, dimnames(centroids)[[2]]) |
|
|
14 |
return(mat) |
|
|
15 |
} |
|
|
16 |
} |
|
|
17 |
dd <- t(x) %*% centroids |
|
|
18 |
dd0 <- drop(rep(1, nrow(centroids)) %*% (centroids^2))/2 - |
|
|
19 |
log(prior) |
|
|
20 |
names(dd0) <- NULL |
|
|
21 |
scale(dd, dd0, FALSE) |
|
|
22 |
} |