|
a |
|
b/dDA.R |
|
|
1 |
|
|
|
2 |
> dDA |
|
|
3 |
function (x, cll, pool = TRUE) |
|
|
4 |
{ |
|
|
5 |
x <- data.matrix(x) |
|
|
6 |
n <- nrow(x) |
|
|
7 |
p <- ncol(x) |
|
|
8 |
cl0 <- as.integer(min(cll, na.rm = TRUE) - 1) |
|
|
9 |
cll <- as.integer(cll) - cl0 |
|
|
10 |
inaC <- is.na(cll) |
|
|
11 |
clL <- cll[!inaC] |
|
|
12 |
K <- max(clL) |
|
|
13 |
if (K != length(unique(clL))) |
|
|
14 |
stop(sQuote("cll"), " did not contain *consecutive* integers") |
|
|
15 |
nk <- integer(K) |
|
|
16 |
m <- v <- matrix(0, p, K) |
|
|
17 |
colVars <- function(x, means = colMeans(x, na.rm = na.rm), |
|
|
18 |
na.rm = FALSE) { |
|
|
19 |
x <- sweep(x, 2, means) |
|
|
20 |
colSums(x * x, na.rm = na.rm)/(nrow(x) - 1) |
|
|
21 |
} |
|
|
22 |
sum.na <- function(x) sum(x, na.rm = TRUE) |
|
|
23 |
for (k in 1:K) { |
|
|
24 |
which <- (cll == k) |
|
|
25 |
nk[k] <- sum.na(which) |
|
|
26 |
lsk <- x[which, , drop = FALSE] |
|
|
27 |
m[, k] <- colMeans(lsk, na.rm = TRUE) |
|
|
28 |
if (nk[k] > 1) |
|
|
29 |
v[, k] <- colVars(lsk, na.rm = TRUE, means = m[, |
|
|
30 |
k]) |
|
|
31 |
} |
|
|
32 |
structure(list(call = match.call(), cl0 = cl0, n = n, p = p, |
|
|
33 |
K = K, means = m, vars = v, nk = nk, pool = pool), class = "dDA") |
|
|
34 |
} |