2019-09-12 16:42:28 +00:00
|
|
|
|
#' Conditional Variance Estimator (CVE)
|
|
|
|
|
#'
|
|
|
|
|
#' Conditional Variance Estimator for Sufficient Dimension
|
|
|
|
|
#' Reduction
|
|
|
|
|
#'
|
|
|
|
|
#' TODO: And some details
|
|
|
|
|
#'
|
|
|
|
|
#'
|
|
|
|
|
#' @references Fertl Likas, Bura Efstathia. Conditional Variance Estimation for Sufficient Dimension Reduction, 2019
|
|
|
|
|
#'
|
|
|
|
|
#' @docType package
|
|
|
|
|
#' @author Loki
|
2019-09-25 12:49:12 +00:00
|
|
|
|
#' @useDynLib CVE, .registration = TRUE
|
2019-09-12 16:42:28 +00:00
|
|
|
|
"_PACKAGE"
|
|
|
|
|
|
2019-08-30 19:16:52 +00:00
|
|
|
|
#' Implementation of the CVE method.
|
|
|
|
|
#'
|
|
|
|
|
#' Conditional Variance Estimator (CVE) is a novel sufficient dimension
|
|
|
|
|
#' reduction (SDR) method assuming a model
|
|
|
|
|
#' \deqn{Y \sim g(B'X) + \epsilon}{Y ~ g(B'X) + epsilon}
|
|
|
|
|
#' where B'X is a lower dimensional projection of the predictors.
|
|
|
|
|
#'
|
|
|
|
|
#' @param formula Formel for the regression model defining `X`, `Y`.
|
|
|
|
|
#' See: \code{\link{formula}}.
|
|
|
|
|
#' @param data data.frame holding data for formula.
|
|
|
|
|
#' @param method The different only differe in the used optimization.
|
|
|
|
|
#' All of them are Gradient based optimization on a Stiefel manifold.
|
|
|
|
|
#' \itemize{
|
|
|
|
|
#' \item "simple" Simple reduction of stepsize.
|
|
|
|
|
#' \item "sgd" stocastic gradient decent.
|
|
|
|
|
#' \item TODO: further
|
|
|
|
|
#' }
|
|
|
|
|
#' @param ... Further parameters depending on the used method.
|
|
|
|
|
#' @examples
|
|
|
|
|
#' library(CVE)
|
|
|
|
|
#'
|
|
|
|
|
#' # sample dataset
|
|
|
|
|
#' ds <- dataset("M5")
|
|
|
|
|
#'
|
|
|
|
|
#' # call ´cve´ with default method (aka "simple")
|
|
|
|
|
#' dr.simple <- cve(ds$Y ~ ds$X, k = ncol(ds$B))
|
|
|
|
|
#' # plot optimization history (loss via iteration)
|
|
|
|
|
#' plot(dr.simple, main = "CVE M5 simple")
|
|
|
|
|
#'
|
|
|
|
|
#' # call ´cve´ with method "linesearch" using ´data.frame´ as data.
|
|
|
|
|
#' data <- data.frame(Y = ds$Y, X = ds$X)
|
|
|
|
|
#' # Note: ´Y, X´ are NOT defined, they are extracted from ´data´.
|
|
|
|
|
#' dr.linesearch <- cve(Y ~ ., data, method = "linesearch", k = ncol(ds$B))
|
|
|
|
|
#' plot(dr.linesearch, main = "CVE M5 linesearch")
|
|
|
|
|
#'
|
|
|
|
|
#' @references Fertl L., Bura E. Conditional Variance Estimation for Sufficient Dimension Reduction, 2019
|
|
|
|
|
#'
|
|
|
|
|
#' @seealso \code{\link{formula}}. For a complete parameters list (dependent on
|
|
|
|
|
#' the method) see \code{\link{cve_simple}}, \code{\link{cve_sgd}}
|
|
|
|
|
#' @import stats
|
|
|
|
|
#' @importFrom stats model.frame
|
|
|
|
|
#' @export
|
2019-09-25 12:49:12 +00:00
|
|
|
|
cve <- function(formula, data, method = "simple", max.dim = 10L, ...) {
|
2019-08-30 19:16:52 +00:00
|
|
|
|
# check for type of `data` if supplied and set default
|
|
|
|
|
if (missing(data)) {
|
|
|
|
|
data <- environment(formula)
|
|
|
|
|
} else if (!is.data.frame(data)) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
stop("Parameter 'data' must be a 'data.frame' or missing.")
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# extract `X`, `Y` from `formula` with `data`
|
|
|
|
|
model <- stats::model.frame(formula, data)
|
2019-09-25 12:49:12 +00:00
|
|
|
|
X <- as.matrix(model[ ,-1L, drop = FALSE])
|
|
|
|
|
Y <- as.double(model[ , 1L])
|
2019-08-30 19:16:52 +00:00
|
|
|
|
|
|
|
|
|
# pass extracted data on to [cve.call()]
|
2019-09-25 12:49:12 +00:00
|
|
|
|
dr <- cve.call(X, Y, method = method, max.dim = max.dim, ...)
|
2019-08-30 19:16:52 +00:00
|
|
|
|
|
|
|
|
|
# overwrite `call` property from [cve.call()]
|
|
|
|
|
dr$call <- match.call()
|
|
|
|
|
return(dr)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 08:33:41 +00:00
|
|
|
|
#' @param nObs as described in the Paper.
|
2019-08-30 19:16:52 +00:00
|
|
|
|
#' @param X Data
|
|
|
|
|
#' @param Y Responces
|
|
|
|
|
#' @param nObs Like in the paper.
|
|
|
|
|
#' @param k guess for SDR dimension.
|
|
|
|
|
#' @param ... Method specific parameters.
|
|
|
|
|
#' @rdname cve
|
|
|
|
|
#' @export
|
2019-09-25 12:49:12 +00:00
|
|
|
|
cve.call <- function(X, Y, method = "simple",
|
|
|
|
|
nObs = sqrt(nrow(X)), h = NULL,
|
|
|
|
|
min.dim = 1L, max.dim = 10L, k = NULL,
|
|
|
|
|
tau = 1.0, tol = 1e-3,
|
|
|
|
|
epochs = 50L, attempts = 10L,
|
|
|
|
|
logger = NULL) {
|
2019-08-30 19:16:52 +00:00
|
|
|
|
|
|
|
|
|
# parameter checking
|
2019-09-25 12:49:12 +00:00
|
|
|
|
if (!(is.matrix(X) && is.numeric(X))) {
|
|
|
|
|
stop("Parameter 'X' should be a numeric matrices.")
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
2019-09-25 12:49:12 +00:00
|
|
|
|
if (!is.numeric(Y)) {
|
|
|
|
|
stop("Parameter 'Y' must be numeric.")
|
|
|
|
|
}
|
|
|
|
|
if (is.matrix(Y) || !is.double(Y)) {
|
|
|
|
|
Y <- as.double(Y)
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
if (nrow(X) != length(Y)) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
stop("Rows of 'X' and 'Y' elements are not compatible.")
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
if (ncol(X) < 2) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
stop("'X' is one dimensional, no need for dimension reduction.")
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 12:49:12 +00:00
|
|
|
|
if (missing(k) || is.null(k)) {
|
2019-09-12 16:42:28 +00:00
|
|
|
|
min.dim <- as.integer(min.dim)
|
|
|
|
|
max.dim <- as.integer(min(max.dim, ncol(X) - 1L))
|
2019-09-25 12:49:12 +00:00
|
|
|
|
} else {
|
|
|
|
|
min.dim <- as.integer(k)
|
|
|
|
|
max.dim <- as.integer(k)
|
2019-09-12 16:42:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (min.dim > max.dim) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
stop("'min.dim' bigger 'max.dim'.")
|
2019-09-12 16:42:28 +00:00
|
|
|
|
}
|
|
|
|
|
if (max.dim >= ncol(X)) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
stop("'max.dim' (or 'k') must be smaller than 'ncol(X)'.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is.function(h)) {
|
|
|
|
|
estimate.bandwidth <- h
|
|
|
|
|
h <- NULL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is.numeric(tau) || length(tau) > 1L || tau <= 0.0) {
|
|
|
|
|
stop("Initial step-width 'tau' must be positive number.")
|
|
|
|
|
} else {
|
|
|
|
|
tau <- as.double(tau)
|
|
|
|
|
}
|
|
|
|
|
if (!is.numeric(tol) || length(tol) > 1L || tol < 0.0) {
|
|
|
|
|
stop("Break condition tolerance 'tol' must be not negative number.")
|
|
|
|
|
} else {
|
|
|
|
|
tol <- as.double(tol)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is.numeric(epochs) || length(epochs) > 1L) {
|
|
|
|
|
stop("Parameter 'epochs' must be positive integer.")
|
|
|
|
|
} else if (!is.integer(epochs)) {
|
|
|
|
|
epochs <- as.integer(epochs)
|
|
|
|
|
}
|
|
|
|
|
if (epochs < 1L) {
|
|
|
|
|
stop("Parameter 'epochs' must be at least 1L.")
|
|
|
|
|
}
|
|
|
|
|
if (!is.numeric(attempts) || length(attempts) > 1L) {
|
|
|
|
|
stop("Parameter 'attempts' must be positive integer.")
|
|
|
|
|
} else if (!is.integer(attempts)) {
|
|
|
|
|
attempts <- as.integer(attempts)
|
|
|
|
|
}
|
|
|
|
|
if (attempts < 1L) {
|
|
|
|
|
stop("Parameter 'attempts' must be at least 1L.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is.function(logger)) {
|
|
|
|
|
loggerEnv <- environment(logger)
|
|
|
|
|
} else {
|
|
|
|
|
loggerEnv <- NULL
|
2019-09-12 16:42:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 19:16:52 +00:00
|
|
|
|
# Call specified method.
|
|
|
|
|
method <- tolower(method)
|
2019-09-12 16:42:28 +00:00
|
|
|
|
call <- match.call()
|
|
|
|
|
dr <- list()
|
|
|
|
|
for (k in min.dim:max.dim) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
|
|
|
|
|
if (missing(h) || is.null(h)) {
|
|
|
|
|
h <- estimate.bandwidth(X, k, nObs)
|
|
|
|
|
} else if (is.numeric(h) && h > 0.0) {
|
|
|
|
|
h <- as.double(h)
|
|
|
|
|
} else {
|
|
|
|
|
stop("Bandwidth 'h' must be positive numeric.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 16:42:28 +00:00
|
|
|
|
if (method == 'simple') {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
dr.k <- .Call('cve_simple', PACKAGE = 'CVE',
|
|
|
|
|
X, Y, k, h,
|
|
|
|
|
tau, tol,
|
|
|
|
|
epochs, attempts,
|
|
|
|
|
logger, loggerEnv)
|
|
|
|
|
# dr.k <- cve_simple(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'linesearch') {
|
|
|
|
|
# dr.k <- cve_linesearch(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'rcg') {
|
|
|
|
|
# dr.k <- cve_rcg(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'momentum') {
|
|
|
|
|
# dr.k <- cve_momentum(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'rmsprob') {
|
|
|
|
|
# dr.k <- cve_rmsprob(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'sgdrmsprob') {
|
|
|
|
|
# dr.k <- cve_sgdrmsprob(X, Y, k, nObs = nObs, ...)
|
|
|
|
|
# } else if (method == 'sgd') {
|
|
|
|
|
# dr.k <- cve_sgd(X, Y, k, nObs = nObs, ...)
|
2019-09-12 16:42:28 +00:00
|
|
|
|
} else {
|
|
|
|
|
stop('Got unknown method.')
|
|
|
|
|
}
|
2019-09-25 12:49:12 +00:00
|
|
|
|
dr.k$B <- null(dr.k$V)
|
|
|
|
|
dr.k$loss <- mean(dr.k$L)
|
|
|
|
|
dr.k$h <- h
|
2019-09-12 16:42:28 +00:00
|
|
|
|
dr.k$k <- k
|
|
|
|
|
class(dr.k) <- "cve.k"
|
|
|
|
|
dr[[k]] <- dr.k
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# augment result information
|
2019-09-16 09:23:09 +00:00
|
|
|
|
dr$method <- method
|
|
|
|
|
dr$call <- call
|
2019-08-30 19:16:52 +00:00
|
|
|
|
class(dr) <- "cve"
|
|
|
|
|
return(dr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#' Ploting helper for objects of class \code{cve}.
|
|
|
|
|
#'
|
|
|
|
|
#' @param x Object of class \code{cve} (result of [cve()]).
|
|
|
|
|
#' @param content Specifies what to plot:
|
|
|
|
|
#' \itemize{
|
|
|
|
|
#' \item "history" Plots the loss history from stiefel optimization
|
|
|
|
|
#' (default).
|
|
|
|
|
#' \item ... TODO: add (if there are any)
|
|
|
|
|
#' }
|
|
|
|
|
#' @param ... Pass through parameters to [plot()] and [lines()]
|
|
|
|
|
#'
|
|
|
|
|
#' @usage ## S3 method for class 'cve'
|
|
|
|
|
#' plot(x, content = "history", ...)
|
|
|
|
|
#' @seealso see \code{\link{par}} for graphical parameters to pass through
|
|
|
|
|
#' as well as \code{\link{plot}} for standard plot utility.
|
|
|
|
|
#' @importFrom graphics plot lines points
|
|
|
|
|
#' @method plot cve
|
|
|
|
|
#' @export
|
|
|
|
|
plot.cve <- function(x, ...) {
|
2019-09-25 12:49:12 +00:00
|
|
|
|
L <- c()
|
|
|
|
|
k <- c()
|
|
|
|
|
for (dr.k in x) {
|
|
|
|
|
if (class(dr.k) == 'cve.k') {
|
|
|
|
|
k <- c(k, paste0(dr.k$k))
|
|
|
|
|
L <- c(L, dr.k$L)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
L <- matrix(L, ncol = length(k))
|
|
|
|
|
boxplot(L, main = "Loss ...",
|
|
|
|
|
xlab = "SDR dimension k",
|
|
|
|
|
ylab = expression(L(V, X[i])),
|
|
|
|
|
names = k)
|
2019-08-30 19:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#' Prints a summary of a \code{cve} result.
|
|
|
|
|
#' @param object Instance of 'cve' as return of \code{cve}.
|
|
|
|
|
#' @method summary cve
|
|
|
|
|
#' @export
|
|
|
|
|
summary.cve <- function(object, ...) {
|
|
|
|
|
cat('Summary of CVE result - Method: "', object$method, '"\n',
|
|
|
|
|
'\n',
|
|
|
|
|
'Dataset size: ', nrow(object$X), '\n',
|
|
|
|
|
'Data Dimension: ', ncol(object$X), '\n',
|
|
|
|
|
'SDR Dimension: ', object$k, '\n',
|
|
|
|
|
'loss: ', object$loss, '\n',
|
|
|
|
|
'\n',
|
|
|
|
|
'Called via:\n',
|
|
|
|
|
' ',
|
|
|
|
|
sep='')
|
|
|
|
|
print(object$call)
|
|
|
|
|
}
|