1 |
x |
|
y |
|
cov.fun |
|
ols.op |
|
mcd.op |
|
quantile.used |
|
RES |
|
... |
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 | ##---- 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, y, cov.fun = cov.mcd, ols.op = TRUE, mcd.op = TRUE,
quantile.used = floor(0.75 * n), RES = FALSE, ...)
{
library(MASS)
if (!is.matrix(y))
stop("y is not a matrix")
X <- cbind(x, y)
X <- elimna(X)
n <- nrow(X)
qy <- ncol(y)
qx <- ncol(x)
qxp1 <- qx + 1
tqyqx <- qy + qx
y <- X[, qxp1:tqyqx]
if (!mcd.op)
locscat <- cov.fun(X, ...)
if (mcd.op)
locscat <- cov.mcd(X, quan = quantile.used)
sig <- locscat$cov
mu <- locscat$center
sigxx <- sig[1:qx, 1:qx]
sigxy <- sig[1:qx, qxp1:tqyqx]
sigyy <- sig[qxp1:tqyqx, qxp1:tqyqx]
Bhat <- solve(sigxx) %*% sigxy
sige <- sigyy - t(Bhat) %*% sigxx %*% Bhat
sige.inv <- solve(sige)
Ahat <- t(mu[qxp1:tqyqx] - t(Bhat) %*% mu[1:qx])
resL <- matrix(nrow = nrow(X), ncol = qy)
for (i in 1:nrow(X)) resL[i, ] <- y[i, ] - t(Bhat) %*% X[i,
1:qx]
for (j in 1:qy) resL[, j] <- resL[, j] - Ahat[j]
drL <- NA
for (i in 1:nrow(X)) drL[i] <- t(resL[i, ]) %*% sige.inv %*%
resL[i, ]
w <- rep(0, nrow(X))
qdr <- qchisq(0.99, qy)
iflag <- (drL < qdr)
w[iflag] <- 1
term1 <- 0
vec <- c(1:nrow(X))
keep <- vec[iflag == 1]
X <- X[keep, ]
if (ols.op)
output <- lsfit(X[, 1:qx], X[, qxp1:tqyqx])
if (!ols.op)
output <- mopreg(X[, 1:qx], X[, qxp1:tqyqx], KEEP = T)
yhat = X[, qxp1:tqyqx] - output$residuals
res = NULL
if (RES)
res = output$residuals
list(coef = output$coefficients, residuals = res)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.