2
0
Fork 0
CVE/CVE_C/R/estimateBandwidth.R

27 lines
904 B
R

#' Bandwidth estimation for CVE.
#'
#' Estimates a bandwidth \code{h} according
#' \deqn{%
#' h = (2 * tr(\Sigma) / p) * (1.2 * n^{-1 / (4 + k)})^2}{%
#' h = (2 * tr(Sigma) / p) * (1.2 * n^(-1 / (4 + k)))^2}
#' with \eqn{n} the sample size, \eqn{p} its dimension
#' (\code{n <- nrow(X); p <- ncol(X)}) and the covariance-matrix \eqn{\Sigma}
#' which is \code{(n-1)/n} times the sample covariance estimate.
#'
#' @param X data matrix with samples in its rows.
#' @param k Dimension of lower dimensional projection.
#' @param nObs number of points in a slice, see \eqn{nObs} in CVE paper.
#'
#' @return Estimated bandwidth \code{h}.
#'
#' @export
estimate.bandwidth <- function(X, k, nObs) {
n <- nrow(X)
p <- ncol(X)
X_centered <- scale(X, center = TRUE, scale = FALSE)
Sigma <- crossprod(X_centered, X_centered) / n
return((2 * sum(diag(Sigma)) / p) * (1.2 * n^(-1 / (4 + k)))^2)
}