1 |
PNT |
|
X |
|
NDIR |
|
EPS |
|
SEED |
|
PRINT |
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 | ##---- 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 (PNT, X, NDIR = 100, EPS = 1e-06, SEED = NA, PRINT = F)
{
DEP <- function(X, PNT, NDIR, EPS = 1e-07, PRINT = F) {
NSIN <- 0
N <- nrow(X)
NP <- ncol(X)
NDEP <- N
for (NRAN in 1:NDIR) {
foundSingular <- F
JSAMP <- sample(1:N, size = NP, replace = FALSE)
sX <- matrix(X[JSAMP, ], nrow = NP, ncol = NP)
COV <- var(sX)
resEigen <- eigen(COV)
Eval <- resEigen[[1]]
Evec <- resEigen[[2]]
if (Eval[NP] > EPS) {
NSIN <- NSIN + 1
foundSingular <- T
if (PRINT)
paste("ERROR: No Eigenvalue = 0 for sample",
NRAN)
next
}
if (Eval[NP - 1] <= EPS) {
NSIN <- NSIN + 1
}
eigenVec <- Evec[, NP]
NT <- sum(ifelse(eigenVec <= EPS, 1, 0))
KT <- sum(ifelse(eigenVec > EPS, PNT * eigenVec,
0))
if (NT == NP) {
NSIN <- NSIN + 1
foundSingular <- T
if (PRINT)
paste(" ERROR: Eigenvector = 0 for sample",
NRAN)
if (foundSingular)
next
}
K <- X %*% eigenVec
K <- K - KT
NUMH <- sum(ifelse(K > EPS, 1, 0))
NT <- sum(ifelse(abs(K) <= EPS, 1, 0))
if (NT == N) {
NSIN <- -1
return(list(NDEP = NDEP, NSIN = NSIN, EVEC = Evec))
}
NDEP <- min(NDEP, min(NUMH + NT, N - NUMH))
}
return(list(NDEP = NDEP, NSIN = NSIN, EVEC = Evec))
}
Reduce <- function(X, PNT, Evec) {
Det <- det(Evec)
if (Det == 0) {
return(list(X = X, PNT = PNT, DET = Det))
}
NP <- ncol(X)
RedEvec <- matrix(Evec[, 1:(NP - 1)], nrow = NP, ncol = (NP -
1))
PNT <- PNT %*% RedEvec
X <- X %*% RedEvec
if (!is.matrix(X))
X <- matrix(X, ncol = (NP - 1))
return(list(X = X, PNT = PNT, DET = Det))
}
if (!is.na(SEED))
set.seed(SEED)
Nsin <- 0
X <- as.matrix(X)
N <- nrow(X)
NP <- ncol(X)
if (length(PNT) != NP) {
print("Length of 'PNT' has to equal to")
stop("number of columns in X !!! ")
}
if (N == 1) {
NDEP <- ifelse(abs(X[1, ] - PNT) > EPS, 0, 1)
NDEP <- min(NDEP)
DEPTH <- NDEP/N
return(DEPTH)
}
repeat {
if (NP == 1) {
MORE <- sum(ifelse(X[, 1] >= (PNT - EPS), 1, 0))
LESS <- sum(ifelse(X[, 1] <= (PNT + EPS), 1, 0))
NDEP <- min(LESS, MORE)
DEPTH <- NDEP/N
return(DEPTH)
}
if (N > NP) {
RES <- DEP(X = X, PNT = PNT, NDIR = NDIR, EPS = EPS,
PRINT = PRINT)
NDEP <- RES$NDEP
NSIN <- RES$NSIN
EVEC <- RES$EVEC
}
else {
NSIN <- -1
EVEC <- eigen(var(X))[[2]]
}
if (NSIN == -1) {
NSIN <- 0
if (PRINT)
print(" Direction with zero variance detected")
RED <- Reduce(X = X, PNT = PNT, Evec = EVEC)
X <- RED$X
PNT <- RED$PNT
Det <- RED$DET
if (Det == 0) {
print("\n\n\t DIMENSION REDUCTION TERMINATED\n\t EIGENVECTORS ARE NOT")
stop("INDEPENDENT\n\n")
}
NP <- ncol(X)
if (PRINT)
paste(" Dimension reduced to", NP)
}
else {
break
}
}
DEPTH <- NDEP/N
return(DEPTH)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.