1 |
x |
|
p |
|
N1 |
|
N2 |
|
tol |
|
N2p |
|
Nran |
|
Nkeep |
|
SEED |
|
LSCALE |
|
SCORES |
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (x, p = ncol(x) - 1, N1 = 3, N2 = 2, tol = 0.001, N2p = 10,
Nran = 50, Nkeep = 10, SEED = TRUE, LSCALE = TRUE, SCORES = FALSE)
{
x <- elimna(x)
Cmat <- NULL
if (Nkeep > Nran)
stop("Must have Nkeep<=Nran")
if (SEED)
set.seed(2)
n <- nrow(x)
m <- ncol(x)
q <- m - p
if (q < 0)
stop("p should have value between 0 and ncol(x)")
if (q > 0) {
bkeep <- array(dim = c(q, m, Nran))
akeep <- matrix(nrow = Nran, ncol = q)
sig.val <- NA
for (it in 1:Nran) {
temp <- marpca.sub(x, p, N1 = N1, N2 = N2, tol = tol,
LSCALE = LSCALE)
bkeep[, , it] <- temp$B
akeep[it, ] <- temp$a
sig.val[it] <- temp$var.op
}
ord <- order(sig.val)
bkeep2 <- array(dim = c(q, m, Nkeep))
cmatkeep <- array(dim = c(m, m, Nkeep))
akeep2 <- matrix(nrow = Nkeep, ncol = q)
sig.val2 <- NA
for (it in 1:Nkeep) {
temp <- marpca.sub(x, p, N1 = 0, N2 = N2p, tol = tol,
B = bkeep[, , ord[it]], a = akeep[ord[it], ],
LSCALE = LSCALE)
bkeep2[, , it] <- temp$B
akeep2[it, ] <- temp$a
sig.val2[it] <- temp$var.op
cmatkeep[, , it] <- temp$wt.cov
}
ord <- order(sig.val2)
B <- bkeep2[, , ord[1]]
a <- akeep2[ord[1], ]
var.op <- sig.val2[ord[1]]
Cmat <- cmatkeep[, , ord[1]]
}
wt.mu <- NULL
if (q == 0) {
output <- marpca.sub(x, 0, LSCALE = LSCALE)
B <- output$B
a <- output$a
var.op <- output$var.op
wt.mu <- output$mu
Cmat <- output$wt.cov
}
scores <- NULL
if (SCORES) {
ev <- eigen(Cmat)
ord.val <- order(ev$values)
mn1 <- m - p + 1
wt.mu <- marpca.sub(x, p = p)$mu
Bp <- ev$vectors[, ord.val[mn1:m]]
xmmu <- x
for (j in 1:m) xmmu[, j] <- x[, j] - wt.mu[j]
scores <- matrix(ncol = p, nrow = n)
for (i in 1:n) scores[i, ] <- t(Bp) %*% as.matrix(xmmu[i,
])
}
list(B = B, a = a, var.op = var.op, wt.cov = Cmat, wt.mu = wt.mu,
scores = scores)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.