1 | classSVD(x)
|
x |
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 | ##---- 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)
{
if (!is.matrix(x)) {
stop("The function classSVD requires input of type 'matrix'.")
}
n <- nrow(x)
p <- ncol(x)
if (n == 1) {
stop("The sample size is 1. No singular value decomposition can be performed.")
}
if (p < 5) {
tolerance <- 1e-12
}
else {
if (p <= 8) {
tolerance <- 1e-14
}
else {
tolerance <- 1e-16
}
}
centerofX <- apply(x, 2, mean)
Xcentered <- scale(x, center = TRUE, scale = FALSE)
XcenteredSVD <- svd(Xcentered/sqrt(n - 1))
rank <- sum(XcenteredSVD$d > tolerance)
eigenvalues <- (XcenteredSVD$d[1:rank])^2
loadings <- XcenteredSVD$v[, 1:rank]
scores <- Xcentered %*% loadings
return(list(loadings = as.matrix(loadings), scores = as.matrix(scores),
eigenvalues = as.vector(eigenvalues), rank = rank, Xcentered = as.matrix(Xcentered),
centerofX = as.vector(centerofX)))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.