2019-12-20 08:40:46 +00:00
|
|
|
#' Extracts estimated SDR basis.
|
2019-11-22 08:32:14 +00:00
|
|
|
#'
|
2019-12-16 16:34:35 +00:00
|
|
|
#' Returns the SDR basis matrix for dimension \code{k}, i.e. returns the
|
2019-12-20 08:40:46 +00:00
|
|
|
#' cve-estimate of \eqn{B} with dimension \eqn{p\times k}{p x k}.
|
2019-12-16 16:34:35 +00:00
|
|
|
#'
|
2019-12-20 08:40:46 +00:00
|
|
|
#' @param object an object of class \code{"cve"}, usually, a result of a call to
|
|
|
|
#' \code{\link{cve}} or \code{\link{cve.call}}.
|
2019-11-22 08:32:14 +00:00
|
|
|
#' @param k the SDR dimension.
|
2019-12-20 08:40:46 +00:00
|
|
|
#' @param ... ignored (no additional arguments).
|
2019-11-22 08:32:14 +00:00
|
|
|
#'
|
2019-12-20 08:40:46 +00:00
|
|
|
#' @return The matrix \eqn{B} of dimensions \eqn{p\times k}{p x k}.
|
2019-11-22 08:32:14 +00:00
|
|
|
#'
|
|
|
|
#' @examples
|
2019-12-05 16:35:29 +00:00
|
|
|
#' # set dimensions for simulation model
|
|
|
|
#' p <- 8 # sample dimension
|
|
|
|
#' k <- 2 # real dimension of SDR subspace
|
2021-03-05 13:52:45 +00:00
|
|
|
#' n <- 100 # samplesize
|
2019-12-05 16:35:29 +00:00
|
|
|
#' # create B for simulation
|
|
|
|
#' b1 <- rep(1 / sqrt(p), p)
|
|
|
|
#' b2 <- (-1)^seq(1, p) / sqrt(p)
|
|
|
|
#' B <- cbind(b1, b2)
|
2019-12-20 08:40:46 +00:00
|
|
|
#'
|
2019-12-05 16:35:29 +00:00
|
|
|
#' set.seed(21)
|
|
|
|
#' # creat predictor data x ~ N(0, I_p)
|
|
|
|
#' x <- matrix(rnorm(n * p), n, p)
|
|
|
|
#' # simulate response variable
|
|
|
|
#' # y = f(B'x) + err
|
2021-03-05 13:52:45 +00:00
|
|
|
#' # with f(x1, x2) = x1^2 + 2 * x2 and err ~ N(0, 0.1^2)
|
|
|
|
#' y <- (x %*% b1)^2 + 2 * (x %*% b2) + 0.1 * rnorm(100)
|
|
|
|
#' # calculate cve for k = 2, 3
|
|
|
|
#' cve.obj <- cve(y ~ x, min.dim = 2, max.dim = 3)
|
2019-12-05 16:35:29 +00:00
|
|
|
#' # get cve-estimate for B with dimensions (p, k = 2)
|
|
|
|
#' B2 <- coef(cve.obj, k = 2)
|
2019-12-20 08:40:46 +00:00
|
|
|
#'
|
2019-12-05 16:35:29 +00:00
|
|
|
#' # Projection matrix on span(B)
|
|
|
|
#' # equivalent to `B %*% t(B)` since B is semi-orthonormal
|
|
|
|
#' PB <- B %*% solve(t(B) %*% B) %*% t(B)
|
|
|
|
#' # Projection matrix on span(B2)
|
|
|
|
#' # equivalent to `B2 %*% t(B2)` since B2 is semi-orthonormal
|
|
|
|
#' PB2 <- B2 %*% solve(t(B2) %*% B2) %*% t(B2)
|
|
|
|
#' # compare estimation accuracy by Frobenius norm of difference of projections
|
|
|
|
#' norm(PB - PB2, type = 'F')
|
2019-11-22 08:32:14 +00:00
|
|
|
#'
|
|
|
|
#' @method coef cve
|
|
|
|
#' @aliases coef.cve
|
|
|
|
#' @rdname coef.cve
|
|
|
|
#' @export
|
|
|
|
coef.cve <- function(object, k, ...) {
|
|
|
|
if (missing(k)) {
|
|
|
|
Bs <- list()
|
|
|
|
for (k in names(object$res)) {
|
|
|
|
Bs[[k]] <- object$res[[k]]$B
|
|
|
|
}
|
|
|
|
return(Bs)
|
|
|
|
} else if (k %in% names(object$res)) {
|
|
|
|
return(object$res[[as.character(k)]]$B)
|
|
|
|
} else {
|
|
|
|
stop("Requested dimension `k` not computed.")
|
|
|
|
}
|
|
|
|
}
|